hotime/example/app/material.go
2021-12-29 00:46:07 +08:00

214 lines
5.5 KiB
Go

package app
import (
. "../../../hotime"
. "../../../hotime/common"
"strings"
"time"
)
var materialCtr = Ctr{
"info": func(that *Context) {
data := that.Db.Get("admin", "*", Map{"id": that.Session("admin_id").ToCeilInt()})
str, inData := that.MakeCode.Info(that.RouterString[1], data, that.Db)
where := Map{"id": that.RouterString[2]}
if len(inData) == 1 {
inData["id"] = where["id"]
where = Map{"AND": inData}
} else if len(inData) > 1 {
where["OR"] = inData
where = Map{"AND": where}
}
re := that.Db.Get(that.RouterString[1], str, where)
if re == nil {
that.Display(4, "找不到对应信息")
return
}
for k, v := range re {
column := that.MakeCode.TableColumns[that.RouterString[1]][k]
if column == nil {
continue
}
if (column["list"] == nil || column.GetBool("list")) && column.GetString("link") != "" {
re[column.GetString("link")] = that.Db.Get(column.GetString("link"), "id,"+column.GetString("value"), Map{"id": v})
}
}
that.Display(0, re)
},
"add": func(that *Context) {
adminID := that.Session("id").ToInt()
if adminID == 0 {
that.Display(2, "登录失效,请重新登录")
return
}
name := that.Req.FormValue("name")
img := that.Req.FormValue("img")
validity := ObjToInt(that.Req.FormValue("validity"))
num := ObjToInt(that.Req.FormValue("num"))
rule := that.Req.FormValue("rule")
content := that.Req.FormValue("content")
description := that.Req.FormValue("description")
if name == "" || rule == "" {
that.Display(3, "参数不足,请补充参数")
return
}
data := Map{
"name": name,
"img": img,
"rule": rule,
"admin_id": adminID,
"count": 0,
"used": 0,
"saved": 0,
"num": num,
"validity": validity,
"description": description,
"content": content,
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
}
id := that.Db.Insert("material", data)
if id == 0 {
that.Display(4, "添加材料失败,请重新添加")
return
}
data["id"] = id
that.Display(0, data)
},
"update": func(that *Context) {
inData := that.MakeCode.Edit(that.RouterString[1], that.Req)
if inData == nil {
that.Display(3, "没有找到要更新的数据")
return
}
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetString("index") != "" {
Index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": that.RouterString[2]})
parentIndex := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")})
inData["index"] = parentIndex.GetString("index") + that.RouterString[2] + ","
childNodes := that.Db.Select(that.RouterString[1], "id,`index`", Map{"index[~]": "," + that.RouterString[2] + ","})
for _, v := range childNodes {
v["index"] = strings.Replace(v.GetString("index"), Index.GetString("index"), inData.GetString("index"), -1)
that.Db.Update(that.RouterString[1], Map{"index": v["index"]}, Map{"id": v.GetCeilInt("id")})
}
}
re := that.Db.Update(that.RouterString[1], inData, Map{"id": that.RouterString[2]})
if re == 0 {
that.Display(4, "更新数据失败")
return
}
that.Display(0, re)
},
"inout": func(that *Context) {
adminID := that.Session("id").ToInt()
if adminID == 0 {
that.Display(2, "登录失效,请重新登录")
return
}
data := ObjToMap(that.Req.FormValue("data"))
texts := data.GetSlice("text")
textData := []Map{}
for k, _ := range texts {
v := texts.GetString(k)
if len(v) < 4 {
continue
}
vs := that.Db.Select("material", "name,id,content,rule,num", Map{"content[~]": v[:len(v)/2]})
for _, v1 := range vs {
if len(textData) == 0 {
textData = append(textData, v1)
}
for _, vt := range textData {
if v1.GetString("id") != vt.GetString("id") {
add := true
for _, vt1 := range textData {
if vt1.GetCeilInt("id") == v1.GetCeilInt("id") {
add = false
break
}
}
if add {
v1["count"] = 1
textData = append(textData, v1)
}
} else {
vt["count"] = vt.GetCeilInt("count") + 1
}
}
}
}
qrcode := data.GetSlice("qrcode")
for k, _ := range qrcode {
v := qrcode.GetString(k)
if len(v) < 4 {
continue
}
vs := that.Db.Select("material", "name,id,content,rule,num", Map{"content[~]": v[:len(v)/2]})
for _, v1 := range vs {
if len(textData) == 0 {
textData = append(textData, v1)
}
for _, vt := range textData {
if v1.GetString("id") != vt.GetString("id") {
v1["count"] = 1
textData = append(textData, v1)
} else {
vt["count"] = vt.GetCeilInt("count") + 1
}
}
}
}
that.Display(0, textData)
},
"search": func(that *Context) {
adminID := that.Session("id").ToInt()
if adminID == 0 {
that.Display(2, "登录失效,请重新登录")
return
}
page := ObjToInt(that.Req.FormValue("page"))
pageSize := ObjToInt(that.Req.FormValue("pageSize"))
if page < 1 {
page = 1
}
if pageSize <= 0 {
pageSize = 10
}
leftJoin := Map{"[><]admin": "material.admin_id=admin.id"}
columnStr := "material.id,material.name,material.img,material.count,material.used,material.saved,material.admin_id,admin.name AS admin_name,material.modify_time,material.state"
where := Map{"ORDER": "modify_time DESC"}
count := that.Db.Count("material", where)
reData := that.Db.Page(page, pageSize).
PageSelect("material", leftJoin, columnStr, where)
that.Display(0, Map{"count": count, "data": reData})
},
}