树状结构优化

This commit is contained in:
hoteas 2022-08-25 13:02:44 +08:00
parent 96d8868694
commit 72c23499f2
3 changed files with 76 additions and 49 deletions

View File

@ -587,9 +587,7 @@ func Init(config string) *Application {
appIns.Router[codeMake.GetString("name")][k1] = appIns.Router[codeMake.GetString("name")]["hotimeCommon"] appIns.Router[codeMake.GetString("name")][k1] = appIns.Router[codeMake.GetString("name")]["hotimeCommon"]
} }
go func() { setMakeCodeListener(codeMake.GetString("name"), &appIns)
setMakeCodeLintener(codeMake.GetString("name"), &appIns)
}()
} }
@ -659,7 +657,7 @@ func SetSqliteDB(appIns *Application, config Map) {
}) })
} }
func setMakeCodeLintener(name string, appIns *Application) { func setMakeCodeListener(name string, appIns *Application) {
appIns.SetConnectListener(func(context *Context) (isFinished bool) { appIns.SetConnectListener(func(context *Context) (isFinished bool) {
codeIns := appIns.MakeCodeRouter[name] codeIns := appIns.MakeCodeRouter[name]

View File

@ -368,7 +368,7 @@ var TptProject = Proj{
return return
} }
parent_ids := parentIndex.GetString("parent_ids") + that.RouterString[2] + "," parent_ids := parentIndex.GetString("parent_ids") + that.RouterString[2] + ","
childNodes := that.Db.Select(tableName, "id,`parent_ids``", Map{"parent_ids[~]": "," + that.RouterString[2] + ","}) childNodes := that.Db.Select(tableName, "id,`parent_ids`", Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
for _, v := range childNodes { for _, v := range childNodes {
v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), parent_ids, -1) v["parent_ids"] = strings.Replace(v.GetString("parent_ids"), Index.GetString("parent_ids"), parent_ids, -1)
that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")}) that.Db.Update(tableName, Map{"parent_ids": v["parent_ids"]}, Map{"id": v.GetCeilInt("id")})
@ -395,11 +395,11 @@ var TptProject = Proj{
re := int64(0) re := int64(0)
//有索引则删除对应的参数 //有索引则删除对应的参数
if that.MakeCodeRouter[hotimeName].TableColumns[tableName]["parent_id"] != nil { if that.MakeCodeRouter[hotimeName].TableColumns[tableName]["parent_id"] != nil {
that.Db.Delete(tableName, Map{"parent_ids[~]": "," + that.RouterString[2] + ","}) re = that.Db.Delete(tableName, Map{"parent_ids[~]": "," + that.RouterString[2] + ","})
} else {
re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]})
} }
re = that.Db.Delete(tableName, Map{"id": that.RouterString[2]})
if re == 0 { if re == 0 {
that.Display(4, "删除数据失败") that.Display(4, "删除数据失败")
return return

View File

@ -939,36 +939,41 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
for k, _ := range search { for k, _ := range search {
searchItem := search.GetMap(k) searchItem := search.GetMap(k)
searchItemName := searchItem.GetString("name") searchItemName := searchItem.GetString("name")
if data[table+"."+searchItemName] != nil {
delete(data, table+"."+searchItemName)
}
reqValue := req.Form[searchItemName]
parent_idsStr := "" parent_idsStr := ""
parent_ids := that.TableColumns[searchItem.GetString("link")]["parent_ids"] if searchItem.GetString("link") != "" {
if parent_ids != nil {
parent_idsStr = "parent_ids[~]"
}
index := that.TableColumns[searchItem.GetString("link")]["index"]
if index != nil {
parent_idsStr = "index[~]"
}
reqValue := req.Form[search.GetMap(k).GetString("name")]
//reqValue := req.FormValue(search.GetMap(k).GetString("name"))
if len(reqValue) == 0 || reqValue[0] == "" {
if parent_idsStr != "" && userData[searchItem.GetString("name")] != nil {
where := Map{parent_idsStr: "," + ObjToStr(userData.GetCeilInt64(searchItem.GetString("name"))) + ","}
r := db.Select(searchItem.GetString("link"), "id", where)
reqValue = []string{}
for _, v := range r {
reqValue = append(reqValue, v.GetString("id"))
}
data[table+"."+searchItemName] = reqValue
parent_ids := that.TableColumns[searchItem.GetString("link")]["parent_ids"]
if parent_ids != nil {
parent_idsStr = "parent_ids[~]"
}
index := that.TableColumns[searchItem.GetString("link")]["index"]
if index != nil {
parent_idsStr = "index[~]"
} }
continue //reqValue := req.FormValue(search.GetMap(k).GetString("name"))
} if len(reqValue) == 0 || reqValue[0] == "" {
if parent_idsStr != "" && userData[searchItem.GetString("name")] != nil {
where := Map{parent_idsStr: "," + ObjToStr(userData.GetCeilInt64(searchItem.GetString("name"))) + ","}
r := db.Select(searchItem.GetString("link"), "id", where)
reqValue = []string{}
for _, v := range r {
reqValue = append(reqValue, v.GetString("id"))
}
data[table+"."+searchItemName] = reqValue
}
continue
}
}
//columns := that.TableConfig.GetMap(table).GetSlice("columns") //columns := that.TableConfig.GetMap(table).GetSlice("columns")
if searchItem.GetString("type") == "search" { if searchItem.GetString("type") == "search" {
for _, v := range that.TableColumns[table] { for _, v := range that.TableColumns[table] {
@ -983,7 +988,7 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
} }
if v.GetString("type") == "unixtime" { if v.GetString("type") == "unixtime" {
//fmt.Println(req.Form["daterange"]) //fmt.Println(req.Form["daterange"])
if len(req.Form["daterange"]) == 1 { if len(req.Form["daterange"]) == 1 && req.Form["daterange"][0] != "" {
daterange[table+"."+v.GetString("name")+"[>]"] = req.FormValue("daterange") daterange[table+"."+v.GetString("name")+"[>]"] = req.FormValue("daterange")
} else if len(req.Form["daterange"]) == 2 { } else if len(req.Form["daterange"]) == 2 {
@ -993,7 +998,7 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
if v.GetString("type") == "time" { if v.GetString("type") == "time" {
//fmt.Println(req.Form["daterange"]) //fmt.Println(req.Form["daterange"])
if len(req.Form["daterange"]) == 1 { if len(req.Form["daterange"]) == 1 && req.Form["daterange"][0] != "" {
t := time.Unix(ObjToCeilInt64(req.FormValue("daterange")), 0).Format("2006-01-02 15:04:05") t := time.Unix(ObjToCeilInt64(req.FormValue("daterange")), 0).Format("2006-01-02 15:04:05")
daterange[table+"."+v.GetString("name")+"[>]"] = t daterange[table+"."+v.GetString("name")+"[>]"] = t
@ -1050,12 +1055,13 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
} }
} }
if searchItem.GetString("name") == "sort" { if len(reqValue) != 0 && searchItem.GetString("name") == "sort" && reqValue[0] != "" {
sortMap["ORDER"] = table + "." + reqValue[0] sortMap["ORDER"] = table + "." + reqValue[0]
} }
continue continue
} }
//树节点模式搜索 //树节点模式搜索
if searchItemName == "parent_id" { if searchItemName == "parent_id" {
parentID := ObjToInt(req.FormValue("parent_id")) parentID := ObjToInt(req.FormValue("parent_id"))
@ -1065,7 +1071,15 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
} else { } else {
if req.FormValue("showself") != "" { if req.FormValue("showself") != "" {
data["OR"] = Map{table + ".parent_id": reqValue, table + ".id": reqValue} where := Map{}
for _, v := range reqValue {
if len(where) == 0 {
where[table+"."+parent_idsStr] = "," + v + ","
continue
}
where = Map{"OR": where, table + "." + parent_idsStr: "," + v + ","}
}
data["OR"] = Map{"OR": where, table + ".id": reqValue}
} else { } else {
data[table+".parent_id"] = reqValue data[table+".parent_id"] = reqValue
} }
@ -1079,28 +1093,43 @@ func (that *MakeCode) Search(table string, userData Map, req *http.Request, db *
if parent_idsStr != "" { if parent_idsStr != "" {
where := Map{} where := Map{}
if len(reqValue) == 1 {
for _, v := range reqValue {
if len(where) == 0 { if len(where) == 0 {
where[parent_idsStr] = "," + v + "," where[table+"."+searchItem.GetString("name")] = reqValue[0]
continue
} else {
where = Map{"OR": where, table + "." + searchItem.GetString("name"): reqValue[0]}
}
} else {
for _, v := range reqValue {
if len(where) == 0 {
where[searchItem.GetString("link")+"."+parent_idsStr] = "," + v + ","
continue
}
where = Map{"OR": where, searchItem.GetString("link") + "." + parent_idsStr: "," + v + ","}
} }
where = Map{"OR": where, parent_idsStr: "," + v + ","}
} }
//用户 //用户
if userData[searchItem.GetString("name")] != nil { if userData[searchItem.GetString("name")] != nil {
where = Map{"AND": Map{parent_idsStr: "," + ObjToStr(userData.GetCeilInt64(searchItem.GetString("name"))) + ",", "OR": where}}
} where = Map{"AND": Map{searchItem.GetString("link") + "." + parent_idsStr: "," + ObjToStr(userData.GetCeilInt64(searchItem.GetString("name"))) + ",", "OR": where}}
r := db.Select(searchItem.GetString("link"), "id", where)
for _, v := range r {
reqValue = append(reqValue, v.GetString("id"))
} }
data["OR"] = where
continue
//r := db.Select(searchItem.GetString("link"), "id", where)
//for _, v := range r {
// reqValue = append(reqValue, v.GetString("id"))
//}
} }
} }
data[table+"."+searchItemName] = reqValue if len(reqValue) != 0 && reqValue[0] != "" {
data[table+"."+searchItemName] = reqValue
}
} }
if sortMap["ORDER"] == nil { if sortMap["ORDER"] == nil {