hotime/example/app/material.go

141 lines
3.6 KiB
Go
Raw Normal View History

2021-12-11 09:59:02 +00:00
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")
rule := that.Req.FormValue("rule")
if name == "" || rule == "" {
that.Display(3, "参数不足,请补充参数")
return
}
data := Map{
"name": name,
"img": img,
"rule": rule,
"admin_id": adminID,
"count": 0,
"used": 0,
"saved": 0,
"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)
},
"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 = 20
}
leftJoin := Map{"[><]admin": "material.admin_id=admin.id"}
columnStr := "sn,name,img,count,used,saved,admin_id,admin.name AS admin_name,modify_time,state"
where := Map{"state": 0, "ORDER": "modify_time DESC"}
count := that.Db.Count("material", "id", where)
reData := that.Db.Page(page, pageSize).
PageSelect("material", leftJoin, columnStr, where)
that.Display(0, Map{"count": count, "data": reData})
},
}