接入搜索功能

This commit is contained in:
hoteas 2021-06-13 09:53:37 +08:00
parent 8b6dd5fdad
commit 9e595ab6e8
3 changed files with 122 additions and 30 deletions

View File

@ -522,7 +522,7 @@ func (that *MakeCode) Edit(table string, req *http.Request) Map {
return data return data
} }
func (that *MakeCode) Search(table string, req *http.Request) (string, Map) { func (that *MakeCode) Search(table string, req *http.Request, db *db.HoTimeDB) (string, Map, Map) {
reStr := "" reStr := ""
for _, v := range that.TableColumns[table] { for _, v := range that.TableColumns[table] {
//不可使用,未在前端展示,但在内存中保持有 //不可使用,未在前端展示,但在内存中保持有
@ -530,13 +530,13 @@ func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
continue continue
} }
if v.Get("list") == nil || v.GetBool("list") { if v.Get("list") == nil || v.GetBool("list") {
reStr += v.GetString("name") + "," reStr += table + "." + v.GetString("name") + ","
} }
} }
if len(reStr) != 0 { if len(reStr) != 0 {
reStr = reStr[:len(reStr)-1] reStr = reStr[:len(reStr)-1]
} }
leftJoin := Map{}
data := Map{} data := Map{}
keyword := Map{} keyword := Map{}
daterange := Map{} daterange := Map{}
@ -555,14 +555,46 @@ func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
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 columnKey, _ := range columns { for columnKey, _ := range columns {
if columns.GetMap(columnKey)["list"] == false {
continue
}
if searchItemName == "keyword" && if searchItemName == "keyword" &&
columns.GetMap(columnKey).GetString("type") == "text" { columns.GetMap(columnKey).GetString("type") == "text" {
keyword[columns.GetMap(columnKey).GetString("name")+"[~]"] = reqValue 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" && columns.GetMap(columnKey).GetString("type") == "time" {
fmt.Println(req.Form["daterange"]) fmt.Println(req.Form["daterange"])
daterange[columns.GetMap(columnKey).GetString("name")+"[<>]"] = ObjToSlice(req.Form["daterange"]) daterange[table+"."+columns.GetMap(columnKey).GetString("name")+"[<>]"] = ObjToSlice(req.Form["daterange"])
} }
} }
@ -611,5 +643,5 @@ func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
} }
} }
return reStr, where return reStr, leftJoin, where
} }

View File

