159 lines
5.1 KiB
Go
159 lines
5.1 KiB
Go
package cms
|
|
|
|
import (
|
|
"fmt"
|
|
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
|
"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/business/partner/purchase/mtwm"
|
|
"git.rosy.net.cn/jx-callback/business/partner/putils"
|
|
"time"
|
|
)
|
|
|
|
// CopyOnStoreSkuToOther 将一个美团门店分类和商品复制到另一个门店
|
|
func CopyOnStoreSkuToOther(ctx *jxcontext.Context, fromVendorStoreId, toStoreId int, isAsync bool) (hint string, err error) {
|
|
var (
|
|
db = dao.GetDB()
|
|
)
|
|
|
|
// 门店api加载
|
|
// fromStore, err := dao.GetStoreDetail(db, fromStoreId, model.VendorIDMTWM, "")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
toStore, err := dao.GetStoreDetail(db, toStoreId, model.VendorIDMTWM, "")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
//fromApi := partner.CurAPIManager.GetAPI(model.VendorIDMTWM, toStore.VendorOrgCode).(*mtwmapi.API)
|
|
toApi := partner.CurAPIManager.GetAPI(model.VendorIDMTWM, toStore.VendorOrgCode).(*mtwmapi.API)
|
|
|
|
taskName := fmt.Sprintf("将平台门店[%d],分类和商品复制到[%d]", fromVendorStoreId, toStoreId)
|
|
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)
|
|
if errs != nil && len(errs) > 0 {
|
|
return nil, errs[0]
|
|
}
|
|
case 2:
|
|
// 同步分类
|
|
fromCategoryList, err := toApi.RetailCatList(utils.Int2Str(fromVendorStoreId))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for _, v := range fromCategoryList {
|
|
if err := toApi.RetailCatUpdate(toStore.VendorStoreID, v.Name, &mtwmapi.Param4UpdateCat{
|
|
CategoryCode: v.Code,
|
|
Sequence: v.Sequence,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
if v.Children != nil && len(v.Children) != 0 {
|
|
for _, c := range v.Children {
|
|
if err := toApi.RetailCatUpdate(toStore.VendorStoreID, v.Name, &mtwmapi.Param4UpdateCat{
|
|
CategoryCode: v.Code,
|
|
SecondaryCategoryCode: c.Code,
|
|
SecondaryCategoryName: c.Name,
|
|
Sequence: c.Sequence,
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
case 3:
|
|
// 同步商品
|
|
fromFoodList, err1 := toApi.RetailListAll(utils.Int2Str(fromVendorStoreId))
|
|
if err1 != nil {
|
|
return nil, err1
|
|
}
|
|
if err := BatchInitData(ctx, fromFoodList, toApi, toStore.VendorStoreID); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
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
|
|
}
|
|
|
|
func LoadingStoreSkuList(ctx *jxcontext.Context, api *mtwmapi.API, poiCode string) (err []error) {
|
|
foodList, err1 := api.RetailListAll(poiCode)
|
|
if err1 != nil {
|
|
return append(err, err1)
|
|
}
|
|
|
|
i := 0
|
|
for _, v := range foodList {
|
|
err1 := api.RetailDelete(ctx.GetTrackInfo(), poiCode, v.AppFoodCode)
|
|
if err1 != nil {
|
|
err = append(err, err1)
|
|
}
|
|
if i%40 == 0 {
|
|
time.Sleep(200 * time.Millisecond)
|
|
}
|
|
i++
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// BatchInitData 批量创建商品
|
|
func BatchInitData(ctx *jxcontext.Context, fromSku []*mtwmapi.AppFood, toApi *mtwmapi.API, vendorStoreID string) error {
|
|
foodDataList := make([]map[string]interface{}, len(fromSku))
|
|
for i, storeSku := range fromSku {
|
|
foodData := make(map[string]interface{})
|
|
foodDataList[i] = foodData
|
|
foodData[mtwmapi.KeyAppFoodCode] = storeSku.AppFoodCode
|
|
foodData["skus"] = storeSku.Skus
|
|
foodData["name"] = utils.LimitUTF8StringLen(storeSku.Name, mtwmapi.MaxSkuNameCharCount)
|
|
foodData["description"] = storeSku.Description
|
|
foodData["price"] = storeSku.Price
|
|
if storeSku.MinOrderCount != 0 {
|
|
foodData["min_order_count"] = storeSku.MinOrderCount
|
|
} else {
|
|
foodData["min_order_count"] = 1
|
|
}
|
|
foodData["unit"] = storeSku.Unit
|
|
attr := mtwm.SwitchAttr(int64(storeSku.TagID))
|
|
if attr != "" {
|
|
foodData["common_attr_value"] = attr
|
|
}
|
|
foodData["category_code"] = storeSku.CategoryCode
|
|
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
|
|
}
|
|
|
|
failedFoodList, err2 := toApi.RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList)
|
|
if err := err2; err == nil {
|
|
if err = putils.GenPartialFailedErr(failedFoodList, len(failedFoodList)); err != nil {
|
|
return err
|
|
}
|
|
} else if err2 != nil && len(failedFoodList) == 0 {
|
|
if errExt, ok := err2.(*utils.ErrorWithCode); ok {
|
|
return utils.UnmarshalUseNumber([]byte(errExt.ErrMsg()), &failedFoodList)
|
|
}
|
|
}
|
|
return nil
|
|
}
|