- 整理sync.go

This commit is contained in:
gazebo
2019-08-16 14:12:11 +08:00
parent 9a5bde9d09
commit c27d610fbb
4 changed files with 32 additions and 43 deletions

View File

@@ -3,7 +3,6 @@ package cms
import (
"errors"
"fmt"
"reflect"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
@@ -13,7 +12,6 @@ import (
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"git.rosy.net.cn/jx-callback/globals/refutil"
)
@@ -23,9 +21,6 @@ type LoopStoreMapInfo struct {
}
type VendorSync struct {
MultiStoreVendorIDs []int
SingleStoreVendorIDs []int
PurchaseHandlers map[int]partner.IPurchasePlatformHandler
}
type SyncError struct {
@@ -52,34 +47,6 @@ var (
ErrEntityNotExist = errors.New("找不到相应实体")
)
func Init() {
apiMap := map[int]interface{}{
model.VendorIDJD: api.JdAPI,
model.VendorIDELM: api.ElmAPI,
model.VendorIDEBAI: api.EbaiAPI,
model.VendorIDMTWM: api.MtwmAPI,
model.VendorIDWSC: api.WeimobAPI,
}
CurVendorSync.PurchaseHandlers = make(map[int]partner.IPurchasePlatformHandler)
for k, v := range partner.PurchasePlatformHandlers {
if !reflect.ValueOf(apiMap[k]).IsNil() {
if multiHandler, ok := v.(partner.IMultipleStoresHandler); ok {
CurVendorSync.MultiStoreVendorIDs = append(CurVendorSync.MultiStoreVendorIDs, k)
CurVendorSync.PurchaseHandlers[k] = &MultiStoreHandlerWrapper{
IMultipleStoresHandler: multiHandler,
}
} else if singleHandler, ok := v.(partner.ISingleStoreHandler); ok {
CurVendorSync.SingleStoreVendorIDs = append(CurVendorSync.SingleStoreVendorIDs, k)
CurVendorSync.PurchaseHandlers[k] = &SingleStoreHandlerWrapper{
ISingleStoreHandler: singleHandler,
}
} else {
panic(fmt.Sprintf("platform:%d type is wrong!", k))
}
}
}
}
func (p *MultiStoreHandlerWrapper) DeleteCategory(db *dao.DaoDB, cat *model.SkuCategory, userName string) (err error) {
if jxutils.IsEmptyID(cat.JdID) {
return nil
@@ -112,18 +79,18 @@ func (p *MultiStoreHandlerWrapper) UpdateSku(db *dao.DaoDB, sku *model.Sku, user
}
func (v *VendorSync) GetStoreHandler(vendorID int) partner.IPurchasePlatformHandler {
return v.PurchaseHandlers[vendorID]
return partner.GetPurchasePlatformFromVendorID(vendorID)
}
func (v *VendorSync) GetMultiStoreHandler(vendorID int) partner.IMultipleStoresHandler {
if handler, ok := v.PurchaseHandlers[vendorID].(partner.IMultipleStoresHandler); ok {
if handler, ok := v.GetStoreHandler(vendorID).(partner.IMultipleStoresHandler); ok {
return handler
}
return nil
}
func (v *VendorSync) GetSingleStoreHandler(vendorID int) partner.ISingleStoreHandler {
if handler, ok := v.PurchaseHandlers[vendorID].(partner.ISingleStoreHandler); ok {
if handler, ok := v.GetStoreHandler(vendorID).(partner.ISingleStoreHandler); ok {
return handler
}
return nil
@@ -162,7 +129,7 @@ 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) {
globals.SugarLogger.Debug(v.MultiStoreVendorIDs)
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) {
vendorID := batchItemList[0].(int)
@@ -595,7 +562,7 @@ func (v *VendorSync) LoopStoresMap(ctx *jxcontext.Context, db *dao.DaoDB, taskNa
}
func (v *VendorSync) LoopMultiStoresVendors(ctx *jxcontext.Context, db *dao.DaoDB, taskName string, isAsync bool, isManageIt bool, handler tasksch.WorkFunc) (hint string, err error) {
task := tasksch.NewParallelTask(taskName, tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, handler, v.MultiStoreVendorIDs)
task := tasksch.NewParallelTask(taskName, tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, handler, partner.GetMultiStoreVendorIDs())
tasksch.HandleTask(task, nil, isManageIt).Run()
if !isAsync {
result, err2 := task.GetResult(0)