This commit is contained in:
邹宗楠
2022-09-30 17:16:22 +08:00
parent e2b41bf9db
commit 97c092cd26
36 changed files with 1753 additions and 1895 deletions

View File

@@ -2,6 +2,11 @@ package tiktok_store
import (
"encoding/json"
"fmt"
product_addV2_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/product_addV2/request"
product_detail_response "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/product_detail/response"
product_editV2_request "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/sdk-golang/api/product_editV2/request"
tiktokShop "git.rosy.net.cn/baseapi/platformapi/tiktok_shop/tiktok_api"
"regexp"
"strings"
@@ -34,12 +39,13 @@ var (
sensitiveWordRegexp = regexp.MustCompile(`包含敏感词:(\[.*\])`)
)
// GetStoreSkusBatchSize 当前任务最大更新条数
func (p *PurchaseHandler) GetStoreSkusBatchSize(funcID int) (batchSize int) {
switch funcID {
case partner.FuncUpdateStoreSkusStock, partner.FuncUpdateStoreSkusStatus, partner.FuncUpdateStoreSkusPrice:
batchSize = mtwmapi.MaxStoreSkuBatchSize
batchSize = tiktokShop.MaxStoreSkuBatchSize
case partner.FuncDeleteStoreSkus:
batchSize = mtwmapi.MaxBatchDeleteSize
batchSize = tiktokShop.MaxBatchDeleteSize
case partner.FuncCreateStoreSkus:
batchSize = 1 // 可考虑用批量操作
case partner.FuncUpdateStoreSkus:
@@ -47,36 +53,36 @@ func (p *PurchaseHandler) GetStoreSkusBatchSize(funcID int) (batchSize int) {
case partner.FuncGetStoreSkusFullInfo:
batchSize = 1
case partner.FuncCreateActs:
batchSize = mtwmapi.MaxRetailDiscountCreateBatchSize
batchSize = tiktokShop.MaxRetailDiscountCreateBatchSize
case partner.FuncCancelActs:
batchSize = mtwmapi.MaxRetailDiscountDeleteBatchSize
batchSize = tiktokShop.MaxRetailDiscountDeleteBatchSize
}
return batchSize
}
func getStoreVendorOrgCode(storeID int) (vendorOrgCode string) {
if storeMap, _ := dao.GetStoreDetail(dao.GetDB(), storeID, model.VendorIDMTWM, ""); storeMap != nil {
if storeMap, _ := dao.GetStoreDetail(dao.GetDB(), storeID, model.VendorIDDD, ""); storeMap != nil {
return storeMap.VendorOrgCode
}
return vendorOrgCode
}
// 门店分类
// GetStoreAllCategories 门店分类
func (p *PurchaseHandler) GetStoreAllCategories(ctx *jxcontext.Context, storeID int, vendorStoreID string) (cats []*partner.BareCategoryInfo, err error) {
remoteCats, err := getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatList(vendorStoreID)
remoteCats, err := getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).GetShopCategory(0)
if err == nil {
cats = convertVendorCatList(remoteCats)
}
return cats, err
}
func convertVendorCatList(remoteCats []*mtwmapi.RetailCategoryInfo) (cats []*partner.BareCategoryInfo) {
func convertVendorCatList(remoteCats []*tiktokShop.RetailCategoryInfo) (cats []*partner.BareCategoryInfo) {
for _, rCat := range remoteCats {
cat := &partner.BareCategoryInfo{
VendorCatID: rCat.Code,
VendorCatID: utils.Int64ToStr(rCat.Id),
Name: rCat.Name,
Level: rCat.Level,
Seq: rCat.Sequence,
Level: int(rCat.Level),
Seq: 0,
Children: convertVendorCatList(rCat.Children),
}
if cat.VendorCatID == "" {
@@ -113,63 +119,63 @@ func tryCatName2Code(originName string) (catCodeStr string) {
}
func (p *PurchaseHandler) CreateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
level := 1
if storeCat.ParentCatName != "" {
level = 2
}
originName := ""
catName := storeCat.Name
catCode := storeCat.ID
subCatName := ""
subCatCode := 0
if storeCat.CatSyncStatus&model.SyncFlagNewMask == 0 {
// 修改一级分类
originName = storeCat.VendorCatID
}
if level == 2 { // 二级分类
// 创建二级分类
originName = storeCat.ParentVendorCatID
catName = storeCat.ParentCatName
catCode = storeCat.ParentID
subCatName = storeCat.Name
subCatCode = storeCat.ID
if storeCat.CatSyncStatus&model.SyncFlagNewMask == 0 {
// 修改二级分类
originName = storeCat.VendorCatID
catName = storeCat.Name
catCode = storeCat.ID
subCatName = ""
subCatCode = 0
}
}
if catName == "" {
panic("catName is empty")
}
catName = utils.FilterEmoji(catName)
subCatName = utils.FilterEmoji(subCatName)
globals.SugarLogger.Debugf("mtwm CreateStoreCategory vendorStoreID:%s, originName:%s, catCode:%d, catName:%s, subCatCode:%d, subCatName:%s, seq:%d",
vendorStoreID, originName, catCode, catName, subCatCode, subCatName, storeCat.Seq)
if globals.EnableMtwmStoreWrite {
// err = api.MtwmAPI.RetailCatUpdate2(vendorStoreID, tryCatName2Code(originName), originName, catCode2Str(catCode), catName, catCode2Str(subCatCode), subCatName, storeCat.Seq)
param4Update := &mtwmapi.Param4UpdateCat{
CategoryCodeOrigin: tryCatName2Code(originName),
CategoryNameOrigin: originName,
CategoryCode: catCode2Str(catCode),
SecondaryCategoryCode: catCode2Str(subCatCode),
SecondaryCategoryName: subCatName,
Sequence: storeCat.Seq,
}
err = getAPI(storeCat.VendorOrgCode, storeID, vendorStoreID).RetailCatUpdate(vendorStoreID, catName, param4Update)
if storeCat.CatSyncStatus&model.SyncFlagNewMask == 0 && p.IsErrCategoryNotExist(err) && originName != "" { // 修改分类名,但分类不存在
storeCat.CatSyncStatus |= model.SyncFlagNewMask
err = p.CreateStoreCategory(ctx, storeID, vendorStoreID, storeCat)
}
}
if err == nil {
// storeCat.VendorCatID = utils.FilterEmoji(storeCat.Name)
storeCat.VendorCatID = utils.Int2Str(storeCat.ID)
}
//level := 1
//if storeCat.ParentCatName != "" {
// level = 2
//}
//originName := ""
//catName := storeCat.Name
//catCode := storeCat.ID
//
//subCatName := ""
//subCatCode := 0
//if storeCat.CatSyncStatus&model.SyncFlagNewMask == 0 {
// // 修改一级分类
// originName = storeCat.VendorCatID
//}
//if level == 2 { // 二级分类
// // 创建二级分类
// originName = storeCat.ParentVendorCatID
// catName = storeCat.ParentCatName
// catCode = storeCat.ParentID
// subCatName = storeCat.Name
// subCatCode = storeCat.ID
// if storeCat.CatSyncStatus&model.SyncFlagNewMask == 0 {
// // 修改二级分类
// originName = storeCat.VendorCatID
// catName = storeCat.Name
// catCode = storeCat.ID
// subCatName = ""
// subCatCode = 0
// }
//}
//if catName == "" {
// panic("catName is empty")
//}
//catName = utils.FilterEmoji(catName)
//subCatName = utils.FilterEmoji(subCatName)
//globals.SugarLogger.Debugf("mtwm CreateStoreCategory vendorStoreID:%s, originName:%s, catCode:%d, catName:%s, subCatCode:%d, subCatName:%s, seq:%d",
// vendorStoreID, originName, catCode, catName, subCatCode, subCatName, storeCat.Seq)
//if globals.EnableMtwmStoreWrite {
// // err = api.MtwmAPI.RetailCatUpdate2(vendorStoreID, tryCatName2Code(originName), originName, catCode2Str(catCode), catName, catCode2Str(subCatCode), subCatName, storeCat.Seq)
// param4Update := &mtwmapi.Param4UpdateCat{
// CategoryCodeOrigin: tryCatName2Code(originName),
// CategoryNameOrigin: originName,
// CategoryCode: catCode2Str(catCode),
// SecondaryCategoryCode: catCode2Str(subCatCode),
// SecondaryCategoryName: subCatName,
// Sequence: storeCat.Seq,
// }
// err = getAPI(storeCat.VendorOrgCode, storeID, vendorStoreID).RetailCatUpdate(vendorStoreID, catName, param4Update)
// if storeCat.CatSyncStatus&model.SyncFlagNewMask == 0 && p.IsErrCategoryNotExist(err) && originName != "" { // 修改分类名,但分类不存在
// storeCat.CatSyncStatus |= model.SyncFlagNewMask
// err = p.CreateStoreCategory(ctx, storeID, vendorStoreID, storeCat)
// }
//}
//if err == nil {
// // storeCat.VendorCatID = utils.FilterEmoji(storeCat.Name)
// storeCat.VendorCatID = utils.Int2Str(storeCat.ID)
//}
return err
}
@@ -178,23 +184,23 @@ func (p *PurchaseHandler) UpdateStoreCategory(ctx *jxcontext.Context, storeID in
}
func (p *PurchaseHandler) DeleteStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID, vendorCatID string, level int) (err error) {
if false {
if globals.EnableMtwmStoreWrite {
err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatDelete(vendorStoreID, tryCatName2Code(vendorCatID), vendorCatID)
}
} else {
var catCodes []string
if catCode := tryCatName2Code(vendorCatID); catCode != "" {
catCodes = []string{catCode}
}
if globals.EnableMtwmStoreWrite {
if level == 1 {
err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatSkuBatchDelete2(ctx.GetTrackInfo(), vendorStoreID, catCodes, []string{vendorCatID}, nil, nil, nil)
} else {
err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatSkuBatchDelete2(ctx.GetTrackInfo(), vendorStoreID, nil, nil, catCodes, []string{vendorCatID}, nil)
}
}
}
//if false {
// if globals.EnableMtwmStoreWrite {
// err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatDelete(vendorStoreID, tryCatName2Code(vendorCatID), vendorCatID)
// }
//} else {
// var catCodes []string
// if catCode := tryCatName2Code(vendorCatID); catCode != "" {
// catCodes = []string{catCode}
// }
// if globals.EnableMtwmStoreWrite {
// if level == 1 {
// err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatSkuBatchDelete2(ctx.GetTrackInfo(), vendorStoreID, catCodes, []string{vendorCatID}, nil, nil, nil)
// } else {
// err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatSkuBatchDelete2(ctx.GetTrackInfo(), vendorStoreID, nil, nil, catCodes, []string{vendorCatID}, nil)
// }
// }
//}
return err
}
@@ -231,167 +237,167 @@ func (p *PurchaseHandler) UpdateStoreSkus(ctx *jxcontext.Context, storeID int, v
return failedList, err
}
// CreateStoreSkus 门店创建商品
func (p *PurchaseHandler) CreateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
failedList, err = p.createOrUpdateStoreSkus(ctx, storeID, vendorStoreID, storeSkuList, true)
// if err == nil && vendorStoreID == specialStoreID {
// for i := 0; i < 2; i++ {
// p.createOrUpdateStoreSkus(ctx, storeID, vendorStoreID, duplicateStoreSkuList(storeSkuList, i+1), true)
// }
// }
//if err == nil && vendorStoreID == specialStoreID {
// for i := 0; i < 2; i++ {
// p.createOrUpdateStoreSkus(ctx, storeID, vendorStoreID, duplicateStoreSkuList(storeSkuList, i+1), true)
// }
//}
return failedList, err
}
// 对于多门店平台来说storeSkuList中只有SkuID与VendorSkuID有意义
// 抖店的商品只管创建,创建接口会返回成功,但是审核的时候不一定成功.当前系统无法判定此商品是否已经创建过了!
func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo, isCreate bool) (failedList []*partner.StoreSkuInfoWithErr, err error) {
var syncType string
foodDataList := make([]map[string]interface{}, len(storeSkuList))
storeDetail, _ := dao.GetStoreDetail(dao.GetDB(), storeID, model.VendorIDDD, "")
api := getAPI(storeSkuList[0].VendorOrgCode, storeID, vendorStoreID)
freightId, err := api.GetStoreFreight(int64(storeID))
if err != nil {
return nil, err
}
if isCreate {
syncType = "创建商品"
for _, storeSku := range storeSkuList {
// 创建商品
param := &product_addV2_request.ProductAddV2Param{
CategoryLeafId: utils.Str2Int64(storeSku.VendorCatID),
Name: storeSku.Name,
PayType: tiktokShop.TiktokPayType1,
ReduceType: tiktokShop.SkuReduceTypePayMakeOrder,
FreightId: freightId,
Weight: utils.Int2Float64(storeSku.Weight),
DeliveryDelayDay: tiktokShop.DeliveryDelayDayToDay,
PresellType: tiktokShop.SendGoodsTypeNow,
Supply7dayReturn: 0,
Mobile: storeDetail.Tel1,
Commit: true,
Specs: "净重|" + fmt.Sprintf("%f", storeSku.SpecQuality) + storeSku.SpecUnit,
NeedRechargeMode: false,
SellChannel: []int64{0},
StartSaleType: 0,
PickupMethod: "0",
StoreId: int64(storeSku.StoreID),
MainProductId: int64(storeSku.NameID),
}
// 获取上传图,商品轮播图
img, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeSku.ImgOrigin, storeSku.Img2, storeSku.Img3, storeSku.Img4, storeSku.Img5)
if err != nil {
return nil, err
}
param.Pic = img
// 商品详情图
img2, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeSku.DescImg)
if err != nil {
return nil, err
}
param.Description = img2
// weight_unit 目前抖音只支持g和kg两种
switch storeSku.Unit {
case "g", "ml", "G", "ML":
param.WeightUnit = tiktokShop.WeightUint_G
case "kg", "l", "L", "KG":
param.WeightUnit = tiktokShop.WeightUint_G
}
// spec_prices
param.SpecPrices = GetSpecPrices(param.Specs, storeSku)
// ProductFormatNew
productFormatNew, err := GetProductFormatNew(param.CategoryLeafId)
if err != nil {
return nil, err
}
param.ProductFormatNew = productFormatNew
// 抖店创建商品
tiktokResult, err := api.CreateStoreCommodity(param)
if err != nil {
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType)
storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID)
continue
}
storeSku.VendorSkuID = utils.Int64ToStr(tiktokResult.ProductId)
}
} else {
syncType = "更新商品"
}
for i, storeSku := range storeSkuList {
isNeedUpdatePrice := isCreate //storeSku.SkuSyncStatus&(model.SyncFlagPriceMask|model.SyncFlagNewMask) != 0
foodData := make(map[string]interface{})
foodDataList[i] = foodData
foodData[mtwmapi.KeyAppFoodCode] = utils.Int2Str(storeSku.SkuID)
skus := []map[string]interface{}{
map[string]interface{}{
"sku_id": foodData[mtwmapi.KeyAppFoodCode],
},
}
foodData["skus"] = skus
foodData["name"] = utils.LimitUTF8StringLen(storeSku.SkuName, mtwmapi.MaxSkuNameCharCount)
foodData["description"] = storeSku.Comment
if isNeedUpdatePrice {
foodData["price"] = jxutils.IntPrice2Standard(storeSku.VendorPrice)
}
if storeSku.MinOrderCount != 0 {
foodData["min_order_count"] = storeSku.MinOrderCount
} else {
foodData["min_order_count"] = 1
}
foodData["unit"] = storeSku.Unit
attr := switchAttr(storeSku.VendorVendorCatID)
if attr != "" {
foodData["common_attr_value"] = attr
}
catCode := tryCatName2Code(storeSku.VendorCatID)
if catCode != "" {
foodData["category_code"] = catCode
} else {
foodData["category_name"] = storeSku.VendorCatID
}
foodData["is_sold_out"] = skuStatusJX2Mtwm(storeSku.MergedStatus)
if true { // vendorStoreID == specialStoreID {
img2 := storeSku.Img2
img3 := storeSku.Img3
img4 := storeSku.Img4
img5 := storeSku.Img5
if img2 == "" {
img2 = storeSku.Img
for _, storeSku := range storeSkuList {
param := &product_editV2_request.ProductEditV2Param{
ProductId: utils.Str2Int64(storeSku.VendorSkuID),
CategoryLeafId: utils.Str2Int64(storeSku.VendorCatID),
Name: storeSku.Name,
PayType: tiktokShop.TiktokPayType1,
ReduceType: tiktokShop.SkuReduceTypePayMakeOrder,
FreightId: freightId,
Weight: utils.Int2Float64(storeSku.Weight),
DeliveryDelayDay: tiktokShop.DeliveryDelayDayToDay,
PresellType: tiktokShop.SendGoodsTypeNow,
Supply7dayReturn: 0,
Mobile: storeDetail.Tel1,
Commit: true,
Specs: "净重|" + fmt.Sprintf("%f", storeSku.SpecQuality) + storeSku.SpecUnit,
NeedRechargeMode: false,
SellChannel: []int64{0},
StartSaleType: 0,
PickupMethod: "0",
StoreId: int64(storeSku.StoreID),
MainProductId: int64(storeSku.NameID),
}
if img3 == "" {
img3 = storeSku.Img
}
if img4 == "" {
img4 = storeSku.Img
}
if img5 == "" {
img5 = storeSku.Img
}
if storeSku.ImgMix != "" && ((storeSku.BrandID == storeSku.ExBrandID && storeSku.ExBrandID != 0) || storeSku.ExBrandID == 0) {
foodData["picture"] = strings.Join(jxutils.BatchString2Slice(storeSku.ImgMix, img2, img3, img4, img5), ",")
} else {
foodData["picture"] = strings.Join(jxutils.BatchString2Slice(storeSku.Img, img2, img3, img4, img5), ",")
}
} else {
foodData["picture"] = strings.Join(jxutils.BatchString2Slice(storeSku.Img, storeSku.Img2), ",")
}
if storeSku.DescImg != "" {
foodData["picture_contents"] = storeSku.DescImg
}
foodData["sequence"] = storeSku.GetSeq()
if storeSku.VendorVendorCatID != 0 {
foodData["tag_id"] = utils.Int64ToStr(storeSku.VendorVendorCatID)
} else {
// foodData["tag_id"] = utils.Int64ToStr(defVendorCatID)
}
skus[0]["spec"] = jxutils.ComposeSkuSpec(storeSku.SpecQuality, storeSku.SpecUnit)
if isNeedUpdatePrice {
skus[0]["price"] = foodData["price"]
}
skus[0]["stock"] = stockCount2Mtwm(model.MaxStoreSkuStockQty)
if storeSku.Upc != "" {
skus[0]["upc"] = storeSku.Upc
}
skus[0]["ladder_box_num"] = storeSku.LadderBoxNum
boxPirce := 0
if storeSku.MtLadderBoxPrice != 0 {
boxPirce = storeSku.MtLadderBoxPrice
} else {
boxPirce = storeSku.LadderBoxPrice
}
skus[0]["ladder_box_price"] = jxutils.IntPrice2Standard(int64(boxPirce))
if foodData["tag_id"] != nil {
skus[0]["weight"] = storeSku.Weight // weight字段仅限服饰鞋帽、美妆、日用品、母婴、生鲜果蔬、生活超市下的便利店/超市门店品类的商家使用
}
}
if globals.EnableMtwmStoreWrite {
if len(foodDataList) == 1 {
foodDataList[0]["skus"] = string(utils.MustMarshal(foodDataList[0]["skus"]))
if err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailInitData(ctx.GetTrackInfo(), vendorStoreID, utils.Int2Str(storeSkuList[0].SkuID), foodDataList[0]); err != nil {
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDMTWM], syncType)
// 获取上传图,商品轮播图
img, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeSku.ImgOrigin, storeSku.Img2, storeSku.Img3, storeSku.Img4, storeSku.Img5)
if err != nil {
return nil, err
}
} else if len(foodDataList) > 0 {
failedFoodList, err2 := getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList)
if err = err2; err == nil {
if err = putils.GenPartialFailedErr(failedFoodList, len(failedFoodList)); err != nil {
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], syncType)
// successList = putils.UnselectStoreSkuSyncListByVendorSkuIDs(storeSkuList, getAppFoodCodeList(failedFoodList))
}
} else if err2 != nil && len(failedFoodList) == 0 {
if errExt, ok := err2.(*utils.ErrorWithCode); ok {
err = utils.UnmarshalUseNumber([]byte(errExt.ErrMsg()), &failedFoodList)
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], syncType)
}
param.Pic = img
// 商品详情图
img2, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeSku.DescImg)
if err != nil {
return nil, err
}
param.Description = img2
// weight_unit 目前抖音只支持g和kg两种
switch storeSku.Unit {
case "g", "ml", "G", "ML":
param.WeightUnit = tiktokShop.WeightUint_G
case "kg", "l", "L", "KG":
param.WeightUnit = tiktokShop.WeightUint_G
}
// spec_prices
param.SpecPrices = GetSpecPrices(param.Specs, storeSku)
// ProductFormatNew
productFormatNew, err := GetProductFormatNew(param.CategoryLeafId)
if err != nil {
return nil, err
}
param.ProductFormatNew = productFormatNew
// 抖店更新商品
if err := api.EditStoreCommodity(param); err != nil {
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType)
storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID)
continue
}
}
return
}
for _, storeSku := range storeSkuList {
storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID)
}
if len(failedList) > 0 {
err = nil
}
return failedList, err
}
func getAppFoodCodeList(l []*mtwmapi.AppFoodResult) (vendorSkuIDs []string) {
if len(l) > 0 {
vendorSkuIDs = make([]string, len(l))
for k, v := range l {
vendorSkuIDs[k] = v.AppFoodCode
}
}
return vendorSkuIDs
}
func (p *PurchaseHandler) DeleteStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
if globals.EnableMtwmStoreWrite {
if len(storeSkuList) == 1 {
err = getAPI(storeSkuList[0].VendorOrgCode, storeID, vendorStoreID).RetailDelete(ctx.GetTrackInfo(), vendorStoreID, storeSkuList[0].VendorSkuID)
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDMTWM], "删除商品")
} else {
// todo 部分失败
err = getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID).RetailCatSkuBatchDelete2(ctx.GetTrackInfo(), vendorStoreID, nil, nil, nil, nil, partner.BareStoreSkuInfoList(storeSkuList).GetVendorSkuIDList())
if err != nil {
if errExt, ok := err.(*utils.ErrorWithCode); ok {
myMap := make(map[string][]*mtwmapi.AppFoodResult)
json.Unmarshal([]byte(errExt.ErrMsg()), &myMap)
failedList = SelectStoreSkuListByFoodList(storeSkuList, myMap["retail_error_list"], storeID, model.VendorChineseNames[model.VendorIDMTWM], "批量删除商品")
}
if globals.EnableDdStoreWrite {
for _, v := range storeSkuList {
if err = getAPI(storeSkuList[0].VendorOrgCode, storeID, vendorStoreID).DeleteStoreCommodity(utils.Str2Int64(v.VendorSkuID)); err != nil {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "删除商品")...)
}
}
}
@@ -401,30 +407,6 @@ func (p *PurchaseHandler) DeleteStoreSkus(ctx *jxcontext.Context, storeID int, v
return failedList, err
}
func switchAttr(vendorCatID int64) (attrs string) {
switch vendorCatID {
case 200002727:
return mtwmapi.MtwmSkuAttr200002727
case 200001555:
return mtwmapi.MtwmSkuAttr200001555
case 200002728:
return mtwmapi.MtwmSkuAttr200002728
case 200001519, 200000592:
return mtwmapi.MtwmSkuAttr200000592
case 200002704, 200002731:
return mtwmapi.MtwmSkuAttr200002731
case 200002716:
return mtwmapi.MtwmSkuAttr200002716
case 200002667, 200002713, 200002670:
return mtwmapi.MtwmSkuAttr200002670
case 200002680:
return mtwmapi.MtwmSkuAttr200002680
default:
return ""
}
return attrs
}
func stockCount2Mtwm(stock int) (mtwmStock string) {
return utils.Int2Str(stock)
}
@@ -451,44 +433,74 @@ func storeSku2Mtwm(storeSkuList []*partner.StoreSkuInfo, updateType int) (skuLis
return skuList
}
// UpdateStoreSkusStatus 批量更新商品上下架
func (p *PurchaseHandler) UpdateStoreSkusStatus(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo, status int) (failedList []*partner.StoreSkuInfoWithErr, err error) {
skuList := storeSku2Mtwm(storeSkuList, updateTypeStatus)
mtwmStatus := skuStatusJX2Mtwm(status)
if globals.EnableMtwmStoreWrite {
failedFoodList, err2 := getAPI(vendorOrgCode, storeID, vendorStoreID).RetailSellStatus(ctx.GetTrackInfo(), vendorStoreID, skuList, mtwmStatus)
if err = err2; err == nil {
if len(failedFoodList) > 0 {
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], "更新商品状态")
// successList = putils.UnselectStoreSkuListByVendorSkuIDs(storeSkuList, getAppFoodCodeList(failedFoodList))
api := getAPI(vendorOrgCode, storeID, vendorStoreID)
if status == model.SkuStatusNormal { // 上架
for _, v := range storeSkuList {
if err := api.EditStoreCommodity(&product_editV2_request.ProductEditV2Param{
ProductId: utils.Str2Int64(v.VendorSkuID),
Commit: true,
}); err != nil {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "上架商品正常")...)
} else {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "上架商品异常")...)
}
} else if err2 != nil && len(failedFoodList) == 0 {
if errExt, ok := err2.(*utils.ErrorWithCode); ok {
err = utils.UnmarshalUseNumber([]byte(errExt.ErrMsg()), &failedFoodList)
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], "更新商品状态")
}
} else { // 下架
for _, v := range storeSkuList {
err := api.ProductSetOffline(utils.Str2Int64(v.VendorSkuID))
if err == nil {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "下架商品正常")...)
} else {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "下架商品异常")...)
}
}
}
if len(failedList) > 0 {
err = nil
}
return failedList, err
}
// UpdateStoreSkusPrice 更新商品价格
func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
priceList := storeSku2Mtwm(storeSkuList, updateTypePrice)
if globals.EnableMtwmStoreWrite {
failedFoodList, err2 := getAPI(vendorOrgCode, storeID, vendorStoreID).RetailSkuPrice(ctx.GetTrackInfo(), vendorStoreID, priceList)
if err = err2; err == nil {
if len(failedFoodList) > 0 {
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], "更新商品价格")
}
} else if err2 != nil && len(failedFoodList) == 0 {
if errExt, ok := err2.(*utils.ErrorWithCode); ok {
err = utils.UnmarshalUseNumber([]byte(errExt.ErrMsg()), &failedFoodList)
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], "更新商品价格")
api := getAPI(vendorOrgCode, storeID, vendorStoreID)
for _, v := range storeSkuList {
param := &product_editV2_request.ProductEditV2Param{}
param.ProductId = utils.Str2Int64(v.VendorSkuID)
param.Specs = "净重|" + fmt.Sprintf("%f", v.SpecQuality) + v.SpecUnit
skuSize := make([]*tiktokShop.SpecDetailList, 0, 0)
detail1 := strings.Split(param.Specs, "^")
name1 := strings.Split(strings.Split(detail1[0], "|")[1], ",")
for i := 0; i < len(name1); i++ {
sku := &tiktokShop.SpecDetailList{
SpecDetailName1: name1[i],
StockNum: v.Stock,
Price: int(v.VendorPrice),
Code: utils.Int2Str(v.SkuID),
StepStockNum: 0,
SupplierID: "",
OuterSkuID: utils.Int2Str(v.NameID),
DeliveryInfos: []*tiktokShop.DeliveryInfos{
{InfoType: "weight", InfoUnit: v.SpecUnit, InfoValue: fmt.Sprintf("%f", v.SpecQuality)},
},
}
skuSize = append(skuSize, sku)
}
data, _ := json.Marshal(skuSize)
param.SpecPrices = string(data)
err := api.EditStoreCommodity(param)
if err != nil {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "上架商品正常")...)
} else {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "上架商品异常")...)
}
}
if len(failedList) > 0 {
err = nil
}
@@ -496,31 +508,14 @@ func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, vendorOrg
}
func (p *PurchaseHandler) UpdateStoreSkusStock(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
stockList := storeSku2Mtwm(storeSkuList, updateTypeStock)
if globals.EnableMtwmStoreWrite {
failedFoodList, err2 := getAPI(vendorOrgCode, storeID, vendorStoreID).RetailSkuStock(ctx.GetTrackInfo(), vendorStoreID, stockList)
if err = err2; err == nil {
if len(failedFoodList) > 0 {
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], "更新商品库存")
}
//if err = putils.GenPartialFailedErr(failedFoodList, len(failedFoodList)); err != nil {
// successList = putils.UnselectStoreSkuListByVendorSkuIDs(storeSkuList, getAppFoodCodeList(failedFoodList))
// }
} else if err2 != nil && len(failedFoodList) == 0 {
if errExt, ok := err2.(*utils.ErrorWithCode); ok {
err = utils.UnmarshalUseNumber([]byte(errExt.ErrMsg()), &failedFoodList)
failedList = SelectStoreSkuListByFoodList(storeSkuList, failedFoodList, storeID, model.VendorChineseNames[model.VendorIDMTWM], "更新商品库存")
}
}
if globals.EnableDdStoreWrite {
return p.UpdateStoreSkusPrice(ctx, vendorOrgCode, storeID, vendorStoreID, storeSkuList)
}
if len(failedList) > 0 {
err = nil
}
return failedList, err
return nil, err
}
func mtwmSkuStatus2Jx(mtwmSkuStatus int) (jxSkuStatus int) {
if mtwmSkuStatus == mtwmapi.SellStatusOnline {
func tiktokSkuStatus2Jx(skuStatus int64) (jxSkuStatus int) {
if skuStatus == mtwmapi.SellStatusOnline {
jxSkuStatus = model.SkuStatusNormal
} else {
jxSkuStatus = model.SkuStatusDontSale
@@ -529,188 +524,134 @@ func mtwmSkuStatus2Jx(mtwmSkuStatus int) (jxSkuStatus int) {
}
func (p *PurchaseHandler) GetStoreSkusFullInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (skuNameList []*partner.SkuNameInfo, err error) {
if len(storeSkuList) == 1 {
skuInfo, err := getAPI(storeSkuList[0].VendorOrgCode, storeID, vendorStoreID).RetailGet(vendorStoreID, utils.Int2Str(storeSkuList[0].SkuID))
// 查询单个门店商品
tiktokApi := getAPI(storeSkuList[0].VendorOrgCode, storeID, vendorStoreID)
for _, v := range storeSkuList {
skuInfo, err := tiktokApi.GetSkuDetail(v.VendorSkuID, utils.Int2Str(v.SkuID))
if err != nil {
return nil, err
}
if skuName := vendorSku2Jx(skuInfo); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
} else {
var storeSkuMap map[string]*partner.StoreSkuInfo
if storeSkuList != nil {
storeSkuMap = putils.StoreSkuList2MapByVendorSkuID(storeSkuList)
}
mtapi := getAPI(getStoreVendorOrgCode(storeID), storeID, vendorStoreID)
for {
// todo 待优化获取速度
result, err := mtapi.RetailList(vendorStoreID, len(skuNameList), mtwmapi.GeneralMaxLimit)
if err != nil {
return nil, err
}
if storeSkuMap == nil {
skuNameList = append(skuNameList, vendorSkuList2Jx(result)...)
} else {
for _, v := range result {
if storeSkuMap[v.AppFoodCode] != nil {
if skuName := vendorSku2Jx(v); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
}
}
}
if len(result) < mtwmapi.GeneralMaxLimit {
break
}
}
}
//switch len(storeSkuList) {
//case 1:
//
//default:
// // 列表查询门店商品
// //var storeSkuMap map[string]*partner.StoreSkuInfo
// //if storeSkuList != nil {
// // storeSkuMap = putils.StoreSkuList2MapByVendorSkuID(storeSkuList)
// //}
// //for {
// // result, err := mtapi.RetailList(vendorStoreID, len(skuNameList), mtwmapi.GeneralMaxLimit)
// // if err != nil {
// // return nil, err
// // }
// // if storeSkuMap == nil {
// // skuNameList = append(skuNameList, vendorSkuList2Jx(result)...)
// // } else {
// // for _, v := range result {
// // if storeSkuMap[v.AppFoodCode] != nil {
// // if skuName := vendorSku2Jx(v); skuName != nil {
// // skuNameList = append(skuNameList, skuName)
// // }
// // }
// // }
// // }
// // if len(result) < mtwmapi.GeneralMaxLimit {
// // break
// // }
// //}
//}
return skuNameList, err
}
func vendorSku2Jx(appFood *mtwmapi.AppFood) (skuName *partner.SkuNameInfo) {
if len(appFood.SkuList) == 0 {
globals.SugarLogger.Warnf("vendorSku2Jx, strange appFood:%s", utils.Format4Output(appFood, true))
func vendorSku2Jx(tiktokSku *product_detail_response.ProductDetailData) (skuName *partner.SkuNameInfo) {
if len(tiktokSku.SpecPrices) == 0 {
globals.SugarLogger.Warnf("vendorSku2Jx, strange tiktokSku:%s", utils.Format4Output(tiktokSku, true))
return nil
}
prefix, name, comment, specUnit, unit, specQuality := jxutils.SplitSkuName(appFood.Name)
vendorSku := appFood.SkuList[0]
weight := int(utils.Str2Int64WithDefault(vendorSku.Weight, 0))
if weight <= 0 {
weight = jxutils.FormatSkuWeight(specQuality, specUnit)
}
skuID := int(utils.Str2Int64WithDefault(vendorSku.SkuID, 0))
prefix, name, comment, specUnit, unit, specQuality := jxutils.SplitSkuName(tiktokSku.Name)
vendorSku := tiktokSku.SpecPrices[0]
weight := jxutils.FormatSkuWeight(specQuality, specUnit)
skuName = &partner.SkuNameInfo{
NameID: int(utils.Str2Int64WithDefault(appFood.AppFoodCode, 0)),
VendorNameID: appFood.AppFoodCode,
UPC: appFood.SkuList[0].Upc,
NameID: utils.Str2Int(vendorSku.OuterSkuId),
VendorNameID: utils.Int64ToStr(tiktokSku.MainProductId),
UPC: tiktokSku.ProductIdStr,
Prefix: prefix,
Name: name,
Unit: unit,
Status: mtwmSkuStatus2Jx(appFood.IsSoldOut), //此处为之前一个bug 如果吧状态放到切片内层 对于内层函数中映射无法关联 永远获取到的初始值为0
Status: tiktokSkuStatus2Jx(tiktokSku.Status), //此处为之前一个bug 如果吧状态放到切片内层 对于内层函数中映射无法关联 永远获取到的初始值为0
SkuList: []*partner.SkuInfo{
&partner.SkuInfo{
StoreSkuInfo: partner.StoreSkuInfo{
VendorSkuID: vendorSku.SkuID,
SkuID: skuID,
IsSpecialty: appFood.IsSpecialty,
Stock: int(utils.Str2Int64WithDefault(vendorSku.Stock, partner.UnlimitedStoreSkuStock)),
VendorPrice: jxutils.StandardPrice2Int(utils.Str2Float64WithDefault(vendorSku.Price, 0)),
Status: mtwmSkuStatus2Jx(appFood.IsSoldOut),
VendorSkuID: utils.Int64ToStr(vendorSku.SkuId),
SkuID: int(vendorSku.OutSkuId),
IsSpecialty: 0,
Stock: int(vendorSku.StockNum),
VendorPrice: vendorSku.Price,
Status: tiktokSkuStatus2Jx(tiktokSku.Status),
},
SkuName: appFood.Name,
SkuName: tiktokSku.Name,
Comment: comment,
SpecQuality: float64(specQuality),
SpecUnit: specUnit,
Weight: weight,
},
},
PictureList: appFood.PictureList,
PictureList: tiktokSku.Pic,
}
if appFood.CategoryName != "" {
if tiktokSku.CategoryDetail != nil {
// todo, 因为当前我们用的是分类名操作这种方式所以要返回分类名而不是分类code
skuName.VendorCatIDList = []string{appFood.CategoryName}
if appFood.SecondaryCategoryName != "" {
skuName.VendorCatIDList = append(skuName.VendorCatIDList, appFood.SecondaryCategoryName)
}
skuName.VendorCatIDList = []string{tiktokSku.CategoryDetail.FirstCname, tiktokSku.CategoryDetail.SecondCname, tiktokSku.CategoryDetail.ThirdCname, tiktokSku.CategoryDetail.FourthCname}
}
return skuName
}
func vendorSkuList2Jx(appFoodList []*mtwmapi.AppFood) (skuNameList []*partner.SkuNameInfo) {
for _, appFood := range appFoodList {
if skuName := vendorSku2Jx(appFood); skuName != nil {
skuNameList = append(skuNameList, skuName)
}
}
return skuNameList
}
func (p *PurchaseHandler) GetSensitiveWordRegexp() *regexp.Regexp {
return sensitiveWordRegexp
}
//美团api返回
func SelectStoreSkuListByFoodList(storeSkuList interface{}, foodList []*mtwmapi.AppFoodResult, storeID int, vendorName, syncType string) (selectedStoreSkuList []*partner.StoreSkuInfoWithErr) {
foodMap := make(map[string]string)
if len(foodList) > 0 {
for _, v := range foodList {
foodMap[v.AppFoodCode] = v.ErrorMsg
}
if storeSkuLists, ok := storeSkuList.([]*partner.StoreSkuInfo); ok {
for _, v := range storeSkuLists {
if foodMap[v.VendorSkuID] != "" {
foodFailed := &partner.StoreSkuInfoWithErr{
StoreSkuInfo: v,
ErrMsg: foodMap[v.VendorSkuID],
StoreID: storeID,
VendoreName: vendorName,
SyncType: syncType,
}
selectedStoreSkuList = append(selectedStoreSkuList, foodFailed)
}
}
}
if storeSkuLists, ok := storeSkuList.([]*dao.StoreSkuSyncInfo); ok {
for _, v := range storeSkuLists {
if foodMap[v.VendorSkuID] != "" {
storeSkuInfo := &partner.StoreSkuInfo{
SkuID: v.SkuID,
VendorSkuID: v.VendorSkuID,
NameID: v.NameID,
VendorNameID: v.VendorNameID,
VendorPrice: v.VendorPrice,
Status: v.Status,
}
foodFailed := &partner.StoreSkuInfoWithErr{
StoreSkuInfo: storeSkuInfo,
ErrMsg: foodMap[v.VendorSkuID],
StoreID: storeID,
VendoreName: vendorName,
SyncType: syncType,
}
selectedStoreSkuList = append(selectedStoreSkuList, foodFailed)
}
}
}
}
return selectedStoreSkuList
}
// CreateStoreSkusAct 创建商品活动
func (p *PurchaseHandler) CreateStoreSkusAct(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
actStoreSkuList := putils.StoreSku2ActStoreSku(model.SyncFlagNewMask, vendorStoreID, storeSkuList)
failedList, err = createOneShopAct(putils.GetFixDirectDownAct(vendorOrgCode, storeID, 0), vendorStoreID, actStoreSkuList)
storeSkuMap := putils.StoreSkuList2MapBySkuID(storeSkuList)
for _, v := range actStoreSkuList {
storeSkuMap[v.SkuID].VendorActID = v.VendorActID
}
if len(failedList) > 0 {
err = nil
}
//actStoreSkuList := putils.StoreSku2ActStoreSku(model.SyncFlagNewMask, vendorStoreID, storeSkuList)
//failedList, err = createOneShopAct(putils.GetFixDirectDownAct(vendorOrgCode, storeID, 0), vendorStoreID, actStoreSkuList)
//storeSkuMap := putils.StoreSkuList2MapBySkuID(storeSkuList)
//for _, v := range actStoreSkuList {
// storeSkuMap[v.SkuID].VendorActID = v.VendorActID
//}
//if len(failedList) > 0 {
// err = nil
//}
return failedList, err
}
func (p *PurchaseHandler) CancelActs(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
return cancelOneShopAct(putils.GetFixDirectDownAct(vendorOrgCode, storeID, 0), vendorStoreID, putils.StoreSku2ActStoreSku(model.SyncFlagDeletedMask, vendorStoreID, storeSkuList))
return nil, err
// return cancelOneShopAct(putils.GetFixDirectDownAct(vendorOrgCode, storeID, 0), vendorStoreID, putils.StoreSku2ActStoreSku(model.SyncFlagDeletedMask, vendorStoreID, storeSkuList))
}
func (p *PurchaseHandler) UpdateStoreSkusSpecTag(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (err error) {
var foodDataList = []map[string]interface{}{}
for _, v := range storeSkuList {
var foodData = make(map[string]interface{})
if v.IsSpecialty != 0 && v.IsSpecialty == -1 {
v.IsSpecialty = 0
}
foodData["is_specialty"] = v.IsSpecialty
foodData["app_food_code"] = v.SkuID
foodDataList = append(foodDataList, foodData)
}
if globals.EnableMtwmStoreWrite {
if len(foodDataList) == 1 {
err = getAPI(vendorOrgCode, storeID, vendorStoreID).RetailInitData(ctx.GetTrackInfo(), vendorStoreID, utils.Int2Str(storeSkuList[0].SkuID), foodDataList[0])
} else if len(foodDataList) > 0 {
_, err = getAPI(vendorOrgCode, storeID, vendorStoreID).RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList)
}
}
//var foodDataList []map[string]interface{}
//for _, v := range storeSkuList {
// var foodData = make(map[string]interface{})
// if v.IsSpecialty != 0 && v.IsSpecialty == -1 {
// v.IsSpecialty = 0
// }
// foodData["is_specialty"] = v.IsSpecialty
// foodData["app_food_code"] = v.SkuID
// foodDataList = append(foodDataList, foodData)
//}
//if globals.EnableMtwmStoreWrite {
// if len(foodDataList) == 1 {
// err = getAPI(vendorOrgCode, storeID, vendorStoreID).RetailInitData(ctx.GetTrackInfo(), vendorStoreID, utils.Int2Str(storeSkuList[0].SkuID), foodDataList[0])
// } else if len(foodDataList) > 0 {
// _, err = getAPI(vendorOrgCode, storeID, vendorStoreID).RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList)
// }
//}
return err
}