- putils/store_sku.go用于一些通用实现

This commit is contained in:
gazebo
2019-07-13 15:31:30 +08:00
parent 01c522b5b0
commit f31b158259
10 changed files with 177 additions and 203 deletions

View File

@@ -0,0 +1,102 @@
package putils
import (
"fmt"
"sort"
"git.rosy.net.cn/jx-callback/globals"
"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/partner"
)
type DefSingleStorePlatform struct {
partner.ISingleStoreStoreSkuHandler
}
func (p *DefSingleStorePlatform) DeleteStoreAllSkus(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, isContinueWhenError bool) (err error) {
skuNameList, err := p.GetStoreSkusFullInfo(ctx, parentTask, storeID, vendorStoreID, nil)
if err != nil {
return err
}
batchSize := p.GetStoreSkusBatchSize(partner.FuncDeleteStoreSkus)
task := tasksch.NewParallelTask(fmt.Sprintf("DeleteStoreAllSkus, vendorStoreID:%s", vendorStoreID),
tasksch.NewParallelConfig().SetBatchSize(batchSize).SetIsContinueWhenError(isContinueWhenError), ctx,
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
var skuList []*partner.StoreSkuInfo
for _, v := range batchItemList {
skuName := v.(*partner.SkuNameInfo)
skuList = append(skuList, &partner.StoreSkuInfo{
VendorSkuID: skuName.SkuList[0].VendorSkuID,
})
}
err = p.DeleteStoreSkus(ctx, storeID, vendorStoreID, skuList)
return nil, err
}, skuNameList)
tasksch.HandleTask(task, parentTask, true).Run()
_, err = task.GetResult(0)
return err
}
func (p *DefSingleStorePlatform) DeleteStoreAllCategories(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, isContinueWhenError bool) (err error) {
catList, err := p.GetStoreAllCategories(ctx, storeID, vendorStoreID)
if err != nil {
return err
}
catListMap := make(map[int][]*partner.BareCategoryInfo)
flattedCatList := flatCatList(catList)
for _, v := range flattedCatList {
catListMap[v.Level] = append(catListMap[v.Level], v)
}
var levelList []int
for k := range catListMap {
levelList = append(levelList, k)
}
sort.Sort(sort.Reverse(sort.IntSlice(levelList)))
task1 := tasksch.NewSeqTask(fmt.Sprintf("DeleteStoreAllCategories1, vendorStoreID:%s", vendorStoreID), ctx,
func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
task2 := tasksch.NewParallelTask(fmt.Sprintf("DeleteStoreAllCategories2, vendorStoreID:%s", vendorStoreID), tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
catInfo := batchItemList[0].(*partner.BareCategoryInfo)
err = p.DeleteStoreCategory(ctx, storeID, vendorStoreID, catInfo.VendorCatID)
return nil, err
}, catListMap[levelList[step]])
tasksch.HandleTask(task2, task, true).Run()
_, err = task2.GetResult(0)
return nil, err
}, len(levelList))
tasksch.HandleTask(task1, parentTask, true).Run()
_, err = task1.GetResult(0)
return err
}
func flatCatList(catList []*partner.BareCategoryInfo) (flattedCatList []*partner.BareCategoryInfo) {
flattedCatList = append(flattedCatList, catList...)
for _, v := range catList {
flattedCatList = append(flattedCatList, flatCatList(v.Children)...)
}
return flattedCatList
}
func (p *DefSingleStorePlatform) GetStoreSkusBareInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, inStoreSkuList []*partner.StoreSkuInfo) (outStoreSkuList []*partner.StoreSkuInfo, err error) {
var skuIDs []int
isSingle := len(inStoreSkuList) == 1
if isSingle {
skuIDs = []int{inStoreSkuList[0].SkuID}
}
globals.SugarLogger.Debug(p)
skuNameList, err := p.GetStoreSkusFullInfo(ctx, parentTask, storeID, vendorStoreID, skuIDs)
if err != nil {
return nil, err
}
for _, skuName := range skuNameList {
storeSkuBareInfo := &skuName.SkuList[0].StoreSkuInfo
if isSingle {
storeSkuBareInfo.VendorNameID = inStoreSkuList[0].VendorNameID
storeSkuBareInfo.VendorSkuID = inStoreSkuList[0].VendorSkuID
}
outStoreSkuList = append(outStoreSkuList, storeSkuBareInfo)
}
return outStoreSkuList, err
}