From d85f595fee067fb42e9bd216198789e91c3ad13b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Thu, 28 Nov 2019 17:58:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=B0=B8=E8=BE=89excel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/yonghui/yonghui.go | 179 ++++++++++++++-------------- 1 file changed, 92 insertions(+), 87 deletions(-) diff --git a/business/jxstore/yonghui/yonghui.go b/business/jxstore/yonghui/yonghui.go index 72ba900b6..fd9eccd86 100644 --- a/business/jxstore/yonghui/yonghui.go +++ b/business/jxstore/yonghui/yonghui.go @@ -133,12 +133,12 @@ var ( SkuRow: 1, }, "净配": &SheetParam{ SkuIDCol: 0, - SkuPriceCol: 12, + SkuPriceCol: 14, SkuNameCol: 1, OrgSkuIdCol: 4, OrgSkuPriceCol: 7, OrgSkuNameCol: 5, - SkuRow: 1, + SkuRow: 2, }, "水产": &SheetParam{ SkuIDCol: 1, SkuPriceCol: 15, @@ -239,43 +239,37 @@ func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, is costPrice float64 //成本价 goodsList []*weimobapi.GoodsInfo goodsIDListForPutAway []interface{} - // isCompare bool + isCompare bool ) db := dao.GetDB() - //读取excel文件 - xlsx, err := excelize.OpenReader(reader) - if err != nil { - return "", err - } - for k := range sheetMap { - sheetParam := sheetMap[k] - rows, _ := xlsx.GetRows(k) - for rowNum, row := range rows { - if rowNum < sheetParam.SkuRow { - continue - } - GetCellIntoMap(sheetParam, skuMap, row, k, rowNum) - if len(skuMap) < 1 { - errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]\n", k) - } - } - if errMsg != "" { - return "", errors.New(errMsg) - } - } - //修改分组名 - // 分类名格式为:可定XX日 - // XX为上传永辉 提供的 价格表时间 +2天 - // isCompare, err = UpdateClassifyAndGetLastClassify() taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) { switch step { case 0: + //读取excel文件 + xlsx, err := excelize.OpenReader(reader) + if err != nil { + return "", err + } + for k := range sheetMap { + sheetParam := sheetMap[k] + rows, _ := xlsx.GetRows(k) + for rowNum, row := range rows { + errMsg += GetCellIntoMap(sheetParam, skuMap, row, k, rowNum) + } + } + //修改分组名 + // 分类名格式为:可定XX日 + // XX为上传永辉 提供的 价格表时间 +2天 + if errMsg == "" { + isCompare, err = UpdateClassifyAndGetLastClassify() + } + case 1: //获取微盟所有商品 - // param := &weimobapi.QueryGoodsListParam{ - // PageNum: 1, - // PageSize: jdapi.MaxSkuIDsCount4QueryListBySkuIds, - // } - // goodsList, err = GetWeiMobGoodsList(param) + param := &weimobapi.QueryGoodsListParam{ + PageNum: 1, + PageSize: jdapi.MaxSkuIDsCount4QueryListBySkuIds, + } + goodsList, err = GetWeiMobGoodsList(param) if err != nil { baseapi.SugarLogger.Errorf("GetWeiMobGoodsList error:%v", err) } @@ -302,10 +296,9 @@ func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, is Comment: "在微盟上未找到该商品", } dataFailed.AppendData2(outPutData) - // errMsg += fmt.Sprintf("在微盟上未找到该商品xxx", xxx) } } - case 1: + case 2: //找出微盟上有,excel上没有的,有就更新,没有就下架 taskFunc3 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { goods := batchItemList[0].(*weimobapi.GoodsInfo) @@ -323,7 +316,6 @@ func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, is Comment: "在京西库中未找到该商品", } dataFailed.AppendData2(outPutData) - // 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 { @@ -334,9 +326,9 @@ func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, is } else { costPrice = skuMap[spuCode].Price } - // if errMsg == "" { - // _, _, _ = updateWeiMobGoods(costPrice, skuMap[spuCode].Price, skuList[0].Unit, isCompare, goodsDetail) - // } + if errMsg == "" { + _, _, _ = updateWeiMobGoods(costPrice, skuMap[spuCode].Price, skuList[0].Unit, isCompare, goodsDetail) + } } } else { //下架微盟商品 @@ -352,30 +344,30 @@ func LoadExcelBinByYongHui(ctx *jxcontext.Context, reader io.Reader, isAsync, is return "", err } goodsIDListForPutAway = goodsIDListForPutAwayInterface - case 2: + case 3: // 批量下架微盟商品 taskFunc4 := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { int64Slice := []int64{} for _, v := range batchItemList { int64Slice = append(int64Slice, v.(int64)) } - // if errMsg == "" { - // PutAwayWeiMobSku(int64Slice) - // } + if errMsg == "" { + 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) - case 3: + case 4: WriteToExcel(task, dataSuccess.dataSuccessList, dataFailed.dataFailedList) } - // if errMsg != "" { - // return result, errors.New(errMsg) - // } + if errMsg != "" { + return result, errors.New(errMsg) + } return result, err } - taskSeq := tasksch.NewSeqTask2("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, isContinueWhenError, taskSeqFunc, 4) + taskSeq := tasksch.NewSeqTask2("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, isContinueWhenError, taskSeqFunc, 5) tasksch.HandleTask(taskSeq, nil, true).Run() if !isAsync { _, err = taskSeq.GetResult(0) @@ -404,17 +396,6 @@ func GetGoodsInfoAndDetailMap(goodsList []*weimobapi.GoodsInfo) (goodsMap map[st return goodsMap } -// func getSelectedClassifyLeafList(l []weimobapi.SelectedClassifyList) (list []int64) { -// for _, v := range l { -// if v.ChildrenClassify == nil { -// list = append(list, v.ClassifyID) -// } else { -// subList := getSelectedClassifyLeafList(v.ChildrenClassify) -// } -// } -// return list -// } - func updateWeiMobGoods(costPrice, salePrice float64, unit string, isCompare bool, goodsDetail *weimobapi.GoodsDetailInfo) (goodsID int64, skuMap map[string]int64, err error) { var ( categoryList []*weimobapi.CategoryList @@ -580,7 +561,7 @@ func IsChineseChar(str string) bool { return false } -func GetCellIntoMap(sheetParam *SheetParam, skuMap map[string]*ExcelParam, row []string, sheetName string, rowNum int) { +func GetCellIntoMap(sheetParam *SheetParam, skuMap map[string]*ExcelParam, row []string, sheetName string, rowNum int) (errMsg string) { var ( skuID string orgSkuID string @@ -597,19 +578,27 @@ func GetCellIntoMap(sheetParam *SheetParam, skuMap map[string]*ExcelParam, row [ ) for k, cell := range row { if cell != "" { - if !IsChineseChar(cell) { - if k == skuIDCol && skuIDCol >= 0 { - skuID = cell + if k == skuIDCol && skuIDCol >= 0 { + skuID = cell + } + if k == skuPriceCol && skuPriceCol >= 0 { + if rowNum == sheetParam.SkuRow-1 { + if !strings.Contains(cell, "今日供价") && !strings.Contains(cell, "单价") { + errMsg += fmt.Sprintf("sheet页:[%v],Excel排版发生变化!第[%v]列今日供价附近可能增加或减少了一列,请确认!", sheetName, k+1) + } } - if k == skuPriceCol && skuPriceCol >= 0 { - skuPrice = Float64Round(utils.Str2Float64WithDefault(cell, 0)) - } - if k == orgSkuIDCol && orgSkuIDCol >= 0 { - orgSkuID = "0" + cell - } - if k == orgSkuPriceCol && orgSkuPriceCol >= 0 { - orgSkuPrice = Float64Round(utils.Str2Float64WithDefault(cell, 0)) + skuPrice = Float64Round(utils.Str2Float64WithDefault(cell, 0)) + } + if k == orgSkuIDCol && orgSkuIDCol >= 0 { + orgSkuID = "0" + cell + } + if k == orgSkuPriceCol && orgSkuPriceCol >= 0 { + if rowNum == sheetParam.SkuRow-1 { + if !strings.Contains(cell, "进价") { + errMsg += fmt.Sprintf("sheet页:[%v],Excel排版发生变化!第[%v]列进价附近可能增加或减少了一列,请确认!", sheetName, k+1) + } } + orgSkuPrice = Float64Round(utils.Str2Float64WithDefault(cell, 0)) } if k == skuNameCol && skuNameCol >= 0 { skuName = cell @@ -619,34 +608,50 @@ func GetCellIntoMap(sheetParam *SheetParam, skuMap map[string]*ExcelParam, row [ } } } - if len(skuMap) > 0 { - if skuMap[skuID] != nil { - if skuMap[skuID].Price != 0 && skuMap[skuID].Price != skuPrice && skuPrice != 0 { - if skuPrice > skuMap[skuID].Price { + if rowNum >= sheetParam.SkuRow { + if rowNum == sheetParam.SkuRow { + if IsChineseChar(skuID) { + if IsChineseChar(skuID) { + errMsg += fmt.Sprintf("sheet页:[%v],Excel排版发生变化!第[%v]行附近可能增加了一行,请确认!", sheetName, rowNum) + } + } + } + if len(skuMap) > 0 { + if skuMap[skuID] != nil { + if skuMap[skuID].Price != 0 && skuMap[skuID].Price != skuPrice && skuPrice != 0 { + if skuPrice > skuMap[skuID].Price { + BuildSkuMap(skuID, skuName, skuPrice, skuMap) + } + } else { BuildSkuMap(skuID, skuName, skuPrice, skuMap) } - } else { + } else if skuPrice != 0 { BuildSkuMap(skuID, skuName, skuPrice, skuMap) } - } else if skuPrice != 0 { - BuildSkuMap(skuID, skuName, skuPrice, skuMap) - } - if skuMap[orgSkuID] != nil { - if skuMap[orgSkuID].Price != 0 && skuMap[orgSkuID].Price != orgSkuPrice && orgSkuPrice != 0 { - if orgSkuPrice > skuMap[orgSkuID].Price { + if skuMap[orgSkuID] != nil { + if skuMap[orgSkuID].Price != 0 && skuMap[orgSkuID].Price != orgSkuPrice && orgSkuPrice != 0 { + if orgSkuPrice > skuMap[orgSkuID].Price { + BuildSkuMap(orgSkuID, orgSkuName, orgSkuPrice, skuMap) + } + } else if orgSkuPriceCol >= 0 && orgSkuIDCol >= 0 && orgSkuNameCol >= 0 { BuildSkuMap(orgSkuID, orgSkuName, orgSkuPrice, skuMap) } - } else if orgSkuPriceCol >= 0 && orgSkuIDCol >= 0 && orgSkuNameCol >= 0 { + } else if orgSkuPrice != 0 { BuildSkuMap(orgSkuID, orgSkuName, orgSkuPrice, skuMap) } - } else if orgSkuPrice != 0 { + } else { + BuildSkuMap(skuID, skuName, skuPrice, skuMap) BuildSkuMap(orgSkuID, orgSkuName, orgSkuPrice, skuMap) } + delete(skuMap, "") } else { - BuildSkuMap(skuID, skuName, skuPrice, skuMap) - BuildSkuMap(orgSkuID, orgSkuName, orgSkuPrice, skuMap) + for i := rowNum; i < sheetParam.SkuRow; i++ { + if !IsChineseChar(skuID) { + errMsg += fmt.Sprintf("sheet页:[%v],Excel排版发生变化!第[%v]行附近可能减少了一行,请确认!", sheetName, rowNum) + } + } } - delete(skuMap, "") + return errMsg } func BuildSkuMap(id, name string, price float64, skuMap map[string]*ExcelParam) { excelParam := &ExcelParam{