312 lines
11 KiB
Go
312 lines
11 KiB
Go
package cms
|
|
|
|
import (
|
|
"fmt"
|
|
"git.rosy.net.cn/baseapi/platformapi/ebaiapi"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
"git.rosy.net.cn/jx-callback/business/model/dao"
|
|
"git.rosy.net.cn/jx-callback/business/partner"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
"git.rosy.net.cn/jx-callback/globals/api"
|
|
"time"
|
|
|
|
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
)
|
|
|
|
// CopyOnStoreSkuToOther 将一个美团门店分类和商品复制到另一个门店
|
|
func CopyOnStoreSkuToOther(ctx *jxcontext.Context, fromStoreId, toStoreId string, vendorId int, isAsync bool, offSet int) (hint string, err error) {
|
|
var (
|
|
db = dao.GetDB()
|
|
)
|
|
|
|
// 门店api加载
|
|
toStore, err := dao.GetStoreDetailByVendorStoreID(db, toStoreId, vendorId, "")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
fromStore, err := dao.GetStoreDetailByVendorStoreID(db, fromStoreId, vendorId, "")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
switch vendorId {
|
|
case model.VendorIDMTWM:
|
|
return CopyMtToMT(ctx, fromStore, toStore, isAsync, offSet)
|
|
case model.VendorIDEBAI:
|
|
return CopyEBaiToEBai(ctx, fromStore, toStore, isAsync, offSet)
|
|
default:
|
|
return "", fmt.Errorf("暂时还不支持")
|
|
}
|
|
}
|
|
|
|
// CopyEBaiToEBai 饿了么商品复制到饿了么
|
|
func CopyEBaiToEBai(ctx *jxcontext.Context, fromStore, toStore *dao.StoreDetail, isAsync bool, offset int) (hint string, err error) {
|
|
VendorCategoryIDMap := map[int64]int64{}
|
|
api := api.EbaiAPI
|
|
taskName := fmt.Sprintf("将饿了么平台门店[%s],分类和商品复制到[%s]", fromStore.VendorStoreID, toStore.VendorStoreID)
|
|
config := tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(false)
|
|
work := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
step := batchItemList[0].(int)
|
|
switch step {
|
|
case 1:
|
|
// 1.加载门店商品,删除商品.当分类下没有商品时.删除分类
|
|
//errs := LoadingStoreSkuList(ctx, toApi, toStore.VendorStoreID)
|
|
// errs := LoadingStoreSkuList(ctx, toApi, toStore.VendorStoreID)
|
|
//if errs != nil && len(errs) > 0 {
|
|
// return nil, errs[0]
|
|
//}
|
|
case 2:
|
|
//同步分类
|
|
fromCategoryList, err := api.ShopCategoryGet(utils.Int2Str(fromStore.ID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, v := range fromCategoryList {
|
|
parentID, categoryErr := api.ShopCategoryCreate(utils.Int2Str(toStore.ID), 0, v.Name, v.Rank)
|
|
if categoryErr != nil {
|
|
globals.SugarLogger.Debugf("err := RetailCatUpdate : %v", categoryErr)
|
|
continue
|
|
}
|
|
VendorCategoryIDMap[v.CategoryID] = parentID
|
|
if v.Children != nil && len(v.Children) != 0 {
|
|
for _, c := range v.Children {
|
|
childrenCateId, err := api.ShopCategoryCreate(utils.Int2Str(toStore.ID), parentID, c.Name, c.Rank)
|
|
if err != nil {
|
|
globals.SugarLogger.Debugf("err := RetailCatUpdate Children : %v", err)
|
|
continue
|
|
}
|
|
VendorCategoryIDMap[c.CategoryID] = childrenCateId
|
|
}
|
|
}
|
|
}
|
|
case 3:
|
|
i := 1
|
|
for {
|
|
// 同步商品
|
|
fromFoodList, err1 := api.SkuList(utils.Int2Str(fromStore.ID), &ebaiapi.SkuListParams{
|
|
Page: i,
|
|
})
|
|
if len(fromFoodList.List) == 0 || fromFoodList == nil {
|
|
return nil, fmt.Errorf("fromFoodList 为空 %s ,i:= %d", utils.Format4Output(err1, false), i)
|
|
}
|
|
if err = BatchInitSkuEBai2EBai(ctx, fromFoodList.List, api, utils.Int2Str(toStore.ID), VendorCategoryIDMap); err != nil {
|
|
globals.SugarLogger.Debugf("BatchInitData : %s", utils.Format4Output(err, false))
|
|
}
|
|
i = i + 1
|
|
}
|
|
|
|
}
|
|
return
|
|
}
|
|
task := tasksch.NewParallelTask(taskName, config, ctx, work, []int{1, 2, 3})
|
|
tasksch.HandleTask(task, nil, true).Run()
|
|
if !isAsync {
|
|
_, err = task.GetResult(0)
|
|
hint = "1"
|
|
} else {
|
|
hint = task.ID
|
|
}
|
|
return hint, err
|
|
}
|
|
|
|
// BatchInitSkuEBai2EBai 批量创建商品
|
|
func BatchInitSkuEBai2EBai(ctx *jxcontext.Context, fromSku []*ebaiapi.SkuInfo, toApi *ebaiapi.API, storeID string, VendorCategoryIDMap map[int64]int64) error {
|
|
for _, storeSku := range fromSku {
|
|
params := map[string]interface{}{
|
|
"left_num": model.MaxStoreSkuStockQty,
|
|
"weight": storeSku.Weight,
|
|
"photos": storeSku.Photos,
|
|
"preparation_time": storeSku.PreparationTime,
|
|
}
|
|
params["upc"] = storeSku.Upc
|
|
params["name"] = storeSku.Name
|
|
params["cat3_id"] = storeSku.CateId
|
|
params["category_id"] = VendorCategoryIDMap[utils.Str2Int64(storeSku.CustomCatList[0].CustomCatID)]
|
|
params["rtf"] = storeSku.Rtf
|
|
params["left_num"] = storeSku.LeftNum
|
|
params["process_type"] = storeSku.ProcessType
|
|
params["process_detail"] = storeSku.ProcessDetail
|
|
params["sale_price"] = storeSku.SalePrice
|
|
params["status"] = storeSku.Status
|
|
params["minimum"] = storeSku.Minimum
|
|
params["seven_days_no_reason"] = storeSku.SevenDaysNoReason
|
|
if len(storeSku.SkuProperty) != model.NO {
|
|
params["sku_property"] = storeSku.SkuProperty
|
|
}
|
|
customSkuID := storeSku.CustomSkuID
|
|
if customSkuID == "" {
|
|
customSkuID = utils.Int64ToStr(storeSku.SkuId)
|
|
}
|
|
_, err := toApi.SkuCreate(ctx.GetTrackInfo(), storeID, utils.Str2Int64(storeSku.CustomSkuID), params)
|
|
if err != nil {
|
|
globals.SugarLogger.Debugf("错误商品id: %d,商品名称:%s, %v", storeSku.SkuId, storeSku.Name, err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// CopyMtToMT 美团商品复制到美团
|
|
func CopyMtToMT(ctx *jxcontext.Context, fromStore, toStore *dao.StoreDetail, isAsync bool, offSet int) (hint string, err error) {
|
|
fromAPI := partner.CurAPIManager.GetAPI(model.VendorIDMTWM, fromStore.VendorOrgCode).(*mtwmapi.API)
|
|
if fromStore.VendorOrgCode == "5873" {
|
|
globals.SugarLogger.Debugf("=====token1 := %s", utils.Format4Output(fromStore, false))
|
|
fromAPI.SetToken(fromStore.MtwmToken)
|
|
}
|
|
|
|
toAPI := partner.CurAPIManager.GetAPI(model.VendorIDMTWM, toStore.VendorOrgCode).(*mtwmapi.API)
|
|
if toStore.VendorOrgCode == "5873" {
|
|
globals.SugarLogger.Debugf("=====token1 := %s", utils.Format4Output(toStore, false))
|
|
toAPI.SetToken(toStore.MtwmToken)
|
|
}
|
|
|
|
taskName := fmt.Sprintf("将美团平台门店[%s],分类和商品复制到[%s]", fromStore.VendorStoreID, toStore.VendorStoreID)
|
|
config := tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(false)
|
|
work := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
step := batchItemList[0].(int)
|
|
switch step {
|
|
case 1:
|
|
// 1.加载门店商品,删除商品.当分类下没有商品时.删除分类
|
|
//errs := LoadingStoreSkuList(ctx, toApi, toStore.VendorStoreID)
|
|
// errs := LoadingStoreSkuList(ctx, toApi, toStore.VendorStoreID)
|
|
//if errs != nil && len(errs) > 0 {
|
|
// return nil, errs[0]
|
|
//}
|
|
case 2:
|
|
// 同步分类
|
|
fromCategoryList, err := fromAPI.RetailCatList(fromStore.VendorStoreID)
|
|
globals.SugarLogger.Debugf("==fromCategoryList= : %d", len(fromCategoryList))
|
|
globals.SugarLogger.Debugf("==fromCategoryList= : %v", err)
|
|
for _, v := range fromCategoryList {
|
|
categoryErr := toAPI.RetailCatUpdate(toStore.VendorStoreID, v.Name, &mtwmapi.Param4UpdateCat{
|
|
CategoryCode: v.Code,
|
|
Sequence: v.Sequence,
|
|
})
|
|
if categoryErr != nil {
|
|
globals.SugarLogger.Debugf("err := RetailCatUpdate : %v", categoryErr)
|
|
}
|
|
if v.Children != nil && len(v.Children) != 0 {
|
|
for _, c := range v.Children {
|
|
if err3 := toAPI.RetailCatUpdate(toStore.VendorStoreID, v.Name, &mtwmapi.Param4UpdateCat{
|
|
CategoryNameOrigin: v.Name,
|
|
SecondaryCategoryCode: c.Code,
|
|
SecondaryCategoryName: c.Name,
|
|
Sequence: c.Sequence,
|
|
}); err3 != nil {
|
|
globals.SugarLogger.Debugf("err := RetailCatUpdate Children : %v", err3)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
case 3:
|
|
i := offSet
|
|
for {
|
|
// 同步商品
|
|
fromFoodList, err1 := fromAPI.RetailListAll(fromStore.VendorStoreID, i)
|
|
if len(fromFoodList) == 0 || fromFoodList == nil {
|
|
return nil, fmt.Errorf("fromFoodList 为空 %s ,i:= %d", utils.Format4Output(err1, false), i)
|
|
}
|
|
|
|
if err = BatchInitSkuMT2MT(ctx, fromFoodList, toAPI, toStore.VendorStoreID, i); err != nil {
|
|
globals.SugarLogger.Debugf("BatchInitData : %s", utils.Format4Output(err, false))
|
|
}
|
|
if len(fromFoodList) < 100 {
|
|
break
|
|
}
|
|
i++
|
|
}
|
|
|
|
}
|
|
return
|
|
}
|
|
task := tasksch.NewParallelTask(taskName, config, ctx, work, []int{1, 2, 3})
|
|
tasksch.HandleTask(task, nil, true).Run()
|
|
if !isAsync {
|
|
_, err = task.GetResult(0)
|
|
hint = "1"
|
|
} else {
|
|
hint = task.ID
|
|
}
|
|
return hint, err
|
|
}
|
|
|
|
// BatchInitSkuMT2MT 批量创建商品
|
|
func BatchInitSkuMT2MT(ctx *jxcontext.Context, fromSku []*mtwmapi.AppFood, toApi *mtwmapi.API, vendorStoreID string, i int) error {
|
|
foodDataList := make([]map[string]interface{}, len(fromSku))
|
|
for i, storeSku := range fromSku {
|
|
foodData := make(map[string]interface{})
|
|
if storeSku.AppFoodCode != "" {
|
|
foodData[mtwmapi.KeyAppFoodCode] = storeSku.AppFoodCode
|
|
} else {
|
|
foodData[mtwmapi.KeyAppFoodCode] = time.Now().UnixNano() + int64(i*3)
|
|
}
|
|
|
|
skus := make([]interface{}, 0)
|
|
for _, v := range storeSku.SkuList {
|
|
mapSkuList := utils.Struct2MapByJson(v)
|
|
if v.BoxNum == "" {
|
|
delete(mapSkuList, "box_num")
|
|
}
|
|
if v.BoxPrice == "" {
|
|
delete(mapSkuList, "box_price")
|
|
}
|
|
skus = append(skus, mapSkuList)
|
|
}
|
|
foodData["skus"] = skus
|
|
foodData["name"] = utils.LimitUTF8StringLen(storeSku.Name, mtwmapi.MaxSkuNameCharCount)
|
|
foodData["description"] = storeSku.Description
|
|
foodData["price"] = storeSku.Price
|
|
foodData["min_order_count"] = storeSku.MinOrderCount
|
|
foodData["unit"] = storeSku.Unit
|
|
//todo 增加商品必填属性
|
|
foodData["common_attr_value"] = storeSku.CommonAttrValue
|
|
if storeSku.SecondaryCategoryCode != "" {
|
|
foodData["category_code"] = storeSku.SecondaryCategoryCode
|
|
} else {
|
|
foodData["category_name"] = storeSku.SecondaryCategoryName
|
|
}
|
|
|
|
if storeSku.SecondaryCategoryName == "" && storeSku.SecondaryCategoryCode == "" {
|
|
if storeSku.SecondaryCategoryCode != "" {
|
|
foodData["category_code"] = storeSku.CategoryCode
|
|
} else {
|
|
foodData["category_name"] = storeSku.CategoryName
|
|
}
|
|
}
|
|
|
|
foodData["is_sold_out"] = storeSku.IsSoldOut
|
|
foodData["picture"] = storeSku.Picture
|
|
foodData["picture_contents"] = storeSku.PictureContents
|
|
foodData["sequence"] = storeSku.Sequence
|
|
foodData["tag_id"] = storeSku.TagID
|
|
|
|
foodDataList[i] = foodData
|
|
}
|
|
|
|
count := len(foodDataList) / 10
|
|
if len(foodDataList)%10 != 0 {
|
|
count += 1
|
|
}
|
|
for i := 0; i < count; i++ {
|
|
if i == count-1 {
|
|
failedFoodList, _ := toApi.RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList[i*10:])
|
|
if len(failedFoodList) != 0 {
|
|
globals.SugarLogger.Debugf("RetailBatchInitData2 failedFoodList :%s", utils.Format4Output(failedFoodList, false))
|
|
}
|
|
} else {
|
|
failedFoodList, _ := toApi.RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList[i*10:(i+1)*10])
|
|
if len(failedFoodList) != 0 {
|
|
globals.SugarLogger.Debugf("RetailBatchInitData4 failedFoodList :%s", utils.Format4Output(failedFoodList, false))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
}
|