接入搜索功能

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
}
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 := ""
for _, v := range that.TableColumns[table] {
//不可使用,未在前端展示,但在内存中保持有
@ -530,13 +530,13 @@ func (that *MakeCode) Search(table string, req *http.Request) (string, Map) {
continue
}
if v.Get("list") == nil || v.GetBool("list") {
reStr += v.GetString("name") + ","
reStr += table + "." + v.GetString("name") + ","
}
}
if len(reStr) != 0 {
reStr = reStr[:len(reStr)-1]
}
leftJoin := Map{}
data := Map{}
keyword := 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")
if searchItem.GetString("type") == "search" {
for columnKey, _ := range columns {
if columns.GetMap(columnKey)["list"] == false {
continue
}
if searchItemName == "keyword" &&
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" {
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) {
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"))
pageSize:=ObjToInt(that.Req.FormValue("pageSize"))
@ -96,9 +96,10 @@ var {{table}}Ctr = Ctr{
if pageSize<=0{
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).
PageSelect(that.RouterString[1], columnStr, where)
PageSelect(that.RouterString[1],leftJoin,columnStr, where)
for _, v := range reData {

View File

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