diff --git a/business/jxstore/cms/sync.go b/business/jxstore/cms/sync.go index ff44b092f..21def0ef1 100644 --- a/business/jxstore/cms/sync.go +++ b/business/jxstore/cms/sync.go @@ -1084,7 +1084,7 @@ func syncStoreSkusFromYb(ctx *jxcontext.Context, storeID, vendorID int, vendorSt return hint, err } -func (v *VendorSync) SyncJdsStoresSkus(ctx *jxcontext.Context, storeIDs []int, vendorStoreID string, isAsync, isContinueWhenError bool) (hint string, err error) { +func (v *VendorSync) SyncJdsStoresSkus(ctx *jxcontext.Context, storeIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) { var ( db = dao.GetDB() ) @@ -1101,7 +1101,7 @@ func (v *VendorSync) SyncJdsStoresSkus(ctx *jxcontext.Context, storeIDs []int, v func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { storeMap := batchItemList[0].(*model.StoreMap) if storeMap.Status > model.StoreStatusDisabled && storeMap.StoreID != model.JdShopMainStoreID && storeMap.SyncRule != 0 { - err = syncJdsStoresSkus(ctx, db, task, storeMap) + _, err = syncJdsStoresSkus(ctx, db, task, storeMap, isAsync, isContinueWhenError) } return nil, err }, loopMapInfo.StoreMapList) @@ -1113,9 +1113,16 @@ func (v *VendorSync) SyncJdsStoresSkus(ctx *jxcontext.Context, storeIDs []int, v return hint, err } -func syncJdsStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, parentTask tasksch.ITask, storeMap *model.StoreMap) (err error) { +func syncJdsStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, parentTask tasksch.ITask, storeMap *model.StoreMap, isAsync, isContinueWhenError bool) (hint string, err error) { var ( - mainSkusMap = make(map[int]*model.StoreSkuBind) + mainSkusMap = make(map[int]*model.StoreSkuBind) + skusMap = make(map[int]*model.StoreSkuBind) + updateList []*model.StoreSkuBind + addList []*model.StoreSkuBind + skuBindInfos1 []*StoreSkuBindInfo + skuBindInfos2 []*StoreSkuBindInfo + resultInterface1 []interface{} + resultInterface2 []interface{} ) storeSkusMain, err := dao.GetStoresSkusInfo(db, []int{model.JdShopMainStoreID}, nil) for _, v := range storeSkusMain { @@ -1123,14 +1130,87 @@ func syncJdsStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, parentTask tasksch } storeSkus, err := dao.GetStoresSkusInfo(db, []int{storeMap.StoreID}, nil) for _, v := range storeSkus { - if mainSkusMap[v.SkuID] != nil { - - } else { + skusMap[v.SkuID] = v + if mainSkusMap[v.SkuID] != nil && mainSkusMap[v.SkuID].Status != v.Status { + updateList = append(updateList, mainSkusMap[v.SkuID]) + } + } + for k, v := range mainSkusMap { + if skusMap[k] == nil { if storeMap.SyncRule == 2 { - + addList = append(addList, v) } } } - - return err + task := tasksch.NewParallelTask("正在同步京东商城的库存商品", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(isContinueWhenError), ctx, + func(ptTask *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + step := batchItemList[0].(int) + switch step { + case 0: + if len(updateList) > 0 { + task := tasksch.NewParallelTask("同步门店库存", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(isContinueWhenError), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + storeBind := batchItemList[0].(*model.StoreSkuBind) + retVal = []*StoreSkuBindInfo{buildStoreSkuBindInfo(db, storeMap.StoreID, storeBind, false)} + return retVal, err + }, updateList) + tasksch.HandleTask(task, ptTask, false).Run() + resultInterface1, err = task.GetResult(0) + for _, v := range resultInterface1 { + skuBindInfos1 = append(skuBindInfos1, v.(*StoreSkuBindInfo)) + } + hint, err = UpdateStoresSkusByBind(ctx, ptTask, skuBindInfos1, isAsync, isContinueWhenError, false) + } + case 1: + if len(addList) > 0 { + task := tasksch.NewParallelTask("同步门店商品", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(isContinueWhenError), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + storeBind := batchItemList[0].(*model.StoreSkuBind) + retVal = []*StoreSkuBindInfo{buildStoreSkuBindInfo(db, storeMap.StoreID, storeBind, true)} + return retVal, err + }, addList) + tasksch.HandleTask(task, ptTask, false).Run() + resultInterface2, err = task.GetResult(0) + for _, v := range resultInterface2 { + skuBindInfos2 = append(skuBindInfos2, v.(*StoreSkuBindInfo)) + } + hint, err = UpdateStoresSkusByBind(ctx, ptTask, skuBindInfos2, isAsync, isContinueWhenError, false) + } + } + return retVal, err + }, []int{0, 1}) + tasksch.HandleTask(task, nil, true).Run() + if isAsync { + hint = task.GetID() + } else { + _, err = task.GetResult(0) + hint = "1" + } + return hint, err +} + +func buildStoreSkuBindInfo(db *dao.DaoDB, storeID int, storeBind *model.StoreSkuBind, isFocus bool) (skuBindInfo *StoreSkuBindInfo) { + result, err := dao.GetSkus(db, []int{storeBind.SkuID}, nil, nil, nil, nil) + if err != nil || len(result) == 0 { + return nil + } + skus := []*StoreSkuBindSkuInfo{ + &StoreSkuBindSkuInfo{ + SkuID: storeBind.SkuID, + }, + } + skuBindInfo = &StoreSkuBindInfo{ + StoreID: storeID, + NameID: result[0].NameID, + } + if isFocus { + skuBindInfo.IsFocus = 1 + } + if storeBind.Status == model.SkuStatusNormal { + skus[0].IsSale = 1 + } else { + skus[0].IsSale = -1 + } + skuBindInfo.Skus = skus + return skuBindInfo } diff --git a/business/jxstore/misc/misc.go b/business/jxstore/misc/misc.go index ea2bf20e9..a9574af87 100644 --- a/business/jxstore/misc/misc.go +++ b/business/jxstore/misc/misc.go @@ -295,10 +295,10 @@ func Init() { if configs, err := dao.QueryConfigs(dao.GetDB(), "feiePageCookie", model.ConfigTypeCookie, ""); err == nil { feiePageCookie = configs[0].Value } - // if configs, err := dao.QueryConfigs(dao.GetDB(), "jdsCookie", model.ConfigTypeCookie, ""); err == nil { - // jdsCookie = configs[0].Value - // api.JdShopAPI.SetCookieWithStr(jdsCookie) - // } + if configs, err := dao.QueryConfigs(dao.GetDB(), "jdsCookie", model.ConfigTypeCookie, ""); err == nil { + jdsCookie = configs[0].Value + api.JdShopAPI.SetCookieWithStr(jdsCookie) + } if configs, err := dao.QueryConfigs(dao.GetDB(), "yinbaoCookie", model.ConfigTypeCookie, ""); err == nil { yinbaoCookie := configs[0].Value api.YinBaoAPI.SetCookie(".POSPALAUTH30220", yinbaoCookie) diff --git a/controllers/cms_sync.go b/controllers/cms_sync.go index 4cdba082e..5a01cec7a 100644 --- a/controllers/cms_sync.go +++ b/controllers/cms_sync.go @@ -366,3 +366,16 @@ func (c *SyncController) SyncStoreSkusFromYb() { return retVal, "", err }) } + +// @Title 同步京东商城门店商品和库存 +// @Description 同步京东商城门店商品和库存 +// @Param token header string true "认证token" +// @Param storeIDs formData string false "门店ID列表" +// @Param isAsync formData bool false "是否异步操作" +// @Param isContinueWhenError formData bool false "单个同步失败是否继续,缺省false" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /SyncJdsStoresSkus [put] +func (c *SyncController) SyncJdsStoresSkus() { + +} diff --git a/globals/api/api.go b/globals/api/api.go index 1b876630e..1cc86b19f 100644 --- a/globals/api/api.go +++ b/globals/api/api.go @@ -123,9 +123,9 @@ func Init() { } if !beego.AppConfig.DefaultBool("disableJdShop", false) { JdShopAPI = jdshopapi.New(beego.AppConfig.String("jdShopAccessToken"), beego.AppConfig.String("jdShopAppKey"), beego.AppConfig.String("jdShopAppSecret")) - if jdsCookie := beego.AppConfig.DefaultString("jdsCookie", ""); jdsCookie != "" { - JdShopAPI.SetCookieWithStr(jdsCookie) - } + // if jdsCookie := beego.AppConfig.DefaultString("jdsCookie", ""); jdsCookie != "" { + // JdShopAPI.SetCookieWithStr(jdsCookie) + // } } else { JdShopAPI = nil }