引入ants(正式服已go get)携程池 已支持多店铺批量操作

This commit is contained in:
807875765@qq.com
2022-03-15 11:47:46 +08:00
parent 39a3a636db
commit 788755c2a3
2 changed files with 47 additions and 10 deletions

View File

@@ -2992,7 +2992,7 @@ func GetVendorStoreSkusToStruct(ctx *jxcontext.Context, storeID, vendorID int) (
if len(tmpList) == 0 { //数据库中无匹配项 直接将第三方数据进行导出
goto creatExec
} else { //进行比较
if result := ComPareLocalWithVendors(tmpList, vendorStoreSkus); result != nil {
if result := ComPareLocalWithVendors(tmpList, vendorStoreSkus, vendorID); result != nil {
// 将返回的切片进行覆盖
vendorStoreSkus = result
goto creatExec
@@ -3064,7 +3064,7 @@ func GetLocalStoreSkusByStoreID(storeID int) (tmp []*tGetStoresSkusInfoContainCa
}
// 比较本地和第三方平台的差异 返回一个Excel 可接收实体
func ComPareLocalWithVendors(localSku []*tGetStoresSkusInfoContainCategoryName, vendorSkus []*VendorStoreSkus) (result []*VendorStoreSkus) {
func ComPareLocalWithVendors(localSku []*tGetStoresSkusInfoContainCategoryName, vendorSkus []*VendorStoreSkus, vendorID int) (result []*VendorStoreSkus) {
// 进行取参比较
var localStoreSkus []*VendorStoreSkus
var updateStoreSkus []*VendorStoreSkus
@@ -3074,8 +3074,8 @@ func ComPareLocalWithVendors(localSku []*tGetStoresSkusInfoContainCategoryName,
SkuID: sku.StoreSkuExt.SkuID,
SkuName: sku.SkuName.Name,
Status: sku.Status,
CategoryList: jxStatusTransForm(sku.CategoryName), //此处需要使用函数进行转换 进行查询根据类别ID去sku_category查询返回一个子类别放入List中
Price: vendorPriceTransForm(&sku.StoreSkuExt, 1), //此处需要使用函数转换传入一个VendorID进行比较根据ID的不同返回对应不同的字段
CategoryList: jxStatusTransForm(sku.CategoryName), //此处需要使用函数进行转换 进行查询根据类别ID去sku_category查询返回一个子类别放入List中
Price: vendorPriceTransForm(&sku.StoreSkuExt, vendorID), //此处需要使用函数转换传入一个VendorID进行比较根据ID的不同返回对应不同的字段
Reason: defaultReason,
}
localStoreSkus = append(localStoreSkus, skus)
@@ -3108,11 +3108,13 @@ func ComPareLocalWithVendors(localSku []*tGetStoresSkusInfoContainCategoryName,
buffer.WriteString("商品上下架状态不一致")
count++
}
if value.CategoryList[0] != vendorSku.CategoryList[len(vendorSku.CategoryList)-1] {
buffer.WriteString(strconv.Itoa(count))
buffer.WriteString(".")
buffer.WriteString("类别不一致:平台类别为" + vendorSku.CategoryList[len(vendorSku.CategoryList)-1])
count++
if vendorSku.CategoryList != nil { // Ebai 同步过得商品中的类别中都是nil
if value.CategoryList[0] != vendorSku.CategoryList[len(vendorSku.CategoryList)-1] {
buffer.WriteString(strconv.Itoa(count))
buffer.WriteString(".")
buffer.WriteString("类别不一致:平台类别为" + vendorSku.CategoryList[len(vendorSku.CategoryList)-1])
count++
}
}
value.Reason = buffer.String()
}

View File

@@ -2,8 +2,11 @@ package controllers
import (
"fmt"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/globals"
"github.com/panjf2000/ants"
"io"
"sync"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
@@ -142,11 +145,43 @@ func (c *SyncController) CompareStoreWithOperator() {
return retVal, "", err
}
globals.SugarLogger.Debug(params.StoreIDs, params.VendorIDs)
err = cms.GetVendorStoreSkusToStruct(params.Ctx, storeIDs[0], vendorIDs[0])
p, _ := ants.NewPoolWithFunc(10, func(data interface{}) {
task := data.(*CompareTask)
task.Do()
})
defer p.Release()
var wg sync.WaitGroup
wg.Add(len(storeIDs))
tasks := make([]*CompareTask, 0, len(storeIDs))
for i := 0; i < len(storeIDs); i++ {
task := &CompareTask{
StoreId: storeIDs[i],
VendorId: vendorIDs[0],
Ctx: params.Ctx,
Wg: &wg,
}
tasks = append(tasks, task)
p.Invoke(task)
}
wg.Wait()
// err = cms.GetVendorStoreSkusToStruct(params.Ctx, storeIDs[0], vendorIDs[0])
return retVal, "", err
})
}
type CompareTask struct {
StoreId int
VendorId int
Ctx *jxcontext.Context
Wg *sync.WaitGroup
}
func (t *CompareTask) Do() {
cms.GetVendorStoreSkusToStruct(t.Ctx, t.StoreId, t.VendorId)
t.Wg.Done()
}
// @Title 删除门店平台商品信息(包括分类)
// @Description 删除门店平台商品信息(包括分类)(!!!此操作会先清除平台上已有的商品及分类信息),此操作只适用于单门店模式平台
// @Param token header string true "认证token"