diff --git a/.gitignore b/.gitignore index 81812bb..c40d862 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /.idea/* .idea -/example/config/app.json \ No newline at end of file +/example/config/app.json +/example/tpt/demo/ diff --git a/code.go b/code.go index 960a79f..517acf0 100644 --- a/code.go +++ b/code.go @@ -139,7 +139,6 @@ var TptProject = Proj{ inData["parent_id"] = data.GetCeilInt64(v.GetString("name")) pre := that.Db.Get(v.GetString("link"), "parent_ids", Map{"id": data.GetCeilInt64(v.GetString("name"))}) inData["parent_ids"] = pre.GetString("parent_ids") - } } diff --git a/example/app/article.go b/example/app/article.go new file mode 100644 index 0000000..0d29e40 --- /dev/null +++ b/example/app/article.go @@ -0,0 +1,94 @@ +package app + +import ( + . "code.hoteas.com/golang/hotime" + . "code.hoteas.com/golang/hotime/common" + "time" +) + +var ArticleCtr = Ctr{ + "info": func(that *Context) { + sn := that.Req.FormValue("sn") + article := that.Db.Get("article", Map{"[><]ctg_article": "article.id=ctg_article.article_id"}, "article.*,ctg_article.ctg_id AS sctg_id", Map{"ctg_article.sn": sn}) + if article == nil { + that.Display(4, "找不到对应数据") + return + } + ctgId := article.GetCeilInt64("sctg_id") + ctg := that.Db.Get("ctg", "*", Map{"id": ctgId}) + parents := []Map{} + parentId := ctg.GetCeilInt64("parent_id") + article["tongji"] = that.Db.Select("ctg", "sn,name,img,parent_id", Map{"parent_id": parentId}) + for true { + + if parentId == 0 { + break + } + + parent := that.Db.Get("ctg", "sn,name,img,parent_id", Map{"id": parentId}) + if parent == nil { + break + } + + parents = append(parents, parent) + parentId = parent.GetCeilInt64("parent_id") + + } + + ctg["parents"] = parents + + article["ctg"] = ctg + that.Display(0, article) + }, + "list": func(that *Context) { + sn := that.Req.FormValue("ctg_sn") //ctgsn + page := ObjToInt(that.Req.FormValue("page")) + pageSize := ObjToInt(that.Req.FormValue("pageSize")) + + if page == 0 { + page = 1 + } + if pageSize == 0 { + pageSize = 10 + } + + keywords := that.Req.FormValue("keywords") + + lunbo := ObjToInt(that.Req.FormValue("lunbo")) + + sort := that.Req.FormValue("sort") + + where := Map{"article.push_time[<=]": time.Now().Format("2006-01-02 15:04"), "article.state": 0} + if sn != "" { + ctg := that.Db.Get("ctg", "id", Map{"sn": sn}) + if ctg != nil { + where["ctg_article.ctg_id"] = ctg.GetCeilInt("id") + } + } + + if lunbo != 0 { + where["article.lunbo"] = lunbo + } + + if keywords != "" { + where["OR"] = Map{"article.title[~]": keywords, "article.description[~]": keywords, "article.author[~]": keywords, "article.sn[~]": keywords, "article.origin[~]": keywords, "article.url[~]": keywords} + } + + if len(where) > 1 { + where = Map{"AND": where} + } + + if sort == "" { + where["ORDER"] = Slice{"article.sort DESC", "article.push_time DESC"} + } + + if sort == "time" { + where["ORDER"] = "article.push_time DESC" + } + count := that.Db.Count("article", Map{"[><]ctg_article": "article.id=ctg_article.article_id"}, where) + article := that.Db.Page(page, pageSize).PageSelect("article", Map{"[><]ctg_article": "article.id=ctg_article.article_id"}, "ctg_article.sn,article.img,article.title,article.description,article.push_time,article.lunbo,article.author,article.origin,article.url", where) + + that.Display(0, Map{"count": count, "data": article}) + + }, +} diff --git a/example/app/ctg.go b/example/app/ctg.go new file mode 100644 index 0000000..2773e63 --- /dev/null +++ b/example/app/ctg.go @@ -0,0 +1,77 @@ +package app + +import ( + . "code.hoteas.com/golang/hotime" + . "code.hoteas.com/golang/hotime/common" +) + +var CtgCtr = Ctr{ + "info": func(that *Context) { + sn := that.Req.FormValue("sn") + ctg := that.Db.Get("ctg", "*", Map{"sn": sn}) + parents := []Map{} + parentId := ctg.GetCeilInt64("parent_id") + ctg["tongji"] = that.Db.Select("ctg", "sn,name,img,parent_id", Map{"parent_id": parentId}) + for true { + + if parentId == 0 { + break + } + + parent := that.Db.Get("ctg", "sn,name,img,parent_id", Map{"id": parentId}) + if parent == nil { + break + } + + parents = append(parents, parent) + parentId = parent.GetCeilInt64("parent_id") + + } + + if ctg.GetCeilInt64("article_id") != 0 { + ctg["article"] = that.Db.Get("article", "*", Map{"id": ctg.GetCeilInt64("article_id")}) + } + + ctg["parents"] = parents + that.Display(0, ctg) + }, + "list": func(that *Context) { + sn := that.Req.FormValue("sn") //ctgsn + 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{"state": 0} + if sn != "" { + ctg := that.Db.Get("ctg", "id", Map{"sn": sn}) + if ctg != nil { + where["parent_id"] = ctg.GetCeilInt("id") + } + } + + if keywords != "" { + where["OR"] = Map{"name[~]": keywords, "url[~]": keywords, "sn[~]": keywords} + } + + if len(where) > 1 { + where = Map{"AND": where} + } + + where["ORDER"] = Slice{"sort DESC", "id DESC"} + + article := that.Db.Page(page, pageSize).PageSelect("ctg", "name,sn,sort,url,img", where) + + that.Display(0, article) + + }, +} diff --git a/example/app/init.go b/example/app/init.go new file mode 100644 index 0000000..f507825 --- /dev/null +++ b/example/app/init.go @@ -0,0 +1,117 @@ +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": { + "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) + } +} diff --git a/example/app/mail.go b/example/app/mail.go new file mode 100644 index 0000000..3066e9e --- /dev/null +++ b/example/app/mail.go @@ -0,0 +1,94 @@ +package app + +import ( + . "code.hoteas.com/golang/hotime" + . "code.hoteas.com/golang/hotime/common" +) + +var MailCtr = Ctr{ + "add": func(that *Context) { + title := that.Req.FormValue("title") + name := that.Req.FormValue("name") + phone := that.Req.FormValue("phone") + content := that.Req.FormValue("content") + + tp := ObjToInt(that.Req.FormValue("type")) + show := ObjToInt(that.Req.FormValue("show")) + + if len(title) < 5 { + that.Display(3, "标题过短") + return + } + if len(name) < 2 { + that.Display(3, "姓名错误") + return + } + if len(phone) < 8 { + that.Display(3, "联系方式错误") + return + } + if len(content) < 10 { + that.Display(3, "内容过短") + return + } + + data := Map{ + "name": name, "title": title, "phone": phone, "content": content, "type": tp, "show": show, + "modify_time[#]": "NOW()", "create_time[#]": "NOW()", + } + id := that.Db.Insert("mail", data) + if id == 0 { + that.Display(4, "创建失败") + return + } + + that.Display(0, "成功") + return + + }, + "info": func(that *Context) { + sn := that.Req.FormValue("sn") + + mail := that.Db.Get("mail", "*", Map{"sn": sn}) + + that.Display(0, mail) + }, + "list": func(that *Context) { + page := ObjToInt(that.Req.FormValue("page")) + pageSize := ObjToInt(that.Req.FormValue("pageSize")) + + if page == 0 { + page = 1 + } + if pageSize == 0 { + pageSize = 10 + } + + //keywords:=that.Req.FormValue("keywords") + + //sort:=that.Req.FormValue("sort") + + where := Map{"state": 0} + + //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","push_time DESC"} + //} + // + //if sort=="time"{ + where["ORDER"] = "create_time DESC" + //} + count := that.Db.Count("mail", where) + mail := that.Db.Page(page, pageSize).PageSelect("mail", "*", where) + + that.Display(0, Map{"count": count, "data": mail}) + + }, +} diff --git a/example/app/org.go b/example/app/org.go new file mode 100644 index 0000000..f5908f5 --- /dev/null +++ b/example/app/org.go @@ -0,0 +1,60 @@ +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) + + }, +} diff --git a/example/main.go b/example/main.go index 88de27e..7f4f6b1 100644 --- a/example/main.go +++ b/example/main.go @@ -2,144 +2,12 @@ package main import ( . "code.hoteas.com/golang/hotime" - . "code.hoteas.com/golang/hotime/common" - "fmt" - "github.com/360EntSecGroup-Skylar/excelize" - "golang.org/x/net/websocket" - "os" - "time" + "code.hoteas.com/golang/hotime/example/app" ) -func convertToTitle(columnNumber int) string { - var res []byte - for columnNumber > 0 { - a := columnNumber % 26 - if a == 0 { - a = 26 - } - res = append(res, 'A'+byte(a-1)) - columnNumber = (columnNumber - a) / 26 - } - // 上面输出的res是反着的,前后交换 - for i, n := 0, len(res); i < n/2; i++ { - res[i], res[n-1-i] = res[n-1-i], res[i] - } - return string(res) -} - func main() { appIns := Init("config/config.json") - appIns.Run(Router{"app": Proj{ - "user": Ctr{ - "test": func(that *Context) { - fmt.Println("dasdasd") - //id := that.Db.Insert("user", Map{"name": "test"}) - //ok := that.Db.Update("user", Map{"name": "test1"}, Map{"name": "test"}) - ps := that.Db.Select("user", "*") - p := that.Db.Get("user", "*") - //row := that.Db.Delete("user", Map{"id": id}) - //that.Display(0, Slice{id, ok, ps, p, row}) - that.Display(0, Slice{ps, p}) - }, - "download": func(that *Context) { - orgId := ObjToInt(that.Req.FormValue("org_id")) - if orgId == 0 { - that.Display(3, "参数错误") - return - } - - filePath := "temp/home" + ObjToStr(orgId) + ".xlsx" - f, e := excelize.OpenFile(that.Config.GetString("tpt") + "/" + filePath) - // 设置单元格的值 - questions := that.Db.Query("SELECT user.name AS user_name,user.phone,company.name AS company_name,question_company.name,question_company.question_data,question_company.status,question_company.modify_time FROM question_company INNER JOIN `user` ON question_company.`user_id`=`user`.id INNER JOIN `company` ON question_company.`company_id`=`company`.id WHERE question_company.org_id=?", orgId) - if e != nil { - f = excelize.NewFile() - } - // 创建一个工作表 - f.NewSheet("企业填报") - - f.DeleteSheet("Sheet1") - - //cs := append(NewCompanys{}, companys...) - f.SetCellValue("企业填报", convertToTitle(1)+"1", "调查名") - f.SetCellValue("企业填报", convertToTitle(2)+"1", "企业名") - f.SetCellValue("企业填报", convertToTitle(3)+"1", "姓名") - f.SetCellValue("企业填报", convertToTitle(4)+"1", "手机号") - f.SetCellValue("企业填报", convertToTitle(5)+"1", "状态") - f.SetCellValue("企业填报", convertToTitle(6)+"1", "修改时间") - for k, question := range questions { - //企业基础信息 - f.SetCellValue("企业填报", convertToTitle(1)+ObjToStr(k+5), question.GetString("name")) - f.SetCellValue("企业填报", convertToTitle(2)+ObjToStr(k+5), question.GetString("company_name")) - f.SetCellValue("企业填报", convertToTitle(3)+ObjToStr(k+5), question.GetString("user_name")) - f.SetCellValue("企业填报", convertToTitle(4)+ObjToStr(k+5), question.GetString("phone")) - f.SetCellValue("企业填报", convertToTitle(5)+ObjToStr(k+5), question.GetString("status")) - f.SetCellValue("企业填报", convertToTitle(6)+ObjToStr(k+5), time.Unix(question.GetCeilInt64("modify_time"), 0).Format("2006-01-02 15:04")) - //企业问题信息基础信息 - questionData := question.GetSlice("question_data") - for k1, _ := range questionData { - v1 := questionData.GetMap(k1) - if k == 0 { - f.SetCellValue("企业填报", convertToTitle(k1+7)+"1", v1.GetString("label")) - f.SetCellValue("企业填报", convertToTitle(k1+7)+"2", v1.GetString("unit")) - f.SetCellValue("企业填报", convertToTitle(k1+7)+"3", v1.GetString("remarks")) - f.SetCellValue("企业填报", convertToTitle(k1+7)+"4", v1.GetString("type")) - } - value := v1.GetString("value") - extend := v1.GetSlice("extend") - if extend != nil && len(extend) > 0 { - for k2, _ := range extend { - v2 := extend.GetMap(k2) - value = value + ";" + v2.GetString("value") - } - } - - f.SetCellValue("企业填报", convertToTitle(k1+7)+ObjToStr(k+5), value) - - } - - //break - - } - - os.MkdirAll(that.Config.GetString("tpt")+"/temp/", os.ModeDir) - // 根据指定路径保存文件 - if err := f.SaveAs(that.Config.GetString("tpt") + "/" + filePath); err != nil { - fmt.Println(err) - that.Display(4, "输出异常") - return - } - - //} - f.Save() - that.Resp.Header().Set("Location", "/"+filePath) - that.Resp.WriteHeader(307) //关键在这里! - that.Display(0, filePath) - - }, - - "websocket": func(that *Context) { - hdler := websocket.Handler(func(ws *websocket.Conn) { - for true { - msg := make([]byte, 5120) - n, err := ws.Read(msg) - - if err != nil { - return - } - fmt.Printf("Receive: %s\n", msg[:n]) - - send_msg := "[" + string(msg[:n]) + "]" - m, err := ws.Write([]byte(send_msg)) - if err != nil { - return - } - fmt.Printf("Send: %s\n", msg[:m]) - } - }) - hdler.ServeHTTP(that.Resp, that.Req) - }, - }}}) + appIns.Run(Router{"app": app.AppProj}) }