From d71b0e6763b631fe664366aa264cfa2debe1e5bd Mon Sep 17 00:00:00 2001 From: gazebo Date: Sun, 9 Sep 2018 14:54:17 +0800 Subject: [PATCH] - handle store.Status. --- business/jxstore/cms/store.go | 42 ++++++++++++++++++++++++++++------ business/jxutils/jxutils.go | 1 + business/model/dao/dao.go | 2 ++ business/model/dao/dao_test.go | 18 +++++++++++++++ business/model/store.go | 9 ++++---- controllers/cms_store.go | 2 +- 6 files changed, 62 insertions(+), 12 deletions(-) diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index b97f8a4c7..349b5caa6 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -217,7 +217,7 @@ func UpdateStore(storeID int, payload map[string]interface{}, userName string) ( if num, err = dao.UpdateEntityByKV(db, store, valid, nil); err == nil { globals.SugarLogger.Debug("2") dummy := &model.StoreMap{} - _, err2 := dao.UpdateEntityByKV(db, dummy, utils.Params2Map("SyncStatus", 2), utils.Params2Map("StoreID", store.ID)) + _, err2 := dao.UpdateEntityByKV(db, dummy, utils.Params2Map("SyncStatus", model.SyncFlagModifiedMask), utils.Params2Map("StoreID", store.ID)) if err = err2; err == nil { if err = dao.GetEntity(db, store); err == nil { err = SyncStore2Vendor(db, -1, store, false, userName) @@ -239,12 +239,17 @@ func CreateStore(store *model.Store, userName string) (id int, err error) { } func SyncStore2Vendor(db *dao.DaoDB, vendorID int, store *model.Store, isForce bool, userName string) (err error) { + if db == nil { + db = dao.GetDB() + } storeMaps, err := GetStoreVendorMaps(db, store.ID, -1) if err == nil { // globals.SugarLogger.Debug(utils.Format4Output(store, false)) + copiedStore := *store for _, storeMap := range storeMaps { if (vendorID == -1 || storeMap.VendorID == vendorID) && (isForce || storeMap.SyncStatus != 0) { - if err = GetPurchaseHandler(storeMap.VendorID).UpdateStore(storeMap.VendorStoreID, store, userName); err == nil { + copiedStore.Status = mergeStoreStatus(store.Status, storeMap.Status) + if err = GetPurchaseHandler(storeMap.VendorID).UpdateStore(storeMap.VendorStoreID, &copiedStore, userName); err == nil { storeMap.SyncStatus = 0 dao.UpdateEntity(db, storeMap, "SyncStatus") } @@ -267,6 +272,7 @@ func AddStoreVendorMap(db *dao.DaoDB, storeMap *model.StoreMap, userName string) store, err := GetPurchaseHandler(storeMap.VendorID).ReadStore(storeMap.VendorStoreID) if err == nil { storeMap.DeliveryType = store.DeliveryType + storeMap.Status = store.Status outStoreMap = storeMap if err = dao.CreateEntity(db, storeMap); err == nil { @@ -281,17 +287,39 @@ func DeleteStoreVendorMap(db *dao.DaoDB, storeID, vendorID int, userName string) } 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 != "" { - store, err2 := GetPurchaseHandler(vendorID).ReadStore(vendorStoreID) + jdStore, err2 := GetPurchaseHandler(vendorID).ReadStore(vendorStoreID) if err = err2; err == nil { - payload["deliveryType"] = store.DeliveryType + payload["deliveryType"] = jdStore.DeliveryType } } if err == nil { - dummy := &model.StoreMap{} - valid := jxutils.NormalMakeMapByStructObject(payload, dummy, userName) + dummyStoreMap := &model.StoreMap{} + valid := jxutils.NormalMakeMapByStructObject(payload, dummyStoreMap, userName) + if valid["status"] != nil { + valid["SyncStatus"] = model.SyncFlagModifiedMask + } globals.SugarLogger.Debug(utils.Format4Output(valid, false)) - num, err = dao.UpdateEntityByKV(db, dummy, valid, utils.Params2Map("StoreID", storeID, "VendorID", vendorID)) + if num, err = dao.UpdateEntityByKV(db, dummyStoreMap, valid, utils.Params2Map("StoreID", storeID, "VendorID", vendorID)); err == nil { + if valid["status"] != nil { + store := &model.Store{} + store.ID = storeID + if err = dao.GetEntity(db, store); err == nil { + err = SyncStore2Vendor(db, vendorID, store, true, userName) + } + } + } } return num, err } + +// 合并得到最终的门店状态 +func mergeStoreStatus(status int, vendorStatus int) int { + if status < vendorStatus { + return status + } + return vendorStatus +} diff --git a/business/jxutils/jxutils.go b/business/jxutils/jxutils.go index c4ef91339..87d34617c 100644 --- a/business/jxutils/jxutils.go +++ b/business/jxutils/jxutils.go @@ -296,6 +296,7 @@ func Struct2FlatMap(obj interface{}) map[string]interface{} { return FlatMap(m) } +// todo 这里看是否需要将key值转换成标准格式(即字母大写),因为beego orm不区分,不转换也可以 func FilterMapByStructObject(mapData map[string]interface{}, obj interface{}, excludedFields []string) (valid map[string]interface{}, invalid map[string]interface{}) { excludedMap := make(map[string]int) for _, v := range excludedFields { diff --git a/business/model/dao/dao.go b/business/model/dao/dao.go index a3977677e..55dfae07f 100644 --- a/business/model/dao/dao.go +++ b/business/model/dao/dao.go @@ -48,6 +48,8 @@ type DaoDB struct { // return err // } +// beego orm的对于传代表字段的字串,数据库字段名(完全匹配,区分大小写),结构体字段名(不区分大小写)都可以 + func GetDB() *DaoDB { return &DaoDB{db: orm.NewOrm()} } diff --git a/business/model/dao/dao_test.go b/business/model/dao/dao_test.go index ada61ba5b..68f9f9fd3 100644 --- a/business/model/dao/dao_test.go +++ b/business/model/dao/dao_test.go @@ -40,3 +40,21 @@ func TestGetPlaceByName(t *testing.T) { } t.Log(result) } + +func TestUpdateKV(t *testing.T) { + dummy := &model.Store{} + kvs := map[string]interface{}{ + "status": 100, + "Tel1": "tel1", + "tEl2": "tel2", + "deliverY_Range_type": 15, + } + cond := map[string]interface{}{ + "id": 100002, + } + num, err := UpdateEntityByKV(nil, dummy, kvs, cond) + if err != nil { + t.Fatal(err) + } + t.Log(num) +} diff --git a/business/model/store.go b/business/model/store.go index a473e6249..2159ec232 100644 --- a/business/model/store.go +++ b/business/model/store.go @@ -66,11 +66,12 @@ type StoreMap struct { VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` Status int `json:"status"` // 取值同Store.Status - SyncStatus int8 `orm:"default(2)" json:"syncStatus"` + PricePercentage int16 `orm:"default(100)" json:"pricePercentage"` // todo 厂商价格相对于本地价格的百分比,这个字段的修改会比较特殊,因为可能需要刷新厂商价格 + AutoPickup int8 `orm:"default(1)" json:"autoPickup"` // 是否自动拣货 + DeliveryType int8 `orm:"default(0)" json:"deliveryType"` // 配送类型 + DeliveryCompetition int8 `orm:"default(1)" json:"deliveryCompetition"` // 是否支持配送竞争 - AutoPickup int8 `json:"autoPickup"` // 是否自动拣货 - DeliveryType int8 `json:"deliveryType"` // 配送类型 - DeliveryCompetition int8 `json:"deliveryCompetition"` // 是否支持配送竞争 + SyncStatus int8 `orm:"default(2)" json:"syncStatus"` } func (*StoreMap) TableUnique() [][]string { diff --git a/controllers/cms_store.go b/controllers/cms_store.go index 5ef0a40d3..06b05f29b 100644 --- a/controllers/cms_store.go +++ b/controllers/cms_store.go @@ -198,7 +198,7 @@ func (c *StoreController) UpdateStoreVendorMap() { // @Title 新增门店映射信息 // @Description 新增门店映射信息 // @Param token header string true "认证token" -// @Param payload formData string true "json数据,storeMap对象" +// @Param payload formData string true "json数据,storeMap对象({'vendorID':0,'vendorStoreID':'11732425','autoPickup':1,'deliveryCompetition':1, 'pricePercentage':100})" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /AddStoreVendorMap [post]