hotime/example/app/material_inout.go

175 lines
4.8 KiB
Go
Raw Normal View History

2021-12-11 09:59:02 +00:00
package app
import (
. "../../../hotime"
. "../../../hotime/common"
"strings"
"time"
)
var material_inoutCtr = Ctr{
"info": func(that *Context) {
adminID := that.Session("id").ToInt()
if adminID == 0 {
that.Display(2, "登录失效,请重新登录")
return
}
id := ObjToInt(that.Req.FormValue("id"))
if id == 0 {
that.Display(3, "请求参数不足,请检查参数")
return
}
re := that.Db.Get("material_inout", "*", Map{"id": id})
if re == nil {
that.Display(4, "找不到对应信息")
return
}
that.Display(0, re)
},
"add": func(that *Context) {
adminID := that.Session("id").ToInt()
if adminID == 0 {
that.Display(2, "登录失效,请重新登录")
return
}
img := that.Req.FormValue("img")
rule := that.Req.FormValue("rule")
materialId := ObjToInt(that.Req.FormValue("material_id"))
produceId := ObjToInt(that.Req.FormValue("produce_id"))
2021-12-14 21:56:31 +00:00
count := ObjToInt(that.Req.FormValue("num"))
2021-12-11 09:59:02 +00:00
state := ObjToInt(that.Req.FormValue("state"))
2021-12-14 21:56:31 +00:00
content := that.Req.FormValue("content")
description := that.Req.FormValue("description")
2021-12-11 09:59:02 +00:00
if rule == "" || materialId == 0 || count == 0 {
that.Display(3, "参数不足,请补充参数")
return
}
2021-12-14 21:56:31 +00:00
count1 := count
2021-12-11 09:59:02 +00:00
if state > 0 {
count = -count
}
that.Db.Update("material", Map{"count[#]": "count+" + ObjToStr(count), "saved[#]": "saved+" + ObjToStr(count)}, Map{"id": materialId})
material := that.Db.Get("material", "*", Map{"id": materialId})
data := Map{
"img": img,
"rule": rule,
"admin_id": adminID,
"material_id": materialId,
2021-12-14 21:56:31 +00:00
"count": count1,
2021-12-11 09:59:02 +00:00
"saved": material.GetCeilInt("count"),
"create_time": time.Now().Unix(),
"modify_time": time.Now().Unix(),
"produce_id": produceId,
2021-12-14 21:56:31 +00:00
"description": description,
"content": content,
2021-12-11 09:59:02 +00:00
"state": state,
}
id := that.Db.Insert("material_inout", data)
2021-12-14 21:56:31 +00:00
2021-12-11 09:59:02 +00:00
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)
},
"remove": func(that *Context) {
inData := that.MakeCode.Delete(that.RouterString[1], that.Req)
if inData == nil {
that.Display(3, "请求参数不足")
return
}
re := int64(0)
//索引管理,便于检索以及权限
if inData.Get("parent_id") != nil && inData.GetSlice("index") != nil {
re = that.Db.Delete(that.RouterString[1], Map{"index[~]": "," + that.RouterString[2] + ","})
} else {
re = that.Db.Delete(that.RouterString[1], Map{"id": that.RouterString[2]})
}
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"))
2021-12-11 20:22:30 +00:00
materialId := ObjToInt(that.Req.FormValue("id"))
2021-12-11 09:59:02 +00:00
if page < 1 {
page = 1
}
if pageSize <= 0 {
2021-12-11 20:22:30 +00:00
pageSize = 10
2021-12-11 09:59:02 +00:00
}
2021-12-11 20:22:30 +00:00
columnStr := "material_inout.id,material_inout.material_id,material_inout.sn,material.name,material_inout.img,material_inout.count,material_inout.saved,material_inout.admin_id,admin.name AS admin_name,material_inout.modify_time,material_inout.state"
2021-12-11 09:59:02 +00:00
leftJoin := Map{"[><]material": "material_inout.material_id=material.id",
"[><]admin": "material_inout.admin_id=admin.id",
}
where := Map{"ORDER": "modify_time DESC"}
2021-12-11 20:22:30 +00:00
if materialId != 0 {
where["material_id"] = materialId
}
count := that.Db.Count("material_inout", where)
2021-12-11 09:59:02 +00:00
reData := that.Db.Page(page, pageSize).
PageSelect("material_inout", leftJoin, columnStr, where)
that.Display(0, Map{"count": count, "data": reData})
},
}