forked from golang/hotime
189 lines
5.3 KiB
Go
189 lines
5.3 KiB
Go
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"))
|
|
count := ObjToInt(that.Req.FormValue("num"))
|
|
state := ObjToInt(that.Req.FormValue("state"))
|
|
|
|
content := that.Req.FormValue("content")
|
|
description := that.Req.FormValue("description")
|
|
|
|
if rule == "" || materialId == 0 || count == 0 {
|
|
that.Display(3, "参数不足,请补充参数")
|
|
return
|
|
}
|
|
count1 := count
|
|
if state > 0 {
|
|
count = -count
|
|
}
|
|
|
|
produce_material := that.Db.Get("produce_material", "id", Map{"AND": Map{"produce_id": produceId, "material_id": materialId}})
|
|
if produce_material == nil {
|
|
that.Db.Insert("produce_material", Map{"produce_id": produceId,
|
|
"create_time": time.Now().Unix(),
|
|
"modify_time": time.Now().Unix(),
|
|
"admin_id": adminID,
|
|
"material_id": materialId})
|
|
}
|
|
if state == 0 {
|
|
|
|
that.Db.Update("material", Map{"count[#]": "count+" + ObjToStr(count), "saved[#]": "saved+" + ObjToStr(count)}, Map{"id": materialId})
|
|
} else {
|
|
that.Db.Update("material", Map{"count[#]": "count" + ObjToStr(count), "used[#]": "used+" + 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,
|
|
"count": count1,
|
|
"saved": material.GetCeilInt("count"),
|
|
"create_time": time.Now().Unix(),
|
|
"modify_time": time.Now().Unix(),
|
|
"produce_id": produceId,
|
|
"description": description,
|
|
"content": content,
|
|
"state": state,
|
|
}
|
|
id := that.Db.Insert("material_inout", 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)
|
|
},
|
|
"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"))
|
|
materialId := ObjToInt(that.Req.FormValue("id"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
|
|
if pageSize <= 0 {
|
|
pageSize = 10
|
|
}
|
|
columnStr := "material_inout.id,material_inout.material_id,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"
|
|
leftJoin := Map{"[><]material": "material_inout.material_id=material.id",
|
|
"[><]admin": "material_inout.admin_id=admin.id",
|
|
}
|
|
where := Map{"ORDER": "modify_time DESC"}
|
|
|
|
if materialId != 0 {
|
|
where["material_id"] = materialId
|
|
}
|
|
count := that.Db.Count("material_inout", where)
|
|
reData := that.Db.Page(page, pageSize).
|
|
PageSelect("material_inout", leftJoin, columnStr, where)
|
|
|
|
that.Display(0, Map{"count": count, "data": reData})
|
|
},
|
|
}
|