package cms import ( "errors" "strconv" "strings" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model/dao" ) type StoreExt struct { model.Store CityName string `json:"cityName"` DistrictName string `json:"districtName"` JdID string `orm:"column(jd_id)" json:"jdID"` ElmID string `orm:"column(elm_id)" json:"elmID"` EbaiID string `orm:"column(ebai_id)" json:"ebaiID"` } type StoresInfo struct { TotalCount int `json:"totalCount"` Stores []*StoreExt `json:"stores"` } var ( ErrMissingInput = errors.New("没有有效的输入参数") ErrCanNotFindVendor = errors.New("vendorID参数不合法") ) func GetPlaces(parentCode int, vendorID int, includeDisabled bool) ([]*model.Place, error) { db := dao.GetDB() places := []*model.Place{} sql := "SELECT * FROM place WHERE enabled = 1 " if includeDisabled { sql = "1 = 1 " } if vendorID >= 0 { if vendorID == model.VendorIDJD { return places, dao.GetRows(db, &places, sql+"AND jd_code <> 0 AND level >= 2") } return nil, ErrHaveNotImplementedYet } return places, dao.GetRows(db, &places, sql+"AND parent_code = ?", parentCode) } func UpdatePlaces(places []map[string]interface{}, userName string) (num int64, err error) { if len(places) == 0 { return 0, ErrMissingInput } for _, place := range places { if place["code"] == nil { return 0, ErrMissingInput } placeid := &model.Place{} valid := dao.NormalMakeMapByFieldList(place, []string{"jdCode", "enabled", "mtpsPrice"}, userName) if num, err = dao.UpdateEntityByKV(nil, placeid, valid, utils.Params2Map("Code", place["code"])); err != nil { return num, err } } return num, err } func UpdatePlace(placeCode int, payload map[string]interface{}, userName string) (num int64, err error) { payload["code"] = placeCode return UpdatePlaces([]map[string]interface{}{payload}, userName) } // todo 门店绑定信息可以考虑以数组形式返回,而不是现在这样 func GetStores(keyword string, params map[string]interface{}, offset, pageSize int) (retVal *StoresInfo, err error) { sqlFrom := ` 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 jdm ON t1.id = jdm.store_id AND jdm.vendor_id = 0 LEFT JOIN store_map elmm ON t1.id = elmm.store_id AND elmm.vendor_id = 2 LEFT JOIN store_map ebaim ON t1.id = ebaim.store_id AND ebaim.vendor_id = 3 ` sqlWhere := ` WHERE ` sqlParams := make([]interface{}, 0) if keyword != "" { keywordLike := "%" + keyword + "%" sqlWhere += " (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 += ")" } else { sqlWhere += " 1 = 1" } 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["cardCond"] != nil { // cardCond := params["cardCond"].(int) // if cardCond == -1 || cardCond == 1 { // sqlFrom += "\nLEFT JOIN bill_info ON t1.store_id = bill_info.jx_store_id\n" // if cardCond == -1 { // sqlWhere += " AND bill_info.id IS NULL" // } else { // sqlWhere += " AND bill_info.id IS NOT NULL" // } // } // } 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 t1.*, city.name city_name, district.name district_name, jdm.vendor_store_id jd_id, elmm.vendor_store_id elm_id, ebaim.vendor_store_id ebai_id\n" + sqlFrom + sqlWhere + ` ORDER BY t1.id LIMIT ? OFFSET ?` if pageSize == 0 { pageSize = model.DefPageSize } if offset < 0 { offset = 0 } sqlParams = append(sqlParams, pageSize, offset) retVal = &StoresInfo{} db := dao.GetDB() dao.Begin(db) 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 { retVal.TotalCount = countInfo.Ct } } dao.Commit(db) return retVal, err } func GetVendorStore(vendorStoreID string, vendorID int) (retVal *StoreExt, err error) { if handler := GetPurchaseHandler(vendorID); handler != nil { result, err2 := handler.ReadStore(vendorStoreID) if err = err2; err == nil { retVal = &StoreExt{ Store: *result, } db := dao.GetDB() if city, err2 := dao.GetPlaceByCode(db, result.CityCode); err2 == nil { retVal.CityName = city.Name } if district, err2 := dao.GetPlaceByCode(db, result.DistrictCode); err2 == nil { retVal.DistrictName = district.Name } return retVal, nil } return nil, err } return nil, ErrCanNotFindVendor } func UpdateStore(storeID int, payload map[string]interface{}, userName string) (num int64, err error) { store := &model.Store{} store.ID = storeID valid := dao.NormalMakeMapByStructObject(payload, store, userName) if len(valid) > 0 { db := dao.GetDB() if num, err = dao.UpdateEntityByKV(db, store, valid, nil); err == nil && num == 1 { dummy := &model.StoreMap{} _, err2 := dao.UpdateEntityByKV(db, dummy, utils.Params2Map(model.FieldSyncStatus, model.SyncFlagModifiedMask), utils.Params2Map(model.FieldStoreID, store.ID)) if err = err2; err == nil { if err = dao.GetEntity(db, store); err == nil { err = CurVendorSync.SyncStore(db, -1, store, false, userName) } } } } return num, err } func CreateStore(store *model.Store, userName string) (id int, err error) { dao.WrapAddIDCULEntity(store, userName) if err = dao.CreateEntity(nil, store); err == nil { return store.ID, nil } return 0, err } func GetStoreVendorMaps(db *dao.DaoDB, storeID int, vendorID int) (storeMaps []*model.StoreMap, err error) { cond := map[string]interface{}{ model.FieldStoreID: storeID, } if vendorID != -1 { cond[model.FieldVendorID] = vendorID } return storeMaps, dao.GetEntities(db, &storeMaps, cond, false) } func AddStoreVendorMap(db *dao.DaoDB, storeID, vendorID int, storeMap *model.StoreMap, userName string) (outStoreMap *model.StoreMap, err error) { if handler := GetPurchaseHandler(storeMap.VendorID); handler != nil { store, err := handler.ReadStore(storeMap.VendorStoreID) if err == nil { dao.WrapAddIDCULEntity(storeMap, userName) storeMap.StoreID = storeID storeMap.VendorID = vendorID storeMap.DeliveryType = store.DeliveryType storeMap.Status = store.Status storeMap.SyncStatus = model.SyncFlagModifiedMask // 新增绑定门店是修改的概念 if err = dao.CreateEntity(db, storeMap); err == nil { outStoreMap = storeMap err = CurVendorSync.SyncStore(db, storeMap.VendorID, store, true, userName) } } } else { err = ErrCanNotFindVendor } return outStoreMap, err } func DeleteStoreVendorMap(db *dao.DaoDB, storeID, vendorID int, userName string) (num int64, err error) { storeMap := &model.StoreMap{ StoreID: storeID, VendorID: vendorID, } return dao.DeleteEntity(db, storeMap, model.FieldStoreID, model.FieldVendorID) } func UpdateStoreVendorMap(db *dao.DaoDB, storeID, vendorID int, payload map[string]interface{}, userName string) (num int64, err error) { if db == nil { db = dao.GetDB() } if vendorStoreID := utils.Interface2String(payload["vendorStoreID"]); vendorStoreID != "" { if handler := GetPurchaseHandler(vendorID); handler != nil { jdStore, err2 := handler.ReadStore(vendorStoreID) if err = err2; err == nil { payload["deliveryType"] = jdStore.DeliveryType } } else { err = ErrCanNotFindVendor } } if err == nil { dummyStoreMap := &model.StoreMap{} valid := dao.NormalMakeMapByStructObject(payload, dummyStoreMap, userName) if len(valid) > 0 { if valid["status"] != nil { // 对于store vendor map,只有Status改变才需要同步到厂商 valid[model.FieldSyncStatus] = model.SyncFlagModifiedMask } if num, err = dao.UpdateEntityByKV(db, dummyStoreMap, valid, utils.Params2Map(model.FieldStoreID, storeID, model.FieldVendorID, vendorID)); err == nil { if valid["status"] != nil { store := &model.Store{} store.ID = storeID if err = dao.GetEntity(db, store); err == nil { err = CurVendorSync.SyncStore(db, vendorID, store, false, userName) } } } } } return num, err }