diff --git a/code/makecode.go b/code/makecode.go index 461de86..f84f66d 100644 --- a/code/makecode.go +++ b/code/makecode.go @@ -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 } diff --git a/code/template.go b/code/template.go index a50882f..3d98f8a 100644 --- a/code/template.go +++ b/code/template.go @@ -85,10 +85,10 @@ 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")) - + if page<1{ page=1 } @@ -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 { diff --git a/db/hotimedb.go b/db/hotimedb.go index 7bed21a..359ee4c 100644 --- a/db/hotimedb.go +++ b/db/hotimedb.go @@ -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 + "`=]": 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) }