同步错误返回解决冲突

This commit is contained in:
苏尹岚
2019-12-12 18:10:13 +08:00
124 changed files with 4979 additions and 2028 deletions

View File

@@ -60,11 +60,6 @@ type SingleStoreHandlerWrapper struct {
partner.ISingleStoreHandler
}
type tMultiStoreVendorInfo struct {
VendorID int
OrgCode string
}
var (
CurVendorSync VendorSync
)
@@ -134,20 +129,6 @@ func (v *VendorSync) GetSingleStoreHandler(vendorID int) partner.ISingleStoreHan
return nil
}
func getMultiStoreVendorInfoList() (list []*tMultiStoreVendorInfo) {
vendorIDs := partner.GetMultiStoreVendorIDs()
for _, vendorID := range vendorIDs {
orgCodeList := partner.CurAPIManager.GetAppOrgCodeList(vendorID)
for _, v := range orgCodeList {
list = append(list, &tMultiStoreVendorInfo{
VendorID: vendorID,
OrgCode: v,
})
}
}
return list
}
func (v *VendorSync) syncCategories(ctx *jxcontext.Context, parentTask tasksch.ITask, vendorID int, db *dao.DaoDB, cats []*model.SkuCategory, userName string) (err error) {
multiStoresHandler := v.GetMultiStoreHandler(vendorID)
syncStatusFieldName := dao.GetSyncStatusStructField(model.VendorNames[vendorID])
@@ -181,10 +162,17 @@ func (v *VendorSync) syncCategories(ctx *jxcontext.Context, parentTask tasksch.I
}
func (v *VendorSync) SyncCategory(ctx *jxcontext.Context, db *dao.DaoDB, categoryID int, isAsync bool, userName string) (hint string, err error) {
if !globals.IsUseThingMap {
return v.oldSyncCategory(ctx, db, categoryID, isAsync, userName)
}
return SyncCategories(ctx, nil, nil, nil, []int{categoryID}, isAsync)
}
func (v *VendorSync) oldSyncCategory(ctx *jxcontext.Context, db *dao.DaoDB, categoryID int, isAsync bool, userName string) (hint string, err error) {
globals.SugarLogger.Debug("SyncCategory")
hint, err = v.LoopMultiStoresVendors(ctx, db, fmt.Sprintf("同步分类信息:%d", categoryID), isAsync, false,
func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (interface{}, error) {
vendorInfo := batchItemList[0].(*tMultiStoreVendorInfo)
vendorInfo := batchItemList[0].(*MultiStoreVendorInfo)
var cats []*model.SkuCategory
cond := make(map[string]interface{})
if categoryID > 0 {
@@ -210,8 +198,15 @@ func (v *VendorSync) SyncCategory(ctx *jxcontext.Context, db *dao.DaoDB, categor
}
func (v *VendorSync) SyncReorderCategories(ctx *jxcontext.Context, db *dao.DaoDB, categoryID int, isAsync bool, userName string) (hint string, err error) {
if !globals.IsUseThingMap {
return v.oldSyncReorderCategories(ctx, db, categoryID, isAsync, userName)
}
return SyncReorderCategories(ctx, -1, isAsync)
}
func (v *VendorSync) oldSyncReorderCategories(ctx *jxcontext.Context, db *dao.DaoDB, categoryID int, isAsync bool, userName string) (hint string, err error) {
hint, err = v.LoopMultiStoresVendors(ctx, db, fmt.Sprintf("分类重排序:%d", categoryID), isAsync, false, func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (interface{}, error) {
vendorInfo := batchItemList[0].(*tMultiStoreVendorInfo)
vendorInfo := batchItemList[0].(*MultiStoreVendorInfo)
multiStoresHandler := v.GetMultiStoreHandler(vendorInfo.VendorID)
err2 := multiStoresHandler.ReorderCategories(db, categoryID, userName)
if err2 == nil {
@@ -226,7 +221,6 @@ func (v *VendorSync) SyncReorderCategories(ctx *jxcontext.Context, db *dao.DaoDB
func (v *VendorSync) SyncStore(ctx *jxcontext.Context, db *dao.DaoDB, vendorID, storeID int, isAsync bool, userName string) (hint string, err error) {
globals.SugarLogger.Debugf("SyncStore, storeID:%d", storeID)
var vendorIDs []int
if vendorID != -1 {
vendorIDs = []int{
@@ -236,35 +230,38 @@ func (v *VendorSync) SyncStore(ctx *jxcontext.Context, db *dao.DaoDB, vendorID,
hint, err = v.LoopStoresMap(ctx, db, fmt.Sprintf("同步门店信息:%d", storeID), isAsync, false, vendorIDs, []int{storeID}, func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (resultList interface{}, err error) {
loopMapInfo := batchItemList[0].(*LoopStoreMapInfo)
handler := v.GetStoreHandler(loopMapInfo.VendorID)
if len(loopMapInfo.StoreMapList) > 1 {
loopStoreTask := tasksch.NewParallelTask(fmt.Sprintf("处理平台%s", model.VendorChineseNames[loopMapInfo.VendorID]), nil, ctx,
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
var resultList []interface{}
storeMap := batchItemList[0].(*model.StoreMap)
db2 := db
if len(loopMapInfo.StoreMapList) > 1 {
db2 = dao.GetDB()
}
if err = handler.UpdateStore(db2, storeMap.StoreID, userName); err == nil {
storeMap.SyncStatus = 0
_, err = dao.UpdateEntity(db2, storeMap, model.FieldSyncStatus)
resultList = append(resultList, 1)
}
return resultList, err
}, loopMapInfo.StoreMapList)
t.AddChild(loopStoreTask).Run()
resultList, err = loopStoreTask.GetResult(0)
} else {
storeMap := loopMapInfo.StoreMapList[0]
if err = handler.UpdateStore(db, storeMap.StoreID, userName); err == nil {
storeMap.SyncStatus = 0
_, err = dao.UpdateEntity(db, storeMap, model.FieldSyncStatus)
}
if err == nil {
resultList = []interface{}{1}
if handler != nil {
if len(loopMapInfo.StoreMapList) > 1 {
loopStoreTask := tasksch.NewParallelTask(fmt.Sprintf("处理平台%s", model.VendorChineseNames[loopMapInfo.VendorID]), nil, ctx,
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
var resultList []interface{}
storeMap := batchItemList[0].(*model.StoreMap)
db2 := db
if len(loopMapInfo.StoreMapList) > 1 {
db2 = dao.GetDB()
}
if err = handler.UpdateStore(db2, storeMap.StoreID, userName); err == nil {
storeMap.SyncStatus = 0
_, err = dao.UpdateEntity(db2, storeMap, model.FieldSyncStatus)
resultList = append(resultList, 1)
}
return resultList, err
}, loopMapInfo.StoreMapList)
t.AddChild(loopStoreTask).Run()
resultList, err = loopStoreTask.GetResult(0)
} else {
storeMap := loopMapInfo.StoreMapList[0]
if err = handler.UpdateStore(db, storeMap.StoreID, userName); err == nil {
storeMap.SyncStatus = 0
_, err = dao.UpdateEntity(db, storeMap, model.FieldSyncStatus)
}
if err == nil {
resultList = []interface{}{1}
}
}
err = partner.AddVendorInfo2Err(err, loopMapInfo.VendorID)
}
return resultList, partner.AddVendorInfo2Err(err, loopMapInfo.VendorID)
return resultList, err
}, true)
return hint, makeSyncError(err)
}
@@ -284,12 +281,19 @@ func (v *VendorSync) SyncSku(ctx *jxcontext.Context, db *dao.DaoDB, nameID, skuI
}
func (v *VendorSync) SyncSkus(ctx *jxcontext.Context, db *dao.DaoDB, nameIDs []int, skuIDs []int, isAsync, isContinueWhenError bool, userName string) (hint string, err error) {
if !globals.IsUseThingMap {
return v.oldSyncSkus(ctx, db, nameIDs, skuIDs, isAsync, isContinueWhenError, userName)
}
return SyncSkus(ctx, nil, nil, nil, nameIDs, skuIDs, isAsync)
}
func (v *VendorSync) oldSyncSkus(ctx *jxcontext.Context, db *dao.DaoDB, nameIDs []int, skuIDs []int, isAsync, isContinueWhenError bool, userName string) (hint string, err error) {
globals.SugarLogger.Debugf("SyncSku trackInfo:%s, nameIDs:%v, skuIDs:%v, userName:%s", ctx.GetTrackInfo(), nameIDs, skuIDs, userName)
isManagedIt := !(len(nameIDs) > 0 && len(nameIDs) <= 2 || len(skuIDs) > 0 && len(skuIDs) < 8)
return v.LoopMultiStoresVendors(ctx, db, fmt.Sprintf("同步商品信息, nameIDs:%v, skuIDs:%v", nameIDs, skuIDs), isAsync, isManagedIt,
func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (interface{}, error) {
var resultList []interface{}
vendorInfo := batchItemList[0].(*tMultiStoreVendorInfo)
vendorInfo := batchItemList[0].(*MultiStoreVendorInfo)
multiStoresHandler := v.GetMultiStoreHandler(vendorInfo.VendorID)
syncStatusFieldName := dao.GetSyncStatusStructField(model.VendorNames[multiStoresHandler.GetVendorID()])
dbField := dao.ConvertDBFieldPrefix(model.VendorNames[multiStoresHandler.GetVendorID()])
@@ -463,11 +467,11 @@ func (v *VendorSync) proxySyncStoreSku(ctx *jxcontext.Context, parentTask tasksc
return SyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, nil, skuIDs, excludeSkuIDs, isAsync, isContinueWhenError)
}
func (v *VendorSync) proxyFullSyncStoreSku(ctx *jxcontext.Context, parentTask tasksch.ITask, storeMap *model.StoreMap, isAsync, isContinueWhenError bool) (hint string, err error) {
return FullSyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, isAsync, isContinueWhenError)
func (v *VendorSync) proxyFullSyncStoreSku(ctx *jxcontext.Context, parentTask tasksch.ITask, storeMap *model.StoreMap, excludeSkuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
return FullSyncStoreSkuNew(ctx, parentTask, storeMap.VendorID, storeMap.StoreID, storeMap.VendorStoreID, excludeSkuIDs, isAsync, isContinueWhenError)
}
func (v *VendorSync) FullSyncStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, vendorIDs []int, storeIDs []int, syncDisabled, isAsync, isContinueWhenError bool) (hint string, err error) {
func (v *VendorSync) FullSyncStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, vendorIDs []int, storeIDs []int, syncDisabled bool, excludeSkuIDs []int, isAsync, isContinueWhenError bool) (hint string, err error) {
globals.SugarLogger.Debug("FullSyncStoresSkus")
hint, err = v.LoopStoresMap(ctx, db, fmt.Sprintf("初始化门店商品信息:%v", storeIDs), isAsync, true, vendorIDs, storeIDs,
func(t *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (interface{}, error) {
@@ -482,7 +486,7 @@ func (v *VendorSync) FullSyncStoresSkus(ctx *jxcontext.Context, db *dao.DaoDB, v
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
storeMap := batchItemList[0].(*model.StoreMap)
if syncDisabled || storeMap.Status > model.StoreStatusDisabled {
if _, err = v.proxyFullSyncStoreSku(ctx, task, storeMap, false, isContinueWhenError); err != nil {
if _, err = v.proxyFullSyncStoreSku(ctx, task, storeMap, excludeSkuIDs, false, isContinueWhenError); err != nil {
globals.SugarLogger.Debugf("FullSyncStoresSkus failed2 store:%d failed with error:%v", storeMap.StoreID, err)
}
}
@@ -592,6 +596,7 @@ func (v *VendorSync) LoopStoresMap2(ctx *jxcontext.Context, db *dao.DaoDB, taskN
if len(loopInfoList) == 1 {
taskName = fmt.Sprintf("%s,处理平台%s", taskName, model.VendorChineseNames[loopInfoList[0].VendorID])
}
// globals.SugarLogger.Debugf("LoopStoresMap2 loopInfoList:%s", utils.Format4Output(loopInfoList, false))
task = tasksch.NewParallelTask(taskName, tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, handler, loopInfoList)
task.SetFinishHook(func(task tasksch.ITask, ctx *jxcontext.Context) {
err = WirteToExcelBySyncFailed(task, ctx)
@@ -688,24 +693,16 @@ func isSyncError(err error) bool {
}
func (v *VendorSync) SyncSkuNames(ctx *jxcontext.Context, nameIDs []int, isForce, isAsync, isContinueWhenError bool) (hint string, err error) {
if !globals.IsUseThingMap {
return v.oldSyncSkuNames(ctx, nameIDs, isForce, isAsync, isContinueWhenError)
}
return SyncSkus(ctx, nil, nil, nil, nameIDs, nil, isAsync)
}
func (v *VendorSync) oldSyncSkuNames(ctx *jxcontext.Context, nameIDs []int, isForce, isAsync, isContinueWhenError bool) (hint string, err error) {
db := dao.GetDB()
if isForce {
sql := `
UPDATE sku t1
SET t1.jd_sync_status = t1.jd_sync_status | ?
WHERE t1.deleted_at = ?
`
sqlParams := []interface{}{
model.SyncFlagModifiedMask,
utils.DefaultTimeValue,
}
if len(nameIDs) > 0 {
sql += " AND t1.name_id IN(" + dao.GenQuestionMarks(len(nameIDs)) + ")"
sqlParams = append(sqlParams, nameIDs)
}
if _, err = dao.ExecuteSQL(db, sql, sqlParams...); err != nil {
return "", err
}
dao.SetSkuNameSyncStatus(db, nil, nil, nameIDs, model.SyncFlagModifiedMask)
}
return v.SyncSkus(ctx, db, nameIDs, nil, isAsync, isContinueWhenError, ctx.GetUserName())
}
@@ -806,6 +803,7 @@ func (d *SyncErrResultLock) AppendData(syncErrResult SyncErrResult) {
defer d.locker.Unlock()
d.syncErrResult = append(d.syncErrResult, syncErrResult)
}
func OnCreateThing(db *dao.DaoDB, thingID int64, thingType int8) (err error) {
return err
}