249 lines
9.6 KiB
Go
249 lines
9.6 KiB
Go
package putils
|
||
|
||
import (
|
||
"fmt"
|
||
"sort"
|
||
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"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/model/dao"
|
||
"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 || len(skuNameList) == 0 {
|
||
return err
|
||
}
|
||
storeStoreList := make([]*partner.StoreSkuInfo, len(skuNameList))
|
||
for k, v := range skuNameList {
|
||
storeStoreList[k] = &partner.StoreSkuInfo{
|
||
SkuID: v.SkuList[0].SkuID,
|
||
VendorSkuID: v.SkuList[0].VendorSkuID,
|
||
}
|
||
}
|
||
_, err = FreeBatchStoreSkuInfo(func(batchedStoreSkuList []*partner.StoreSkuInfo) (result interface{}, err error) {
|
||
_, err = p.DeleteStoreSkus(ctx, storeID, vendorStoreID, batchedStoreSkuList)
|
||
return nil, err
|
||
}, ctx, parentTask, storeStoreList, p.GetStoreSkusBatchSize(partner.FuncDeleteStoreSkus), isContinueWhenError)
|
||
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) {
|
||
vendorCatIDs := make([]string, len(catListMap[levelList[step]]))
|
||
for k, v := range catListMap[levelList[step]] {
|
||
vendorCatIDs[k] = v.VendorCatID
|
||
}
|
||
err = FreeBatchCategoryIDOp(func(vendorCatID string) (err error) {
|
||
return p.DeleteStoreCategory(ctx, storeID, vendorStoreID, vendorCatID)
|
||
}, ctx, task, vendorCatIDs, isContinueWhenError)
|
||
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) {
|
||
resultList, err := FreeBatchStoreSkuInfo(func(batchedStoreSkuList []*partner.StoreSkuInfo) (result interface{}, err error) {
|
||
return p.GetStoreSkusFullInfo(ctx, parentTask, storeID, vendorStoreID, batchedStoreSkuList)
|
||
}, ctx, parentTask, inStoreSkuList, p.GetStoreSkusBatchSize(partner.FuncGetStoreSkusFullInfo), true)
|
||
if err != nil || len(resultList) == 0 {
|
||
return nil, err
|
||
}
|
||
inStoreSkuMap := make(map[string]*partner.StoreSkuInfo)
|
||
for _, v := range inStoreSkuList {
|
||
if v.VendorSkuID != "" {
|
||
inStoreSkuMap[v.VendorNameID] = v
|
||
}
|
||
}
|
||
for _, v := range resultList {
|
||
skuName := v.(*partner.SkuNameInfo)
|
||
storeSkuBareInfo := &skuName.SkuList[0].StoreSkuInfo
|
||
if storeSkuBareInfo.SkuID == 0 && inStoreSkuMap[storeSkuBareInfo.VendorSkuID] != nil {
|
||
storeSkuBareInfo.SkuID = inStoreSkuMap[storeSkuBareInfo.VendorSkuID].SkuID
|
||
storeSkuBareInfo.NameID = inStoreSkuMap[storeSkuBareInfo.VendorSkuID].NameID
|
||
}
|
||
outStoreSkuList = append(outStoreSkuList, storeSkuBareInfo)
|
||
}
|
||
return outStoreSkuList, err
|
||
}
|
||
|
||
func findCategoryByName(catList []*partner.BareCategoryInfo, catName string) (cat *partner.BareCategoryInfo) {
|
||
for _, v := range catList {
|
||
if v.Name == catName {
|
||
cat = v
|
||
break
|
||
}
|
||
if cat = findCategoryByName(v.Children, catName); cat != nil {
|
||
break
|
||
}
|
||
}
|
||
return cat
|
||
}
|
||
func (p *DefSingleStorePlatform) GetStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID, catName string) (cat *partner.BareCategoryInfo, err error) {
|
||
catList, err := p.GetStoreAllCategories(ctx, storeID, vendorStoreID)
|
||
if err == nil {
|
||
if cat = findCategoryByName(catList, catName); cat == nil {
|
||
err = fmt.Errorf("门店:%d,%s不能找到商家分类:%s", storeID, vendorStoreID, catName)
|
||
}
|
||
}
|
||
return cat, err
|
||
}
|
||
|
||
func FreeBatchStoreSkuInfo(handler func([]*partner.StoreSkuInfo) (interface{}, error), ctx *jxcontext.Context, parentTask tasksch.ITask, storeSkuList []*partner.StoreSkuInfo, batchSize int, isContinueWhenError bool) (resultList []interface{}, err error) {
|
||
if len(storeSkuList) > batchSize {
|
||
task := tasksch.NewParallelTask("FreeBatchStoreSkuInfo", tasksch.NewParallelConfig().SetBatchSize(batchSize).SetIsContinueWhenError(isContinueWhenError), ctx,
|
||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||
batchStoreSkuList := make([]*partner.StoreSkuInfo, len(batchItemList))
|
||
for k, v := range batchItemList {
|
||
batchStoreSkuList[k] = v.(*partner.StoreSkuInfo)
|
||
}
|
||
retVal, err = handler(batchStoreSkuList)
|
||
if err != nil {
|
||
retVal = nil
|
||
}
|
||
return retVal, err
|
||
}, storeSkuList)
|
||
tasksch.HandleTask(task, parentTask, false).Run()
|
||
resultList, err = task.GetResult(0)
|
||
} else {
|
||
result, err2 := handler(storeSkuList)
|
||
if err = err2; err == nil {
|
||
resultList = utils.Interface2Slice(result)
|
||
}
|
||
}
|
||
return resultList, err
|
||
}
|
||
|
||
func FreeBatchStoreSkuSyncInfo(handler func([]*dao.StoreSkuSyncInfo) (interface{}, error), ctx *jxcontext.Context, parentTask tasksch.ITask, storeSkuList []*dao.StoreSkuSyncInfo, batchSize int, isContinueWhenError bool) (resultList []interface{}, err error) {
|
||
if len(storeSkuList) > batchSize {
|
||
task := tasksch.NewParallelTask("FreeBatchStoreSkuSyncInfo", tasksch.NewParallelConfig().SetBatchSize(batchSize).SetIsContinueWhenError(isContinueWhenError), ctx,
|
||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||
batchStoreSkuList := make([]*dao.StoreSkuSyncInfo, len(batchItemList))
|
||
for k, v := range batchItemList {
|
||
batchStoreSkuList[k] = v.(*dao.StoreSkuSyncInfo)
|
||
}
|
||
retVal, err = handler(batchStoreSkuList)
|
||
if err != nil {
|
||
retVal = nil
|
||
}
|
||
return retVal, err
|
||
}, storeSkuList)
|
||
tasksch.HandleTask(task, parentTask, false).Run()
|
||
resultList, err = task.GetResult(0)
|
||
} else {
|
||
result, err2 := handler(storeSkuList)
|
||
if err = err2; err == nil {
|
||
resultList = utils.Interface2Slice(result)
|
||
}
|
||
}
|
||
return resultList, err
|
||
}
|
||
|
||
func FreeBatchCategoryIDOp(handler func(vendorCatID string) (err error), ctx *jxcontext.Context, parentTask tasksch.ITask, vendorCatIDs []string, isContinueWhenError bool) (err error) {
|
||
if len(vendorCatIDs) > 1 {
|
||
task := tasksch.NewParallelTask("FreeBatchCategoryIDOp", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx,
|
||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||
err = handler(batchItemList[0].(string))
|
||
return nil, err
|
||
}, vendorCatIDs)
|
||
tasksch.HandleTask(task, parentTask, false).Run()
|
||
_, err = task.GetResult(0)
|
||
} else if len(vendorCatIDs) > 0 {
|
||
err = handler(vendorCatIDs[0])
|
||
}
|
||
return err
|
||
}
|
||
|
||
func StoreSkuList2MapByVendorSkuID(storeSkuList []*partner.StoreSkuInfo) (storeSkuMap map[string]*partner.StoreSkuInfo) {
|
||
storeSkuMap = make(map[string]*partner.StoreSkuInfo)
|
||
for _, v := range storeSkuList {
|
||
storeSkuMap[v.VendorSkuID] = v
|
||
}
|
||
return storeSkuMap
|
||
}
|
||
|
||
func StoreSkuList2MapBySkuID(storeSkuList []*partner.StoreSkuInfo) (storeSkuMap map[int]*partner.StoreSkuInfo) {
|
||
storeSkuMap = make(map[int]*partner.StoreSkuInfo)
|
||
for _, v := range storeSkuList {
|
||
storeSkuMap[v.SkuID] = v
|
||
}
|
||
return storeSkuMap
|
||
}
|
||
|
||
func UnselectStoreSkuListByVendorSkuIDs(storeSkuList []*partner.StoreSkuInfo, vendorSkuIDs []string) (selectedStoreSkuList []*partner.StoreSkuInfo) {
|
||
if len(vendorSkuIDs) > 0 {
|
||
storeSkuMap := StoreSkuList2MapByVendorSkuID(storeSkuList)
|
||
for _, v := range vendorSkuIDs {
|
||
if storeSkuMap[v] != nil {
|
||
selectedStoreSkuList = append(selectedStoreSkuList, storeSkuMap[v])
|
||
}
|
||
}
|
||
}
|
||
return selectedStoreSkuList
|
||
}
|
||
|
||
func UnselectStoreSkuSyncListByVendorSkuIDs(storeSkuList []*dao.StoreSkuSyncInfo, vendorSkuIDs []string) (selectedStoreSkuList []*dao.StoreSkuSyncInfo) {
|
||
if len(vendorSkuIDs) > 0 {
|
||
storeSkuMap := make(map[string]*dao.StoreSkuSyncInfo)
|
||
for _, v := range storeSkuList {
|
||
storeSkuMap[v.VendorSkuID] = v
|
||
}
|
||
for _, v := range vendorSkuIDs {
|
||
if storeSkuMap[v] != nil {
|
||
selectedStoreSkuList = append(selectedStoreSkuList, storeSkuMap[v])
|
||
}
|
||
}
|
||
}
|
||
return selectedStoreSkuList
|
||
}
|
||
|
||
func UnselectStoreSkuListBySkuIDs(storeSkuList []*partner.StoreSkuInfo, skuIDs []int) (selectedStoreSkuList []*partner.StoreSkuInfo) {
|
||
if len(skuIDs) > 0 {
|
||
storeSkuMap := StoreSkuList2MapBySkuID(storeSkuList)
|
||
for _, v := range skuIDs {
|
||
if storeSkuMap[v] != nil {
|
||
selectedStoreSkuList = append(selectedStoreSkuList, storeSkuMap[v])
|
||
}
|
||
}
|
||
}
|
||
return selectedStoreSkuList
|
||
}
|
||
|
||
func GenPartialFailedErr(failedInfo interface{}, failedCount int) (err error) {
|
||
if failedCount > 0 {
|
||
err = fmt.Errorf("部分失败了%d个:%s", failedCount, utils.Format4Output(failedInfo, true))
|
||
}
|
||
return err
|
||
}
|