From d8eb17f6beea467c51d3ef4783195230dbd41ae7 Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 20 Sep 2018 17:02:25 +0800 Subject: [PATCH] - fk --- business/jxstore/cms/store.go | 6 +- business/jxstore/cms/store_sku.go | 185 ++++++++++--------- business/jxutils/jxutils_cms.go | 2 +- business/model/store_sku.go | 2 +- business/partner/purchase/ebai/ebai_test.go | 23 +++ business/partner/purchase/ebai/store.go | 34 +++- business/partner/purchase/ebai/store_test.go | 22 +++ business/partner/purchase/jd/store_sku.go | 4 +- controllers/cms_store.go | 2 +- 9 files changed, 176 insertions(+), 104 deletions(-) create mode 100644 business/partner/purchase/ebai/ebai_test.go create mode 100644 business/partner/purchase/ebai/store_test.go diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index ccde1ec75..a17f8628a 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -229,9 +229,9 @@ func GetStoreVendorMaps(db *dao.DaoDB, storeID int, vendorID int) (storeMaps []* } 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 { + if handler := GetPurchaseHandler(vendorID); handler != nil { + store, err2 := handler.ReadStore(storeMap.VendorStoreID) + if err = err2; err == nil { dao.WrapAddIDCULDEntity(storeMap, userName) storeMap.StoreID = storeID storeMap.VendorID = vendorID diff --git a/business/jxstore/cms/store_sku.go b/business/jxstore/cms/store_sku.go index 8e41d5334..418a85e45 100644 --- a/business/jxstore/cms/store_sku.go +++ b/business/jxstore/cms/store_sku.go @@ -28,9 +28,8 @@ type StoreSkuNamesInfo struct { // UpdateStoreSku用,API调用时 type StoreSkuBindSkuInfo struct { - SubStoreID int `json:"subStoreID"` - SkuID int `json:"skuID"` - IsSale int `json:"isSale"` // -1:不可售,0:忽略,1:可售 + SkuID int `json:"skuID"` + IsSale int `json:"isSale"` // -1:不可售,0:忽略,1:可售 ElmID int64 `json:"elmID"` EbaiID int64 `json:"ebaiID"` @@ -38,16 +37,19 @@ type StoreSkuBindSkuInfo struct { // UpdateStoreSku用,API调用时 type StoreSkuBindInfo struct { - NameID int `json:"nameID"` - UnitPrice int `json:"unitPrice"` - IsFocus int `json:"isFocus"` // -1:不关注,0:忽略,1:关注 - Skus []*StoreSkuBindSkuInfo `json:"skus"` + NameID int `json:"nameID"` + UnitPrice int `json:"unitPrice"` + IsFocus int `json:"isFocus"` // -1:不关注,0:忽略,1:关注 + SubStoreID int `json:"subStoreID"` + Skus []*StoreSkuBindSkuInfo `json:"skus"` } type tStoreSkuBindAndSpec struct { model.StoreSkuBind - SpecQuality float32 - SpecUnit string + SpecQuality float32 + SpecUnit string + SkuNamePrice int + RealSkuID int `orm:"column(real_sku_id)"` } func GetStoreSkus(storeID int, isFocused bool, keyword string, params map[string]interface{}, offset, pageSize int) (skuNamesInfo *StoreSkuNamesInfo, err error) { @@ -230,104 +232,109 @@ func UpdateStoreSkus(storeID int, skuBindInfos []*StoreSkuBindInfo, userName str } }() for _, skuBindInfo := range skuBindInfos { - skuBinds := skuBindInfo.Skus - var existBinds []*tStoreSkuBindAndSpec - if err = dao.GetRows(db, &existBinds, ` - SELECT t1.*, t2.spec_quality, t2.spec_unit - FROM store_sku_bind t1 - JOIN sku t2 ON t1.sku_id = t2.id - WHERE store_id = ? - AND t2.name_id = ?`, storeID, skuBindInfo.NameID); err == nil { - existBindsMap := make(map[int]*model.StoreSkuBind, len(existBinds)) + inSkuBinds := skuBindInfo.Skus + var allBinds []*tStoreSkuBindAndSpec + if err = dao.GetRows(db, &allBinds, ` + SELECT t2.*, t1.id real_sku_id, t1.spec_quality, t1.spec_unit, t3.price sku_name_price + FROM sku t1 + LEFT JOIN store_sku_bind t2 ON t2.sku_id = t1.id AND store_id = ? + JOIN sku_name t3 ON t1.name_id = t3.id + WHERE t1.name_id = ? + `, storeID, skuBindInfo.NameID); err == nil { + globals.SugarLogger.Debug(len(allBinds)) + inSkuBinsMap := make(map[int]*StoreSkuBindSkuInfo, len(inSkuBinds)) + for _, v := range inSkuBinds { + inSkuBinsMap[v.SkuID] = v + } unitPrice := 0 if skuBindInfo.UnitPrice != 0 { unitPrice = skuBindInfo.UnitPrice } else { - unitPrice = existBinds[0].UnitPrice - } - for _, v := range existBinds { - if skuBindInfo.UnitPrice != 0 { - v.UnitPrice = unitPrice - v.Price = calStoreSkuPrice(unitPrice, v.SpecQuality, v.SpecUnit) - setStoreSkuBindStatus(&v.StoreSkuBind, model.SyncFlagPriceMask) - dao.WrapUpdateULEntity(v, userName) - if num, err = dao.UpdateEntity(db, &v.StoreSkuBind); err != nil { - dao.Rollback(db) - return 0, err - } - if num == 1 { - needSyncIDMap[v.ID] = 1 - } + unitPrice = allBinds[0].UnitPrice + if unitPrice == 0 { + unitPrice = allBinds[0].SkuNamePrice } - if skuBindInfo.IsFocus != 0 { + } + for _, v := range allBinds { + inSkuBind := inSkuBinsMap[v.SkuID] + var skuBind *model.StoreSkuBind + // globals.SugarLogger.Debug(ok) + if v.ID == 0 { + if skuBindInfo.IsFocus == 1 { + skuBind = &model.StoreSkuBind{ + StoreID: storeID, + SkuID: v.RealSkuID, + SubStoreID: skuBindInfo.SubStoreID, // todo 这个应该从用户信息中自动获得 + UnitPrice: unitPrice, + Price: calStoreSkuPrice(unitPrice, v.SpecQuality, v.SpecUnit), + Status: model.StoreSkuBindStatusDontSale, // 缺省不可售? + } + if inSkuBind != nil && inSkuBind.IsSale == 1 { + skuBind.Status = model.StoreSkuBindStatusNormal + } + setStoreSkuBindStatus(skuBind, model.SyncFlagNewMask) + dao.WrapAddIDCULDEntity(skuBind, userName) + globals.SugarLogger.Debug(utils.Format4Output(skuBind, false)) + if err = dao.CreateEntity(db, skuBind); err != nil { + dao.Rollback(db) + return 0, err + } + num = 1 + } + } else { + skuBind = &v.StoreSkuBind + if skuBindInfo.IsFocus == -1 { - globals.SugarLogger.Debug(utils.Format4Output(v, false)) - num, err = dao.DeleteEntityLogically(db, &v.StoreSkuBind, map[string]interface{}{ + if num, err = dao.DeleteEntityLogically(db, skuBind, map[string]interface{}{ model.FieldStatus: model.StoreSkuBindStatusDeleted, model.FieldJdSyncStatus: model.SyncFlagDeletedMask, model.FieldElmSyncStatus: model.SyncFlagDeletedMask, model.FieldEbaiSyncStatus: model.SyncFlagDeletedMask, - }, userName, nil) - globals.SugarLogger.Debug(num) - } else { - // todo 如果修改ebai与elm ID怎么处理呢? - v.DeletedAt = utils.DefaultTimeValue - setStoreSkuBindStatus(&v.StoreSkuBind, model.SyncFlagModifiedMask) - num, err = dao.UpdateEntity(db, &v.StoreSkuBind) - globals.SugarLogger.Debug(num) - } - if num == 1 { - needSyncIDMap[v.ID] = 1 - } - } - existBindsMap[v.SkuID] = &v.StoreSkuBind - } - for _, v := range skuBinds { - skuBind, ok := existBindsMap[v.SkuID] - if !ok { - if skuBindInfo.IsFocus == 1 { - sku := &model.Sku{} - sku.ID = v.SkuID - if err2 := dao.GetEntity(db, sku); err2 != nil { + }, userName, nil); err != nil { dao.Rollback(db) - return 0, err2 + return 0, err } - skuBind = &model.StoreSkuBind{ - StoreID: storeID, - SkuID: v.SkuID, - SubStoreID: v.SubStoreID, // todo 这个应该从用户信息中自动获得 - UnitPrice: unitPrice, - Price: calStoreSkuPrice(unitPrice, sku.SpecQuality, sku.SpecUnit), - Status: model.StoreSkuBindStatusDontSale, + } else { + needUpdate := false + if skuBindInfo.IsFocus == 1 { + skuBind.DeletedAt = utils.DefaultTimeValue + needUpdate = true } - dao.WrapAddIDCULDEntity(skuBind, userName) - } - } - if skuBind != nil { - if v.IsSale != 0 { - if v.IsSale == 1 { - skuBind.Status = model.StoreSkuBindStatusNormal - } else { - skuBind.Status = model.StoreSkuBindStatusDontSale + if inSkuBind != nil && inSkuBind.IsSale != 0 { + if inSkuBind.IsSale == 1 { + skuBind.Status = model.StoreSkuBindStatusNormal + } else { + skuBind.Status = model.StoreSkuBindStatusDontSale + } + setStoreSkuBindStatus(skuBind, model.SyncFlagSaleMask) + needUpdate = true } - setStoreSkuBindStatus(skuBind, model.SyncFlagSaleMask) - } - if !ok { - if v.EbaiID != 0 { - skuBind.EbaiID = v.EbaiID + if skuBindInfo.UnitPrice != 0 { // 这里是否需要加此条件限制 + skuBind.UnitPrice = unitPrice + skuBind.Price = calStoreSkuPrice(unitPrice, v.SpecQuality, v.SpecUnit) + setStoreSkuBindStatus(skuBind, model.SyncFlagPriceMask) + needUpdate = true } - if v.ElmID != 0 { - skuBind.ElmID = v.ElmID + if inSkuBind != nil && inSkuBind.EbaiID != 0 { + skuBind.EbaiID = inSkuBind.EbaiID + needUpdate = true } - setStoreSkuBindStatus(skuBind, model.SyncFlagNewMask) - if err = dao.CreateEntity(db, skuBind); err == nil { - needSyncIDMap[skuBind.ID] = 1 + if inSkuBind != nil && inSkuBind.ElmID != 0 { + skuBind.ElmID = inSkuBind.ElmID + needUpdate = true + } + if needUpdate { + setStoreSkuBindStatus(skuBind, model.SyncFlagModifiedMask) + dao.WrapUpdateULEntity(skuBind, userName) + if num, err = dao.UpdateEntity(db, skuBind); err != nil { + dao.Rollback(db) + return 0, err + } } } - } - if err != nil { - dao.Rollback(db) - return 0, err + if skuBind != nil && num == 1 { + needSyncIDMap[skuBind.ID] = 1 + } } } } else { diff --git a/business/jxutils/jxutils_cms.go b/business/jxutils/jxutils_cms.go index f52bfd0c1..fa4010514 100644 --- a/business/jxutils/jxutils_cms.go +++ b/business/jxutils/jxutils_cms.go @@ -63,7 +63,7 @@ func StrTime2JxOperationTime(strTime string, defValue int16) int16 { } func JxOperationTime2StrTime(value int16) string { - return fmt.Sprintf("%02d:%02d:00", value/100, value%100) + return fmt.Sprintf("%02d:%02d", value/100, value%100) } func GetPolygonFromCircle(lng, lat, distance float64, pointCount int) (points [][2]float64) { diff --git a/business/model/store_sku.go b/business/model/store_sku.go index 5bc1ae9c0..d23b05d24 100644 --- a/business/model/store_sku.go +++ b/business/model/store_sku.go @@ -45,6 +45,6 @@ type StoreSkuBind struct { func (*StoreSkuBind) TableUnique() [][]string { return [][]string{ - []string{"StoreID", "SkuID"}, + []string{"StoreID", "SkuID", "DeletedAt"}, } } diff --git a/business/partner/purchase/ebai/ebai_test.go b/business/partner/purchase/ebai/ebai_test.go new file mode 100644 index 000000000..bc740e920 --- /dev/null +++ b/business/partner/purchase/ebai/ebai_test.go @@ -0,0 +1,23 @@ +package ebai + +import ( + _ "git.rosy.net.cn/jx-callback/business/jxcallback/scheduler/defsch" + "git.rosy.net.cn/jx-callback/globals" + "git.rosy.net.cn/jx-callback/globals/api" + "git.rosy.net.cn/jx-callback/globals/beegodb" + "github.com/astaxie/beego" +) + +const ( + testShopBaiduID = 2233043816 + testShopID = "test_708706_63032" +) + +func init() { + beego.InitBeegoBeforeTest("/Users/xujianhua/go/src/git.rosy.net.cn/jx-callback/conf/app.conf") + // beego.BConfig.RunMode = "dev" // InitBeegoBeforeTest会将runmode设置为test + + globals.Init() + beegodb.Init() + api.Init() +} diff --git a/business/partner/purchase/ebai/store.go b/business/partner/purchase/ebai/store.go index 2fc072972..c2dc6d481 100644 --- a/business/partner/purchase/ebai/store.go +++ b/business/partner/purchase/ebai/store.go @@ -25,6 +25,7 @@ type tEbaiStoreInfo struct { VendorStoreID string `orm:"column(vendor_store_id)"` RealLastOperator string EbaiStoreStatus int + SyncStatus int } func (p *PurchaseHandler) ReadStore(vendorStoreID string) (*model.Store, error) { @@ -90,11 +91,12 @@ func (p *PurchaseHandler) UpdateStore(storeID int, userName string) (err error) var store tEbaiStoreInfo sql := ` SELECT t1.*, t2.status ebai_store_status, t2.vendor_store_id, - IF(t1.updated_at > t2.updated_at, t1.last_operator, t2.last_operator) real_last_operator + IF(t1.updated_at > t2.updated_at, t1.last_operator, t2.last_operator) real_last_operator, t2.sync_status FROM store t1 JOIN store_map t2 ON t1.id = t2.store_id AND t2.vendor_id = ? - WHERE t1.id = ?` - if err = dao.GetRow(db, &store, sql, model.VendorIDJD, storeID); err == nil { + WHERE t1.id = ?; + ` + if err = dao.GetRow(db, &store, sql, model.VendorIDEBAI, storeID); err == nil { params := map[string]interface{}{ "baidu_shop_id": store.VendorStoreID, "name": jxutils.ComposeStoreName(store.Name, partner.StoreNameSeparator, VendorStorePrefix), @@ -107,10 +109,26 @@ func (p *PurchaseHandler) UpdateStore(storeID int, userName string) (err error) "delivery_region": JxDeliveryRegion2Ebai(&store.Store), "business_time": JxBusinessTime2Ebai(&store.Store), } - // globals.SugarLogger.Debug(utils.Format4Output(params, false)) + globals.SugarLogger.Debug(utils.Format4Output(params, false)) if globals.EnableStoreWrite { if err = api.EbaiAPI.ShopUpdate(params); err == nil { - // todo 设置 shop_id + shopID := 0 + if store.SyncStatus&model.SyncFlagDeletedMask == 0 { + shopID = store.ID + } + store2, err2 := p.ReadStore(store.VendorStoreID) + if err = err2; err == nil { + if store2.ID == store.ID { + shopID = -1 + } + } + if err == nil { + if shopID > 0 { + err = api.EbaiAPI.ShopIDBatchUpdate([]string{store.VendorStoreID}, []string{utils.Int2Str(shopID)}) + } else if shopID == 0 { + // todo remove out shop id + } + } } } } @@ -171,8 +189,10 @@ func JxDeliveryRegion2Ebai(store *model.Store) interface{} { func JxBusinessTime2Ebai(store *model.Store) interface{} { bTime := make([]map[string]interface{}, 1) - bTime[0]["start"] = jxutils.JxOperationTime2StrTime(store.OpenTime1) - bTime[0]["end"] = jxutils.JxOperationTime2StrTime(store.CloseTime1) + bTime[0] = map[string]interface{}{ + "start": jxutils.JxOperationTime2StrTime(store.OpenTime1), + "end": jxutils.JxOperationTime2StrTime(store.CloseTime1), + } if store.OpenTime2 != 0 { bTime = append(bTime, map[string]interface{}{ "start": jxutils.JxOperationTime2StrTime(store.OpenTime2), diff --git a/business/partner/purchase/ebai/store_test.go b/business/partner/purchase/ebai/store_test.go new file mode 100644 index 000000000..93aca443e --- /dev/null +++ b/business/partner/purchase/ebai/store_test.go @@ -0,0 +1,22 @@ +package ebai + +import ( + "testing" + + "git.rosy.net.cn/baseapi/utils" +) + +func TestReadStore(t *testing.T) { + result, err := new(PurchaseHandler).ReadStore(utils.Int2Str(testShopBaiduID)) + if err != nil { + t.Fatal(err.Error()) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestUpdateStore(t *testing.T) { + err := new(PurchaseHandler).UpdateStore(100002, "autotest") + if err != nil { + t.Fatal(err.Error()) + } +} diff --git a/business/partner/purchase/jd/store_sku.go b/business/partner/purchase/jd/store_sku.go index 8951e2bba..0f3f2ba56 100644 --- a/business/partner/purchase/jd/store_sku.go +++ b/business/partner/purchase/jd/store_sku.go @@ -22,7 +22,7 @@ func (p *PurchaseHandler) SyncStoreSku(storeID int, skuIDs []int, isForce bool, FROM store_sku_bind WHERE store_id = ? ` - if skuIDs != nil { + if skuIDs != nil && len(skuIDs) > 0 { sql += " AND sku_id IN (" + dao.GenQuestionMarks(len(skuIDs)) + ")" sqlParams = append(sqlParams, skuIDs) } @@ -84,7 +84,7 @@ func (p *PurchaseHandler) SyncStoreSku(storeID int, skuIDs []int, isForce bool, SET jd_sync_status = 0 WHERE store_id = ? ` - if skuIDs != nil { + if skuIDs != nil && len(skuIDs) > 0 { sql += " AND sku_id IN (" + dao.GenQuestionMarks(len(skuIDs)) + ")" } _, err = dao.ExecuteSQL(db, sql, sqlParams) diff --git a/controllers/cms_store.go b/controllers/cms_store.go index b3f285e32..3444804f1 100644 --- a/controllers/cms_store.go +++ b/controllers/cms_store.go @@ -141,7 +141,7 @@ func (c *StoreController) UpdateStoreVendorMap() { // @Param token header string true "认证token" // @Param storeID formData int true "门店ID,payload中的相应字段会被忽略" // @Param vendorID formData int true "厂商ID,payload中的相应字段会被忽略" -// @Param payload formData string true "json数据,storeMap对象({'vendorID':0,'vendorStoreID':'11732425','autoPickup':1,'deliveryCompetition':1, 'pricePercentage':100})" +// @Param payload formData string true "json数据,storeMap对象({'vendorStoreID':'11732425','autoPickup':1,'deliveryCompetition':1, 'pricePercentage':100})" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /AddStoreVendorMap [post]