接入搜索功能

This commit is contained in:
hoteas 2021-06-13 10:40:19 +08:00
parent 9e595ab6e8
commit 9ea6082d04
2 changed files with 62 additions and 53 deletions

View File

@ -524,24 +524,62 @@ func (that *MakeCode) Edit(table string, req *http.Request) Map {
func (that *MakeCode) Search(table string, req *http.Request, db *db.HoTimeDB) (string, Map, Map) {
reStr := ""
for _, v := range that.TableColumns[table] {
//不可使用,未在前端展示,但在内存中保持有
if v.GetBool("notUse") {
continue
}
if v.Get("list") == nil || v.GetBool("list") {
reStr += table + "." + v.GetString("name") + ","
}
}
if len(reStr) != 0 {
reStr = reStr[:len(reStr)-1]
}
leftJoin := Map{}
data := Map{}
keyword := Map{}
daterange := Map{}
sort := Map{}
keywordStr := req.FormValue("keyword")
for _, v := range that.TableColumns[table] {
//不可使用,未在前端展示,但在内存中保持有
if v.GetBool("notUse") {
continue
}
if v["list"] != false {
if v.GetString("link") != "" &&
v.GetString("name") != "parent_id" {
reStr += v.GetString("link") + "." +
v.GetString("value") + " AS " +
v.GetString("link") + "_" + v.GetString("name") + "_" + v.GetString("value") + ","
leftJoin["[>]"+v.GetString("link")] =
v.GetString("name") + "=" +
v.GetString("link") + ".id"
} else {
reStr += table + "." + v.GetString("name") + ","
}
if keywordStr != "" {
if v.GetString("type") == "text" {
keyword[table+"."+v.GetString("name")+"[~]"] = keywordStr
}
if v.GetString("name") == "id" {
keyword[table+"."+v.GetString("name")] = keywordStr
}
if v.GetString("link") != "" &&
v.GetString("name") != "parent_id" {
childs := db.Select(v.GetString("link"), "id", Map{v.GetString("value") + "[~]": keywordStr})
childIds := Slice{}
for _, cv := range childs {
childIds = append(childIds, cv.GetString("id"))
}
if len(childIds) != 0 {
keyword[v.GetString("link")+".id"] = childIds
}
}
}
}
}
if len(reStr) != 0 {
reStr = reStr[:len(reStr)-1]
}
search := that.TableConfig.GetMap(table).GetSlice("search")
for k, _ := range search {
@ -552,49 +590,16 @@ func (that *MakeCode) Search(table string, req *http.Request, db *db.HoTimeDB) (
searchItem := search.GetMap(k)
searchItemName := searchItem.GetString("name")
columns := that.TableConfig.GetMap(table).GetSlice("columns")
//columns := that.TableConfig.GetMap(table).GetSlice("columns")
if searchItem.GetString("type") == "search" {
for columnKey, _ := range columns {
if columns.GetMap(columnKey)["list"] == false {
for _, v := range that.TableColumns[table] {
if v["list"] == false {
continue
}
if searchItemName == "keyword" &&
columns.GetMap(columnKey).GetString("type") == "text" {
keyword[table+"."+columns.GetMap(columnKey).GetString("name")+"[~]"] = reqValue
}
if searchItemName == "keyword" &&
columns.GetMap(columnKey).GetString("name") == "id" {
keyword[table+"."+columns.GetMap(columnKey).GetString("name")] = reqValue
}
if searchItemName == "keyword" &&
columns.GetMap(columnKey).GetString("link") != "" &&
columns.GetMap(columnKey).GetString("name") != "parent_id" {
for columnChildKey, _ := range columns {
if columns.GetMap(columnChildKey).GetString("link") != "" &&
columns.GetMap(columnChildKey).GetString("name") != "parent_id" {
childs := db.Select(columns.GetMap(columnChildKey).GetString("link"), "id", Map{columns.GetMap(columnChildKey).GetString("value") + "[~]": reqValue})
childIds := Slice{}
for _, v := range childs {
childIds = append(childIds, v.GetString("id"))
}
if len(childIds) != 0 {
keyword[columns.GetMap(columnChildKey).GetString("link")+".id"] = childIds
leftJoin["[>]"+columns.GetMap(columnChildKey).GetString("link")] =
columns.GetMap(columnChildKey).GetString("name") + "=" +
columns.GetMap(columnChildKey).GetString("link") + ".id"
}
}
}
}
//日期类型
if searchItemName == "daterange" && columns.GetMap(columnKey).GetString("type") == "time" {
if searchItemName == "daterange" && v.GetString("type") == "time" {
fmt.Println(req.Form["daterange"])
daterange[table+"."+columns.GetMap(columnKey).GetString("name")+"[<>]"] = ObjToSlice(req.Form["daterange"])
daterange[table+"."+v.GetString("name")+"[<>]"] = ObjToSlice(req.Form["daterange"])
}
}

View File

@ -85,7 +85,7 @@ var {{table}}Ctr = Ctr{
"search": func(that *Context) {
columnStr,leftJoin,where := that.MakeCode.Search(that.RouterString[1], that.Req,that.Db)
columnStr,leftJoin,where := that.MakeCode.Search(that.RouterString[1], that.Req,that.Db)
page:=ObjToInt(that.Req.FormValue("page"))
pageSize:=ObjToInt(that.Req.FormValue("pageSize"))
@ -109,8 +109,12 @@ var {{table}}Ctr = Ctr{
continue
}
if (column["list"]==nil||column.GetBool("list"))&&column.GetString("link")!=""{
v[column.GetString("link")] = that.Db.Get(column.GetString("link"), column.GetString("value"), Map{"id": v.GetCeilInt(k)})
if column["list"]!=false&&column["name"]=="parent_id"&&column.GetString("link")!=""{
parentC := that.Db.Get(column.GetString("link"), column.GetString("value"), Map{"id": v.GetCeilInt(k)})
v[column.GetString("link")+"_"+column.GetString("name")+"_"+column.GetString("value")]=""
if parentC!=nil{
v[column.GetString("link")+"_"+column.GetString("name")+"_"+column.GetString("value")]=parentC.GetString(column.GetString("value"))
}
}