KunWeb/web/admin/rigger/config.go
2025-05-16 01:00:48 +08:00

171 lines
4.2 KiB
Go

package rigger
import (
"fmt"
"net/http"
"strings"
"ciyon/web/admin"
. "ciyon/zciyon"
)
func config_setwhere(post *CiyPost) (map[string]any, *CiySQL) {
query := post.Getobj("query")
csql := NewCiySQL("zc_config")
csql.Where("types like", Getstr(query, "types"))
csql.Where("params like", Getstr(query, "params"))
order := Getstr(query, "order", "id desc")
csql.Order(order)
query["order"] = order
return query, csql
}
func Config_init(w http.ResponseWriter, r *http.Request) bool {
post := NewCiyPost(w, r)
_, userid := admin.Verifyfast(CiyDB, post)
if userid == 0 {
return false
}
where, csql := config_setwhere(post)
pageno := post.Getint("pageno", 1)
pagecount := post.Getint("pagecount", 10)
csql.Limit(pageno, pagecount)
rows, mainrowcount, err := CiyDB.Get(csql, post.Getint("count"))
if err != nil {
return ErrJSON(w, "读取错误", err)
}
rows = append(rows, map[string]any{
"id": 0,
"types": "",
})
ret := map[string]any{}
ret["where"] = where
ret["pageno"] = pageno
ret["pagecount"] = pagecount
ret["count"] = mainrowcount
ret["list"] = rows
if post.Getbool("field") {
field := map[string]map[string]any{}
fshow := ""
FieldAdd(&field, &fshow, -1, "types", "参数代码")
FieldAdd(&field, &fshow, -1, "params", "参数值")
FieldAdd(&field, &fshow, -1, "_btn", "操作")
field["types"]["thwidth"] = "12em"
field["params"]["thwidth"] = "21em"
ret["fshow"] = strings.TrimLeft(fshow, ",")
ret["field"] = field
}
if post.Getbool("once") {
once := map[string]any{}
input := make([]map[string]any, 0)
input = append(input, map[string]any{
"form": "types",
"type": "input",
"name": "参数代码",
"prop": ` style="width:8em;"`,
})
input = append(input, map[string]any{
"form": "params",
"type": "input",
"name": "参数值",
"prop": ` style="width:8em;"`,
})
once["input"] = input
ret["once"] = once
}
return SuccJSON(w, ret)
}
func Config_update(w http.ResponseWriter, r *http.Request) bool {
post := NewCiyPost(w, r)
_, userid := admin.Verifyfast(CiyDB, post)
if userid == 0 {
return false
}
if admin.Nopower(CiyDB, userid, "p600u") {
return ErrJSON(w, "您未被授权操作")
}
id := post.Getint("id")
types := post.Get("types")
if types == "" {
return ErrJSON(w, "请填写代码")
}
params := post.Get("params")
var err error
var datarow map[string]any
if id > 0 {
csql := NewCiySQL("zc_config")
csql.Where("id", id)
datarow, err = CiyDB.Getone(csql)
if datarow == nil {
return ErrJSON(w, "数据不存在", err)
}
}
var updata = map[string]any{}
err = CiyDB.Tran(func() error {
var csql *CiySQL
csql = NewCiySQL("zc_config")
csql.Where("types", types)
csql.Column("id")
chkid := Toint(CiyDB.Get1(csql))
if chkid > 0 && ((id > 0 && chkid != id) || id == 0) {
return fmt.Errorf("代码重复")
}
updata["types"] = types
updata["params"] = params
csql = NewCiySQL("zc_config")
if id > 0 {
csql.Where("id", id)
_, err = CiyDB.Update(csql, updata)
} else {
id, err = CiyDB.Insert(csql, updata)
}
updata["id"] = id
if err != nil {
return fmt.Errorf("更新失败:%v", err)
}
admin.SaveLogDB(CiyDB, "zc_config", datarow, updata)
return nil
})
if err != nil {
return ErrJSON(w, "事务"+err.Error())
}
ret := map[string]any{}
ret["data"] = updata
return SuccJSON(w, ret)
}
func Config_del(w http.ResponseWriter, r *http.Request) bool {
post := NewCiyPost(w, r)
_, userid := admin.Verifyfast(CiyDB, post)
if userid == 0 {
return false
}
if admin.Nopower(CiyDB, userid, "p600d") {
return ErrJSON(w, "您未被授权操作")
}
ids := post.Get("ids")
if ids == "" {
return ErrJSON(w, "请选择至少一条")
}
csql := NewCiySQL("zc_config")
csql.Where("id in", ids)
rows, _, err := CiyDB.Get(csql)
if err != nil {
return ErrJSON(w, "读取数据错误", err)
}
vids := make([]int, 0)
err = CiyDB.Tran(func() error {
for _, row := range rows {
delid := Toint(row["id"])
Delme(CiyDB, delid, "zc_config")
admin.SaveLogDB(CiyDB, "zc_config", row, nil)
vids = append(vids, delid)
}
return nil
})
if err != nil {
return ErrJSON(w, "事务"+err.Error())
}
ret := map[string]any{}
ret["ids"] = vids
return SuccJSON(w, ret)
}