diff --git a/business/partner/purchase/tiktok_store/store_sku2_utils.go b/business/partner/purchase/tiktok_store/store_sku2_utils.go index 6beb2b807..928e36813 100644 --- a/business/partner/purchase/tiktok_store/store_sku2_utils.go +++ b/business/partner/purchase/tiktok_store/store_sku2_utils.go @@ -28,7 +28,6 @@ import ( "git.rosy.net.cn/jx-callback/globals" "math/rand" "strings" - "time" "unicode" ) @@ -215,15 +214,20 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI // 根据本地商品id获取线上商品是否存在,存在则只创建子商品 // 获取本地存储映射关系,获取本地主商品id是否存在 var mainProductId int64 = 0 + var mainOrderDetail *product_detail_response.ProductDetailData localThing, _ := dao.GetThingToTiktokMapList(db, model.VendorIDDD, int64(storeSku.SkuID), storeDetail.VendorOrgCode) - mainOrderDetail, err := api.GetSkuDetailLocalID("", utils.Int2Str(storeSku.SkuID)) - if err != nil { - failedList = putils.GetErrMsg2FailedSingleList(storeSku, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) - continue + if len(localThing) != 0 { + mainProductId = utils.Str2Int64(localThing[0].VendorThingID) + } else { + mainOrderDetail, failedList = loadMainProductId(api, storeSku, storeID, syncType) + if len(failedList) > 0 { + continue + } + mainProductId = mainOrderDetail.MainProductId } if len(localThing) == 0 { - if mainOrderDetail == nil || mainOrderDetail.ProductIdStr == "" { + if mainProductId == 0 { tiktokResult, err := api.CreateStoreCommodity(param) // 创建主商品,同步主商品 if err != nil { failedList = putils.GetErrMsg2FailedSingleList(storeSku, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) @@ -247,18 +251,8 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI storeSku.VendorSonSkuID = utils.Int2Str(storeSku.SkuID) // 还没同步子商品 failedList = putils.GetErrMsg2FailedSingleList(storeSku, errors.New("只创建了主商品,没创建子商品"), storeID, model.VendorChineseNames[model.VendorIDDD], syncType) } else { - if mainOrderDetail.MainProductId != 0 { - mainProductId = mainOrderDetail.MainProductId - } else { - mainProductId = mainOrderDetail.ProductId - } // 本地不存在,线上存在.直接创建子商品保存本地同步记录 var childrenProductId int64 = 0 - if mainOrderDetail.CheckStatus != tiktokShop.SkuCheckStatusPass && mainOrderDetail.CheckStatus != tiktokShop.SkuCheckStatusPassNotPutOn { - failedList = putils.GetErrMsg2FailedSingleList(storeSku, fmt.Errorf("主商品审核中或审核失败:[%d]", storeSku.SkuID), storeID, model.VendorChineseNames[model.VendorIDDD], syncType) - continue - } - if err := dao.CreateThingMap(int64(storeSku.SkuID), utils.Int64ToStr(mainProductId), storeDetail.VendorOrgCode, "线上存在本地不存在", model.ThingTypeSku, 0); err != nil { failedList = putils.GetErrMsg2FailedSingleList(storeSku, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) } @@ -289,45 +283,45 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI } } else { // 线上本地都存在,但是线上审核不成功,就去更新主商品 - if mainOrderDetail.CheckStatus != tiktokShop.SkuCheckStatusPass && mainOrderDetail.CheckStatus != tiktokShop.SkuCheckStatusPassNotPutOn { - // 更新主商品,在同步到子门店,考虑审核时间 - updateParam := &product_editV2_request.ProductEditV2Param{ - PayType: tiktokShop.TiktokPayType1, - ReduceType: tiktokShop.SkuReduceTypePayMakeOrder, - Weight: utils.Int2Float64(storeSku.Weight), - DeliveryDelayDay: tiktokShop.DeliveryDelayDayToDay, - PresellType: tiktokShop.SendGoodsTypeNow, - Supply7dayReturn: 0, - Mobile: storeDetail.Tel1, - Commit: true, - Specs: "重量|" + utils.Float64ToStr(float64(storeSku.SpecQuality)) + storeSku.SpecUnit, - NeedRechargeMode: false, - SellChannel: []int64{0}, - StartSaleType: 0, - PickupMethod: "0", - CategoryLeafId: param.CategoryLeafId, - Name: param.Name, - ProductFormatNew: param.ProductFormatNew, - } - updateParam.Pic = img - updateParam.Description = detailImg - updateParam.WeightUnit = tiktokShop.WeightUint_G - updateParam.StandardBrandId = param.StandardBrandId // 默认品牌京西菜市 - updateParam.ProductId = mainOrderDetail.ProductId - updateParam.MainProductId = mainProductId - updateParam.SpecPrices = param.SpecPrices - //updateParam.StoreId = utils.Str2Int64(vendorStoreID) - if err := api.EditStoreCommodity(updateParam); err != nil { - failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) - storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID) - } else { - localThing[0].UpdatedAt = time.Now() - localThing[0].SyncStatus = 0 - localThing[0].ThingType = model.ThingTypeSku - dao.UpdateThingMap(localThing[0]) - } - continue - } + //if mainOrderDetail.CheckStatus == tiktokShop.SkuCheckStatusProhibit || mainOrderDetail.CheckStatus == tiktokShop.SkuCheckStatusNotPass { + // // 更新主商品,在同步到子门店,考虑审核时间 + // updateParam := &product_editV2_request.ProductEditV2Param{ + // PayType: tiktokShop.TiktokPayType1, + // ReduceType: tiktokShop.SkuReduceTypePayMakeOrder, + // Weight: utils.Int2Float64(storeSku.Weight), + // DeliveryDelayDay: tiktokShop.DeliveryDelayDayToDay, + // PresellType: tiktokShop.SendGoodsTypeNow, + // Supply7dayReturn: 0, + // Mobile: storeDetail.Tel1, + // Commit: true, + // Specs: "重量|" + utils.Float64ToStr(float64(storeSku.SpecQuality)) + storeSku.SpecUnit, + // NeedRechargeMode: false, + // SellChannel: []int64{0}, + // StartSaleType: 0, + // PickupMethod: "0", + // CategoryLeafId: param.CategoryLeafId, + // Name: param.Name, + // ProductFormatNew: param.ProductFormatNew, + // } + // updateParam.Pic = img + // updateParam.Description = detailImg + // updateParam.WeightUnit = tiktokShop.WeightUint_G + // updateParam.StandardBrandId = param.StandardBrandId // 默认品牌京西菜市 + // updateParam.ProductId = mainOrderDetail.ProductId + // updateParam.MainProductId = mainProductId + // updateParam.SpecPrices = param.SpecPrices + // //updateParam.StoreId = utils.Str2Int64(vendorStoreID) + // if err := api.EditStoreCommodity(updateParam); err != nil { + // failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) + // storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID) + // } else { + // localThing[0].UpdatedAt = time.Now() + // localThing[0].SyncStatus = 0 + // localThing[0].ThingType = model.ThingTypeSku + // dao.UpdateThingMap(localThing[0]) + // } + // continue + //} // 主商品存在,直接同步子商品 childrenProductId, err := api.CreateSubProduct(utils.Str2Int64(localThing[0].VendorThingID), utils.Str2Int64(vendorStoreID)) @@ -404,17 +398,12 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI if storeSku.VendorMainId == "" { localThing, _ := dao.GetThingToTiktokMapList(db, model.VendorIDDD, int64(storeSku.SkuID), storeDetail.VendorOrgCode) if len(localThing) == 0 || localThing == nil { - mainOrderDetail, err := api.GetSkuDetailLocalID("", utils.Int2Str(storeSku.SkuID)) - if err != nil { - failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) + mainOrderDetail, failedList2 := loadMainProductId(api, storeSku, storeID, syncType) + if len(failedList2) > 0 { + failedList = append(failedList, failedList2...) continue } - if mainOrderDetail.MainProductId != 0 { - mainIdInt = mainOrderDetail.MainProductId - } else { - mainIdInt = mainOrderDetail.ProductId - } - + mainIdInt = mainOrderDetail.MainProductId storeSku.VendorMainId = utils.Int64ToStr(mainIdInt) if err := dao.CreateThingMap(int64(storeSku.SkuID), utils.Int64ToStr(mainIdInt), storeDetail.VendorOrgCode, "更新发现不存在", model.ThingTypeSku, 0); err != nil { failedList = putils.GetErrMsg2FailedSingleList(storeSku, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) @@ -448,6 +437,30 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI return failedList, err } +func loadMainProductId(api *tiktokShop.API, storeSku *dao.StoreSkuSyncInfo, storeID int, syncType string) (mainOrderDetail *product_detail_response.ProductDetailData, failedList []*partner.StoreSkuInfoWithErr) { + // 这有可能获取的值主商品也有可能是子商品,我们需要的是主商品id和状态 + mainOrderDetail, err := api.GetSkuDetailLocalID("", utils.Int2Str(storeSku.SkuID)) + if err != nil { + failedList = putils.GetErrMsg2FailedSingleList(storeSku, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) + return + } + if mainOrderDetail.MainProductId != 0 { // 子商品时,获取主商品id和状态 + mainOrderDetail, err = api.GetSkuDetail(utils.Int64ToStr(mainOrderDetail.MainProductId), "") + if err != nil { + failedList = putils.GetErrMsg2FailedSingleList(storeSku, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType) + return + } + if mainOrderDetail == nil || mainOrderDetail.Status == 2 { // 主商品为空或已删除状态时,需要创建 + mainOrderDetail.MainProductId = 0 + } else { + mainOrderDetail.MainProductId = mainOrderDetail.ProductId + } + } else { + mainOrderDetail.MainProductId = mainOrderDetail.ProductId // 主商品 + } + return +} + func checkNameLenght(name string) string { var chinesLen int // 中文 var punctZh int // 中文标点