读取永辉excel
This commit is contained in:
@@ -84,7 +84,7 @@ const (
|
|||||||
UpdateGoodsShelfStatusCount = 50 //微盟下架商品api限制一次50个
|
UpdateGoodsShelfStatusCount = 50 //微盟下架商品api限制一次50个
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, isAsync, IsContinueWhenError bool) (hint string, err error) {
|
func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
if len(files) == 0 {
|
if len(files) == 0 {
|
||||||
return "", errors.New("没有文件上传!")
|
return "", errors.New("没有文件上传!")
|
||||||
}
|
}
|
||||||
@@ -95,7 +95,7 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i
|
|||||||
return hint, err
|
return hint, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, IsContinueWhenError bool) (hint string, err error) {
|
func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, isContinueWhenError bool) (hint string, err error) {
|
||||||
var (
|
var (
|
||||||
skuMap = make(map[string]float64)
|
skuMap = make(map[string]float64)
|
||||||
errMsg string
|
errMsg string
|
||||||
@@ -104,108 +104,133 @@ func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, Is
|
|||||||
goodsIDListForPutAway []interface{}
|
goodsIDListForPutAway []interface{}
|
||||||
)
|
)
|
||||||
db := dao.GetDB()
|
db := dao.GetDB()
|
||||||
|
taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||||
//读取excel文件
|
switch step {
|
||||||
xlsx, err := excelize.OpenReader(reader)
|
case 0:
|
||||||
if err != nil {
|
//读取excel文件
|
||||||
return "", err
|
xlsx, err := excelize.OpenReader(reader)
|
||||||
}
|
|
||||||
for k := range sheetMap {
|
|
||||||
sheetParam := sheetMap[k]
|
|
||||||
rows, _ := xlsx.GetRows(k)
|
|
||||||
for rowNum, row := range rows {
|
|
||||||
if rowNum < sheetParam.SkuRow {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
GetCellIntoMap(sheetParam.SkuIDCol, sheetParam.SkuPriceCol, sheetParam.OrgSkuIdCol, sheetParam.OrgSkuPriceCol, skuMap, row, k, rowNum)
|
|
||||||
if len(skuMap) < 1 {
|
|
||||||
errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]\n", k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if errMsg != "" {
|
|
||||||
return "", errors.New(errMsg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//获取微盟所有商品
|
|
||||||
goodsList, err = GetWeiMobGoodsList()
|
|
||||||
if err != nil {
|
|
||||||
baseapi.SugarLogger.Errorf("GetWeiMobGoodsList error:%v", err)
|
|
||||||
}
|
|
||||||
task := tasksch.NewParallelTask("获取微盟商品", tasksch.NewParallelConfig().SetIsContinueWhenError(IsContinueWhenError), ctx,
|
|
||||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
v := batchItemList[0].(*weimobapi.GoodsInfo)
|
|
||||||
goodsDetail, err := api.WeimobAPI.QueryGoodsDetail(v.GoodsID)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
baseapi.SugarLogger.Errorf("QueryGoodsDetail error:%v", err)
|
return "", err
|
||||||
}
|
}
|
||||||
v.GoodsDetailInfo = goodsDetail
|
for k := range sheetMap {
|
||||||
return retVal, err
|
sheetParam := sheetMap[k]
|
||||||
}, goodsList)
|
rows, _ := xlsx.GetRows(k)
|
||||||
tasksch.HandleTask(task, nil, true).Run()
|
for rowNum, row := range rows {
|
||||||
|
if rowNum < sheetParam.SkuRow {
|
||||||
//找出excel上有,微盟没有的,有就列出报错,不进行更新
|
continue
|
||||||
goodsInfoAndDetailMap := GetGoodsInfoAndDetailMap(goodsList)
|
|
||||||
skuMapList := GetSkuMapList(skuMap)
|
|
||||||
task1 := tasksch.NewParallelTask("对比Excel上有微盟上没有的商品", tasksch.NewParallelConfig().SetIsContinueWhenError(IsContinueWhenError), ctx,
|
|
||||||
func(task1 *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
k := batchItemList[0].(string)
|
|
||||||
//表示excel上有,微盟上没有
|
|
||||||
if goodsInfoAndDetailMap[k] == nil {
|
|
||||||
return "", errors.New(fmt.Sprintf("在微盟上未找到该商品!excel商品ID : [%v]\n", k))
|
|
||||||
}
|
|
||||||
return retVal, err
|
|
||||||
}, skuMapList)
|
|
||||||
tasksch.HandleTask(task1, nil, true).Run()
|
|
||||||
|
|
||||||
//找出微盟上有,excel上没有的,有就更新,没有就下架
|
|
||||||
task2 := tasksch.NewParallelTask("根据获取的微盟所有商品并更新", tasksch.NewParallelConfig().SetIsContinueWhenError(IsContinueWhenError), ctx,
|
|
||||||
func(task1 *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
goods := batchItemList[0].(*weimobapi.GoodsInfo)
|
|
||||||
goodsDetail := goods.GoodsDetailInfo
|
|
||||||
spuCode := goodsDetail.OuterGoodsCode
|
|
||||||
if spuCode != "" {
|
|
||||||
//如果微盟商品里找得到excel中的商品
|
|
||||||
if skuMap[spuCode] != 0 {
|
|
||||||
//获取京西库商品
|
|
||||||
skuList, _ := dao.GetSkus(db, nil, []int{int(utils.Str2Int64(goodsDetail.SkuMap.SingleSku.OuterSkuCode))}, nil, nil)
|
|
||||||
if len(skuList) == 0 {
|
|
||||||
return "", errors.New(fmt.Sprintf("在京西库中未找到该商品!name_id : [%v]\n", goodsDetail.SkuMap.SingleSku.OuterSkuCode))
|
|
||||||
} else {
|
|
||||||
if skuList[0].Unit == "份" {
|
|
||||||
if goodsDetail.SkuMap.SingleSku.B2CSku.Weight == 0 {
|
|
||||||
costPrice = skuMap[spuCode]
|
|
||||||
} else {
|
|
||||||
costPrice = Float64Round(0.5 / goodsDetail.SkuMap.SingleSku.B2CSku.Weight * skuMap[spuCode])
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
costPrice = skuMap[spuCode]
|
|
||||||
}
|
|
||||||
// if errMsg == "" {
|
|
||||||
_, _, err = updateWeiMobGoods(costPrice, skuMap[spuCode], goodsDetail)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
} else {
|
GetCellIntoMap(sheetParam.SkuIDCol, sheetParam.SkuPriceCol, sheetParam.OrgSkuIdCol, sheetParam.OrgSkuPriceCol, skuMap, row, k, rowNum)
|
||||||
//下架微盟商品
|
if len(skuMap) < 1 {
|
||||||
retVal = []int64{goodsDetail.GoodsID}
|
errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]\n", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if errMsg != "" {
|
||||||
|
return "", errors.New(errMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return retVal, err
|
case 1:
|
||||||
}, goodsList)
|
//获取微盟所有商品
|
||||||
tasksch.HandleTask(task2, nil, true).Run()
|
goodsList, err = GetWeiMobGoodsList()
|
||||||
goodsIDListForPutAwayInterface, err := task2.GetResult(0)
|
if err != nil {
|
||||||
goodsIDListForPutAway = goodsIDListForPutAwayInterface
|
baseapi.SugarLogger.Errorf("GetWeiMobGoodsList error:%v", err)
|
||||||
// 批量下架微盟商品
|
|
||||||
task3 := tasksch.NewParallelTask("下架微盟商品", tasksch.NewParallelConfig().SetIsContinueWhenError(IsContinueWhenError), ctx,
|
|
||||||
func(task1 *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
|
||||||
int64Slice := []int64{}
|
|
||||||
for _, v := range batchItemList {
|
|
||||||
int64Slice = append(int64Slice, v.(int64))
|
|
||||||
}
|
}
|
||||||
PutAwayWeiMobSku(int64Slice)
|
taskFunc2 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
return retVal, err
|
v := batchItemList[0].(*weimobapi.GoodsInfo)
|
||||||
}, goodsIDListForPutAway)
|
goodsDetail, err := api.WeimobAPI.QueryGoodsDetail(v.GoodsID)
|
||||||
tasksch.HandleTask(task3, nil, true).Run()
|
if err != nil {
|
||||||
|
baseapi.SugarLogger.Errorf("QueryGoodsDetail error:%v", err)
|
||||||
|
}
|
||||||
|
v.GoodsDetailInfo = goodsDetail
|
||||||
|
return retVal, err
|
||||||
|
}
|
||||||
|
taskParallel2 := tasksch.NewParallelTask("获取微盟商品", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList)
|
||||||
|
tasksch.HandleTask(taskParallel2, task, true).Run()
|
||||||
|
_, err = taskParallel2.GetResult(0)
|
||||||
|
//找出excel上有,微盟没有的,有就列出报错,不进行更新
|
||||||
|
goodsInfoAndDetailMap := GetGoodsInfoAndDetailMap(goodsList)
|
||||||
|
for k, _ := range skuMap {
|
||||||
|
//表示excel上有,微盟上没有
|
||||||
|
if goodsInfoAndDetailMap[k] == nil {
|
||||||
|
errMsg += fmt.Sprintf("在微盟上未找到该商品!excel商品ID : [%v]\n", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// if errMsg != "" {
|
||||||
|
// return "", errors.New(errMsg)
|
||||||
|
// }
|
||||||
|
case 2:
|
||||||
|
//找出微盟上有,excel上没有的,有就更新,没有就下架
|
||||||
|
taskFunc3 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
goods := batchItemList[0].(*weimobapi.GoodsInfo)
|
||||||
|
goodsDetail := goods.GoodsDetailInfo
|
||||||
|
spuCode := goodsDetail.OuterGoodsCode
|
||||||
|
errTmp := ""
|
||||||
|
if spuCode != "" {
|
||||||
|
//如果微盟商品里找得到excel中的商品
|
||||||
|
if skuMap[spuCode] != 0 {
|
||||||
|
//获取京西库商品
|
||||||
|
skuList, _ := dao.GetSkus(db, nil, []int{int(utils.Str2Int64(goodsDetail.SkuMap.SingleSku.OuterSkuCode))}, nil, nil)
|
||||||
|
if len(skuList) == 0 {
|
||||||
|
errTmp += fmt.Sprintf("在京西库中未找到该商品!name_id : [%v]\n", goodsDetail.SkuMap.SingleSku.OuterSkuCode)
|
||||||
|
} else {
|
||||||
|
if skuList[0].Unit == "份" {
|
||||||
|
if goodsDetail.SkuMap.SingleSku.B2CSku.Weight == 0 {
|
||||||
|
costPrice = skuMap[spuCode]
|
||||||
|
} else {
|
||||||
|
costPrice = Float64Round(0.5 / goodsDetail.SkuMap.SingleSku.B2CSku.Weight * skuMap[spuCode])
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
costPrice = skuMap[spuCode]
|
||||||
|
}
|
||||||
|
// if errMsg == "" {
|
||||||
|
_, _, err = updateWeiMobGoods(costPrice, skuMap[spuCode], goodsDetail)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
//下架微盟商品
|
||||||
|
retVal = []int64{goodsDetail.GoodsID}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if errTmp != "" {
|
||||||
|
return retVal, errors.New(errTmp)
|
||||||
|
}
|
||||||
|
return retVal, err
|
||||||
|
}
|
||||||
|
taskParallel3 := tasksch.NewParallelTask("根据获取的微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc3, goodsList)
|
||||||
|
tasksch.HandleTask(taskParallel3, task, true).Run()
|
||||||
|
goodsIDListForPutAwayInterface, err2 := taskParallel3.GetResult(0)
|
||||||
|
if err = err2; err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
goodsIDListForPutAway = goodsIDListForPutAwayInterface
|
||||||
|
case 3:
|
||||||
|
// 批量下架微盟商品
|
||||||
|
// if errMsg == "" {
|
||||||
|
taskFunc4 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||||
|
int64Slice := []int64{}
|
||||||
|
for _, v := range batchItemList {
|
||||||
|
int64Slice = append(int64Slice, v.(int64))
|
||||||
|
}
|
||||||
|
PutAwayWeiMobSku(int64Slice)
|
||||||
|
return retVal, err
|
||||||
|
}
|
||||||
|
taskParallel4 := tasksch.NewParallelTask("下架微盟商品", tasksch.NewParallelConfig().SetParallelCount(parallelCount).SetBatchSize(UpdateGoodsShelfStatusCount), ctx, taskFunc4, goodsIDListForPutAway)
|
||||||
|
tasksch.HandleTask(taskParallel4, task, true).Run()
|
||||||
|
_, err = taskParallel4.GetResult(0)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
if errMsg != "" {
|
||||||
|
return result, errors.New(errMsg)
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
taskSeq := tasksch.NewSeqTask2("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, isContinueWhenError, taskSeqFunc, 4)
|
||||||
|
tasksch.HandleTask(taskSeq, nil, true).Run()
|
||||||
|
if !isAsync {
|
||||||
|
_, err = taskSeq.GetResult(0)
|
||||||
|
hint = "1"
|
||||||
|
} else {
|
||||||
|
hint = taskSeq.GetID()
|
||||||
|
}
|
||||||
return hint, err
|
return hint, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,13 +244,6 @@ func PutAwayWeiMobSku(goodsIDListForPutAway []int64) (err error) {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetSkuMapList(skuMap map[string]float64) (skuMapList []string) {
|
|
||||||
for k, _ := range skuMap {
|
|
||||||
skuMapList = append(skuMapList, k)
|
|
||||||
}
|
|
||||||
return skuMapList
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetGoodsInfoAndDetailMap(goodsList []*weimobapi.GoodsInfo) (goodsMap map[string]*weimobapi.GoodsInfo) {
|
func GetGoodsInfoAndDetailMap(goodsList []*weimobapi.GoodsInfo) (goodsMap map[string]*weimobapi.GoodsInfo) {
|
||||||
goodsMap = make(map[string]*weimobapi.GoodsInfo)
|
goodsMap = make(map[string]*weimobapi.GoodsInfo)
|
||||||
for _, v := range goodsList {
|
for _, v := range goodsList {
|
||||||
|
|||||||
Reference in New Issue
Block a user