hotime/example/app/product.go
2021-12-11 17:59:02 +08:00

121 lines
3.2 KiB
Go

package app
import (
. "../../../hotime"
. "../../../hotime/common"
"time"
)
var productCtr = 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) {
inData := that.MakeCode.Add(that.RouterString[1], that.Req)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
re := that.Db.Insert(that.RouterString[1], inData)
if re == 0 {
that.Display(4, "无法插入对应数据")
return
}
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetString("index") != "" {
index := that.Db.Get(that.RouterString[1], "`index`", Map{"id": inData.Get("parent_id")})
inData["index"] = index.GetString("index") + ObjToStr(re) + ","
that.Db.Update(that.RouterString[1], Map{"index": inData["index"]}, Map{"id": re})
} else if inData.GetString("index") != "" {
inData["index"] = "," + ObjToStr(re) + ","
that.Db.Update(that.RouterString[1], Map{"index": inData["index"]}, Map{"id": re})
}
that.Display(0, re)
},
"update": func(that *Context) {
adminID := that.Session("id").ToInt()
if adminID == 0 {
that.Display(2, "登录失效,请重新登录")
return
}
id := ObjToInt(that.Req.FormValue("id"))
ruleSpotCheck := that.Req.FormValue("rule_spot_check")
if id == 0 || ruleSpotCheck == "" {
that.Display(3, "请求参数不足,请检查参数")
return
}
re := that.Db.Update("product", Map{"rule_spot_check": ruleSpotCheck, "modify_time": time.Now().Unix()}, Map{"id": id})
if re == 0 {
that.Display(4, "更新失败,无法更新抽检参数")
return
}
that.Display(0, "更新成功")
},
"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": "product.admin_id=admin.id"}
columnStr := "sn,name,img,count,used,saved,spot_check_saved,admin_id,admin.name AS admin_name,modify_time,state"
where := Map{"state": 0, "ORDER": "modify_time DESC"}
count := that.Db.Count("product", "id", where)
reData := that.Db.Page(page, pageSize).
PageSelect("product", leftJoin, columnStr, where)
that.Display(0, Map{"product": count, "data": reData})
},
}