From 95c37a847e3172eb6652545b9de08c3f10206a3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Wed, 13 Nov 2019 09:57:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AF=BB=E5=8F=96=E6=B0=B8=E8=BE=89Excel?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/yonghui/yonghui.go | 177 ++++++++++++++++------------ controllers/yonghui.go | 3 - 2 files changed, 101 insertions(+), 79 deletions(-) diff --git a/business/jxstore/yonghui/yonghui.go b/business/jxstore/yonghui/yonghui.go index 3c5bbf6fa..898a456aa 100644 --- a/business/jxstore/yonghui/yonghui.go +++ b/business/jxstore/yonghui/yonghui.go @@ -15,6 +15,7 @@ import ( "git.rosy.net.cn/baseapi/platformapi/weimobapi" "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/globals/api" ) @@ -97,13 +98,13 @@ const ( func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, isAsync bool) (hint string, err error) { var ( - skuMap = make(map[string]float64) - errMsg string - // costPrice float64 //成本价 - // goodsIDListForPutAway []int64 - // goodsList []*weimobapi.GoodsInfo + skuMap = make(map[string]float64) + errMsg string + costPrice float64 //成本价 + goodsIDListForPutAway []int64 + goodsList []*weimobapi.GoodsInfo ) - // db := dao.GetDB() + db := dao.GetDB() if len(files) == 0 { return "", errors.New("没有文件上传!") } @@ -127,117 +128,141 @@ func LoadExcelByYongHui(ctx *jxcontext.Context, files []*multipart.FileHeader, i continue } GetCellIntoMap(sheet1SkuIDCol, sheet1SkuPriceCol, sheet1OrgSkuIdCol, sheet1OrgSkuPriceCol, skuMap, true, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } case sheet2Name: if rowNum < sheet2SkuRow { continue } GetCellIntoMap(sheet2SkuIDCol, sheet2SkuPriceCol, sheet2OrgSkuIdCol, sheet2OrgSkuPriceCol, skuMap, true, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } case sheet3Name: if rowNum < sheet3SkuRow { continue } GetCellIntoMap(sheet3SkuIDCol, sheet3SkuPriceCol, sheet3OrgSkuIdCol, sheet3OrgSkuPriceCol, skuMap, true, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } case sheet4Name: if rowNum < sheet4SkuRow { continue } GetCellIntoMap(sheet4SkuIDCol, sheet4SkuPriceCol, sheet4OrgSkuIdCol, sheet4OrgSkuPriceCol, skuMap, true, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } case sheet5Name: if rowNum < sheet5SkuRow { continue } GetCellIntoMap(sheet5SkuIDCol, sheet5SkuPriceCol, sheet5OrgSkuIdCol, sheet5OrgSkuPriceCol, skuMap, true, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } case sheet6Name: if rowNum < sheet6SkuRow { continue } GetCellIntoMap(sheet6SkuIDCol, sheet6SkuPriceCol, sheet6OrgSkuIdCol, sheet6OrgSkuPriceCol, skuMap, true, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } case sheet7Name: if rowNum < sheet7SkuRow { continue } GetCellIntoMap(sheet7SkuIDCol, sheet7SkuPriceCol, 0, 0, skuMap, false, row, v, rowNum) + if len(skuMap) < 1 { + errMsg += fmt.Sprintf("读取Excel数据失败,Excel格式排版可能发生了变化!sheetName: [%v]", v) + } default: return retVal, errors.New("未找到配置的sheet页,请检查excel内容排版格式是否发生变化!\n") } } + if errMsg != "" { + return "", errors.New(errMsg) + } return retVal, err } taskParallel1 := tasksch.NewParallelTask("读取Excel", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc1, sheetNames) tasksch.HandleTask(taskParallel1, task, true).Run() _, err = taskParallel1.GetResult(0) - // case 1: - // //获取微盟所有商品 - // goodsList = GetGoodsInfoAndDetail() - // //找出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上没有的,有就更新,没有就下架 - // taskFunc2 := func(task *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 { - // errMsg += fmt.Sprintf("在京西库中未找到该商品!name_id : [%v] \n", goodsDetail.SkuMap.SingleSku.OuterSkuCode) - // } else { - // if skuList[0].Unit != "份" { - // costPrice = Float64Round(0.5 / goodsDetail.SkuMap.SingleSku.B2CSku.Weight * skuMap[spuCode]) - // } else { - // costPrice = skuMap[spuCode] - // } - // if errMsg == "" { - // _, _, err = updateWeiMobGoods(costPrice, skuMap[spuCode], goodsDetail) - // } - // } - // } else { - // //下架微盟商品 - // goodsIDListForPutAway = append(goodsIDListForPutAway, goodsDetail.GoodsID) - // } - // } - // return retVal, err - // } - // taskParallel2 := tasksch.NewParallelTask("获取微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList) - // tasksch.HandleTask(taskParallel2, task, true).Run() - // _, err = taskParallel2.GetResult(0) - // case 3: - // //批量下架微盟商品 - // if errMsg == "" { - // if len(goodsIDListForPutAway) <= 50 { //api要求最多传50个 - // PutAwayWeiMobSku(goodsIDListForPutAway) - // } else { - // for i := 0; i < len(goodsIDListForPutAway)/50+1; i++ { - // var int64Slice []int64 - // if len(goodsIDListForPutAway) > 50*(i+1) { - // int64Slice = goodsIDListForPutAway[i*50 : (i+1)*50+1] - // } else { - // int64Slice = goodsIDListForPutAway[i*50 : len(goodsIDListForPutAway)] - // } - // PutAwayWeiMobSku(int64Slice) - // } - // } - // } + case 1: + //获取微盟所有商品 + goodsList = GetGoodsInfoAndDetail() + //找出excel上有,微盟没有的,有就列出报错,不进行更新 + goodsInfoAndDetailMap := GetGoodsInfoAndDetailMap(goodsList) + delete(skuMap, "") + for k, v := range skuMap { + fmt.Printf("key %v, value %v\n", k, v) + //表示excel上有,微盟上没有 + if goodsInfoAndDetailMap[k] == nil { + errMsg += fmt.Sprintf("在微盟上未找到该商品!excel商品ID : [%v]\n", k) + } + } + fmt.Println(len(skuMap)) + if errMsg != "" { + return "", errors.New(errMsg) + } + case 2: + //找出微盟上有,excel上没有的,有就更新,没有就下架 + taskFunc2 := func(task *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 { + errMsg += fmt.Sprintf("在京西库中未找到该商品!name_id : [%v] \n", goodsDetail.SkuMap.SingleSku.OuterSkuCode) + } else { + if skuList[0].Unit != "份" { + costPrice = Float64Round(0.5 / goodsDetail.SkuMap.SingleSku.B2CSku.Weight * skuMap[spuCode]) + } else { + costPrice = skuMap[spuCode] + } + if errMsg == "" { + _, _, err = updateWeiMobGoods(costPrice, skuMap[spuCode], goodsDetail) + } + } + } else { + //下架微盟商品 + goodsIDListForPutAway = append(goodsIDListForPutAway, goodsDetail.GoodsID) + } + } + return retVal, err + } + taskParallel2 := tasksch.NewParallelTask("获取微盟所有商品并更新", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc2, goodsList) + tasksch.HandleTask(taskParallel2, task, true).Run() + _, err = taskParallel2.GetResult(0) + case 3: + //批量下架微盟商品 + if errMsg == "" { + if len(goodsIDListForPutAway) <= 50 { //api要求最多传50个 + PutAwayWeiMobSku(goodsIDListForPutAway) + } else { + for i := 0; i < len(goodsIDListForPutAway)/50+1; i++ { + var int64Slice []int64 + if len(goodsIDListForPutAway) > 50*(i+1) { + int64Slice = goodsIDListForPutAway[i*50 : (i+1)*50+1] + } else { + int64Slice = goodsIDListForPutAway[i*50 : len(goodsIDListForPutAway)] + } + PutAwayWeiMobSku(int64Slice) + } + } + } } return result, err } - taskSeq := tasksch.NewSeqTask("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, taskSeqFunc, 4) + taskSeq := tasksch.NewSeqTask("读取永辉Excel文件修改微盟商品价格可售状态-序列任务", ctx, taskSeqFunc, 2) tasksch.HandleTask(taskSeq, nil, true).Run() - for k, v := range skuMap { - fmt.Println(k, v) - } if errMsg != "" { baseapi.SugarLogger.Debugf(errMsg) } @@ -401,7 +426,7 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM } } } - if skuMap[skuID] != 0 && skuMap[skuID] != skuPrice { + if skuMap[skuID] != 0 && skuMap[skuID] != skuPrice && skuID != "" { if skuPrice > skuMap[skuID] { skuMap[skuID] = skuPrice } @@ -410,7 +435,7 @@ func GetCellIntoMap(skuIDCol, skuPriceCol, orgSkuIDCol, orgSkuPriceCol int, skuM skuMap[skuID] = skuPrice } if isHaveOrg { - if skuMap[orgSkuID] != 0 && skuMap[orgSkuID] != orgSkuPrice { + if skuMap[orgSkuID] != 0 && skuMap[orgSkuID] != orgSkuPrice && orgSkuID != "" { if orgSkuPrice > skuMap[orgSkuID] { skuMap[orgSkuID] = orgSkuPrice } diff --git a/controllers/yonghui.go b/controllers/yonghui.go index 3aa519f6c..d714ea019 100644 --- a/controllers/yonghui.go +++ b/controllers/yonghui.go @@ -1,7 +1,6 @@ package controllers import ( - "fmt" "io" "git.rosy.net.cn/jx-callback/business/jxstore/yonghui" @@ -36,8 +35,6 @@ func (c *YongHuiController) LoadExcelByYongHui() { } else if c.Ctx.Input.IsPost() { c.callLoadExcelByYongHui(func(params *tYonghuiLoadExcelByYongHuiParams) (retVal interface{}, errCode string, err error) { r := c.Ctx.Request - fmt.Println(r) - fmt.Println(r.MultipartForm) files := r.MultipartForm.File["userfiles"] retVal, err = yonghui.LoadExcelByYongHui(params.Ctx, files, params.IsAsync) return retVal, "", err