package app

import (
	. "code.hoteas.com/golang/hotime"
	. "code.hoteas.com/golang/hotime/common"
	"time"
)

var OrgCtr = Ctr{
	"info": func(that *Context) {
		sn := that.Req.FormValue("sn")
		article := that.Db.Get("article", "*", Map{"sn": sn})
		that.Display(0, article)
	},
	"list": func(that *Context) {
		sn := that.Req.FormValue("sn") //orgsn
		page := ObjToInt(that.Req.FormValue("page"))
		pageSize := ObjToInt(that.Req.FormValue("pageSize"))

		if page == 0 {
			page = 1
		}
		if pageSize == 0 {
			pageSize = 50
		}

		keywords := that.Req.FormValue("keywords")

		sort := that.Req.FormValue("sort")

		where := Map{"push_time[<=]": time.Now().Format("2006-01-02 15:04"), "state": 0}
		if sn != "" {
			org := that.Db.Get("org", "id", Map{"sn": sn})
			if org != nil {
				where["org_id"] = org.GetCeilInt("id")
			}
		}

		if keywords != "" {
			where["OR"] = Map{"title[~]": keywords, "description[~]": keywords, "author[~]": keywords, "sn[~]": keywords, "origin[~]": keywords, "url[~]": keywords}
		}

		if len(where) > 1 {
			where = Map{"AND": where}
		}

		if sort == "" {
			where["ORDER"] = Slice{"sort DESC", "id DESC"}
		}

		if sort == "time" {
			where["ORDER"] = "push_time DESC"
		}

		article := that.Db.Page(page, pageSize).PageSelect("article", "sn,title,description,push_time,lunbo,author,origin,url", where)

		that.Display(0, article)

	},
}
