package app

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

var AppProj = Proj{
	"article": ArticleCtr,
	"org":     OrgCtr,
	"ctg":     CtgCtr,
	"mail":    MailCtr,
	"test": {
		"test": func(that *Context) {
			//data:=that.Db.Table("admin").Order("id DESC").Select("*")
			//data1:=that.Db.Table("admin").Where(Map{"name[~]":"m"}).Order("id DESC").Select("*")
			//
			//data3:=that.Db.Select("admin","*",Map{"name[~]":"m"})
			data2 := that.Db.Table("article").Where(Map{"title[~]": "m"}).Order("id DESC").Page(1, 10).Select("*")
			c := that.Db.Table("article").Where(Map{"title[~]": "m"}).Order("id DESC").Group("title").Select("*")
			//that.Display(0,Slice{data1,data,data3,data2})
			that.Display(0, Slice{data2, c})
		},
		"res": func(that *Context) {
			ebw_res := that.Db.Select("ebw_res", "*")

			for _, v := range ebw_res {
				data := Map{"id": v.GetCeilInt("id"), "name": v.GetString("name"),
					"parent_id": v.GetCeilInt64("pid"),
					"sn":        v.GetString("url"), "create_time": time.Now().Format("2006-01-02 15:04"),
					"modify_time": time.Now().Format("2006-01-02 15:04"), "admin_id": 1}
				if data.GetCeilInt("parent_id") == 0 {
					data["parent_id"] = nil
				}
				that.Db.Insert("ctg", data)
			}

			that.Db.Exec("UPDATE ctg SET parent_id =NULL WHERE parent_id=id")

			ss(0, that)

			that.Display(0, len(ebw_res))

		},
		"news": func(that *Context) {
			ebw_news := that.Db.Select("ebw_news", "*")

			for _, v := range ebw_news {
				ctg := that.Db.Get("ctg", "*", Map{"sn": v.GetString("type")})
				data := Map{"sn": v.GetString("id"), "title": v.GetString("title"),
					"content": v.GetString("content"), "push_time": v.GetString("timedate"),
					"author": v.GetString("owner"), "origin": v.GetString("source"), "click_num": v.GetString("readtime"),
					"sort": v.GetCeilInt("zhiding"), "create_time": time.Now().Format("2006-01-02 15:04"),
					"modify_time": time.Now().Format("2006-01-02 15:04"), "admin_id": 1}
				if ctg != nil {
					data["ctg_id"] = ctg.GetCeilInt("id")
				}
				that.Db.Insert("article", data)
			}

			that.Display(0, len(ebw_news))

		},
		"res2news": func(that *Context) {
			ebw_news_addition_res := that.Db.Select("ebw_news_addition_res", "*")

			for _, v := range ebw_news_addition_res {
				ctg := that.Db.Get("ctg", "*", Map{"sn": v.GetString("fk_res")})
				article := that.Db.Get("article", "*", Map{"sn": v.GetString("fk_newsid")})
				data := Map{"sn": Md5(ObjToStr(time.Now().UnixNano()) + ObjToStr(RandX(10000, 100000))),
					"create_time": time.Now().Format("2006-01-02 15:04"),
					"modify_time": time.Now().Format("2006-01-02 15:04"), "admin_id": 1}
				if ctg != nil {
					data["ctg_id"] = ctg.GetCeilInt("id")
				}
				if article != nil {
					data["article_id"] = article.GetCeilInt("id")
				}
				that.Db.Insert("ctg_article", data)
			}

			that.Display(0, len(ebw_news_addition_res))
		},
		//将文章没有关联的ctg_article进行关联
		"article": func(that *Context) {
			articles := that.Db.Select("article", "id,ctg_id")
			for _, v := range articles {
				ctg_article := that.Db.Get("ctg_article", "id", Map{"article_id": v.GetCeilInt("id")})
				if ctg_article == nil {

					data := Map{"sn": Md5(ObjToStr(time.Now().UnixNano()) + ObjToStr(RandX(10000, 100000))),
						"create_time": time.Now().Format("2006-01-02 15:04"),
						"modify_time": time.Now().Format("2006-01-02 15:04"), "admin_id": 1}
					if v.GetCeilInt("ctg_id") == 0 || v.GetCeilInt("id") == 0 {
						continue
					}
					data["ctg_id"] = v.GetCeilInt("ctg_id")
					data["article_id"] = v.GetCeilInt("id")
					that.Db.Insert("ctg_article", data)
				}

			}
			that.Display(0, len(articles))

		},
	},
}

func ss(parent_id int, that *Context) {
	var ctgs []Map
	ctg := that.Db.Get("ctg", "*", Map{"id": parent_id})
	if parent_id == 0 {
		ctgs = that.Db.Select("ctg", "*", Map{"parent_id": nil})
	} else {
		ctgs = that.Db.Select("ctg", "*", Map{"parent_id": parent_id})
	}

	for _, v := range ctgs {
		if ctg == nil {
			ctg = Map{"parent_ids": ","}
		}
		ids := ctg.GetString("parent_ids") + ObjToStr(v.GetCeilInt("id")) + ","
		that.Db.Update("ctg", Map{"parent_ids": ids}, Map{"id": v.GetCeilInt("id")})
		ss(v.GetCeilInt("id"), that)
	}
}
