- mtwm SyncStoreSkus done.
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package mtwm
|
package mtwm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
|
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
|
||||||
"git.rosy.net.cn/baseapi/utils"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||||
@@ -16,62 +18,69 @@ const (
|
|||||||
defVendorCatID = 200000380
|
defVendorCatID = 200000380
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync bool) (hint string, err error) {
|
func (p *PurchaseHandler) SyncStoreCategory(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, isAsync bool) (hint string, err error) {
|
||||||
userName := ctx.GetUserName()
|
userName := ctx.GetUserName()
|
||||||
strStoreID := utils.Int2Str(storeID)
|
strStoreID := utils.Int2Str(storeID)
|
||||||
|
num := 0
|
||||||
db := dao.GetDB()
|
db := dao.GetDB()
|
||||||
rootTask := tasksch.NewSeqTask("mtwm SyncStoreCategory", userName, func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
rootTask := tasksch.NewSeqTask("mtwm SyncStoreCategory", userName, func(rootTask *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||||
level := step + 1
|
level := step + 1
|
||||||
catList, err := dao.GetStoreCategories(db, model.VendorIDMTWM, storeID, level)
|
catList, err := dao.GetStoreCategories(db, model.VendorIDMTWM, storeID, level)
|
||||||
task := tasksch.NewParallelTask("mtwm SyncStoreCategory2", nil, userName, func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
if len(catList) > 0 {
|
||||||
updateFields := []string{dao.GetSyncStatusStructField(model.VendorNames[model.VendorIDMTWM])}
|
num += len(catList)
|
||||||
catInfo := batchItemList[0].(*dao.StoreCatSyncInfo)
|
task := tasksch.NewParallelTask("mtwm SyncStoreCategory2", nil, userName, func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
globals.SugarLogger.Debug(globals.EnableStoreWrite, " ", globals.EnableMtwmStoreWrite)
|
updateFields := []string{dao.GetSyncStatusStructField(model.VendorNames[model.VendorIDMTWM])}
|
||||||
if globals.EnableStoreWrite && globals.EnableMtwmStoreWrite {
|
catInfo := batchItemList[0].(*dao.StoreCatSyncInfo)
|
||||||
if catInfo.MtwmSyncStatus&model.SyncFlagDeletedMask != 0 { // 删除
|
if globals.EnableStoreWrite && globals.EnableMtwmStoreWrite {
|
||||||
err = api.MtwmAPI.RetailCatDelete(strStoreID, catInfo.MtwmID)
|
if catInfo.MtwmSyncStatus&model.SyncFlagDeletedMask != 0 { // 删除
|
||||||
} else if catInfo.MtwmSyncStatus&(model.SyncFlagNewMask|model.SyncFlagModifiedMask) != 0 { // 新增
|
err = api.MtwmAPI.RetailCatDelete(strStoreID, catInfo.MtwmID)
|
||||||
catName := catInfo.CatName
|
} else if catInfo.MtwmSyncStatus&(model.SyncFlagNewMask|model.SyncFlagModifiedMask) != 0 { // 新增
|
||||||
subCatName := ""
|
catName := catInfo.CatName
|
||||||
originName := ""
|
subCatName := ""
|
||||||
if catInfo.MtwmSyncStatus&model.SyncFlagNewMask == 0 {
|
originName := ""
|
||||||
originName = catInfo.MtwmID
|
|
||||||
}
|
|
||||||
if level == 2 {
|
|
||||||
originName = catInfo.ParentCatName
|
|
||||||
catName = catInfo.ParentCatName
|
|
||||||
subCatName = catInfo.CatName
|
|
||||||
if catInfo.MtwmSyncStatus&model.SyncFlagNewMask == 0 {
|
if catInfo.MtwmSyncStatus&model.SyncFlagNewMask == 0 {
|
||||||
originName = catInfo.MtwmID
|
originName = catInfo.MtwmID
|
||||||
catName = catInfo.CatName
|
}
|
||||||
subCatName = ""
|
if level == 2 {
|
||||||
|
originName = catInfo.ParentCatName
|
||||||
|
catName = catInfo.ParentCatName
|
||||||
|
subCatName = catInfo.CatName
|
||||||
|
if catInfo.MtwmSyncStatus&model.SyncFlagNewMask == 0 {
|
||||||
|
originName = catInfo.MtwmID
|
||||||
|
catName = catInfo.CatName
|
||||||
|
subCatName = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if catName == "" {
|
||||||
|
panic("catName is empty")
|
||||||
|
}
|
||||||
|
if err := api.MtwmAPI.RetailCatUpdate(strStoreID, originName, catName, subCatName, catInfo.Seq); err == nil {
|
||||||
|
catInfo.MtwmID = catInfo.CatName
|
||||||
|
updateFields = append(updateFields, dao.GetVendorThingIDStructField(model.VendorNames[model.VendorIDMTWM]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if catName == "" {
|
|
||||||
panic("catName is empty")
|
|
||||||
}
|
|
||||||
if err := api.MtwmAPI.RetailCatUpdate(strStoreID, originName, catName, subCatName, catInfo.Seq); err == nil {
|
|
||||||
catInfo.MtwmID = catInfo.CatName
|
|
||||||
updateFields = append(updateFields, dao.GetVendorThingIDStructField(model.VendorNames[model.VendorIDMTWM]))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
if err == nil {
|
||||||
if err == nil {
|
db2 := dao.GetDB()
|
||||||
db2 := dao.GetDB()
|
catInfo.MtwmSyncStatus = 0
|
||||||
catInfo.MtwmSyncStatus = 0
|
_, err = dao.UpdateEntity(db2, &catInfo.StoreSkuCategoryMap, updateFields...)
|
||||||
_, err = dao.UpdateEntity(db2, &catInfo.StoreSkuCategoryMap, updateFields...)
|
}
|
||||||
}
|
return nil, err
|
||||||
return nil, err
|
}, catList)
|
||||||
}, catList)
|
rootTask.AddChild(task).Run()
|
||||||
rootTask.AddChild(task).Run()
|
_, err = task.GetResult(0)
|
||||||
_, err = task.GetResult(0)
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}, 2)
|
}, 2)
|
||||||
tasksch.AddChild(parentTask, rootTask).Run()
|
tasksch.AddChild(parentTask, rootTask).Run()
|
||||||
if !isAsync {
|
if !isAsync {
|
||||||
|
hint = utils.Int2Str(num)
|
||||||
_, err = rootTask.GetResult(0)
|
_, err = rootTask.GetResult(0)
|
||||||
|
} else {
|
||||||
|
hint = rootTask.ID
|
||||||
}
|
}
|
||||||
return rootTask.ID, err
|
return hint, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PurchaseHandler) ReadStoreCategories(storeID int) (cats []*model.SkuCategory, err error) {
|
func (p *PurchaseHandler) ReadStoreCategories(storeID int) (cats []*model.SkuCategory, err error) {
|
||||||
@@ -84,6 +93,7 @@ func (p *PurchaseHandler) ReadStoreCategories(storeID int) (cats []*model.SkuCat
|
|||||||
// 远程有,本地有,映射有, --> 不处理
|
// 远程有,本地有,映射有, --> 不处理
|
||||||
// 远程无,本地有,映射无, --> 添加本地
|
// 远程无,本地有,映射无, --> 添加本地
|
||||||
// 远程无,本地有,映射有, --> 同步标记改为新增
|
// 远程无,本地有,映射有, --> 同步标记改为新增
|
||||||
|
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, isCheckRemote, isAsync bool) (hint string, err error) {
|
func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao.DaoDB, storeID int, isCheckRemote, isAsync bool) (hint string, err error) {
|
||||||
if db == nil {
|
if db == nil {
|
||||||
db = dao.GetDB()
|
db = dao.GetDB()
|
||||||
@@ -118,9 +128,10 @@ func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao
|
|||||||
localCat := catMap[level-1][parentCatName+"/"+catName]
|
localCat := catMap[level-1][parentCatName+"/"+catName]
|
||||||
// globals.SugarLogger.Debug(parentCatName, " ", catName, " ", localCat)
|
// globals.SugarLogger.Debug(parentCatName, " ", catName, " ", localCat)
|
||||||
if localCat == nil { // 本地分类就没有这个名字,直接删除
|
if localCat == nil { // 本地分类就没有这个名字,直接删除
|
||||||
globals.SugarLogger.Debug(parentCatName, " ", catName, " ", localCat, strStoreID)
|
if globals.EnableStoreWrite && globals.EnableMtwmStoreWrite {
|
||||||
if err = api.MtwmAPI.RetailCatDelete(strStoreID, catName); err != nil {
|
if err = api.MtwmAPI.RetailCatDelete(strStoreID, catName); err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else { // 本地分类有这个名字
|
} else { // 本地分类有这个名字
|
||||||
if localCat.MapID == 0 { // 本地映射没有
|
if localCat.MapID == 0 { // 本地映射没有
|
||||||
@@ -138,6 +149,7 @@ func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao
|
|||||||
defer func() {
|
defer func() {
|
||||||
dao.Rollback(db)
|
dao.Rollback(db)
|
||||||
}()
|
}()
|
||||||
|
num := 0
|
||||||
for i := 0; i < 2; i++ {
|
for i := 0; i < 2; i++ {
|
||||||
for _, v := range catMap[i] {
|
for _, v := range catMap[i] {
|
||||||
if v.MapID == -1 || v.MapID == 0 { // 本地缺失
|
if v.MapID == -1 || v.MapID == 0 { // 本地缺失
|
||||||
@@ -153,6 +165,7 @@ func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao
|
|||||||
EbaiSyncStatus: model.SyncFlagNewMask,
|
EbaiSyncStatus: model.SyncFlagNewMask,
|
||||||
ElmSyncStatus: model.SyncFlagNewMask,
|
ElmSyncStatus: model.SyncFlagNewMask,
|
||||||
}
|
}
|
||||||
|
num++
|
||||||
dao.WrapAddIDCULDEntity(catMap, ctx.GetUserName())
|
dao.WrapAddIDCULDEntity(catMap, ctx.GetUserName())
|
||||||
if err = dao.CreateEntity(db, catMap); err != nil {
|
if err = dao.CreateEntity(db, catMap); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -162,6 +175,7 @@ func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao
|
|||||||
MtwmSyncStatus: model.SyncFlagNewMask,
|
MtwmSyncStatus: model.SyncFlagNewMask,
|
||||||
}
|
}
|
||||||
catMap.ID = v.MapID
|
catMap.ID = v.MapID
|
||||||
|
num++
|
||||||
if _, err = dao.UpdateEntity(db, catMap, "MtwmSyncStatus"); err != nil {
|
if _, err = dao.UpdateEntity(db, catMap, "MtwmSyncStatus"); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -169,7 +183,7 @@ func (p *PurchaseHandler) SyncLocalStoreCategory(ctx *jxcontext.Context, db *dao
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
dao.Commit(db)
|
dao.Commit(db)
|
||||||
return "", err
|
return utils.Int2Str(num), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func TranverseRemoteCatList(parentCatName string, remoteCats []*mtwmapi.RetailCategoryInfo, handler func(level int, parentCatName, catName string) error) (err error) {
|
func TranverseRemoteCatList(parentCatName string, remoteCats []*mtwmapi.RetailCategoryInfo, handler func(level int, parentCatName, catName string) error) (err error) {
|
||||||
@@ -187,8 +201,26 @@ func (p *PurchaseHandler) ReadStoreSku(storeID, skuID int) (skuNameExt *model.Sk
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hint,如果是异步,返回的是任务ID,如果是同步,返回是本次需要同步的目录数
|
||||||
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) {
|
||||||
db := dao.GetDB()
|
db := dao.GetDB()
|
||||||
|
for i := 0; i < 3; i++ { // 最多重试三次
|
||||||
|
if hint, err = p.SyncLocalStoreCategory(ctx, db, storeID, false, 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("同步门店商品所需目录失败")
|
||||||
|
}
|
||||||
|
|
||||||
skus, err := dao.GetStoreSkus(db, model.VendorIDMTWM, storeID, skuIDs)
|
skus, err := dao.GetStoreSkus(db, model.VendorIDMTWM, storeID, skuIDs)
|
||||||
// globals.SugarLogger.Debug(utils.Format4Output(skus, false))
|
// globals.SugarLogger.Debug(utils.Format4Output(skus, false))
|
||||||
strStoreID := utils.Int2Str(storeID)
|
strStoreID := utils.Int2Str(storeID)
|
||||||
@@ -227,7 +259,9 @@ func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasks
|
|||||||
"stock": "*",
|
"stock": "*",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
err = api.MtwmAPI.RetailBatchInitData(strStoreID, foodDataList)
|
if globals.EnableStoreWrite && globals.EnableMtwmStoreWrite {
|
||||||
|
err = api.MtwmAPI.RetailBatchInitData(strStoreID, foodDataList)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
storeSkuBind := &model.StoreSkuBind{}
|
storeSkuBind := &model.StoreSkuBind{}
|
||||||
@@ -242,8 +276,11 @@ func (p *PurchaseHandler) SyncStoreSkus(ctx *jxcontext.Context, parentTask tasks
|
|||||||
rootTask.Run()
|
rootTask.Run()
|
||||||
if !isAsync {
|
if !isAsync {
|
||||||
_, err = rootTask.GetResult(0)
|
_, err = rootTask.GetResult(0)
|
||||||
|
hint = utils.Int2Str(len(skus))
|
||||||
|
} else {
|
||||||
|
hint = rootTask.ID
|
||||||
}
|
}
|
||||||
return rootTask.ID, 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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user