@ -85,7 +85,7 @@ var {{table}}Ctr = Ctr{
"search": func(that *Context) { "search": func(that *Context) {
columnStr, where := that.MakeCode.Search(that.RouterString[1], that.Req) columnStr,leftJoin,where := that.MakeCode.Search(that.RouterString[1], that.Req,that.Db)
page:=ObjToInt(that.Req.FormValue("page")) page:=ObjToInt(that.Req.FormValue("page"))
pageSize:=ObjToInt(that.Req.FormValue("pageSize")) pageSize:=ObjToInt(that.Req.FormValue("pageSize"))
@ -96,9 +96,10 @@ var {{table}}Ctr = Ctr{
if pageSize<=0{ if pageSize<=0{
pageSize=20 pageSize=20
} }
count:=that.Db.Count(that.RouterString[1], where)
count:=that.Db.Count(that.RouterString[1],leftJoin,where)
reData := that.Db.Page(page, pageSize). reData := that.Db.Page(page, pageSize).
PageSelect(that.RouterString[1], columnStr, where) PageSelect(that.RouterString[1],leftJoin,columnStr, where)
for _, v := range reData { for _, v := range reData {

View File

@ -581,6 +581,15 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
y++ y++
} }
if x == len(condition) && y == len(vcond) { if x == len(condition) && y == len(vcond) {
if reflect.ValueOf(v).Type().String() == "common.Slice" && len(v.(Slice)) == 0 {
continue
}
if reflect.ValueOf(v).Type().String() == "[]interface {}" && len(v.([]interface{})) == 0 {
continue
}
tv, vv := that.varCond(k, v) tv, vv := that.varCond(k, v)
where += tv where += tv
@ -636,6 +645,7 @@ func (that *HoTimeDB) where(data Map) (string, []interface{}) {
} }
func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) { func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
where := "" where := ""
res := make([]interface{}, 0) res := make([]interface{}, 0)
length := len(k) length := len(k)
@ -645,42 +655,72 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
switch Substr(k, length-3, 3) { switch Substr(k, length-3, 3) {
case "[>]": case "[>]":
k = strings.Replace(k, "[>]", "", -1) k = strings.Replace(k, "[>]", "", -1)
where += "`" + k + "`>? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + ">? "
res = append(res, v) res = append(res, v)
case "[<]": case "[<]":
k = strings.Replace(k, "[<]", "", -1) k = strings.Replace(k, "[<]", "", -1)
where += "`" + k + "`<? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + "<? "
res = append(res, v) res = append(res, v)
case "[!]": case "[!]":
k = strings.Replace(k, "[!]", "", -1) k = strings.Replace(k, "[!]", "", -1)
if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where, res = that.notIn(k, v, where, res) where, res = that.notIn(k, v, where, res)
case "[#]": case "[#]":
k = strings.Replace(k, "[#]", "", -1) k = strings.Replace(k, "[#]", "", -1)
if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += " " + k + "=" + ObjToStr(v) where += " " + k + "=" + ObjToStr(v)
case "[#!]": case "[#!]":
k = strings.Replace(k, "[#!]", "", -1) k = strings.Replace(k, "[#!]", "", -1)
if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += " " + k + "!=" + ObjToStr(v) where += " " + k + "!=" + ObjToStr(v)
case "[!#]": case "[!#]":
k = strings.Replace(k, "[!#]", "", -1) k = strings.Replace(k, "[!#]", "", -1)
if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += " " + k + "!=" + ObjToStr(v) where += " " + k + "!=" + ObjToStr(v)
case "[~]": case "[~]":
k = strings.Replace(k, "[~]", "", -1) k = strings.Replace(k, "[~]", "", -1)
where += "`" + k + "` LIKE ? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + " LIKE ? "
v = "%" + ObjToStr(v) + "%" v = "%" + ObjToStr(v) + "%"
res = append(res, v) res = append(res, v)
case "[!~]": //左边任意 case "[!~]": //左边任意
k = strings.Replace(k, "[~]", "", -1) k = strings.Replace(k, "[~]", "", -1)
where += "`" + k + "` LIKE ? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + " LIKE ? "
v = "%" + ObjToStr(v) v = "%" + ObjToStr(v)
res = append(res, v) res = append(res, v)
case "[~!]": //右边任意 case "[~!]": //右边任意
k = strings.Replace(k, "[~]", "", -1) k = strings.Replace(k, "[~]", "", -1)
where += "`" + k + "` LIKE ? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + " LIKE ? "
v = ObjToStr(v) + "%" v = ObjToStr(v) + "%"
res = append(res, v) res = append(res, v)
case "[~~]": //手动任意 case "[~~]": //手动任意
k = strings.Replace(k, "[~]", "", -1) k = strings.Replace(k, "[~]", "", -1)
where += "`" + k + "` LIKE ? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + " LIKE ? "
//v = ObjToStr(v) //v = ObjToStr(v)
res = append(res, v) res = append(res, v)
default: default:
@ -691,25 +731,40 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
switch Substr(k, length-4, 4) { switch Substr(k, length-4, 4) {
case "[>=]": case "[>=]":
k = strings.Replace(k, "[>=]", "", -1) k = strings.Replace(k, "[>=]", "", -1)
where += "`" + k + "`>=? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + ">=? "
res = append(res, v) res = append(res, v)
case "[<=]": case "[<=]":
k = strings.Replace(k, "[<=]", "", -1) k = strings.Replace(k, "[<=]", "", -1)
where += "`" + k + "`<=? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + "<=? "
res = append(res, v) res = append(res, v)
case "[><]": case "[><]":
k = strings.Replace(k, "[><]", "", -1) k = strings.Replace(k, "[><]", "", -1)
where += "`" + k + "` NOT BETWEEN ? AND ? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + " NOT BETWEEN ? AND ? "
res = append(res, v.(Slice)[0]) res = append(res, v.(Slice)[0])
res = append(res, v.(Slice)[1]) res = append(res, v.(Slice)[1])
case "[<>]": case "[<>]":
k = strings.Replace(k, "[<>]", "", -1) k = strings.Replace(k, "[<>]", "", -1)
where += "`" + k + "` BETWEEN ? AND ? " if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
where += k + " BETWEEN ? AND ? "
res = append(res, v.(Slice)[0]) res = append(res, v.(Slice)[0])
res = append(res, v.(Slice)[1]) res = append(res, v.(Slice)[1])
default: default:
if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
if reflect.ValueOf(v).Type().String() == "common.Slice" { if reflect.ValueOf(v).Type().String() == "common.Slice" {
where += "`" + k + "` IN (" where += k + " IN ("
res = append(res, v.(Slice)...) res = append(res, v.(Slice)...)
if len(v.(Slice)) == 0 { if len(v.(Slice)) == 0 {
where += ") " where += ") "
@ -725,7 +780,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
} }
} else { } else {
where += "`" + k + "`=? " where += k + "=? "
res = append(res, v) res = append(res, v)
} }
@ -737,12 +792,15 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
where += " " + ObjToStr(v) + " " where += " " + ObjToStr(v) + " "
} else { } else {
//fmt.Println(reflect.ValueOf(v).Type().String()) //fmt.Println(reflect.ValueOf(v).Type().String())
if !strings.Contains(k, ".") {
k = "`" + k + "`"
}
if v == nil { if v == nil {
where += "`" + k + "` IS NULL" where += k + " IS NULL"
} else if reflect.ValueOf(v).Type().String() == "common.Slice" { } else if reflect.ValueOf(v).Type().String() == "common.Slice" {
//fmt.Println(v) //fmt.Println(v)
where += "`" + k + "` IN (" where += k + " IN ("
res = append(res, v.(Slice)...) res = append(res, v.(Slice)...)
for i := 0; i < len(v.(Slice)); i++ { for i := 0; i < len(v.(Slice)); i++ {
if i+1 != len(v.(Slice)) { if i+1 != len(v.(Slice)) {
@ -753,7 +811,8 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
//res=append(res,(v.(Slice))[i]) //res=append(res,(v.(Slice))[i])
} }
} else if reflect.ValueOf(v).Type().String() == "[]interface {}" { } else if reflect.ValueOf(v).Type().String() == "[]interface {}" {
where += "`" + k + "` IN ("
where += k + " IN ("
res = append(res, v.([]interface{})...) res = append(res, v.([]interface{})...)
for i := 0; i < len(v.([]interface{})); i++ { for i := 0; i < len(v.([]interface{})); i++ {
if i+1 != len(v.([]interface{})) { if i+1 != len(v.([]interface{})) {
@ -765,7 +824,7 @@ func (that *HoTimeDB) varCond(k string, v interface{}) (string, []interface{}) {
} }
} else { } else {
where += "`" + k + "`=? " where += k + "=? "
res = append(res, v) res = append(res, v)
} }
@ -780,10 +839,10 @@ func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
//fmt.Println(reflect.ValueOf(v).Type().String()) //fmt.Println(reflect.ValueOf(v).Type().String())
if v == nil { if v == nil {
where += "`" + k + "` IS NOT NULL " where += k + " IS NOT NULL "
} else if reflect.ValueOf(v).Type().String() == "common.Slice" { } else if reflect.ValueOf(v).Type().String() == "common.Slice" {
where += "`" + k + "` NOT IN (" where += k + " NOT IN ("
res = append(res, v.(Slice)...) res = append(res, v.(Slice)...)
for i := 0; i < len(v.(Slice)); i++ { for i := 0; i < len(v.(Slice)); i++ {
if i+1 != len(v.(Slice)) { if i+1 != len(v.(Slice)) {
@ -794,7 +853,7 @@ func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
//res=append(res,(v.(Slice))[i]) //res=append(res,(v.(Slice))[i])
} }
} else if reflect.ValueOf(v).Type().String() == "[]interface {}" { } else if reflect.ValueOf(v).Type().String() == "[]interface {}" {
where += "`" + k + "` NOT IN (" where += k + " NOT IN ("
res = append(res, v.([]interface{})...) res = append(res, v.([]interface{})...)
for i := 0; i < len(v.([]interface{})); i++ { for i := 0; i < len(v.([]interface{})); i++ {
if i+1 != len(v.([]interface{})) { if i+1 != len(v.([]interface{})) {
@ -806,7 +865,7 @@ func (that *HoTimeDB) notIn(k string, v interface{}, where string, res []interfa
} }
} else { } else {
where += "`" + k + "` !=? " where += k + " !=? "
res = append(res, v) res = append(res, v)
} }