diff --git a/business/jxstore/cms/sku.go b/business/jxstore/cms/sku.go index 846c0dd84..234b2eeac 100644 --- a/business/jxstore/cms/sku.go +++ b/business/jxstore/cms/sku.go @@ -1789,10 +1789,11 @@ func CreateUpcSkuByExcel(ctx *jxcontext.Context, files []*multipart.FileHeader) func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint string, err error) { var ( - db = dao.GetDB() - skuParams []*model.SkuName - createUpcSkuByExcelErrList []*CreateUpcSkuByExcelErr - excelTitle = []string{ + db = dao.GetDB() + skuParams []*model.SkuName + createUpcSkuByExcelErrList []*CreateUpcSkuByExcelErr + createUpcSkuByExcelErrListInterface []interface{} + excelTitle = []string{ "商品条码", "商品名称", "单位", @@ -1849,7 +1850,8 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri skuParams = append(skuParams, skuParam) } case 1: - for _, v := range skuParams { + taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + v := batchItemList[0].(*model.SkuName) skuName := &model.SkuNameExt{ SkuName: model.SkuName{}, Skus: []*model.SkuWithVendor{ @@ -1861,29 +1863,29 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri if v.Upc != nil { upc := upcRegexp.FindString(*v.Upc) if upc == "" { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "请输入正确的商品条码!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "请输入正确的商品条码!")} + return retVal, err } skuName.Upc = v.Upc } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "商品的条码不能为空!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "商品的条码不能为空!")} + return retVal, err } skuNames, _ := dao.GetSkuNames(db, nil, []string{*v.Upc}, "") if len(skuNames) > 0 { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "商品在京西库中已存在!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "商品在京西库中已存在!")} + return retVal, err } productInfos, err := GetJdUpcCodeByCode(ctx, *v.Upc) if err != nil { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, err.Error())) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} + return retVal, err } if len(productInfos) == 0 { result, _ := api.AliUpcAPI.GetAliUpcInfo(*v.Upc) if result == nil { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "未在标品库查到此商品,请手动创建!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "未在标品库查到此商品,请手动创建!")} + return retVal, err } if result.Img != "" { if resBinary, _, err := jxutils.DownloadFileByURL(result.Img); err == nil { @@ -1893,16 +1895,16 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri if err == nil { skuName.Img = downloadURL2 } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, err.Error())) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} + return retVal, err } } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, err.Error())) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} + return retVal, err } } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, err.Error())) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} + return retVal, err } } else { skuName.Img = model.NOSkuNameImg @@ -1914,8 +1916,8 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri } if v.Price == 0 { if result.Price == "" { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "未查询到商品售价,请补充商品的售价!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "未查询到商品售价,请补充商品的售价!")} + return retVal, err } else { skuName.Price = int(utils.Str2Int64(result.Price) * 100) } @@ -1924,13 +1926,13 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri } if v.Unit != "" { if v.Unit == model.UnitNames[0] { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "标品的单位不能为份!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "标品的单位不能为份!")} + return retVal, err } skuName.Unit = v.Unit } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "未查询到商品单位,请补充商品单位!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "未查询到商品单位,请补充商品单位!")} + return retVal, err } if v.SpecQuality != 0 { skuName.Skus[0].SpecQuality = v.SpecQuality @@ -1941,8 +1943,8 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri skuName.Skus[0].SpecQuality = specQuality skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(specQuality)))) } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "未查询到商品重量,请补充商品重量!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "未查询到商品重量,请补充商品重量!")} + return retVal, err } } skuName.Skus[0].SpecUnit = model.SpecialSpecUnit @@ -1952,30 +1954,30 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri if v.Name != "" { productInfo.Name = v.Name } else { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "需要填上一个合适的商品名!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "需要填上一个合适的商品名!")} + return retVal, err } } skuNames2, _ := dao.GetSkuNames(db, nil, nil, productInfo.Name) if len(skuNames2) > 1 { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "此商品名在京西库中查询出了大于1个商品!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "此商品名在京西库中查询出了大于1个商品!")} + return retVal, err } //表示查到了,需要把upc更新上去,没查到就要新建 if len(skuNames2) == 1 && (productInfo.SpecQuality == skuNames2[0].SpecQuality && productInfo.SpecUnit == skuNames2[0].SpecUnit) { skuNames2[0].Upc = v.Upc dao.UpdateEntity(db, skuNames2[0], "Upc") - continue + return retVal, err } else { if v.Price == 0 { result, err := api.AliUpcAPI.GetAliUpcInfo(*v.Upc) if err != nil { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, err.Error())) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} + return retVal, err } if result.Price == "" { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "未查询到商品售价,请补充商品售价!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "未查询到商品售价,请补充商品售价!")} + return retVal, err } else { skuName.Price = int(utils.Str2Int64(result.Price) * 100) } @@ -1995,17 +1997,18 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri } } if skuName.Img == "" { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, "未查询到商品图片,请联系开发!")) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, "未查询到商品图片,请联系开发!")} + return retVal, err } suffix := skuName.Img[strings.LastIndex(skuName.Img, "."):] if suffix != ".jpg" && suffix != ".png" && suffix != ".jpeg" && suffix != ".gif" { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, fmt.Sprintf("暂不支持的图片格式:[%v]", skuName.Img))) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, fmt.Sprintf("暂不支持的图片格式:[%v]", skuName.Img))} + return retVal, err } //需要把图片传到七牛云上 if !strings.Contains(skuName.Img, "image.jxc4.com") { if resBinary, _, err := jxutils.DownloadFileByURL(skuName.Img); err == nil { + time.Sleep(time.Second / 4) if downloadURL, err := jxutils.UploadExportContent(resBinary, skuName.Img[strings.LastIndex(skuName.Img, "/")+1:len(skuName.Img)]); err == nil { if img, _, err := datares.Binary2Image(resBinary, http.DetectContentType(resBinary)); err == nil { if img.Bounds().Dx() != datares.MainImgWidth || img.Bounds().Dy() != datares.MainImgHeight { @@ -2025,16 +2028,24 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader) (hint stri skuName.IsGlobal = model.YES skuName.CategoryID = model.NoCatCatgoryID skuName.Skus[0].Status = model.SkuStatusNormal - time.Sleep(time.Second / 2) //addskuname里下载图片有点问题,所以设置一个时间。。 _, err = AddSkuName(ctx, skuName, ctx.GetUserName()) if err != nil { - createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, buildCreateUpcSkuByExcelErr(v, err.Error())) - continue + retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} + return retVal, err } + return retVal, err } + taskParallel := tasksch.NewParallelTask("创建标品中", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, taskFunc, skuParams) + tasksch.HandleTask(taskParallel, task, true).Run() + createUpcSkuByExcelErrListInterface, err = taskParallel.GetResult(0) case 2: - if len(createUpcSkuByExcelErrList) > 0 { - err = writeToExcel(excelTitle, createUpcSkuByExcelErrList, task) + if len(createUpcSkuByExcelErrListInterface) > 0 { + for _, v := range createUpcSkuByExcelErrListInterface { + createUpcSkuByExcelErrList = append(createUpcSkuByExcelErrList, v.(*CreateUpcSkuByExcelErr)) + } + if len(createUpcSkuByExcelErrList) > 0 { + err = writeToExcel(excelTitle, createUpcSkuByExcelErrList, task) + } } } return result2, err