From e8b68d4ccbeb5070ee11f0aa22f8b4043b304600 Mon Sep 17 00:00:00 2001 From: gazebo Date: Tue, 23 Oct 2018 11:20:44 +0800 Subject: [PATCH] - GetStore store map query cond changed. --- business/jxstore/cms/store.go | 198 ++++++++++++++++++---------------- controllers/cms_store.go | 8 +- 2 files changed, 110 insertions(+), 96 deletions(-) diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index 8256e1327..638a1a6ac 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -44,96 +44,6 @@ var ( // todo 门店绑定信息可以考虑以数组形式返回,而不是现在这样 func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interface{}, offset, pageSize int) (retVal *StoresInfo, err error) { - sqlWhere := ` - FROM store t1 - LEFT JOIN place city ON t1.city_code = city.code AND city.level = 2 - LEFT JOIN place district ON t1.district_code = district.code AND district.level = 3 - LEFT JOIN store_map m1 ON t1.id = m1.store_id AND m1.deleted_at = ? - LEFT JOIN store_courier_map m2 ON t1.id = m2.store_id AND m2.deleted_at = ? - WHERE t1.deleted_at = ? - ` - sqlParams := []interface{}{ - utils.DefaultTimeValue, - utils.DefaultTimeValue, - utils.DefaultTimeValue, - } - if keyword != "" { - keywordLike := "%" + keyword + "%" - sqlWhere += " AND (t1.name LIKE ? OR t1.address LIKE ? OR t1.tel1 LIKE ? OR t1.tel2 LIKE ? OR t1.last_operator LIKE ? OR city.name LIKE ?" - sqlParams = append(sqlParams, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike) - - if keywordInt64, err2 := strconv.ParseInt(keyword, 10, 64); err2 == nil { - sqlWhere += " OR t1.id = ? OR t1.city_code = ? OR t1.district_code = ? OR t1.lng = ? OR t1.lat = ?" - sqlParams = append(sqlParams, keywordInt64, keywordInt64, keywordInt64, keywordInt64, keywordInt64) - } - sqlWhere += ")" - } - - if params["storeID"] != nil { - sqlWhere += " AND t1.id = ?" - sqlParams = append(sqlParams, params["storeID"].(int)) - } - if params["name"] != nil { - sqlWhere += " AND t1.name LIKE ?" - sqlParams = append(sqlParams, "%"+params["name"].(string)+"%") - } - if params["placeID"] != nil { - level := 2 - if params["placeLevel"] != nil { - level = params["placeLevel"].(int) - } - if level == 2 { - sqlWhere += " AND t1.city_code = ?" - } else { - sqlWhere += " AND t1.district_code = ?" - } - sqlParams = append(sqlParams, params["placeID"].(int)) - } - if params["address"] != nil { - sqlWhere += " AND t1.address LIKE ?" - sqlParams = append(sqlParams, "%"+params["address"].(string)+"%") - } - if params["tel"] != nil { - sqlWhere += " AND (t1.tel1 LIKE ? OR t1.tel2 LIKE ?)" - sqlParams = append(sqlParams, "%"+params["tel"].(string)+"%") - sqlParams = append(sqlParams, "%"+params["tel"].(string)+"%") - } - - if params["fromStatus"] != nil { - fromStatus := params["fromStatus"].(int) - toStatus := fromStatus - if params["toStatus"] != nil { - toStatus = params["toStatus"].(int) - } - sqlWhere += " AND t1.status >= ? AND t1.status <= ?" - sqlParams = append(sqlParams, fromStatus, toStatus) - } - if vendorStoreCond := strings.ToUpper(utils.Interface2String(params["vendorStoreCond"])); vendorStoreCond == "AND" || vendorStoreCond == "OR" { - sqlVendorStoreCond := "" - if vendorStoreCond == "AND" { - sqlVendorStoreCond += " AND ( 1 = 1" - } else { - sqlVendorStoreCond += " AND ( 1 = 0" - } - condMap := map[string]int{ - "jdm": utils.Interface2DirectIntWithDefault(params["jdCond"], 0), - "elmm": utils.Interface2DirectIntWithDefault(params["elmCond"], 0), - "ebaim": utils.Interface2DirectIntWithDefault(params["ebaiCond"], 0), - } - for tableName, cond := range condMap { - if cond != 0 { - sqlVendorStoreCond += " " + vendorStoreCond + " " + tableName - } - if cond == -1 { - sqlVendorStoreCond += ".vendor_store_id IS NULL" - } else if cond == 1 { - sqlVendorStoreCond += ".vendor_store_id IS NOT NULL" - } - } - if sqlVendorStoreCond != " AND ( 1 = 0" { - sqlWhere += sqlVendorStoreCond + ")" - } - } sql := ` SELECT SQL_CALC_FOUND_ROWS CAST(t1.lng AS DECIMAL(15,6))/1000000 float_lng, @@ -160,7 +70,110 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa district.name district_name, CONCAT("[", GROUP_CONCAT(DISTINCT CONCAT('{"vendorStoreID":"', m1.vendor_store_id, '", "vendorID":', m1.vendor_id, ', "status":', m1.status, "}")), "]") store_map_str, CONCAT("[", GROUP_CONCAT(DISTINCT CONCAT('{"vendorStoreID":"', m2.vendor_store_id, '", "vendorID":', m2.vendor_id, ', "status":', m2.status, "}")), "]") courier_map_str - ` + sqlWhere + ` + FROM store t1 + LEFT JOIN place city ON t1.city_code = city.code AND city.level = 2 + LEFT JOIN place district ON t1.district_code = district.code AND district.level = 3 + LEFT JOIN store_map m1 ON t1.id = m1.store_id AND m1.deleted_at = ? + LEFT JOIN store_courier_map m2 ON t1.id = m2.store_id AND m2.deleted_at = ? + ` + sqlParams := []interface{}{ + utils.DefaultTimeValue, + utils.DefaultTimeValue, + } + sqlWhere := ` + WHERE t1.deleted_at = ? + ` + sqlWhereParams := []interface{}{ + utils.DefaultTimeValue, + } + for mapCondKey, tableName := range map[string]string{ + "vendorStoreCond": "store_map", + "courierStoreCond": "store_courier_map", + } { + if mapCond := strings.ToUpper(utils.Interface2String(params[mapCondKey])); mapCond == "AND" || mapCond == "OR" { + mapCondsStr := utils.Interface2String(params[mapCondKey+"s"]) + if mapCondsStr != "" { + var vendorStoreConds map[string]int + if err = utils.UnmarshalUseNumber([]byte(mapCondsStr), &vendorStoreConds); err != nil { + return nil, err + } + sqlVendorStoreCond := "" + if mapCond == "AND" { + sqlVendorStoreCond += " AND ( 1 = 1" + } else { + sqlVendorStoreCond += " AND ( 1 = 0" + } + for vendor, cond := range vendorStoreConds { + tableAlias := tableName + vendor + if cond != 0 { + sql += " LEFT JOIN " + tableName + " " + tableAlias + " ON " + tableAlias + ".vendor_id = ? AND " + tableAlias + ".store_id = t1.id AND " + tableAlias + ".deleted_at = ?" + sqlParams = append(sqlParams, vendor, utils.DefaultTimeValue) + if cond == 1 { + sqlVendorStoreCond += " " + mapCond + " " + tableAlias + ".id IS NOT NULL" + } else { + sqlVendorStoreCond += " " + mapCond + " " + tableAlias + ".id IS NULL" + } + } + } + if sqlVendorStoreCond != " AND ( 1 = 0" { + sqlWhere += sqlVendorStoreCond + ")" + } + } + } + } + if keyword != "" { + keywordLike := "%" + keyword + "%" + sqlWhere += " AND (t1.name LIKE ? OR t1.address LIKE ? OR t1.tel1 LIKE ? OR t1.tel2 LIKE ? OR t1.last_operator LIKE ? OR city.name LIKE ?" + sqlWhereParams = append(sqlWhereParams, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike, keywordLike) + + if keywordInt64, err2 := strconv.ParseInt(keyword, 10, 64); err2 == nil { + sqlWhere += " OR t1.id = ? OR t1.city_code = ? OR t1.district_code = ? OR t1.lng = ? OR t1.lat = ?" + sqlWhereParams = append(sqlWhereParams, keywordInt64, keywordInt64, keywordInt64, keywordInt64, keywordInt64) + } + sqlWhere += ")" + } + + if params["storeID"] != nil { + sqlWhere += " AND t1.id = ?" + sqlWhereParams = append(sqlWhereParams, params["storeID"].(int)) + } + if params["name"] != nil { + sqlWhere += " AND t1.name LIKE ?" + sqlWhereParams = append(sqlWhereParams, "%"+params["name"].(string)+"%") + } + if params["placeID"] != nil { + level := 2 + if params["placeLevel"] != nil { + level = params["placeLevel"].(int) + } + if level == 2 { + sqlWhere += " AND t1.city_code = ?" + } else { + sqlWhere += " AND t1.district_code = ?" + } + sqlWhereParams = append(sqlWhereParams, params["placeID"].(int)) + } + if params["address"] != nil { + sqlWhere += " AND t1.address LIKE ?" + sqlWhereParams = append(sqlWhereParams, "%"+params["address"].(string)+"%") + } + if params["tel"] != nil { + sqlWhere += " AND (t1.tel1 LIKE ? OR t1.tel2 LIKE ?)" + sqlWhereParams = append(sqlWhereParams, "%"+params["tel"].(string)+"%") + sqlWhereParams = append(sqlWhereParams, "%"+params["tel"].(string)+"%") + } + + if params["fromStatus"] != nil { + fromStatus := params["fromStatus"].(int) + toStatus := fromStatus + if params["toStatus"] != nil { + toStatus = params["toStatus"].(int) + } + sqlWhere += " AND t1.status >= ? AND t1.status <= ?" + sqlWhereParams = append(sqlWhereParams, fromStatus, toStatus) + } + + sql += sqlWhere + ` GROUP BY 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22 ORDER BY t1.id LIMIT ? OFFSET ?` @@ -168,6 +181,7 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa if offset < 0 { offset = 0 } + sqlParams = append(sqlParams, sqlWhereParams...) sqlParams = append(sqlParams, pageSize, offset) retVal = &StoresInfo{} db := dao.GetDB() @@ -178,7 +192,7 @@ func GetStores(ctx *jxcontext.Context, keyword string, params map[string]interfa panic(r) } }() - globals.SugarLogger.Debug(sql) + // globals.SugarLogger.Debug(sql) if err = dao.GetRows(db, &retVal.Stores, sql, sqlParams...); err == nil { countInfo := &struct{ Ct int }{} if err = dao.GetRow(db, countInfo, "SELECT FOUND_ROWS() ct"); err == nil { diff --git a/controllers/cms_store.go b/controllers/cms_store.go index c72463d4a..bcbd2bf01 100644 --- a/controllers/cms_store.go +++ b/controllers/cms_store.go @@ -23,10 +23,10 @@ type StoreController struct { // @Param tel query string false "电话" // @Param fromStatus query int false "查询起始状态(-1:禁用;0:休息,关店;1:正常开店)" // @Param toStatus query int false "查询结束状态(-1:禁用;0:休息,关店;1:正常开店)" -// @Param vendorStoreCond query string false "查询关联门店的条件(如果此字段没有设置,jdCond,elmCond与ebaiCond无效),and:与,or:或,指的是jdCond,elmCond,ebaiCod这三个条件间的关系,这组条件与其它条件都是与的关系" -// @Param jdCond query int false "京东关联条件,-1:没有关联,0:不限定,1:有关联,缺省为0" -// @Param elmCond query int false "饿了么关联条件,-1:没有关联,0:不限定,1:有关联,缺省为0" -// @Param ebaiCond query int false "饿百关联条件,-1:没有关联,0:不限定,1:有关联,缺省为0" +// @Param vendorStoreCond query string false "查询关联门店的条件(如果此字段没有设置,vendorStoreConds无效),and:与,or:或,指的是vendorStoreConds里的条件间的关系,这组条件与其它条件都是与的关系" +// @Param vendorStoreConds query string false "为厂商条件对象{vendorID: cond},注意vendorID是字符串形式,cond,-1:没有关联,0:不限定,1:有关联,缺省为0" +// @Param courierStoreCond query string false "查询关联门店的条件(如果此字段没有设置,courierStoreConds无效),and:与,or:或,指的是courierStoreConds里的条件间的关系,这组条件与其它条件都是与的关系" +// @Param courierStoreConds query string false "为厂商条件对象{vendorID: cond},注意vendorID是字符串形式,cond,-1:没有关联,0:不限定,1:有关联,缺省为0" // @Param offset query int false "门店列表起始序号(以0开始,缺省为0)" // @Param pageSize query int false "门店列表页大小(缺省为50,-1表示全部)" // @Success 200 {object} controllers.CallResult