京东活动重构,删除与添加时遇错继续

This commit is contained in:
gazebo
2020-01-03 17:29:42 +08:00
parent e89af0849e
commit 13922ead60

View File

@@ -13,6 +13,7 @@ import (
"git.rosy.net.cn/baseapi/platformapi/jdapi" "git.rosy.net.cn/baseapi/platformapi/jdapi"
"git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/baseapi/utils/errlist"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "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/jxutils/tasksch"
"git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/model"
@@ -242,67 +243,80 @@ func (c *PurchaseHandler) SyncAct(ctx *jxcontext.Context, parentTask tasksch.ITa
var updateItems []*dao.KVUpdateItem var updateItems []*dao.KVUpdateItem
actStoreSkuMap := partner.SplitActStoreSku(actStoreSkuList) actStoreSkuMap := partner.SplitActStoreSku(actStoreSkuList)
actSkuCount := 0
toDelActSkuCount := 0
for storeID := range actStoreSkuMap { for storeID := range actStoreSkuMap {
for _, actStoreSku := range actStoreSkuMap[storeID] { for _, actStoreSku := range actStoreSkuMap[storeID] {
vendorActInfoMap[actStoreSku.VendorActID] = append(vendorActInfoMap[actStoreSku.VendorActID], actStoreSku) vendorActID := actStoreSku.VendorActID
if vendorActID == "" {
vendorActID = act.VendorActID
}
actSkuCount++
vendorActInfoMap[vendorActID] = append(vendorActInfoMap[vendorActID], actStoreSku)
if model.IsSyncStatusDelete(actStoreSku.SyncStatus) { if model.IsSyncStatusDelete(actStoreSku.SyncStatus) {
vendorActID := actStoreSku.VendorActID toDelActSkuCount++
if vendorActID == "" {
vendorActID = act.VendorActID
}
deleteActInfoMap[vendorActID] = append(deleteActInfoMap[vendorActID], actStoreSku) deleteActInfoMap[vendorActID] = append(deleteActInfoMap[vendorActID], actStoreSku)
} else if model.IsSyncStatusNew(actStoreSku.SyncStatus) { } else if model.IsSyncStatusNew(actStoreSku.SyncStatus) {
actStoreSkuList4Create = append(actStoreSkuList4Create, actStoreSku) actStoreSkuList4Create = append(actStoreSkuList4Create, actStoreSku)
} }
} }
} }
// 如果是全删,直接添加删除(即取消)标志
if actSkuCount == toDelActSkuCount {
act.SyncStatus |= model.SyncFlagDeletedMask
}
db := dao.GetDB() db := dao.GetDB()
err = func() (err error) { err = func() (err error) {
if model.IsSyncStatusDelete(act.SyncStatus) { if model.IsSyncStatusDelete(act.SyncStatus) {
errList := errlist.New()
for vendorActID := range vendorActInfoMap { for vendorActID := range vendorActInfoMap {
if vendorActID != "" { if vendorActID != "" {
if err = cancelSkuAct(ctx, act, vendorActID); err != nil { if err = cancelSkuAct(ctx, act, vendorActID); err == nil {
return err updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, vendorActInfoMap[vendorActID], model.SyncFlagModifiedMask)...)
} else {
errList.AddErr(err)
} }
} }
} }
for _, actStoreSkuList := range vendorActInfoMap { if err = errList.GetErrListAsOne(); err == nil {
updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, actStoreSkuList, model.SyncFlagModifiedMask)...) updateItems = append(updateItems, partner.Act2Update(ctx, act, model.SyncFlagModifiedMask))
} }
updateItems = append(updateItems, partner.Act2Update(ctx, act, model.SyncFlagModifiedMask))
} else if model.IsSyncStatusNew(act.SyncStatus) { } else if model.IsSyncStatusNew(act.SyncStatus) {
if act.VendorActID, err = createSkuAct(ctx, act, actStoreSkuList4Create); err != nil { if act.VendorActID, err = createSkuAct(ctx, act, actStoreSkuList4Create); err == nil {
updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, actStoreSkuList4Create, model.SyncFlagNewMask)...)
updateItems = append(updateItems, partner.Act2Update(ctx, act, model.SyncFlagNewMask))
} else {
if act.VendorActID != "" { if act.VendorActID != "" {
actMap := partner.Act2ActMap(act) actMap := partner.Act2ActMap(act)
dao.UpdateEntity(db, actMap, "VendorActID") dao.UpdateEntity(db, actMap, model.FieldVendorActID)
} }
return err
} }
updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, actStoreSkuList4Create, model.SyncFlagNewMask)...)
updateItems = append(updateItems, partner.Act2Update(ctx, act, model.SyncFlagNewMask))
} else if model.IsSyncStatusUpdate(act.SyncStatus) { } else if model.IsSyncStatusUpdate(act.SyncStatus) {
// globals.SugarLogger.Debug(utils.Format4Output(updateItems, false)) errList := errlist.New()
if len(actStoreSkuList4Create) > 0 { if len(actStoreSkuList4Create) > 0 {
if _, err = createSkuAct(ctx, act, actStoreSkuList4Create); err != nil { if _, err = createSkuAct(ctx, act, actStoreSkuList4Create); err == nil {
return err updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, actStoreSkuList4Create, model.SyncFlagNewMask)...)
} else {
errList.AddErr(err)
} }
updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, actStoreSkuList4Create, model.SyncFlagNewMask)...)
} }
for vendorActID := range deleteActInfoMap { for vendorActID := range deleteActInfoMap {
if vendorActID != "" { if vendorActID != "" {
if len(vendorActInfoMap[vendorActID]) == len(deleteActInfoMap[vendorActID]) { if len(vendorActInfoMap[vendorActID]) == len(deleteActInfoMap[vendorActID]) {
// todo 如果这个取消导致了整活动被取消的话,怎么设置京西活动的状态
err = cancelSkuAct(ctx, act, vendorActID) err = cancelSkuAct(ctx, act, vendorActID)
} else { } else {
err = cancelSkuActSkus(ctx, act, vendorActID, deleteActInfoMap[vendorActID]) err = cancelSkuActSkus(ctx, act, vendorActID, deleteActInfoMap[vendorActID])
} }
if err != nil { if err == nil {
return err updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, deleteActInfoMap[vendorActID], model.SyncFlagDeletedMask)...)
} else {
errList.AddErr(err)
} }
} else {
updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, deleteActInfoMap[vendorActID], model.SyncFlagDeletedMask)...)
} }
updateItems = append(updateItems, partner.ActStoreSku2Update(ctx, deleteActInfoMap[vendorActID], model.SyncFlagDeletedMask)...)
} }
if err == nil { if err = errList.GetErrListAsOne(); err == nil {
updateItems = append(updateItems, partner.Act2Update(ctx, act, model.SyncFlagModifiedMask)) updateItems = append(updateItems, partner.Act2Update(ctx, act, model.SyncFlagModifiedMask))
} }
} }