- 将老平台同步逻辑注释掉
This commit is contained in:
@@ -446,22 +446,22 @@ func (v *VendorSync) SyncStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, vendo
|
|||||||
return v.SyncStoresSkus2(ctx, db, vendorIDs, storeIDs, skuIDs, setSyncStatus, isAsync, isContinueWhenError)
|
return v.SyncStoresSkus2(ctx, db, vendorIDs, storeIDs, skuIDs, setSyncStatus, isAsync, isContinueWhenError)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isUseOldSyncLogic(storeMap *model.StoreMap) bool {
|
// func isUseOldSyncLogic(storeMap *model.StoreMap) bool {
|
||||||
return false
|
// return false
|
||||||
return globals.IsProductEnv() && storeMap.StoreID != 102652 // 绿城四季鲜店
|
// return globals.IsProductEnv() && storeMap.StoreID != 102652 // 绿城四季鲜店
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (v *VendorSync) proxySyncStoreSku(ctx *jxcontext.Context, parentTask tasksch.ITask, storeMap *model.StoreMap, nameIDs, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
func (v *VendorSync) proxySyncStoreSku(ctx *jxcontext.Context, parentTask tasksch.ITask, storeMap *model.StoreMap, nameIDs, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
if isUseOldSyncLogic(storeMap) {
|
// if isUseOldSyncLogic(storeMap) {
|
||||||
return v.GetStoreHandler(storeMap.VendorID).SyncStoreSkus(ctx, parentTask, storeMap.StoreID, skuIDs, isAsync, isContinueWhenError)
|
// return v.GetStoreHandler(storeMap.VendorID).SyncStoreSkus(ctx, parentTask, storeMap.StoreID, skuIDs, isAsync, isContinueWhenError)
|
||||||
}
|
// }
|
||||||
return SyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, nil, skuIDs, isAsync, isContinueWhenError)
|
return SyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, nil, skuIDs, isAsync, isContinueWhenError)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *VendorSync) proxyFullSyncStoreSku(ctx *jxcontext.Context, parentTask tasksch.ITask, storeMap *model.StoreMap, isAsync, isContinueWhenError bool) (hint string, err error) {
|
func (v *VendorSync) proxyFullSyncStoreSku(ctx *jxcontext.Context, parentTask tasksch.ITask, storeMap *model.StoreMap, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
if isUseOldSyncLogic(storeMap) {
|
// if isUseOldSyncLogic(storeMap) {
|
||||||
return v.GetStoreHandler(storeMap.VendorID).FullSyncStoreSkus(ctx, parentTask, storeMap.StoreID, isAsync, isContinueWhenError)
|
// return v.GetStoreHandler(storeMap.VendorID).FullSyncStoreSkus(ctx, parentTask, storeMap.StoreID, isAsync, isContinueWhenError)
|
||||||
}
|
// }
|
||||||
return FullSyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, isAsync, isContinueWhenError)
|
return FullSyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, isAsync, isContinueWhenError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -143,9 +143,9 @@ type IPurchasePlatformHandler interface {
|
|||||||
// OpenStore(vendorStoreID string, userName string) error
|
// OpenStore(vendorStoreID string, userName string) error
|
||||||
// CloseStore(vendorStoreID, closeNotice, userName string) error
|
// CloseStore(vendorStoreID, closeNotice, userName string) error
|
||||||
|
|
||||||
SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error)
|
// SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error)
|
||||||
// !!!注意,此操作会先清除门店已有的商品,一般用于初始化,小心使用
|
// // !!!注意,此操作会先清除门店已有的商品,一般用于初始化,小心使用
|
||||||
FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error)
|
// FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error)
|
||||||
|
|
||||||
RefreshAllStoresID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool) (hint string, err error)
|
RefreshAllStoresID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool) (hint string, err error)
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -9,14 +9,14 @@ func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask t
|
|||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
return hint, err
|
// return hint, err
|
||||||
}
|
// }
|
||||||
|
|
||||||
func (p *PurchaseHandler) RefreshStoresAllSkusID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool, storeIDs []int) (hint string, err error) {
|
func (p *PurchaseHandler) RefreshStoresAllSkusID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool, storeIDs []int) (hint string, err error) {
|
||||||
return hint, err
|
return hint, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
return hint, err
|
// return hint, err
|
||||||
}
|
// }
|
||||||
|
|||||||
@@ -1,206 +1,191 @@
|
|||||||
package jd
|
package jd
|
||||||
|
|
||||||
import (
|
// // 京东到家,以有库存表示关注(认领)
|
||||||
"fmt"
|
// func (p *PurchaseHandler) syncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, db *dao.DaoDB, storeID int, storeSkus []*dao.StoreSkuSyncInfo, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
|
// globals.SugarLogger.Debugf("jd syncStoreSkus, storeID:%d", storeID)
|
||||||
|
// storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDJD)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// batchSize := jdapi.MaxStoreSkuBatchSize
|
||||||
|
// // storeSkusLen := len(storeSkus)
|
||||||
|
// // if storeSkusLen < jdapi.MaxStoreSkuBatchSize/2 {
|
||||||
|
// // batchSize = 1
|
||||||
|
// // } else if storeSkusLen < jdapi.MaxStoreSkuBatchSize {
|
||||||
|
// // batchSize = (storeSkusLen + 1) / 2
|
||||||
|
// // } else if storeSkusLen < jdapi.MaxStoreSkuBatchSize*2 {
|
||||||
|
// // batchSize = (storeSkusLen + 2) / 3
|
||||||
|
// // }
|
||||||
|
// task := tasksch.NewParallelTask("syncStoreSkus京东", tasksch.NewParallelConfig().SetBatchSize(batchSize).SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||||
|
// func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
// doWork := func(batchItemList []interface{}) (isPartialFailed bool, err error) {
|
||||||
|
// var skuPriceInfoList []*jdapi.SkuPriceInfo
|
||||||
|
// var skuVendibilityList []*jdapi.StockVendibility
|
||||||
|
// var skuStockList []*jdapi.SkuStock
|
||||||
|
// vendorSkuID4Price := ""
|
||||||
|
// vendorSkuID4Qty := ""
|
||||||
|
// stationNo := storeDetail.VendorStoreID
|
||||||
|
// var batchBindIDs []int
|
||||||
|
// for _, v := range batchItemList {
|
||||||
|
// storeSku := v.(*dao.StoreSkuSyncInfo)
|
||||||
|
// alreadyAddStock := false
|
||||||
|
// if storeSku.StoreSkuSyncStatus&model.SyncFlagChangedMask != 0 || storeSku.BindID == 0 || storeSku.NameID == 0 {
|
||||||
|
// if storeSku.BindID > 0 {
|
||||||
|
// batchBindIDs = append(batchBindIDs, storeSku.BindID)
|
||||||
|
// }
|
||||||
|
// if storeSku.StoreSkuSyncStatus&(model.SyncFlagDeletedMask|model.SyncFlagNewMask) != 0 || storeSku.BindID == 0 || storeSku.NameID == 0 { // 关注或取消关注
|
||||||
|
// stock := &jdapi.SkuStock{
|
||||||
|
// OutSkuId: utils.Int2Str(storeSku.SkuID),
|
||||||
|
// StockQty: model.MaxStoreSkuStockQty,
|
||||||
|
// }
|
||||||
|
// if storeSku.StoreSkuSyncStatus&model.SyncFlagDeletedMask != 0 || storeSku.DeletedAt != utils.DefaultTimeValue || storeSku.BindID == 0 || storeSku.NameID == 0 {
|
||||||
|
// stock.StockQty = 0
|
||||||
|
// } else {
|
||||||
|
// alreadyAddStock = true
|
||||||
|
// }
|
||||||
|
// if stock.StockQty != 0 || !storeskulock.IsJdStoreSkuLocked(stationNo, storeSku.JdID) {
|
||||||
|
// vendorSkuID4Qty = storeSku.VendorSkuID
|
||||||
|
// skuStockList = append(skuStockList, stock)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if storeSku.StoreSkuSyncStatus&(model.SyncFlagPriceMask|model.SyncFlagNewMask) != 0 {
|
||||||
|
// vendorSkuID4Price = storeSku.VendorSkuID
|
||||||
|
// pricePercentage := jxutils.GetPricePercentage(storeDetail.PricePercentagePackObj, int(storeSku.Price), int(storeDetail.PricePercentage))
|
||||||
|
// skuPriceInfoList = append(skuPriceInfoList, &jdapi.SkuPriceInfo{
|
||||||
|
// OutSkuId: utils.Int2Str(storeSku.SkuID),
|
||||||
|
// Price: constrainPrice(jxutils.CaculateSkuVendorPrice(int(storeSku.Price), pricePercentage, storeSku.CatPricePercentage)),
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
// if storeSku.StoreSkuSyncStatus&(model.SyncFlagSaleMask|model.SyncFlagNewMask) != 0 {
|
||||||
|
// vendibility := &jdapi.StockVendibility{
|
||||||
|
// OutSkuId: utils.Int2Str(storeSku.SkuID),
|
||||||
|
// DoSale: true,
|
||||||
|
// }
|
||||||
|
// if storeSku.StoreSkuStatus != model.StoreSkuBindStatusNormal {
|
||||||
|
// vendibility.DoSale = false
|
||||||
|
// } else if !alreadyAddStock { // 如果是设置可售则自动将库存加满
|
||||||
|
// stock := &jdapi.SkuStock{
|
||||||
|
// OutSkuId: utils.Int2Str(storeSku.SkuID),
|
||||||
|
// StockQty: model.MaxStoreSkuStockQty,
|
||||||
|
// }
|
||||||
|
// vendorSkuID4Qty = storeSku.VendorSkuID
|
||||||
|
// skuStockList = append(skuStockList, stock)
|
||||||
|
// }
|
||||||
|
// if vendibility.DoSale || !storeskulock.IsJdStoreSkuLocked(stationNo, storeSku.JdID) {
|
||||||
|
// skuVendibilityList = append(skuVendibilityList, vendibility)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// syncMask := 0
|
||||||
|
// errList := []error{}
|
||||||
|
// if globals.EnableJdStoreWrite {
|
||||||
|
// // todo 以下可以优化为并行操作
|
||||||
|
// // globals.SugarLogger.Debug(utils.Format4Output(skuVendibilityList, false), utils.Format4Output(skuPriceInfoList, false), utils.Format4Output(skuStockList, false))
|
||||||
|
// if len(skuVendibilityList) > 0 {
|
||||||
|
// if _, err = api.JdAPI.BatchUpdateVendibility(ctx.GetTrackInfo(), "", stationNo, skuVendibilityList, ctx.GetUserName()); err == nil {
|
||||||
|
// syncMask |= model.SyncFlagSaleMask
|
||||||
|
// } else {
|
||||||
|
// if !isPartialFailed {
|
||||||
|
// isPartialFailed = isErrPartialFailed(err)
|
||||||
|
// }
|
||||||
|
// errList = append(errList, err)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (err == nil || isContinueWhenError) && len(skuStockList) > 0 {
|
||||||
|
// if len(skuStockList) == 1 {
|
||||||
|
// err = api.JdAPI.UpdateCurrentQty(ctx.GetTrackInfo(), stationNo, utils.Str2Int64WithDefault(vendorSkuID4Qty, 0), skuStockList[0].StockQty)
|
||||||
|
// } else {
|
||||||
|
// _, err = api.JdAPI.BatchUpdateCurrentQtys(ctx.GetTrackInfo(), "", stationNo, skuStockList, ctx.GetUserName())
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// syncMask |= model.SyncFlagNewMask | model.SyncFlagDeletedMask
|
||||||
|
// } else {
|
||||||
|
// if !isPartialFailed {
|
||||||
|
// isPartialFailed = isErrPartialFailed(err)
|
||||||
|
// }
|
||||||
|
// errList = append(errList, err)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if (err == nil || isContinueWhenError) && len(skuPriceInfoList) > 0 {
|
||||||
|
// if len(skuPriceInfoList) == 1 {
|
||||||
|
// _, err = api.JdAPI.UpdateStationPrice(ctx.GetTrackInfo(), utils.Str2Int64WithDefault(vendorSkuID4Price, 0), stationNo, skuPriceInfoList[0].Price)
|
||||||
|
// } else {
|
||||||
|
// _, err = api.JdAPI.UpdateVendorStationPrice(ctx.GetTrackInfo(), "", stationNo, skuPriceInfoList)
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// syncMask |= model.SyncFlagPriceMask
|
||||||
|
// } else {
|
||||||
|
// if !isPartialFailed {
|
||||||
|
// isPartialFailed = isErrPartialFailed(err)
|
||||||
|
// }
|
||||||
|
// // errList = append(errList, partner.NewErrorCode(err.Error(), partner.ErrCodeChangePriceFailed, model.VendorIDJD))
|
||||||
|
// errList = append(errList, err)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if len(errList) == 0 {
|
||||||
|
// syncMask = -1
|
||||||
|
// }
|
||||||
|
// if syncMask != 0 && len(batchBindIDs) > 0 {
|
||||||
|
// // db := dao.GetDB() // 多线程问题
|
||||||
|
// sql := `
|
||||||
|
// UPDATE store_sku_bind t1
|
||||||
|
// SET t1.jd_sync_status = t1.jd_sync_status & ?
|
||||||
|
// WHERE t1.id IN (` + dao.GenQuestionMarks(len(batchBindIDs)) + ")"
|
||||||
|
// if _, err = dao.ExecuteSQL(db, sql, ^syncMask, batchBindIDs); err != nil {
|
||||||
|
// errList = append(errList, err)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if len(errList) == 1 {
|
||||||
|
// err = errList[0]
|
||||||
|
// } else if len(errList) > 1 {
|
||||||
|
// err = fmt.Errorf("%v", errList)
|
||||||
|
// }
|
||||||
|
// return isPartialFailed, err
|
||||||
|
// }
|
||||||
|
// isErrPartialFailed, err := doWork(batchItemList)
|
||||||
|
// if isErrPartialFailed && len(batchItemList) > 1 {
|
||||||
|
// for _, v := range batchItemList {
|
||||||
|
// doWork([]interface{}{v})
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return nil, err
|
||||||
|
// }, storeSkus)
|
||||||
|
// tasksch.HandleTask(task, parentTask, false).Run()
|
||||||
|
// if !isAsync {
|
||||||
|
// _, err = task.GetResult(0)
|
||||||
|
// }
|
||||||
|
// return task.ID, err
|
||||||
|
// }
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/platformapi/jdapi"
|
// func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
// globals.SugarLogger.Debugf("jd SyncStoresSkus, storeID:%d, skuIDs:%v", storeID, skuIDs)
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
// db := dao.GetDB()
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
// storeSkus, err := dao.GetStoreSkus(db, model.VendorIDJD, storeID, skuIDs)
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils/storeskulock"
|
// if err != nil {
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
// return "", err
|
||||||
"git.rosy.net.cn/jx-callback/business/model"
|
// }
|
||||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
// return p.syncStoreSkus(ctx, parentTask, db, storeID, storeSkus, isAsync, isContinueWhenError)
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
// }
|
||||||
"git.rosy.net.cn/jx-callback/globals/api"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 京东到家,以有库存表示关注(认领)
|
// func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
func (p *PurchaseHandler) syncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, db *dao.DaoDB, storeID int, storeSkus []*dao.StoreSkuSyncInfo, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// globals.SugarLogger.Debugf("jd FullSyncStoreSkus, storeID:%d", storeID)
|
||||||
globals.SugarLogger.Debugf("jd syncStoreSkus, storeID:%d", storeID)
|
// db := dao.GetDB()
|
||||||
storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDJD)
|
// _, err = dao.SetStoreSkuSyncStatus(db, model.VendorIDJD, []int{storeID}, nil, model.SyncFlagStoreSkuOnlyMask)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
return "", err
|
// return "", err
|
||||||
}
|
// }
|
||||||
batchSize := jdapi.MaxStoreSkuBatchSize
|
// storeSkus, err := dao.GetFullStoreSkus(db, model.VendorIDJD, storeID)
|
||||||
// storeSkusLen := len(storeSkus)
|
// if err != nil {
|
||||||
// if storeSkusLen < jdapi.MaxStoreSkuBatchSize/2 {
|
// return "", err
|
||||||
// batchSize = 1
|
// }
|
||||||
// } else if storeSkusLen < jdapi.MaxStoreSkuBatchSize {
|
// return p.syncStoreSkus(ctx, parentTask, db, storeID, storeSkus, isAsync, isContinueWhenError)
|
||||||
// batchSize = (storeSkusLen + 1) / 2
|
// }
|
||||||
// } else if storeSkusLen < jdapi.MaxStoreSkuBatchSize*2 {
|
|
||||||
// batchSize = (storeSkusLen + 2) / 3
|
|
||||||
// }
|
|
||||||
task := tasksch.NewParallelTask("syncStoreSkus京东", tasksch.NewParallelConfig().SetBatchSize(batchSize).SetIsContinueWhenError(isContinueWhenError), ctx,
|
|
||||||
func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
doWork := func(batchItemList []interface{}) (isPartialFailed bool, err error) {
|
|
||||||
var skuPriceInfoList []*jdapi.SkuPriceInfo
|
|
||||||
var skuVendibilityList []*jdapi.StockVendibility
|
|
||||||
var skuStockList []*jdapi.SkuStock
|
|
||||||
vendorSkuID4Price := ""
|
|
||||||
vendorSkuID4Qty := ""
|
|
||||||
stationNo := storeDetail.VendorStoreID
|
|
||||||
var batchBindIDs []int
|
|
||||||
for _, v := range batchItemList {
|
|
||||||
storeSku := v.(*dao.StoreSkuSyncInfo)
|
|
||||||
alreadyAddStock := false
|
|
||||||
if storeSku.StoreSkuSyncStatus&model.SyncFlagChangedMask != 0 || storeSku.BindID == 0 || storeSku.NameID == 0 {
|
|
||||||
if storeSku.BindID > 0 {
|
|
||||||
batchBindIDs = append(batchBindIDs, storeSku.BindID)
|
|
||||||
}
|
|
||||||
if storeSku.StoreSkuSyncStatus&(model.SyncFlagDeletedMask|model.SyncFlagNewMask) != 0 || storeSku.BindID == 0 || storeSku.NameID == 0 { // 关注或取消关注
|
|
||||||
stock := &jdapi.SkuStock{
|
|
||||||
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
||||||
StockQty: model.MaxStoreSkuStockQty,
|
|
||||||
}
|
|
||||||
if storeSku.StoreSkuSyncStatus&model.SyncFlagDeletedMask != 0 || storeSku.DeletedAt != utils.DefaultTimeValue || storeSku.BindID == 0 || storeSku.NameID == 0 {
|
|
||||||
stock.StockQty = 0
|
|
||||||
} else {
|
|
||||||
alreadyAddStock = true
|
|
||||||
}
|
|
||||||
if stock.StockQty != 0 || !storeskulock.IsJdStoreSkuLocked(stationNo, storeSku.JdID) {
|
|
||||||
vendorSkuID4Qty = storeSku.VendorSkuID
|
|
||||||
skuStockList = append(skuStockList, stock)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if storeSku.StoreSkuSyncStatus&(model.SyncFlagPriceMask|model.SyncFlagNewMask) != 0 {
|
|
||||||
vendorSkuID4Price = storeSku.VendorSkuID
|
|
||||||
pricePercentage := jxutils.GetPricePercentage(storeDetail.PricePercentagePackObj, int(storeSku.Price), int(storeDetail.PricePercentage))
|
|
||||||
skuPriceInfoList = append(skuPriceInfoList, &jdapi.SkuPriceInfo{
|
|
||||||
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
||||||
Price: constrainPrice(jxutils.CaculateSkuVendorPrice(int(storeSku.Price), pricePercentage, storeSku.CatPricePercentage)),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
if storeSku.StoreSkuSyncStatus&(model.SyncFlagSaleMask|model.SyncFlagNewMask) != 0 {
|
|
||||||
vendibility := &jdapi.StockVendibility{
|
|
||||||
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
||||||
DoSale: true,
|
|
||||||
}
|
|
||||||
if storeSku.StoreSkuStatus != model.StoreSkuBindStatusNormal {
|
|
||||||
vendibility.DoSale = false
|
|
||||||
} else if !alreadyAddStock { // 如果是设置可售则自动将库存加满
|
|
||||||
stock := &jdapi.SkuStock{
|
|
||||||
OutSkuId: utils.Int2Str(storeSku.SkuID),
|
|
||||||
StockQty: model.MaxStoreSkuStockQty,
|
|
||||||
}
|
|
||||||
vendorSkuID4Qty = storeSku.VendorSkuID
|
|
||||||
skuStockList = append(skuStockList, stock)
|
|
||||||
}
|
|
||||||
if vendibility.DoSale || !storeskulock.IsJdStoreSkuLocked(stationNo, storeSku.JdID) {
|
|
||||||
skuVendibilityList = append(skuVendibilityList, vendibility)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
syncMask := 0
|
|
||||||
errList := []error{}
|
|
||||||
if globals.EnableJdStoreWrite {
|
|
||||||
// todo 以下可以优化为并行操作
|
|
||||||
// globals.SugarLogger.Debug(utils.Format4Output(skuVendibilityList, false), utils.Format4Output(skuPriceInfoList, false), utils.Format4Output(skuStockList, false))
|
|
||||||
if len(skuVendibilityList) > 0 {
|
|
||||||
if _, err = api.JdAPI.BatchUpdateVendibility(ctx.GetTrackInfo(), "", stationNo, skuVendibilityList, ctx.GetUserName()); err == nil {
|
|
||||||
syncMask |= model.SyncFlagSaleMask
|
|
||||||
} else {
|
|
||||||
if !isPartialFailed {
|
|
||||||
isPartialFailed = isErrPartialFailed(err)
|
|
||||||
}
|
|
||||||
errList = append(errList, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (err == nil || isContinueWhenError) && len(skuStockList) > 0 {
|
|
||||||
if len(skuStockList) == 1 {
|
|
||||||
err = api.JdAPI.UpdateCurrentQty(ctx.GetTrackInfo(), stationNo, utils.Str2Int64WithDefault(vendorSkuID4Qty, 0), skuStockList[0].StockQty)
|
|
||||||
} else {
|
|
||||||
_, err = api.JdAPI.BatchUpdateCurrentQtys(ctx.GetTrackInfo(), "", stationNo, skuStockList, ctx.GetUserName())
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
syncMask |= model.SyncFlagNewMask | model.SyncFlagDeletedMask
|
|
||||||
} else {
|
|
||||||
if !isPartialFailed {
|
|
||||||
isPartialFailed = isErrPartialFailed(err)
|
|
||||||
}
|
|
||||||
errList = append(errList, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (err == nil || isContinueWhenError) && len(skuPriceInfoList) > 0 {
|
|
||||||
if len(skuPriceInfoList) == 1 {
|
|
||||||
_, err = api.JdAPI.UpdateStationPrice(ctx.GetTrackInfo(), utils.Str2Int64WithDefault(vendorSkuID4Price, 0), stationNo, skuPriceInfoList[0].Price)
|
|
||||||
} else {
|
|
||||||
_, err = api.JdAPI.UpdateVendorStationPrice(ctx.GetTrackInfo(), "", stationNo, skuPriceInfoList)
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
syncMask |= model.SyncFlagPriceMask
|
|
||||||
} else {
|
|
||||||
if !isPartialFailed {
|
|
||||||
isPartialFailed = isErrPartialFailed(err)
|
|
||||||
}
|
|
||||||
// errList = append(errList, partner.NewErrorCode(err.Error(), partner.ErrCodeChangePriceFailed, model.VendorIDJD))
|
|
||||||
errList = append(errList, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(errList) == 0 {
|
|
||||||
syncMask = -1
|
|
||||||
}
|
|
||||||
if syncMask != 0 && len(batchBindIDs) > 0 {
|
|
||||||
// db := dao.GetDB() // 多线程问题
|
|
||||||
sql := `
|
|
||||||
UPDATE store_sku_bind t1
|
|
||||||
SET t1.jd_sync_status = t1.jd_sync_status & ?
|
|
||||||
WHERE t1.id IN (` + dao.GenQuestionMarks(len(batchBindIDs)) + ")"
|
|
||||||
if _, err = dao.ExecuteSQL(db, sql, ^syncMask, batchBindIDs); err != nil {
|
|
||||||
errList = append(errList, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(errList) == 1 {
|
|
||||||
err = errList[0]
|
|
||||||
} else if len(errList) > 1 {
|
|
||||||
err = fmt.Errorf("%v", errList)
|
|
||||||
}
|
|
||||||
return isPartialFailed, err
|
|
||||||
}
|
|
||||||
isErrPartialFailed, err := doWork(batchItemList)
|
|
||||||
if isErrPartialFailed && len(batchItemList) > 1 {
|
|
||||||
for _, v := range batchItemList {
|
|
||||||
doWork([]interface{}{v})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, storeSkus)
|
|
||||||
tasksch.HandleTask(task, parentTask, false).Run()
|
|
||||||
if !isAsync {
|
|
||||||
_, err = task.GetResult(0)
|
|
||||||
}
|
|
||||||
return task.ID, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// func constrainPrice(price int) int {
|
||||||
globals.SugarLogger.Debugf("jd SyncStoresSkus, storeID:%d, skuIDs:%v", storeID, skuIDs)
|
// if price <= 0 {
|
||||||
db := dao.GetDB()
|
// price = 1
|
||||||
storeSkus, err := dao.GetStoreSkus(db, model.VendorIDJD, storeID, skuIDs)
|
// }
|
||||||
if err != nil {
|
// return price
|
||||||
return "", err
|
// }
|
||||||
}
|
|
||||||
return p.syncStoreSkus(ctx, parentTask, db, storeID, storeSkus, isAsync, isContinueWhenError)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
||||||
globals.SugarLogger.Debugf("jd FullSyncStoreSkus, storeID:%d", storeID)
|
|
||||||
db := dao.GetDB()
|
|
||||||
_, err = dao.SetStoreSkuSyncStatus(db, model.VendorIDJD, []int{storeID}, nil, model.SyncFlagStoreSkuOnlyMask)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
storeSkus, err := dao.GetFullStoreSkus(db, model.VendorIDJD, storeID)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return p.syncStoreSkus(ctx, parentTask, db, storeID, storeSkus, isAsync, isContinueWhenError)
|
|
||||||
}
|
|
||||||
|
|
||||||
func constrainPrice(price int) int {
|
|
||||||
if price <= 0 {
|
|
||||||
price = 1
|
|
||||||
}
|
|
||||||
return price
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,459 +1,442 @@
|
|||||||
package mtwm
|
package mtwm
|
||||||
|
|
||||||
import (
|
// // hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
"errors"
|
// func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync bool) (hint string, err error) {
|
||||||
"fmt"
|
// globals.SugarLogger.Debugf("mtwm SyncStoreCategory storeID:%d, userName:%s", storeID, ctx.GetUserName())
|
||||||
"strings"
|
// num := 0
|
||||||
"time"
|
// db := dao.GetDB()
|
||||||
|
// storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
||||||
|
// if err != nil {
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
// vendorStoreID := storeDetail.VendorStoreID
|
||||||
|
// rootTask := tasksch.NewSeqTask("美团外卖SyncStoreCategory step1", ctx,
|
||||||
|
// func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||||
|
// level := step + 1
|
||||||
|
// catList, err := dao.GetStoreCategories(db, model.VendorIDMTWM, storeID, level)
|
||||||
|
// if len(catList) > 0 {
|
||||||
|
// num += len(catList)
|
||||||
|
// task := tasksch.NewParallelTask(fmt.Sprintf("美团外卖SyncStoreCategory step2, level=%d", level), nil, ctx,
|
||||||
|
// func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
// updateFields := []string{dao.GetSyncStatusStructField(model.VendorNames[model.VendorIDMTWM])}
|
||||||
|
// catInfo := batchItemList[0].(*dao.SkuStoreCatInfo)
|
||||||
|
// storeCatMap := &model.StoreSkuCategoryMap{}
|
||||||
|
// storeCatMap.ID = catInfo.MapID
|
||||||
|
// if catInfo.StoreCatSyncStatus&model.SyncFlagDeletedMask != 0 { // 删除
|
||||||
|
// if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 && !dao.IsVendorThingIDEmpty(catInfo.VendorCatID) {
|
||||||
|
// globals.SugarLogger.Debugf("RetailCatDelete vendorStoreID:%s, MtwmID:%s", vendorStoreID, catInfo.VendorCatID)
|
||||||
|
// if globals.EnableMtwmStoreWrite {
|
||||||
|
// if err = api.MtwmAPI.RetailCatDelete(vendorStoreID, catInfo.VendorCatID); mtwmapi.IsErrCategoryNotExist(err) {
|
||||||
|
// err = nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else if catInfo.StoreCatSyncStatus&(model.SyncFlagNewMask|model.SyncFlagStoreSkuModifiedMask) != 0 { // 新增或修改
|
||||||
|
// catName := catInfo.Name
|
||||||
|
// subCatName := ""
|
||||||
|
// originName := ""
|
||||||
|
// if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 {
|
||||||
|
// originName = catInfo.VendorCatID
|
||||||
|
// }
|
||||||
|
// if level == 2 {
|
||||||
|
// originName = catInfo.ParentCatName
|
||||||
|
// catName = catInfo.ParentCatName
|
||||||
|
// subCatName = catInfo.Name
|
||||||
|
// if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 {
|
||||||
|
// originName = catInfo.VendorCatID
|
||||||
|
// catName = catInfo.Name
|
||||||
|
// subCatName = ""
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if catName == "" {
|
||||||
|
// panic("catName is empty")
|
||||||
|
// }
|
||||||
|
// globals.SugarLogger.Debugf("RetailCatUpdate vendorStoreID:%s, originName:%s, catName:%s, subCatName:%s, seq:%d", vendorStoreID, originName, catName, subCatName, catInfo.Seq)
|
||||||
|
// if globals.EnableMtwmStoreWrite {
|
||||||
|
// if err = api.MtwmAPI.RetailCatUpdate(vendorStoreID, originName, catName, subCatName, catInfo.Seq); catInfo.StoreCatSyncStatus&model.SyncFlagNewMask != 0 && mtwmapi.IsErrCategoryExist(err) {
|
||||||
|
// err = nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// storeCatMap.MtwmID = catInfo.Name
|
||||||
|
// updateFields = append(updateFields, dao.GetVendorThingIDStructField(model.VendorNames[model.VendorIDMTWM]))
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// db2 := dao.GetDB()
|
||||||
|
// storeCatMap.MtwmSyncStatus = 0
|
||||||
|
// _, err = dao.UpdateEntity(db2, storeCatMap, updateFields...)
|
||||||
|
// }
|
||||||
|
// return nil, err
|
||||||
|
// }, catList)
|
||||||
|
// rootTask.AddChild(task).Run()
|
||||||
|
// _, err = task.GetResult(0)
|
||||||
|
// }
|
||||||
|
// return nil, err
|
||||||
|
// }, 2)
|
||||||
|
// tasksch.AddChild(parentTask, rootTask).Run()
|
||||||
|
// if !isAsync {
|
||||||
|
// hint = utils.Int2Str(num)
|
||||||
|
// _, err = rootTask.GetResult(0)
|
||||||
|
// } else {
|
||||||
|
// hint = rootTask.ID
|
||||||
|
// }
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
|
// // 此函数根据门店商品信息重建分类信息
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
// // 远程有,本地无, --> 删除远程
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
// // 远程有,本地有,映射无, --> 添加关联
|
||||||
"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"
|
// // hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
// func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, isCheckRemote bool, skuIDs []int) (hint string, err error) {
|
||||||
"git.rosy.net.cn/jx-callback/globals/api"
|
// globals.SugarLogger.Debugf("mtwm SyncLocalStoreCategory storeID:%d, userName:%s", storeID, ctx.GetUserName())
|
||||||
)
|
// if db == nil {
|
||||||
|
// db = dao.GetDB()
|
||||||
|
// }
|
||||||
|
// catMap := make([]map[string]*dao.SkuStoreCatInfo, 2)
|
||||||
|
// for i := 0; i < 2; i++ {
|
||||||
|
// catMap[i] = make(map[string]*dao.SkuStoreCatInfo)
|
||||||
|
// localCats, err := dao.GetSkusCategories(db, model.VendorIDMTWM, storeID, skuIDs, i+1)
|
||||||
|
// // globals.SugarLogger.Debug(utils.Format4Output(localCats, false))
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// for _, cat := range localCats {
|
||||||
|
// catMap[i][cat.ParentCatName+"/"+cat.Name] = cat
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// identityCatMap := make(map[int]int) // 这里面表示远程有,本地有,且完全相同,可擦掉本地的修改标记
|
||||||
|
// if isCheckRemote {
|
||||||
|
// storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
||||||
|
// if err != nil {
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
// vendorStoreID := storeDetail.VendorStoreID
|
||||||
|
// remoteCats, err := api.MtwmAPI.RetailCatList(vendorStoreID)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// if err = TranverseRemoteCatList("", remoteCats, func(level int, parentCatName, catName string) (err error) {
|
||||||
|
// localCat := catMap[level-1][parentCatName+"/"+catName]
|
||||||
|
// // globals.SugarLogger.Debug(parentCatName, " ", catName, " ", localCat)
|
||||||
|
// if localCat == nil { // 本地分类就没有这个名字,直接删除
|
||||||
|
// if globals.EnableMtwmStoreWrite {
|
||||||
|
// globals.SugarLogger.Debugf("RetailCatDelete2 vendorStoreID:%s, catName:%s", vendorStoreID, catName)
|
||||||
|
// if err = api.MtwmAPI.RetailCatDelete(vendorStoreID, catName); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else { // 本地分类有这个名字
|
||||||
|
// if localCat.MapID == 0 { // 本地映射没有
|
||||||
|
// localCat.MapID = -1 // 表示远程有同名的
|
||||||
|
// } else { // 本地映射有
|
||||||
|
// identityCatMap[localCat.MapID] = 1
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// return nil
|
||||||
|
// }); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// dao.Begin(db)
|
||||||
|
// defer func() {
|
||||||
|
// dao.Rollback(db)
|
||||||
|
// }()
|
||||||
|
// num := 0
|
||||||
|
// for i := 0; i < 2; i++ {
|
||||||
|
// for _, v := range catMap[i] {
|
||||||
|
// if v.MapID == -1 || v.MapID == 0 { // 本地缺失
|
||||||
|
// mtwmSyncStatus := int8(model.SyncFlagNewMask)
|
||||||
|
// if v.MapID == -1 { // 远程有同名的,只是简单增加一条本地记录关联
|
||||||
|
// mtwmSyncStatus = 0
|
||||||
|
// }
|
||||||
|
// catMap := &model.StoreSkuCategoryMap{
|
||||||
|
// StoreID: storeID,
|
||||||
|
// CategoryID: v.ID,
|
||||||
|
// MtwmID: v.Name,
|
||||||
|
// MtwmSyncStatus: mtwmSyncStatus,
|
||||||
|
// EbaiSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// ElmSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// WscSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// }
|
||||||
|
// num++
|
||||||
|
// dao.WrapAddIDCULDEntity(catMap, ctx.GetUserName())
|
||||||
|
// if err = dao.CreateEntity(db, catMap); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// if dao.IsVendorThingIDEmpty(v.VendorCatID) {
|
||||||
|
// num++
|
||||||
|
// }
|
||||||
|
// if isCheckRemote {
|
||||||
|
// catMap := &model.StoreSkuCategoryMap{
|
||||||
|
// MtwmSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// }
|
||||||
|
// updateFields := []string{
|
||||||
|
// model.FieldUpdatedAt,
|
||||||
|
// model.FieldLastOperator,
|
||||||
|
// model.FieldMtwmSyncStatus,
|
||||||
|
// }
|
||||||
|
// if identityCatMap[v.MapID] == 1 { // 如果一样,则要刷新ID(对于MTWM其实就是名字)
|
||||||
|
// catMap.MtwmID = v.Name
|
||||||
|
// catMap.MtwmSyncStatus = 0
|
||||||
|
// updateFields = append(updateFields, model.FieldMtwmID)
|
||||||
|
// }
|
||||||
|
// catMap.ID = v.MapID
|
||||||
|
// num++
|
||||||
|
// dao.WrapUpdateULEntity(catMap, ctx.GetUserName())
|
||||||
|
// if _, err = dao.UpdateEntity(db, catMap, updateFields...); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// dao.Commit(db)
|
||||||
|
// return utils.Int2Str(num), err
|
||||||
|
// }
|
||||||
|
|
||||||
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
// func TranverseRemoteCatList(parentCatName string, remoteCats []*mtwmapi.RetailCategoryInfo, handler func(level int, parentCatName, catName string) error) (err error) {
|
||||||
func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync bool) (hint string, err error) {
|
// for _, remoteCat := range remoteCats {
|
||||||
globals.SugarLogger.Debugf("mtwm SyncStoreCategory storeID:%d, userName:%s", storeID, ctx.GetUserName())
|
// name := utils.Interface2String(remoteCat.Name)
|
||||||
num := 0
|
// TranverseRemoteCatList(name, remoteCat.Children, handler)
|
||||||
db := dao.GetDB()
|
// if err = handler(remoteCat.Level, parentCatName, name); err != nil {
|
||||||
storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
// return err
|
||||||
if err != nil {
|
// }
|
||||||
return hint, err
|
// }
|
||||||
}
|
// return nil
|
||||||
vendorStoreID := storeDetail.VendorStoreID
|
// }
|
||||||
rootTask := tasksch.NewSeqTask("美团外卖SyncStoreCategory step1", ctx,
|
|
||||||
func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
|
||||||
level := step + 1
|
|
||||||
catList, err := dao.GetStoreCategories(db, model.VendorIDMTWM, storeID, level)
|
|
||||||
if len(catList) > 0 {
|
|
||||||
num += len(catList)
|
|
||||||
task := tasksch.NewParallelTask(fmt.Sprintf("美团外卖SyncStoreCategory step2, level=%d", level), nil, ctx,
|
|
||||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
updateFields := []string{dao.GetSyncStatusStructField(model.VendorNames[model.VendorIDMTWM])}
|
|
||||||
catInfo := batchItemList[0].(*dao.SkuStoreCatInfo)
|
|
||||||
storeCatMap := &model.StoreSkuCategoryMap{}
|
|
||||||
storeCatMap.ID = catInfo.MapID
|
|
||||||
if catInfo.StoreCatSyncStatus&model.SyncFlagDeletedMask != 0 { // 删除
|
|
||||||
if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 && !dao.IsVendorThingIDEmpty(catInfo.VendorCatID) {
|
|
||||||
globals.SugarLogger.Debugf("RetailCatDelete vendorStoreID:%s, MtwmID:%s", vendorStoreID, catInfo.VendorCatID)
|
|
||||||
if globals.EnableMtwmStoreWrite {
|
|
||||||
if err = api.MtwmAPI.RetailCatDelete(vendorStoreID, catInfo.VendorCatID); mtwmapi.IsErrCategoryNotExist(err) {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if catInfo.StoreCatSyncStatus&(model.SyncFlagNewMask|model.SyncFlagStoreSkuModifiedMask) != 0 { // 新增或修改
|
|
||||||
catName := catInfo.Name
|
|
||||||
subCatName := ""
|
|
||||||
originName := ""
|
|
||||||
if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 {
|
|
||||||
originName = catInfo.VendorCatID
|
|
||||||
}
|
|
||||||
if level == 2 {
|
|
||||||
originName = catInfo.ParentCatName
|
|
||||||
catName = catInfo.ParentCatName
|
|
||||||
subCatName = catInfo.Name
|
|
||||||
if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 {
|
|
||||||
originName = catInfo.VendorCatID
|
|
||||||
catName = catInfo.Name
|
|
||||||
subCatName = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if catName == "" {
|
|
||||||
panic("catName is empty")
|
|
||||||
}
|
|
||||||
globals.SugarLogger.Debugf("RetailCatUpdate vendorStoreID:%s, originName:%s, catName:%s, subCatName:%s, seq:%d", vendorStoreID, originName, catName, subCatName, catInfo.Seq)
|
|
||||||
if globals.EnableMtwmStoreWrite {
|
|
||||||
if err = api.MtwmAPI.RetailCatUpdate(vendorStoreID, originName, catName, subCatName, catInfo.Seq); catInfo.StoreCatSyncStatus&model.SyncFlagNewMask != 0 && mtwmapi.IsErrCategoryExist(err) {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
storeCatMap.MtwmID = catInfo.Name
|
|
||||||
updateFields = append(updateFields, dao.GetVendorThingIDStructField(model.VendorNames[model.VendorIDMTWM]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
db2 := dao.GetDB()
|
|
||||||
storeCatMap.MtwmSyncStatus = 0
|
|
||||||
_, err = dao.UpdateEntity(db2, storeCatMap, updateFields...)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, catList)
|
|
||||||
rootTask.AddChild(task).Run()
|
|
||||||
_, err = task.GetResult(0)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, 2)
|
|
||||||
tasksch.AddChild(parentTask, rootTask).Run()
|
|
||||||
if !isAsync {
|
|
||||||
hint = utils.Int2Str(num)
|
|
||||||
_, err = rootTask.GetResult(0)
|
|
||||||
} else {
|
|
||||||
hint = rootTask.ID
|
|
||||||
}
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// 此函数根据门店商品信息重建分类信息
|
// func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
// 远程有,本地无, --> 删除远程
|
// db := dao.GetDB()
|
||||||
// 远程有,本地有,映射无, --> 添加关联
|
// storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
||||||
// 远程有,本地有,映射有, --> 不处理
|
// if err != nil {
|
||||||
// 远程无,本地有,映射无, --> 添加本地
|
// return "", err
|
||||||
// 远程无,本地有,映射有, --> 同步标记改为新增
|
// }
|
||||||
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
// return p.syncStoreSkus(ctx, parentTask, storeDetail, skuIDs, isAsync, isContinueWhenError)
|
||||||
func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, isCheckRemote bool, skuIDs []int) (hint string, err error) {
|
// }
|
||||||
globals.SugarLogger.Debugf("mtwm SyncLocalStoreCategory storeID:%d, userName:%s", storeID, ctx.GetUserName())
|
|
||||||
if db == nil {
|
|
||||||
db = dao.GetDB()
|
|
||||||
}
|
|
||||||
catMap := make([]map[string]*dao.SkuStoreCatInfo, 2)
|
|
||||||
for i := 0; i < 2; i++ {
|
|
||||||
catMap[i] = make(map[string]*dao.SkuStoreCatInfo)
|
|
||||||
localCats, err := dao.GetSkusCategories(db, model.VendorIDMTWM, storeID, skuIDs, i+1)
|
|
||||||
// globals.SugarLogger.Debug(utils.Format4Output(localCats, false))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, cat := range localCats {
|
|
||||||
catMap[i][cat.ParentCatName+"/"+cat.Name] = cat
|
|
||||||
}
|
|
||||||
}
|
|
||||||
identityCatMap := make(map[int]int) // 这里面表示远程有,本地有,且完全相同,可擦掉本地的修改标记
|
|
||||||
if isCheckRemote {
|
|
||||||
storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
|
||||||
if err != nil {
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
vendorStoreID := storeDetail.VendorStoreID
|
|
||||||
remoteCats, err := api.MtwmAPI.RetailCatList(vendorStoreID)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if err = TranverseRemoteCatList("", remoteCats, func(level int, parentCatName, catName string) (err error) {
|
|
||||||
localCat := catMap[level-1][parentCatName+"/"+catName]
|
|
||||||
// globals.SugarLogger.Debug(parentCatName, " ", catName, " ", localCat)
|
|
||||||
if localCat == nil { // 本地分类就没有这个名字,直接删除
|
|
||||||
if globals.EnableMtwmStoreWrite {
|
|
||||||
globals.SugarLogger.Debugf("RetailCatDelete2 vendorStoreID:%s, catName:%s", vendorStoreID, catName)
|
|
||||||
if err = api.MtwmAPI.RetailCatDelete(vendorStoreID, catName); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else { // 本地分类有这个名字
|
|
||||||
if localCat.MapID == 0 { // 本地映射没有
|
|
||||||
localCat.MapID = -1 // 表示远程有同名的
|
|
||||||
} else { // 本地映射有
|
|
||||||
identityCatMap[localCat.MapID] = 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dao.Begin(db)
|
|
||||||
defer func() {
|
|
||||||
dao.Rollback(db)
|
|
||||||
}()
|
|
||||||
num := 0
|
|
||||||
for i := 0; i < 2; i++ {
|
|
||||||
for _, v := range catMap[i] {
|
|
||||||
if v.MapID == -1 || v.MapID == 0 { // 本地缺失
|
|
||||||
mtwmSyncStatus := int8(model.SyncFlagNewMask)
|
|
||||||
if v.MapID == -1 { // 远程有同名的,只是简单增加一条本地记录关联
|
|
||||||
mtwmSyncStatus = 0
|
|
||||||
}
|
|
||||||
catMap := &model.StoreSkuCategoryMap{
|
|
||||||
StoreID: storeID,
|
|
||||||
CategoryID: v.ID,
|
|
||||||
MtwmID: v.Name,
|
|
||||||
MtwmSyncStatus: mtwmSyncStatus,
|
|
||||||
EbaiSyncStatus: model.SyncFlagNewMask,
|
|
||||||
ElmSyncStatus: model.SyncFlagNewMask,
|
|
||||||
WscSyncStatus: model.SyncFlagNewMask,
|
|
||||||
}
|
|
||||||
num++
|
|
||||||
dao.WrapAddIDCULDEntity(catMap, ctx.GetUserName())
|
|
||||||
if err = dao.CreateEntity(db, catMap); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if dao.IsVendorThingIDEmpty(v.VendorCatID) {
|
|
||||||
num++
|
|
||||||
}
|
|
||||||
if isCheckRemote {
|
|
||||||
catMap := &model.StoreSkuCategoryMap{
|
|
||||||
MtwmSyncStatus: model.SyncFlagNewMask,
|
|
||||||
}
|
|
||||||
updateFields := []string{
|
|
||||||
model.FieldUpdatedAt,
|
|
||||||
model.FieldLastOperator,
|
|
||||||
model.FieldMtwmSyncStatus,
|
|
||||||
}
|
|
||||||
if identityCatMap[v.MapID] == 1 { // 如果一样,则要刷新ID(对于MTWM其实就是名字)
|
|
||||||
catMap.MtwmID = v.Name
|
|
||||||
catMap.MtwmSyncStatus = 0
|
|
||||||
updateFields = append(updateFields, model.FieldMtwmID)
|
|
||||||
}
|
|
||||||
catMap.ID = v.MapID
|
|
||||||
num++
|
|
||||||
dao.WrapUpdateULEntity(catMap, ctx.GetUserName())
|
|
||||||
if _, err = dao.UpdateEntity(db, catMap, updateFields...); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dao.Commit(db)
|
|
||||||
return utils.Int2Str(num), err
|
|
||||||
}
|
|
||||||
|
|
||||||
func TranverseRemoteCatList(parentCatName string, remoteCats []*mtwmapi.RetailCategoryInfo, handler func(level int, parentCatName, catName string) error) (err error) {
|
// // hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
for _, remoteCat := range remoteCats {
|
// func (p *PurchaseHandler) syncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeDetail *dao.StoreDetail, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
name := utils.Interface2String(remoteCat.Name)
|
// storeID := storeDetail.Store.ID
|
||||||
TranverseRemoteCatList(name, remoteCat.Children, handler)
|
// globals.SugarLogger.Debugf("mtwm SyncStoreSkus storeID:%d, skuIDs:%v, isContinueWhenError:%t, userName:%s", storeID, skuIDs, isContinueWhenError, ctx.GetUserName())
|
||||||
if err = handler(remoteCat.Level, parentCatName, name); err != nil {
|
// db := dao.GetDB()
|
||||||
return err
|
// for i := 0; i < 3; i++ { // 最多重试三次
|
||||||
}
|
// if hint, err = p.SyncLocalStoreCategory(ctx, db, storeID, false, skuIDs); err != nil {
|
||||||
}
|
// return "", err
|
||||||
return nil
|
// }
|
||||||
}
|
// if hint != "0" {
|
||||||
|
// if hint, err = p.SyncStoreCategory(ctx, parentTask, storeID, false); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if hint == "0" {
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if hint != "0" {
|
||||||
|
// return "", errors.New("同步门店商品所需目录失败")
|
||||||
|
// }
|
||||||
|
// skus, err := dao.GetStoreSkus(db, model.VendorIDMTWM, storeID, skuIDs)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// // globals.SugarLogger.Debug(utils.Format4Output(skus, false))
|
||||||
|
// vendorStoreID := storeDetail.VendorStoreID
|
||||||
|
// rootTask := tasksch.NewParallelTask("美团外卖SyncStoreSkus", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||||
|
// func(rootTask *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
// foodDataList := make([]map[string]interface{}, len(batchItemList))
|
||||||
|
// // for k, v := range batchItemList {
|
||||||
|
// v := batchItemList[0]
|
||||||
|
// skuItem := v.(*dao.StoreSkuSyncInfo)
|
||||||
|
// updateFields := []string{model.FieldMtwmSyncStatus}
|
||||||
|
// storeSkuBind := &model.StoreSkuBind{}
|
||||||
|
// storeSkuBind.ID = skuItem.BindID
|
||||||
|
// if skuItem.NameID == 0 || skuItem.StoreSkuSyncStatus&model.SyncFlagDeletedMask != 0 {
|
||||||
|
// if skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask == 0 && !dao.IsVendorThingIDEmpty(skuItem.VendorSkuID) {
|
||||||
|
// if globals.EnableMtwmStoreWrite {
|
||||||
|
// if err = api.MtwmAPI.RetailDelete(ctx.GetTrackInfo(), vendorStoreID, skuItem.VendorSkuID); mtwmapi.IsErrSkuNotExist(err) {
|
||||||
|
// err = nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// if utils.IsTimeZero(storeSkuBind.DeletedAt) {
|
||||||
|
// storeSkuBind.DeletedAt = time.Now()
|
||||||
|
// updateFields = append(updateFields, model.FieldDeletedAt)
|
||||||
|
// }
|
||||||
|
// if !dao.IsVendorThingIDEmpty(skuItem.VendorSkuID) {
|
||||||
|
// storeSkuBind.MtwmID = 0
|
||||||
|
// updateFields = append(updateFields, model.FieldMtwmID)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else if skuItem.StoreSkuSyncStatus&(model.SyncFlagStoreSkuModifiedMask|model.SyncFlagNewMask) != 0 {
|
||||||
|
// foodData := make(map[string]interface{})
|
||||||
|
// foodDataList[0] = foodData
|
||||||
|
// foodData[mtwmapi.KeyAppFoodCode] = utils.Int2Str(skuItem.SkuID)
|
||||||
|
// skus := []map[string]interface{}{
|
||||||
|
// map[string]interface{}{
|
||||||
|
// "sku_id": foodData[mtwmapi.KeyAppFoodCode],
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
// foodData["skus"] = skus
|
||||||
|
// shouldCallSellStatus := !(skuItem.StoreSkuSyncStatus&(model.SyncFlagModifiedMask|model.SyncFlagNewMask|model.SyncFlagPriceMask) != 0)
|
||||||
|
// if !shouldCallSellStatus {
|
||||||
|
// globals.SugarLogger.Debugf("mtwm SyncStoreSkus3 skuID:%d, SkuSyncStatus:%d", skuItem.SkuID, skuItem.StoreSkuSyncStatus)
|
||||||
|
// mergeStoreSkuStatus := jxutils.MergeSkuStatus(skuItem.Status, skuItem.StoreSkuStatus)
|
||||||
|
// if !(skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask != 0 && mergeStoreSkuStatus != model.SkuStatusNormal) { // 待创建且不可售的,暂不新建
|
||||||
|
// if skuItem.Img == "" {
|
||||||
|
// err = fmt.Errorf("SKUNAME%d:%s没有图片,同步失败", skuItem.NameID, skuItem.Name)
|
||||||
|
// } else {
|
||||||
|
// pricePercentage := jxutils.GetPricePercentage(storeDetail.PricePercentagePackObj, int(skuItem.Price), int(storeDetail.PricePercentage))
|
||||||
|
// // globals.SugarLogger.Debugf("skuID:%d, price:%d, pricePercentage:%d", skuItem.SkuID, skuItem.Price, pricePercentage)
|
||||||
|
// // globals.SugarLogger.Debugf(utils.Format4Output(storeDetail.PricePercentagePackObj, false))
|
||||||
|
// foodData["name"] = jxutils.ComposeSkuName(skuItem.Prefix, skuItem.Name, skuItem.Comment, skuItem.Unit, skuItem.SpecQuality, skuItem.SpecUnit, mtwmapi.MaxSkuNameCharCount)
|
||||||
|
// foodData["description"] = skuItem.Comment
|
||||||
|
// foodData["price"] = jxutils.IntPrice2Standard(int64(jxutils.CaculateSkuVendorPrice(int(skuItem.Price), pricePercentage, skuItem.CatPricePercentage)))
|
||||||
|
// foodData["min_order_count"] = 1
|
||||||
|
// foodData["unit"] = skuItem.Unit
|
||||||
|
// foodData["box_num"] = 0
|
||||||
|
// foodData["box_price"] = 0.0
|
||||||
|
// foodData["category_name"] = skuItem.VendorCatID
|
||||||
|
// foodData["is_sold_out"] = skuStatusJX2Mtwm(mergeStoreSkuStatus)
|
||||||
|
// foodData["picture"] = skuItem.Img
|
||||||
|
// if skuItem.DescImg != "" {
|
||||||
|
// foodData["picture_contents"] = skuItem.DescImg
|
||||||
|
// }
|
||||||
|
// foodData["sequence"] = skuItem.Price
|
||||||
|
// if skuItem.VendorVendorCatID != 0 {
|
||||||
|
// foodData["tag_id"] = utils.Int64ToStr(skuItem.VendorVendorCatID)
|
||||||
|
// } else {
|
||||||
|
// // foodData["tag_id"] = utils.Int64ToStr(defVendorCatID)
|
||||||
|
// }
|
||||||
|
// skus[0]["spec"] = jxutils.ComposeSkuSpec(skuItem.SpecQuality, skuItem.SpecUnit)
|
||||||
|
// skus[0]["price"] = foodData["price"]
|
||||||
|
// skus[0]["stock"] = "*"
|
||||||
|
// skus[0]["upc"] = skuItem.Upc
|
||||||
|
// if foodData["tag_id"] != nil {
|
||||||
|
// skus[0]["weight"] = skuItem.Weight // weight字段仅限服饰鞋帽、美妆、日用品、母婴、生鲜果蔬、生活超市下的便利店/超市门店品类的商家使用
|
||||||
|
// }
|
||||||
|
// if globals.EnableMtwmStoreWrite {
|
||||||
|
// // err = api.MtwmAPI.RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList)
|
||||||
|
|
||||||
func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// foodData["skus"] = string(utils.MustMarshal(skus))
|
||||||
db := dao.GetDB()
|
// err = api.MtwmAPI.RetailInitData(ctx.GetTrackInfo(), vendorStoreID, utils.Int2Str(skuItem.SkuID), foodData)
|
||||||
storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
// }
|
||||||
if err != nil {
|
// if err == nil {
|
||||||
return "", err
|
// storeSkuBind.MtwmID = int64(skuItem.SkuID)
|
||||||
}
|
// updateFields = append(updateFields, model.FieldMtwmID)
|
||||||
return p.syncStoreSkus(ctx, parentTask, storeDetail, skuIDs, isAsync, isContinueWhenError)
|
// }
|
||||||
}
|
// }
|
||||||
|
// } else {
|
||||||
|
// // 暂不创建
|
||||||
|
// updateFields = nil
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err != nil {
|
||||||
|
// if isErrModifyPrice(err) {
|
||||||
|
// shouldCallSellStatus = true
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if shouldCallSellStatus {
|
||||||
|
// if skuItem.StoreSkuSyncStatus&(model.SyncFlagSaleMask) != 0 {
|
||||||
|
// globals.SugarLogger.Debugf("mtwm SyncStoreSkus4 skuID:%d, SkuSyncStatus:%d", skuItem.SkuID, skuItem.StoreSkuSyncStatus)
|
||||||
|
// sellStatus := skuStatusJX2Mtwm(jxutils.MergeSkuStatus(skuItem.Status, skuItem.StoreSkuStatus))
|
||||||
|
// if globals.EnableMtwmStoreWrite {
|
||||||
|
// if _, err2 := api.MtwmAPI.RetailSkuSellStatus(ctx.GetTrackInfo(), vendorStoreID, []*mtwmapi.BareStoreFoodInfo{
|
||||||
|
// &mtwmapi.BareStoreFoodInfo{
|
||||||
|
// AppFoodCode: utils.Int2Str(skuItem.SkuID),
|
||||||
|
// Skus: []*mtwmapi.BareStoreSkuInfo{
|
||||||
|
// &mtwmapi.BareStoreSkuInfo{
|
||||||
|
// SkuID: utils.Int2Str(skuItem.SkuID),
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// }, sellStatus); err2 != nil {
|
||||||
|
// err = err2
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// if len(updateFields) > 0 {
|
||||||
|
// _, err = dao.UpdateEntity(db, storeSkuBind, updateFields...)
|
||||||
|
// }
|
||||||
|
// } else if isErrModifyPrice(err) {
|
||||||
|
// // err = partner.NewErrorCode(err.Error(), partner.ErrCodeChangePriceFailed, model.VendorIDMTWM)
|
||||||
|
// }
|
||||||
|
// return nil, err
|
||||||
|
// }, skus)
|
||||||
|
// if parentTask != nil {
|
||||||
|
// parentTask.AddChild(rootTask)
|
||||||
|
// }
|
||||||
|
// rootTask.Run()
|
||||||
|
// if !isAsync {
|
||||||
|
// _, err = rootTask.GetResult(0)
|
||||||
|
// hint = utils.Int2Str(len(skus))
|
||||||
|
// } else {
|
||||||
|
// hint = rootTask.ID
|
||||||
|
// }
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
|
||||||
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
// func isErrModifyPrice(err error) bool {
|
||||||
func (p *PurchaseHandler) syncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeDetail *dao.StoreDetail, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// if errExt, ok := err.(*utils.ErrorWithCode); ok && errExt.IntCode() == 1 {
|
||||||
storeID := storeDetail.Store.ID
|
// for _, v := range []string{
|
||||||
globals.SugarLogger.Debugf("mtwm SyncStoreSkus storeID:%d, skuIDs:%v, isContinueWhenError:%t, userName:%s", storeID, skuIDs, isContinueWhenError, ctx.GetUserName())
|
// "折扣商品原价不允许修改",
|
||||||
db := dao.GetDB()
|
// } {
|
||||||
for i := 0; i < 3; i++ { // 最多重试三次
|
// if strings.Index(errExt.ErrMsg(), v) >= 0 {
|
||||||
if hint, err = p.SyncLocalStoreCategory(ctx, db, storeID, false, skuIDs); err != nil {
|
// return true
|
||||||
return "", err
|
// }
|
||||||
}
|
// }
|
||||||
if hint != "0" {
|
// }
|
||||||
if hint, err = p.SyncStoreCategory(ctx, parentTask, storeID, false); err != nil {
|
// return false
|
||||||
return "", err
|
// }
|
||||||
}
|
// func (p *PurchaseHandler) RefreshStoresAllSkusID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool, storeIDs []int) (hint string, err error) {
|
||||||
}
|
// return hint, err
|
||||||
if hint == "0" {
|
// }
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if hint != "0" {
|
|
||||||
return "", errors.New("同步门店商品所需目录失败")
|
|
||||||
}
|
|
||||||
skus, err := dao.GetStoreSkus(db, model.VendorIDMTWM, storeID, skuIDs)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
// globals.SugarLogger.Debug(utils.Format4Output(skus, false))
|
|
||||||
vendorStoreID := storeDetail.VendorStoreID
|
|
||||||
rootTask := tasksch.NewParallelTask("美团外卖SyncStoreSkus", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
|
||||||
func(rootTask *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
foodDataList := make([]map[string]interface{}, len(batchItemList))
|
|
||||||
// for k, v := range batchItemList {
|
|
||||||
v := batchItemList[0]
|
|
||||||
skuItem := v.(*dao.StoreSkuSyncInfo)
|
|
||||||
updateFields := []string{model.FieldMtwmSyncStatus}
|
|
||||||
storeSkuBind := &model.StoreSkuBind{}
|
|
||||||
storeSkuBind.ID = skuItem.BindID
|
|
||||||
if skuItem.NameID == 0 || skuItem.StoreSkuSyncStatus&model.SyncFlagDeletedMask != 0 {
|
|
||||||
if skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask == 0 && !dao.IsVendorThingIDEmpty(skuItem.VendorSkuID) {
|
|
||||||
if globals.EnableMtwmStoreWrite {
|
|
||||||
if err = api.MtwmAPI.RetailDelete(ctx.GetTrackInfo(), vendorStoreID, skuItem.VendorSkuID); mtwmapi.IsErrSkuNotExist(err) {
|
|
||||||
err = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
if utils.IsTimeZero(storeSkuBind.DeletedAt) {
|
|
||||||
storeSkuBind.DeletedAt = time.Now()
|
|
||||||
updateFields = append(updateFields, model.FieldDeletedAt)
|
|
||||||
}
|
|
||||||
if !dao.IsVendorThingIDEmpty(skuItem.VendorSkuID) {
|
|
||||||
storeSkuBind.MtwmID = 0
|
|
||||||
updateFields = append(updateFields, model.FieldMtwmID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if skuItem.StoreSkuSyncStatus&(model.SyncFlagStoreSkuModifiedMask|model.SyncFlagNewMask) != 0 {
|
|
||||||
foodData := make(map[string]interface{})
|
|
||||||
foodDataList[0] = foodData
|
|
||||||
foodData[mtwmapi.KeyAppFoodCode] = utils.Int2Str(skuItem.SkuID)
|
|
||||||
skus := []map[string]interface{}{
|
|
||||||
map[string]interface{}{
|
|
||||||
"sku_id": foodData[mtwmapi.KeyAppFoodCode],
|
|
||||||
},
|
|
||||||
}
|
|
||||||
foodData["skus"] = skus
|
|
||||||
shouldCallSellStatus := !(skuItem.StoreSkuSyncStatus&(model.SyncFlagModifiedMask|model.SyncFlagNewMask|model.SyncFlagPriceMask) != 0)
|
|
||||||
if !shouldCallSellStatus {
|
|
||||||
globals.SugarLogger.Debugf("mtwm SyncStoreSkus3 skuID:%d, SkuSyncStatus:%d", skuItem.SkuID, skuItem.StoreSkuSyncStatus)
|
|
||||||
mergeStoreSkuStatus := jxutils.MergeSkuStatus(skuItem.Status, skuItem.StoreSkuStatus)
|
|
||||||
if !(skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask != 0 && mergeStoreSkuStatus != model.SkuStatusNormal) { // 待创建且不可售的,暂不新建
|
|
||||||
if skuItem.Img == "" {
|
|
||||||
err = fmt.Errorf("SKUNAME%d:%s没有图片,同步失败", skuItem.NameID, skuItem.Name)
|
|
||||||
} else {
|
|
||||||
pricePercentage := jxutils.GetPricePercentage(storeDetail.PricePercentagePackObj, int(skuItem.Price), int(storeDetail.PricePercentage))
|
|
||||||
// globals.SugarLogger.Debugf("skuID:%d, price:%d, pricePercentage:%d", skuItem.SkuID, skuItem.Price, pricePercentage)
|
|
||||||
// globals.SugarLogger.Debugf(utils.Format4Output(storeDetail.PricePercentagePackObj, false))
|
|
||||||
foodData["name"] = jxutils.ComposeSkuName(skuItem.Prefix, skuItem.Name, skuItem.Comment, skuItem.Unit, skuItem.SpecQuality, skuItem.SpecUnit, mtwmapi.MaxSkuNameCharCount)
|
|
||||||
foodData["description"] = skuItem.Comment
|
|
||||||
foodData["price"] = jxutils.IntPrice2Standard(int64(jxutils.CaculateSkuVendorPrice(int(skuItem.Price), pricePercentage, skuItem.CatPricePercentage)))
|
|
||||||
foodData["min_order_count"] = 1
|
|
||||||
foodData["unit"] = skuItem.Unit
|
|
||||||
foodData["box_num"] = 0
|
|
||||||
foodData["box_price"] = 0.0
|
|
||||||
foodData["category_name"] = skuItem.VendorCatID
|
|
||||||
foodData["is_sold_out"] = skuStatusJX2Mtwm(mergeStoreSkuStatus)
|
|
||||||
foodData["picture"] = skuItem.Img
|
|
||||||
if skuItem.DescImg != "" {
|
|
||||||
foodData["picture_contents"] = skuItem.DescImg
|
|
||||||
}
|
|
||||||
foodData["sequence"] = skuItem.Price
|
|
||||||
if skuItem.VendorVendorCatID != 0 {
|
|
||||||
foodData["tag_id"] = utils.Int64ToStr(skuItem.VendorVendorCatID)
|
|
||||||
} else {
|
|
||||||
// foodData["tag_id"] = utils.Int64ToStr(defVendorCatID)
|
|
||||||
}
|
|
||||||
skus[0]["spec"] = jxutils.ComposeSkuSpec(skuItem.SpecQuality, skuItem.SpecUnit)
|
|
||||||
skus[0]["price"] = foodData["price"]
|
|
||||||
skus[0]["stock"] = "*"
|
|
||||||
skus[0]["upc"] = skuItem.Upc
|
|
||||||
if foodData["tag_id"] != nil {
|
|
||||||
skus[0]["weight"] = skuItem.Weight // weight字段仅限服饰鞋帽、美妆、日用品、母婴、生鲜果蔬、生活超市下的便利店/超市门店品类的商家使用
|
|
||||||
}
|
|
||||||
if globals.EnableMtwmStoreWrite {
|
|
||||||
// err = api.MtwmAPI.RetailBatchInitData(ctx.GetTrackInfo(), vendorStoreID, foodDataList)
|
|
||||||
|
|
||||||
foodData["skus"] = string(utils.MustMarshal(skus))
|
// func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
err = api.MtwmAPI.RetailInitData(ctx.GetTrackInfo(), vendorStoreID, utils.Int2Str(skuItem.SkuID), foodData)
|
// userName := ctx.GetUserName()
|
||||||
}
|
// globals.SugarLogger.Debugf("mtwm FullSyncStoreSkus storeID:%d, isContinueWhenError:%t, userName:%s", storeID, isContinueWhenError, userName)
|
||||||
if err == nil {
|
|
||||||
storeSkuBind.MtwmID = int64(skuItem.SkuID)
|
|
||||||
updateFields = append(updateFields, model.FieldMtwmID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 暂不创建
|
|
||||||
updateFields = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
if isErrModifyPrice(err) {
|
|
||||||
shouldCallSellStatus = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if shouldCallSellStatus {
|
|
||||||
if skuItem.StoreSkuSyncStatus&(model.SyncFlagSaleMask) != 0 {
|
|
||||||
globals.SugarLogger.Debugf("mtwm SyncStoreSkus4 skuID:%d, SkuSyncStatus:%d", skuItem.SkuID, skuItem.StoreSkuSyncStatus)
|
|
||||||
sellStatus := skuStatusJX2Mtwm(jxutils.MergeSkuStatus(skuItem.Status, skuItem.StoreSkuStatus))
|
|
||||||
if globals.EnableMtwmStoreWrite {
|
|
||||||
if _, err2 := api.MtwmAPI.RetailSkuSellStatus(ctx.GetTrackInfo(), vendorStoreID, []*mtwmapi.BareStoreFoodInfo{
|
|
||||||
&mtwmapi.BareStoreFoodInfo{
|
|
||||||
AppFoodCode: utils.Int2Str(skuItem.SkuID),
|
|
||||||
Skus: []*mtwmapi.BareStoreSkuInfo{
|
|
||||||
&mtwmapi.BareStoreSkuInfo{
|
|
||||||
SkuID: utils.Int2Str(skuItem.SkuID),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, sellStatus); err2 != nil {
|
|
||||||
err = err2
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
if len(updateFields) > 0 {
|
|
||||||
_, err = dao.UpdateEntity(db, storeSkuBind, updateFields...)
|
|
||||||
}
|
|
||||||
} else if isErrModifyPrice(err) {
|
|
||||||
// err = partner.NewErrorCode(err.Error(), partner.ErrCodeChangePriceFailed, model.VendorIDMTWM)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, skus)
|
|
||||||
if parentTask != nil {
|
|
||||||
parentTask.AddChild(rootTask)
|
|
||||||
}
|
|
||||||
rootTask.Run()
|
|
||||||
if !isAsync {
|
|
||||||
_, err = rootTask.GetResult(0)
|
|
||||||
hint = utils.Int2Str(len(skus))
|
|
||||||
} else {
|
|
||||||
hint = rootTask.ID
|
|
||||||
}
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func isErrModifyPrice(err error) bool {
|
// db := dao.GetDB()
|
||||||
if errExt, ok := err.(*utils.ErrorWithCode); ok && errExt.IntCode() == 1 {
|
// storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
||||||
for _, v := range []string{
|
// if err != nil {
|
||||||
"折扣商品原价不允许修改",
|
// return "", err
|
||||||
} {
|
// }
|
||||||
if strings.Index(errExt.ErrMsg(), v) >= 0 {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
func (p *PurchaseHandler) RefreshStoresAllSkusID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool, storeIDs []int) (hint string, err error) {
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
// rootTask := tasksch.NewSeqTask("美团外卖FullSyncStoreSkus", ctx,
|
||||||
userName := ctx.GetUserName()
|
// func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||||
globals.SugarLogger.Debugf("mtwm FullSyncStoreSkus storeID:%d, isContinueWhenError:%t, userName:%s", storeID, isContinueWhenError, userName)
|
// switch step {
|
||||||
|
// case 0:
|
||||||
db := dao.GetDB()
|
// err = p.DeleteStoreAllSkus(ctx, rootTask, storeID, storeDetail.VendorStoreID, isContinueWhenError)
|
||||||
storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDMTWM)
|
// if isContinueWhenError {
|
||||||
if err != nil {
|
// err = nil
|
||||||
return "", err
|
// }
|
||||||
}
|
// if err == nil {
|
||||||
|
// _, err = dao.SetStoreSkuSyncStatus(db, model.VendorIDMTWM, []int{storeID}, nil, model.SyncFlagNewMask)
|
||||||
rootTask := tasksch.NewSeqTask("美团外卖FullSyncStoreSkus", ctx,
|
// }
|
||||||
func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
// case 1:
|
||||||
switch step {
|
// if err = p.DeleteStoreAllCategories(ctx, rootTask, storeID, storeDetail.VendorStoreID, isContinueWhenError); err == nil {
|
||||||
case 0:
|
// _, err = dao.SetStoreCategorySyncStatus(db, model.VendorIDMTWM, []int{storeID}, nil, model.SyncFlagNewMask)
|
||||||
err = p.DeleteStoreAllSkus(ctx, rootTask, storeID, storeDetail.VendorStoreID, isContinueWhenError)
|
// }
|
||||||
if isContinueWhenError {
|
// case 2:
|
||||||
err = nil
|
// _, err = p.SyncLocalStoreCategory(ctx, db, storeID, true, nil)
|
||||||
}
|
// case 3:
|
||||||
if err == nil {
|
// _, err = p.SyncStoreCategory(ctx, rootTask, storeID, false)
|
||||||
_, err = dao.SetStoreSkuSyncStatus(db, model.VendorIDMTWM, []int{storeID}, nil, model.SyncFlagNewMask)
|
// case 4:
|
||||||
}
|
// _, err = p.SyncStoreSkus(ctx, rootTask, storeID, nil, false, isContinueWhenError)
|
||||||
case 1:
|
// }
|
||||||
if err = p.DeleteStoreAllCategories(ctx, rootTask, storeID, storeDetail.VendorStoreID, isContinueWhenError); err == nil {
|
// return nil, err
|
||||||
_, err = dao.SetStoreCategorySyncStatus(db, model.VendorIDMTWM, []int{storeID}, nil, model.SyncFlagNewMask)
|
// }, 5)
|
||||||
}
|
// tasksch.AddChild(parentTask, rootTask).Run()
|
||||||
case 2:
|
// if !isAsync {
|
||||||
_, err = p.SyncLocalStoreCategory(ctx, db, storeID, true, nil)
|
// _, err = rootTask.GetResult(0)
|
||||||
case 3:
|
// }
|
||||||
_, err = p.SyncStoreCategory(ctx, rootTask, storeID, false)
|
// return rootTask.ID, err
|
||||||
case 4:
|
// }
|
||||||
_, err = p.SyncStoreSkus(ctx, rootTask, storeID, nil, false, isContinueWhenError)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, 5)
|
|
||||||
tasksch.AddChild(parentTask, rootTask).Run()
|
|
||||||
if !isAsync {
|
|
||||||
_, err = rootTask.GetResult(0)
|
|
||||||
}
|
|
||||||
return rootTask.ID, err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,340 +1,313 @@
|
|||||||
package wsc
|
package wsc
|
||||||
|
|
||||||
import (
|
// func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync bool) (hint string, err error) {
|
||||||
"errors"
|
// strStoreID := utils.Int2Str(storeID)
|
||||||
"fmt"
|
// num := 0
|
||||||
"math/rand"
|
// db := dao.GetDB()
|
||||||
|
// rootTask := tasksch.NewSeqTask("微盟微商城SyncStoreCategory step1", ctx,
|
||||||
"git.rosy.net.cn/baseapi/platformapi/weimobapi"
|
// func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
// level := step + 1
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
// catList, err := dao.GetStoreCategories(db, model.VendorIDWSC, storeID, level)
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
// // globals.SugarLogger.Debug(utils.Format4Output(catList, false))
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils/tasksch"
|
// if len(catList) > 0 {
|
||||||
"git.rosy.net.cn/jx-callback/business/model"
|
// num += len(catList)
|
||||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
// task := tasksch.NewParallelTask(fmt.Sprintf("微盟微商城SyncStoreCategory step2, level=%d", level), nil, ctx,
|
||||||
"git.rosy.net.cn/jx-callback/globals"
|
// func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
"git.rosy.net.cn/jx-callback/globals/api"
|
// updateFields := []string{dao.GetSyncStatusStructField(model.VendorNames[model.VendorIDWSC])}
|
||||||
)
|
// catInfo := batchItemList[0].(*dao.SkuStoreCatInfo)
|
||||||
|
// storeCatMap := &model.StoreSkuCategoryMap{}
|
||||||
const (
|
// storeCatMap.ID = catInfo.MapID
|
||||||
DefFreightTemplateId = 6537248
|
// if catInfo.StoreCatSyncStatus&model.SyncFlagDeletedMask != 0 { // 删除
|
||||||
DefDeliveryTypeId = 177445
|
// if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 && !dao.IsVendorThingIDEmpty(catInfo.VendorCatID) {
|
||||||
DefVendorCategoryId = 35
|
// globals.SugarLogger.Debugf("UpdateClassify strStoreID:%s, WscID:%s", strStoreID, catInfo.VendorCatID)
|
||||||
DefCatImg = "https://image-c.weimobwmc.com/openruntime/249b77ced5da4736a56641ebcf4875ec.png"
|
// if globals.EnableWscStoreWrite {
|
||||||
)
|
// err = api.WeimobAPI.UpdateClassify(utils.Str2Int64(catInfo.VendorCatID), composeFakeDelName(catInfo.Name), "")
|
||||||
|
// }
|
||||||
func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync bool) (hint string, err error) {
|
// }
|
||||||
strStoreID := utils.Int2Str(storeID)
|
// } else if catInfo.StoreCatSyncStatus&(model.SyncFlagNewMask|model.SyncFlagModifiedMask) != 0 { // 新增
|
||||||
num := 0
|
// catImg := ""
|
||||||
db := dao.GetDB()
|
// if level == 2 {
|
||||||
rootTask := tasksch.NewSeqTask("微盟微商城SyncStoreCategory step1", ctx,
|
// catImg = DefCatImg
|
||||||
func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
// }
|
||||||
level := step + 1
|
// if globals.EnableWscStoreWrite {
|
||||||
catList, err := dao.GetStoreCategories(db, model.VendorIDWSC, storeID, level)
|
// storeCatMap.WscID, err = api.WeimobAPI.AddClassify(catInfo.Name, utils.Str2Int64WithDefault(catInfo.ParentVendorCatID, 0), catImg)
|
||||||
// globals.SugarLogger.Debug(utils.Format4Output(catList, false))
|
// }
|
||||||
if len(catList) > 0 {
|
// if err == nil {
|
||||||
num += len(catList)
|
// updateFields = append(updateFields, dao.GetVendorThingIDStructField(model.VendorNames[model.VendorIDWSC]))
|
||||||
task := tasksch.NewParallelTask(fmt.Sprintf("微盟微商城SyncStoreCategory step2, level=%d", level), nil, ctx,
|
// }
|
||||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
// } else if catInfo.StoreCatSyncStatus&(model.SyncFlagModifiedMask) != 0 { // 修改
|
||||||
updateFields := []string{dao.GetSyncStatusStructField(model.VendorNames[model.VendorIDWSC])}
|
// if globals.EnableWscStoreWrite {
|
||||||
catInfo := batchItemList[0].(*dao.SkuStoreCatInfo)
|
// err = api.WeimobAPI.UpdateClassify(utils.Str2Int64(catInfo.VendorCatID), catInfo.Name, "")
|
||||||
storeCatMap := &model.StoreSkuCategoryMap{}
|
// }
|
||||||
storeCatMap.ID = catInfo.MapID
|
// }
|
||||||
if catInfo.StoreCatSyncStatus&model.SyncFlagDeletedMask != 0 { // 删除
|
// if err == nil {
|
||||||
if catInfo.StoreCatSyncStatus&model.SyncFlagNewMask == 0 && !dao.IsVendorThingIDEmpty(catInfo.VendorCatID) {
|
// db2 := dao.GetDB()
|
||||||
globals.SugarLogger.Debugf("UpdateClassify strStoreID:%s, WscID:%s", strStoreID, catInfo.VendorCatID)
|
// storeCatMap.WscSyncStatus = 0
|
||||||
if globals.EnableWscStoreWrite {
|
// _, err = dao.UpdateEntity(db2, storeCatMap, updateFields...)
|
||||||
err = api.WeimobAPI.UpdateClassify(utils.Str2Int64(catInfo.VendorCatID), composeFakeDelName(catInfo.Name), "")
|
// }
|
||||||
}
|
// return nil, err
|
||||||
}
|
// }, catList)
|
||||||
} else if catInfo.StoreCatSyncStatus&(model.SyncFlagNewMask|model.SyncFlagModifiedMask) != 0 { // 新增
|
// rootTask.AddChild(task).Run()
|
||||||
catImg := ""
|
// _, err = task.GetResult(0)
|
||||||
if level == 2 {
|
// }
|
||||||
catImg = DefCatImg
|
// return nil, err
|
||||||
}
|
// }, 2)
|
||||||
if globals.EnableWscStoreWrite {
|
// tasksch.AddChild(parentTask, rootTask).Run()
|
||||||
storeCatMap.WscID, err = api.WeimobAPI.AddClassify(catInfo.Name, utils.Str2Int64WithDefault(catInfo.ParentVendorCatID, 0), catImg)
|
// if !isAsync {
|
||||||
}
|
// hint = utils.Int2Str(num)
|
||||||
if err == nil {
|
// _, err = rootTask.GetResult(0)
|
||||||
updateFields = append(updateFields, dao.GetVendorThingIDStructField(model.VendorNames[model.VendorIDWSC]))
|
// } else {
|
||||||
}
|
// hint = rootTask.ID
|
||||||
} else if catInfo.StoreCatSyncStatus&(model.SyncFlagModifiedMask) != 0 { // 修改
|
|
||||||
if globals.EnableWscStoreWrite {
|
|
||||||
err = api.WeimobAPI.UpdateClassify(utils.Str2Int64(catInfo.VendorCatID), catInfo.Name, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
db2 := dao.GetDB()
|
|
||||||
storeCatMap.WscSyncStatus = 0
|
|
||||||
_, err = dao.UpdateEntity(db2, storeCatMap, updateFields...)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, catList)
|
|
||||||
rootTask.AddChild(task).Run()
|
|
||||||
_, err = task.GetResult(0)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, 2)
|
|
||||||
tasksch.AddChild(parentTask, rootTask).Run()
|
|
||||||
if !isAsync {
|
|
||||||
hint = utils.Int2Str(num)
|
|
||||||
_, err = rootTask.GetResult(0)
|
|
||||||
} else {
|
|
||||||
hint = rootTask.ID
|
|
||||||
}
|
|
||||||
return "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// 此函数根据门店商品信息重建分类信息
|
|
||||||
// 远程有,本地无, --> 删除远程
|
|
||||||
// 远程有,本地有,映射无, --> 添加关联
|
|
||||||
// 远程有,本地有,映射有, --> 不处理
|
|
||||||
// 远程无,本地有,映射无, --> 添加本地
|
|
||||||
// 远程无,本地有,映射有, --> 同步标记改为新增
|
|
||||||
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
|
||||||
func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, isAsync bool) (hint string, err error) {
|
|
||||||
if db == nil {
|
|
||||||
db = dao.GetDB()
|
|
||||||
}
|
|
||||||
catMap := make([]map[string]*dao.SkuStoreCatInfo, 2)
|
|
||||||
for i := 0; i < 2; i++ {
|
|
||||||
catMap[i] = make(map[string]*dao.SkuStoreCatInfo)
|
|
||||||
localCats, err := dao.GetSkusCategories(db, model.VendorIDWSC, storeID, nil, i+1)
|
|
||||||
// globals.SugarLogger.Debug(utils.Format4Output(localCats, false))
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for _, cat := range localCats {
|
|
||||||
catMap[i][cat.ParentCatName+"/"+cat.Name] = cat
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
dao.Begin(db)
|
|
||||||
defer func() {
|
|
||||||
dao.Rollback(db)
|
|
||||||
}()
|
|
||||||
num := 0
|
|
||||||
for i := 0; i < 2; i++ {
|
|
||||||
for _, v := range catMap[i] {
|
|
||||||
if v.MapID == -1 || v.MapID == 0 { // 本地缺失
|
|
||||||
wscSyncStatus := int8(model.SyncFlagNewMask)
|
|
||||||
if v.MapID == -1 { // 远程有同名的,只是简单增加一条本地记录关联
|
|
||||||
wscSyncStatus = 0
|
|
||||||
}
|
|
||||||
catMap := &model.StoreSkuCategoryMap{
|
|
||||||
StoreID: storeID,
|
|
||||||
CategoryID: v.ID,
|
|
||||||
WscID: utils.Str2Int64WithDefault(v.VendorCatID, 0),
|
|
||||||
WscSyncStatus: wscSyncStatus,
|
|
||||||
MtwmSyncStatus: model.SyncFlagNewMask,
|
|
||||||
EbaiSyncStatus: model.SyncFlagNewMask,
|
|
||||||
ElmSyncStatus: model.SyncFlagNewMask,
|
|
||||||
}
|
|
||||||
num++
|
|
||||||
dao.WrapAddIDCULDEntity(catMap, ctx.GetUserName())
|
|
||||||
if err = dao.CreateEntity(db, catMap); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if dao.IsVendorThingIDEmpty(v.VendorCatID) && ((v.StoreCatSyncStatus & model.SyncFlagNewMask) == 0) {
|
|
||||||
catMap := &model.StoreSkuCategoryMap{}
|
|
||||||
catMap.ID = v.MapID
|
|
||||||
if _, err = dao.UpdateEntityLogically(db, catMap, map[string]interface{}{
|
|
||||||
model.FieldWscSyncStatus: model.SyncFlagNewMask,
|
|
||||||
}, ctx.GetUserName(), nil); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dao.Commit(db)
|
|
||||||
return utils.Int2Str(num), err
|
|
||||||
}
|
|
||||||
|
|
||||||
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
|
||||||
func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
||||||
db := dao.GetDB()
|
|
||||||
for i := 0; i < 3; i++ { // 最多重试三次
|
|
||||||
if hint, err = p.SyncLocalStoreCategory(ctx, db, storeID, false); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if hint != "0" {
|
|
||||||
if hint, err = p.SyncStoreCategory(ctx, parentTask, storeID, false); err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if hint == "0" {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if hint != "0" {
|
|
||||||
return "", errors.New("同步门店商品所需目录失败")
|
|
||||||
}
|
|
||||||
storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDWSC)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
skus, err := dao.GetStoreSkus(db, model.VendorIDWSC, storeID, skuIDs)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
globals.SugarLogger.Debug(utils.Format4Output(skus, false))
|
|
||||||
rootTask := tasksch.NewParallelTask("微盟微商城SyncStoreSkus", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
|
||||||
func(rootTask *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
v := batchItemList[0]
|
|
||||||
skuItem := v.(*dao.StoreSkuSyncInfo)
|
|
||||||
pricePercentage := jxutils.GetPricePercentage(storeDetail.PricePercentagePackObj, int(skuItem.Price), int(storeDetail.PricePercentage))
|
|
||||||
updateFields := []string{model.FieldWscSyncStatus}
|
|
||||||
storeSkuBind := &model.StoreSkuBind{}
|
|
||||||
storeSkuBind.ID = skuItem.BindID
|
|
||||||
if skuItem.StoreSkuSyncStatus&model.SyncFlagDeletedMask != 0 {
|
|
||||||
if skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask == 0 {
|
|
||||||
goodsID := utils.Str2Int64WithDefault(skuItem.VendorNameID, 0)
|
|
||||||
if globals.EnableWscStoreWrite {
|
|
||||||
if err = api.WeimobAPI.UpdateGoodsShelfStatus([]int64{goodsID}, false); err == nil {
|
|
||||||
err = api.WeimobAPI.UpdateGoodsTitle(goodsID, composeFakeDelName(skuItem.Name))
|
|
||||||
} else if intErr, ok := err.(*utils.ErrorWithCode); ok && intErr.Code() == "1001930300001" { // 商品不存在错
|
|
||||||
err = nil // 强制忽略
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if skuItem.StoreSkuSyncStatus&(model.SyncFlagStoreSkuModifiedMask|model.SyncFlagNewMask) != 0 {
|
|
||||||
outerGoodsCode := utils.Int2Str(skuItem.NameID)
|
|
||||||
title := jxutils.ComposeSkuName(skuItem.Prefix, skuItem.Name, skuItem.Comment, skuItem.Unit, skuItem.SpecQuality, skuItem.SpecUnit, 30)
|
|
||||||
isPutAway := jxutils.MergeSkuStatus(skuItem.Status, skuItem.StoreSkuStatus) == model.SkuStatusNormal
|
|
||||||
categoryID := skuItem.VendorVendorCatID
|
|
||||||
if categoryID == 0 {
|
|
||||||
categoryID = DefVendorCategoryId
|
|
||||||
}
|
|
||||||
classifyIDList := []int64{utils.Str2Int64WithDefault(skuItem.VendorCatID, 0)}
|
|
||||||
if skuItem.SkuVendorCatID != "" {
|
|
||||||
if int64Value := utils.Str2Int64WithDefault(skuItem.SkuVendorCatID, 0); int64Value > 0 {
|
|
||||||
classifyIDList = append(classifyIDList, int64Value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
b2cGoods := &weimobapi.PendingSaveB2CGoodsVo{
|
|
||||||
FreightTemplateId: DefFreightTemplateId,
|
|
||||||
DeliveryTypeIdList: []int64{DefDeliveryTypeId},
|
|
||||||
B2cGoodsType: weimobapi.GoodsTypeNormal,
|
|
||||||
}
|
|
||||||
salePrice := int64(jxutils.CaculateSkuVendorPrice(int(skuItem.Price), pricePercentage, skuItem.CatPricePercentage))
|
|
||||||
skuList := []map[string]interface{}{
|
|
||||||
map[string]interface{}{
|
|
||||||
weimobapi.KeyOuterSkuCode: utils.Int2Str(skuItem.ID),
|
|
||||||
weimobapi.KeyImageURL: skuItem.Img,
|
|
||||||
weimobapi.KeySalePrice: jxutils.IntPrice2Standard(salePrice),
|
|
||||||
weimobapi.KeyCostPrice: jxutils.IntPrice2Standard(salePrice * 8 / 10),
|
|
||||||
weimobapi.KeyOriginalPrice: jxutils.IntPrice2Standard(salePrice * 10 / (6 + rand.Int63n(4))),
|
|
||||||
weimobapi.KeyEditStockNum: 0, //model.MaxStoreSkuStockQty,
|
|
||||||
weimobapi.KeyB2cSku: &weimobapi.PendingSaveB2CSkuVo{
|
|
||||||
Weight: jxutils.IntWeight2Float(skuItem.Weight),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
if skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask != 0 {
|
|
||||||
var (
|
|
||||||
goodsID int64
|
|
||||||
skuMap map[string]int64
|
|
||||||
)
|
|
||||||
if globals.EnableWscStoreWrite {
|
|
||||||
goodsID, skuMap, err = api.WeimobAPI.AddGoods(outerGoodsCode, title, false, []string{skuItem.Img}, skuItem.Comment, isPutAway, 0, categoryID, classifyIDList, b2cGoods, skuList, nil)
|
|
||||||
} else {
|
|
||||||
goodsID = jxutils.GenFakeID()
|
|
||||||
skuMap = map[string]int64{
|
|
||||||
utils.Int2Str(skuItem.ID): jxutils.GenFakeID(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
storeSkuBind.WscID = skuMap[utils.Int2Str(skuItem.ID)]
|
|
||||||
storeSkuBind.WscID2 = goodsID
|
|
||||||
updateFields = append(updateFields, model.FieldWscID, model.FieldWscID2)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
goodsID := utils.Str2Int64WithDefault(skuItem.VendorNameID, 0)
|
|
||||||
goodsInfo, err2 := api.WeimobAPI.QueryGoodsDetail(goodsID)
|
|
||||||
if err = err2; err == nil {
|
|
||||||
// http://open.weimob.com/docapi/article?tag=Af
|
|
||||||
// sku id,如果为空,则新增sku; 如果更新之前的skuId与入参skuId对应,则更新sku; 如果更新之前的skuId没有和入参的skuId对应,删除更新之前的sku
|
|
||||||
skuList[0][weimobapi.KeySkuID] = utils.Str2Int64WithDefault(skuItem.VendorSkuID, 0)
|
|
||||||
remoteSkuList := goodsInfo[weimobapi.KeySkuList].([]interface{})
|
|
||||||
if len(remoteSkuList) > 0 {
|
|
||||||
// skuList[0][weimobapi.KeyEditStockNum] = model.MaxStoreSkuStockQty - int(utils.MustInterface2Int64(remoteSkuList[0].(map[string]interface{})[weimobapi.KeyAvailableStockNum]))
|
|
||||||
}
|
|
||||||
if globals.EnableWscStoreWrite {
|
|
||||||
_, _, err = api.WeimobAPI.UpdateGoods(goodsID, title, false, []string{skuItem.Img}, skuItem.Comment, isPutAway, 0, categoryID, classifyIDList, b2cGoods, skuList, nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
_, err = dao.UpdateEntity(nil, storeSkuBind, updateFields...)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, skus)
|
|
||||||
if parentTask != nil {
|
|
||||||
parentTask.AddChild(rootTask)
|
|
||||||
}
|
|
||||||
rootTask.Run()
|
|
||||||
if !isAsync {
|
|
||||||
_, err = rootTask.GetResult(0)
|
|
||||||
hint = utils.Int2Str(len(skus))
|
|
||||||
} else {
|
|
||||||
hint = rootTask.ID
|
|
||||||
}
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PurchaseHandler) RefreshStoresAllSkusID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool, storeIDs []int) (hint string, err error) {
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
||||||
userName := ctx.GetUserName()
|
|
||||||
globals.SugarLogger.Debugf("wsc FullSyncStoreSkus storeID:%d, isContinueWhenError:%t, userName:%s", storeID, isContinueWhenError, userName)
|
|
||||||
|
|
||||||
db := dao.GetDB()
|
|
||||||
rootTask := tasksch.NewSeqTask("微盟微商城FullSyncStoreSkus", ctx,
|
|
||||||
func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
|
||||||
switch step {
|
|
||||||
case 0:
|
|
||||||
_, err = dao.SetStoreCategorySyncStatus(db, model.VendorIDWSC, []int{storeID}, nil, model.SyncFlagNewMask)
|
|
||||||
case 1:
|
|
||||||
_, err = dao.SetStoreSkuSyncStatus(db, model.VendorIDWSC, []int{storeID}, nil, model.SyncFlagNewMask)
|
|
||||||
case 2:
|
|
||||||
_, err = p.SyncLocalStoreCategory(ctx, db, storeID, false)
|
|
||||||
case 3:
|
|
||||||
_, err = p.SyncStoreCategory(ctx, rootTask, storeID, false)
|
|
||||||
case 4:
|
|
||||||
_, err = p.SyncStoreSkus(ctx, rootTask, storeID, nil, true, isContinueWhenError)
|
|
||||||
}
|
|
||||||
return nil, err
|
|
||||||
}, 5)
|
|
||||||
tasksch.AddChild(parentTask, rootTask).Run()
|
|
||||||
if !isAsync {
|
|
||||||
_, err = rootTask.GetResult(0)
|
|
||||||
}
|
|
||||||
return rootTask.ID, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PurchaseHandler) DeleteRemoteStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
|
||||||
return hint, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func composeFakeDelName(name string) string {
|
|
||||||
return "del_" + name
|
|
||||||
}
|
|
||||||
|
|
||||||
// func SplitGoodsAndSkuIDFromJXVendorSkuID(vendorSkuID string) (goodsID, skuID int64) {
|
|
||||||
// list := strings.Split(vendorSkuID, ",")
|
|
||||||
// if len(list) == 2 {
|
|
||||||
// skuID = utils.Str2Int64WithDefault(list[0], 0)
|
|
||||||
// goodsID = utils.Str2Int64WithDefault(list[1], 0)
|
|
||||||
// }
|
// }
|
||||||
// return goodsID, skuID
|
// return "", nil
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// // skuID放在前面的原因是存入数据库后,便于以skuID的查找
|
// // 此函数根据门店商品信息重建分类信息
|
||||||
// func ComposeJXVendorSkuIDFromGoodsAndSkuID(goodsID, skuID int64) (vendorSkuID string) {
|
// // 远程有,本地无, --> 删除远程
|
||||||
// return utils.Int64ToStr(skuID) + "," + utils.Int64ToStr(goodsID)
|
// // 远程有,本地有,映射无, --> 添加关联
|
||||||
|
// // 远程有,本地有,映射有, --> 不处理
|
||||||
|
// // 远程无,本地有,映射无, --> 添加本地
|
||||||
|
// // 远程无,本地有,映射有, --> 同步标记改为新增
|
||||||
|
// // hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
|
// func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, isAsync bool) (hint string, err error) {
|
||||||
|
// if db == nil {
|
||||||
|
// db = dao.GetDB()
|
||||||
|
// }
|
||||||
|
// catMap := make([]map[string]*dao.SkuStoreCatInfo, 2)
|
||||||
|
// for i := 0; i < 2; i++ {
|
||||||
|
// catMap[i] = make(map[string]*dao.SkuStoreCatInfo)
|
||||||
|
// localCats, err := dao.GetSkusCategories(db, model.VendorIDWSC, storeID, nil, i+1)
|
||||||
|
// // globals.SugarLogger.Debug(utils.Format4Output(localCats, false))
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// for _, cat := range localCats {
|
||||||
|
// catMap[i][cat.ParentCatName+"/"+cat.Name] = cat
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// dao.Begin(db)
|
||||||
|
// defer func() {
|
||||||
|
// dao.Rollback(db)
|
||||||
|
// }()
|
||||||
|
// num := 0
|
||||||
|
// for i := 0; i < 2; i++ {
|
||||||
|
// for _, v := range catMap[i] {
|
||||||
|
// if v.MapID == -1 || v.MapID == 0 { // 本地缺失
|
||||||
|
// wscSyncStatus := int8(model.SyncFlagNewMask)
|
||||||
|
// if v.MapID == -1 { // 远程有同名的,只是简单增加一条本地记录关联
|
||||||
|
// wscSyncStatus = 0
|
||||||
|
// }
|
||||||
|
// catMap := &model.StoreSkuCategoryMap{
|
||||||
|
// StoreID: storeID,
|
||||||
|
// CategoryID: v.ID,
|
||||||
|
// WscID: utils.Str2Int64WithDefault(v.VendorCatID, 0),
|
||||||
|
// WscSyncStatus: wscSyncStatus,
|
||||||
|
// MtwmSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// EbaiSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// ElmSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// }
|
||||||
|
// num++
|
||||||
|
// dao.WrapAddIDCULDEntity(catMap, ctx.GetUserName())
|
||||||
|
// if err = dao.CreateEntity(db, catMap); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// if dao.IsVendorThingIDEmpty(v.VendorCatID) && ((v.StoreCatSyncStatus & model.SyncFlagNewMask) == 0) {
|
||||||
|
// catMap := &model.StoreSkuCategoryMap{}
|
||||||
|
// catMap.ID = v.MapID
|
||||||
|
// if _, err = dao.UpdateEntityLogically(db, catMap, map[string]interface{}{
|
||||||
|
// model.FieldWscSyncStatus: model.SyncFlagNewMask,
|
||||||
|
// }, ctx.GetUserName(), nil); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// dao.Commit(db)
|
||||||
|
// return utils.Int2Str(num), err
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// // hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
|
// func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, skuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
|
// db := dao.GetDB()
|
||||||
|
// for i := 0; i < 3; i++ { // 最多重试三次
|
||||||
|
// if hint, err = p.SyncLocalStoreCategory(ctx, db, storeID, false); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// if hint != "0" {
|
||||||
|
// if hint, err = p.SyncStoreCategory(ctx, parentTask, storeID, false); err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if hint == "0" {
|
||||||
|
// break
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if hint != "0" {
|
||||||
|
// return "", errors.New("同步门店商品所需目录失败")
|
||||||
|
// }
|
||||||
|
// storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDWSC)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// skus, err := dao.GetStoreSkus(db, model.VendorIDWSC, storeID, skuIDs)
|
||||||
|
// if err != nil {
|
||||||
|
// return "", err
|
||||||
|
// }
|
||||||
|
// globals.SugarLogger.Debug(utils.Format4Output(skus, false))
|
||||||
|
// rootTask := tasksch.NewParallelTask("微盟微商城SyncStoreSkus", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||||||
|
// func(rootTask *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
// v := batchItemList[0]
|
||||||
|
// skuItem := v.(*dao.StoreSkuSyncInfo)
|
||||||
|
// pricePercentage := jxutils.GetPricePercentage(storeDetail.PricePercentagePackObj, int(skuItem.Price), int(storeDetail.PricePercentage))
|
||||||
|
// updateFields := []string{model.FieldWscSyncStatus}
|
||||||
|
// storeSkuBind := &model.StoreSkuBind{}
|
||||||
|
// storeSkuBind.ID = skuItem.BindID
|
||||||
|
// if skuItem.StoreSkuSyncStatus&model.SyncFlagDeletedMask != 0 {
|
||||||
|
// if skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask == 0 {
|
||||||
|
// goodsID := utils.Str2Int64WithDefault(skuItem.VendorNameID, 0)
|
||||||
|
// if globals.EnableWscStoreWrite {
|
||||||
|
// if err = api.WeimobAPI.UpdateGoodsShelfStatus([]int64{goodsID}, false); err == nil {
|
||||||
|
// err = api.WeimobAPI.UpdateGoodsTitle(goodsID, composeFakeDelName(skuItem.Name))
|
||||||
|
// } else if intErr, ok := err.(*utils.ErrorWithCode); ok && intErr.Code() == "1001930300001" { // 商品不存在错
|
||||||
|
// err = nil // 强制忽略
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// } else if skuItem.StoreSkuSyncStatus&(model.SyncFlagStoreSkuModifiedMask|model.SyncFlagNewMask) != 0 {
|
||||||
|
// outerGoodsCode := utils.Int2Str(skuItem.NameID)
|
||||||
|
// title := jxutils.ComposeSkuName(skuItem.Prefix, skuItem.Name, skuItem.Comment, skuItem.Unit, skuItem.SpecQuality, skuItem.SpecUnit, 30)
|
||||||
|
// isPutAway := jxutils.MergeSkuStatus(skuItem.Status, skuItem.StoreSkuStatus) == model.SkuStatusNormal
|
||||||
|
// categoryID := skuItem.VendorVendorCatID
|
||||||
|
// if categoryID == 0 {
|
||||||
|
// categoryID = DefVendorCategoryId
|
||||||
|
// }
|
||||||
|
// classifyIDList := []int64{utils.Str2Int64WithDefault(skuItem.VendorCatID, 0)}
|
||||||
|
// if skuItem.SkuVendorCatID != "" {
|
||||||
|
// if int64Value := utils.Str2Int64WithDefault(skuItem.SkuVendorCatID, 0); int64Value > 0 {
|
||||||
|
// classifyIDList = append(classifyIDList, int64Value)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// b2cGoods := &weimobapi.PendingSaveB2CGoodsVo{
|
||||||
|
// FreightTemplateId: DefFreightTemplateId,
|
||||||
|
// DeliveryTypeIdList: []int64{DefDeliveryTypeId},
|
||||||
|
// B2cGoodsType: weimobapi.GoodsTypeNormal,
|
||||||
|
// }
|
||||||
|
// salePrice := int64(jxutils.CaculateSkuVendorPrice(int(skuItem.Price), pricePercentage, skuItem.CatPricePercentage))
|
||||||
|
// skuList := []map[string]interface{}{
|
||||||
|
// map[string]interface{}{
|
||||||
|
// weimobapi.KeyOuterSkuCode: utils.Int2Str(skuItem.ID),
|
||||||
|
// weimobapi.KeyImageURL: skuItem.Img,
|
||||||
|
// weimobapi.KeySalePrice: jxutils.IntPrice2Standard(salePrice),
|
||||||
|
// weimobapi.KeyCostPrice: jxutils.IntPrice2Standard(salePrice * 8 / 10),
|
||||||
|
// weimobapi.KeyOriginalPrice: jxutils.IntPrice2Standard(salePrice * 10 / (6 + rand.Int63n(4))),
|
||||||
|
// weimobapi.KeyEditStockNum: 0, //model.MaxStoreSkuStockQty,
|
||||||
|
// weimobapi.KeyB2cSku: &weimobapi.PendingSaveB2CSkuVo{
|
||||||
|
// Weight: jxutils.IntWeight2Float(skuItem.Weight),
|
||||||
|
// },
|
||||||
|
// },
|
||||||
|
// }
|
||||||
|
// if skuItem.StoreSkuSyncStatus&model.SyncFlagNewMask != 0 {
|
||||||
|
// var (
|
||||||
|
// goodsID int64
|
||||||
|
// skuMap map[string]int64
|
||||||
|
// )
|
||||||
|
// if globals.EnableWscStoreWrite {
|
||||||
|
// goodsID, skuMap, err = api.WeimobAPI.AddGoods(outerGoodsCode, title, false, []string{skuItem.Img}, skuItem.Comment, isPutAway, 0, categoryID, classifyIDList, b2cGoods, skuList, nil)
|
||||||
|
// } else {
|
||||||
|
// goodsID = jxutils.GenFakeID()
|
||||||
|
// skuMap = map[string]int64{
|
||||||
|
// utils.Int2Str(skuItem.ID): jxutils.GenFakeID(),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// storeSkuBind.WscID = skuMap[utils.Int2Str(skuItem.ID)]
|
||||||
|
// storeSkuBind.WscID2 = goodsID
|
||||||
|
// updateFields = append(updateFields, model.FieldWscID, model.FieldWscID2)
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// goodsID := utils.Str2Int64WithDefault(skuItem.VendorNameID, 0)
|
||||||
|
// goodsInfo, err2 := api.WeimobAPI.QueryGoodsDetail(goodsID)
|
||||||
|
// if err = err2; err == nil {
|
||||||
|
// // http://open.weimob.com/docapi/article?tag=Af
|
||||||
|
// // sku id,如果为空,则新增sku; 如果更新之前的skuId与入参skuId对应,则更新sku; 如果更新之前的skuId没有和入参的skuId对应,删除更新之前的sku
|
||||||
|
// skuList[0][weimobapi.KeySkuID] = utils.Str2Int64WithDefault(skuItem.VendorSkuID, 0)
|
||||||
|
// remoteSkuList := goodsInfo[weimobapi.KeySkuList].([]interface{})
|
||||||
|
// if len(remoteSkuList) > 0 {
|
||||||
|
// // skuList[0][weimobapi.KeyEditStockNum] = model.MaxStoreSkuStockQty - int(utils.MustInterface2Int64(remoteSkuList[0].(map[string]interface{})[weimobapi.KeyAvailableStockNum]))
|
||||||
|
// }
|
||||||
|
// if globals.EnableWscStoreWrite {
|
||||||
|
// _, _, err = api.WeimobAPI.UpdateGoods(goodsID, title, false, []string{skuItem.Img}, skuItem.Comment, isPutAway, 0, categoryID, classifyIDList, b2cGoods, skuList, nil)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// if err == nil {
|
||||||
|
// _, err = dao.UpdateEntity(nil, storeSkuBind, updateFields...)
|
||||||
|
// }
|
||||||
|
// return nil, err
|
||||||
|
// }, skus)
|
||||||
|
// if parentTask != nil {
|
||||||
|
// parentTask.AddChild(rootTask)
|
||||||
|
// }
|
||||||
|
// rootTask.Run()
|
||||||
|
// if !isAsync {
|
||||||
|
// _, err = rootTask.GetResult(0)
|
||||||
|
// hint = utils.Int2Str(len(skus))
|
||||||
|
// } else {
|
||||||
|
// hint = rootTask.ID
|
||||||
|
// }
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (p *PurchaseHandler) RefreshStoresAllSkusID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool, storeIDs []int) (hint string, err error) {
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (p *PurchaseHandler) FullSyncStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
|
// userName := ctx.GetUserName()
|
||||||
|
// globals.SugarLogger.Debugf("wsc FullSyncStoreSkus storeID:%d, isContinueWhenError:%t, userName:%s", storeID, isContinueWhenError, userName)
|
||||||
|
|
||||||
|
// db := dao.GetDB()
|
||||||
|
// rootTask := tasksch.NewSeqTask("微盟微商城FullSyncStoreSkus", ctx,
|
||||||
|
// func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||||
|
// switch step {
|
||||||
|
// case 0:
|
||||||
|
// _, err = dao.SetStoreCategorySyncStatus(db, model.VendorIDWSC, []int{storeID}, nil, model.SyncFlagNewMask)
|
||||||
|
// case 1:
|
||||||
|
// _, err = dao.SetStoreSkuSyncStatus(db, model.VendorIDWSC, []int{storeID}, nil, model.SyncFlagNewMask)
|
||||||
|
// case 2:
|
||||||
|
// _, err = p.SyncLocalStoreCategory(ctx, db, storeID, false)
|
||||||
|
// case 3:
|
||||||
|
// _, err = p.SyncStoreCategory(ctx, rootTask, storeID, false)
|
||||||
|
// case 4:
|
||||||
|
// _, err = p.SyncStoreSkus(ctx, rootTask, storeID, nil, true, isContinueWhenError)
|
||||||
|
// }
|
||||||
|
// return nil, err
|
||||||
|
// }, 5)
|
||||||
|
// tasksch.AddChild(parentTask, rootTask).Run()
|
||||||
|
// if !isAsync {
|
||||||
|
// _, err = rootTask.GetResult(0)
|
||||||
|
// }
|
||||||
|
// return rootTask.ID, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (p *PurchaseHandler) DeleteRemoteStoreSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
|
// return hint, err
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // func SplitGoodsAndSkuIDFromJXVendorSkuID(vendorSkuID string) (goodsID, skuID int64) {
|
||||||
|
// // list := strings.Split(vendorSkuID, ",")
|
||||||
|
// // if len(list) == 2 {
|
||||||
|
// // skuID = utils.Str2Int64WithDefault(list[0], 0)
|
||||||
|
// // goodsID = utils.Str2Int64WithDefault(list[1], 0)
|
||||||
|
// // }
|
||||||
|
// // return goodsID, skuID
|
||||||
|
// // }
|
||||||
|
|
||||||
|
// // // skuID放在前面的原因是存入数据库后,便于以skuID的查找
|
||||||
|
// // func ComposeJXVendorSkuIDFromGoodsAndSkuID(goodsID, skuID int64) (vendorSkuID string) {
|
||||||
|
// // return utils.Int64ToStr(skuID) + "," + utils.Int64ToStr(goodsID)
|
||||||
|
// // }
|
||||||
|
|||||||
@@ -16,6 +16,13 @@ import (
|
|||||||
"git.rosy.net.cn/jx-callback/globals/api"
|
"git.rosy.net.cn/jx-callback/globals/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefFreightTemplateId = 6537248
|
||||||
|
DefDeliveryTypeId = 177445
|
||||||
|
DefVendorCategoryId = 35
|
||||||
|
DefCatImg = "https://image-c.weimobwmc.com/openruntime/249b77ced5da4736a56641ebcf4875ec.png"
|
||||||
|
)
|
||||||
|
|
||||||
// 门店分类
|
// 门店分类
|
||||||
func (p *PurchaseHandler) GetStoreAllCategories(ctx *jxcontext.Context, vendorStoreID string) (cats []*partner.BareCategoryInfo, err error) {
|
func (p *PurchaseHandler) GetStoreAllCategories(ctx *jxcontext.Context, vendorStoreID string) (cats []*partner.BareCategoryInfo, err error) {
|
||||||
remoteCats, err := api.WeimobAPI.QueryClassifyInfoList()
|
remoteCats, err := api.WeimobAPI.QueryClassifyInfoList()
|
||||||
@@ -190,3 +197,7 @@ func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, storeID i
|
|||||||
func (p *PurchaseHandler) GetStoreSkusInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, inStoreSkuList []*partner.StoreSkuInfo) (outStoreSkuList []*partner.StoreSkuInfo, err error) {
|
func (p *PurchaseHandler) GetStoreSkusInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, inStoreSkuList []*partner.StoreSkuInfo) (outStoreSkuList []*partner.StoreSkuInfo, err error) {
|
||||||
return outStoreSkuList, err
|
return outStoreSkuList, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func composeFakeDelName(name string) string {
|
||||||
|
return "del_" + name
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user