341 lines
12 KiB
Go
341 lines
12 KiB
Go
package cms
|
|
|
|
import (
|
|
"encoding/json"
|
|
"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"
|
|
beego "github.com/astaxie/beego/server/web"
|
|
"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) {
|
|
var fromApi *mtwmapi.API
|
|
var toApi *mtwmapi.API
|
|
|
|
if fromStore.VendorOrgCode == globals.Mtwm2Code {
|
|
fromApi = mtwmapi.New(beego.AppConfig.DefaultString("mtwmAppID2", ""), beego.AppConfig.DefaultString("mtwmSecret2", ""), beego.AppConfig.DefaultString("mtwmCallbackURL2", ""), "")
|
|
fromApi.SetToken(fromStore.MtwmToken)
|
|
} else {
|
|
fromApi = partner.CurAPIManager.GetAPI(model.VendorIDMTWM, fromStore.VendorOrgCode).(*mtwmapi.API)
|
|
}
|
|
|
|
if toStore.VendorOrgCode == globals.Mtwm2Code {
|
|
toApi = mtwmapi.New(beego.AppConfig.DefaultString("mtwmAppID2", ""), beego.AppConfig.DefaultString("mtwmSecret2", ""), beego.AppConfig.DefaultString("mtwmCallbackURL2", ""), "")
|
|
toApi.SetToken(toStore.MtwmToken)
|
|
} else {
|
|
toApi = partner.CurAPIManager.GetAPI(model.VendorIDMTWM, toStore.VendorOrgCode).(*mtwmapi.API)
|
|
}
|
|
|
|
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:
|
|
|
|
// 同步分类
|
|
//fromCategoryList, err := fromApi.RetailCatList(fromStore.VendorStoreID)
|
|
//if len(fromCategoryList) == model.NO {
|
|
// return nil, err
|
|
//}
|
|
//toCategoryList, err := toApi.RetailCatList(toStore.VendorStoreID)
|
|
//if len(toCategoryList) == model.NO {
|
|
// return nil, err
|
|
//}
|
|
//if len(fromCategoryList) != len(toCategoryList) {
|
|
// 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 2:
|
|
i := offSet
|
|
errList := make([]*mtwmapi.AppFoodResult, 0, 0)
|
|
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)
|
|
}
|
|
|
|
errDataList, err := BatchInitSkuMT2MT(ctx, fromFoodList, toApi, toStore.VendorStoreID, i)
|
|
if err != nil {
|
|
globals.SugarLogger.Debugf("BatchInitData : %v", err)
|
|
}
|
|
if len(errDataList) > model.NO {
|
|
errList = append(errList, errDataList...)
|
|
}
|
|
globals.SugarLogger.Debugf("==============i: %d len: %d", i, len(fromFoodList))
|
|
if len(fromFoodList) < 100 {
|
|
break
|
|
}
|
|
i++
|
|
}
|
|
globals.SugarLogger.Debugf("errListData := %s", utils.Format4Output(errList, false))
|
|
}
|
|
return
|
|
}
|
|
task := tasksch.NewParallelTask(taskName, config, ctx, work, []int{1, 2})
|
|
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, offset int) (errList []*mtwmapi.AppFoodResult, err error) {
|
|
foodDataList := make([]map[string]interface{}, len(fromSku))
|
|
for k, storeSku := range fromSku {
|
|
foodData := make(map[string]interface{})
|
|
if storeSku.AppFoodCode != "" {
|
|
foodData[mtwmapi.KeyAppFoodCode] = storeSku.AppFoodCode
|
|
} else {
|
|
foodData[mtwmapi.KeyAppFoodCode] = time.Now().UnixNano() + int64(k*3)
|
|
}
|
|
|
|
skus := make([]interface{}, 0)
|
|
upc := ""
|
|
for _, v := range storeSku.SkuList {
|
|
if v.SkuId == "" {
|
|
v.SkuId = storeSku.AppFoodCode
|
|
}
|
|
salesCycle, _ := json.Marshal(v.AvailableTimes)
|
|
mapSkuList := utils.Struct2MapByJson(v)
|
|
if v.BoxNum == "" {
|
|
delete(mapSkuList, "box_num")
|
|
}
|
|
if v.BoxPrice == "" {
|
|
delete(mapSkuList, "box_price")
|
|
}
|
|
if v.Upc != "" {
|
|
upc = v.Upc
|
|
}
|
|
mapSkuList["available_times"] = string(salesCycle)
|
|
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
|
|
foodData["upc"] = storeSku.UpcCode
|
|
if foodData["upc"].(string) == "" {
|
|
foodData["upc"] = upc
|
|
}
|
|
if foodData["upc"].(string) == "" {
|
|
foodData["upc"] = "no_upc"
|
|
}
|
|
|
|
foodDataList[k] = 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 {
|
|
errList = append(errList, failedFoodList...)
|
|
}
|
|
} else {
|
|
failedFoodList, _ := toApi.RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList[i*10:(i+1)*10])
|
|
if len(failedFoodList) != 0 {
|
|
errList = append(errList, failedFoodList...)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return errList, nil
|
|
}
|