From 99bcaaad05c155a3e6bb3912996e17efacfba9ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com> Date: Mon, 27 Apr 2020 14:08:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E9=93=B6=E8=B1=B9=E5=88=B0?= =?UTF-8?q?=E4=BA=AC=E8=A5=BF=E5=95=86=E5=93=81=EF=BC=8C=E6=B5=8B=E8=AF=95?= =?UTF-8?q?list?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/cms/sku.go | 146 ++++++++++++++++---------- business/jxstore/cms/sync.go | 127 ++++++++++++++++------ controllers/cms_sync.go | 19 ++++ routers/commentsRouter_controllers.go | 9 ++ 4 files changed, 218 insertions(+), 83 deletions(-) diff --git a/business/jxstore/cms/sku.go b/business/jxstore/cms/sku.go index 4b7d62898..2bd9bbdc2 100644 --- a/business/jxstore/cms/sku.go +++ b/business/jxstore/cms/sku.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "math" "mime/multipart" "net/http" "regexp" @@ -13,6 +12,7 @@ import ( "time" "git.rosy.net.cn/baseapi/platformapi/aliupcapi" + "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi/platformapi/dingdingapi" @@ -28,7 +28,6 @@ import ( "git.rosy.net.cn/jx-callback/globals/api" "git.rosy.net.cn/jx-callback/globals/refutil" - "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils/errlist" "git.rosy.net.cn/jx-callback/business/jxutils" "git.rosy.net.cn/jx-callback/business/jxutils/datares" @@ -1976,7 +1975,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID } if v.SpecQuality != 0 { skuName.Skus[0].SpecQuality = v.SpecQuality - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(math.Round(float64(v.SpecQuality))))) + skuName.Skus[0].Weight = utils.Float32ToInt(v.SpecQuality) skuName.Skus[0].SpecUnit = model.SpecUnitNames[0] } else { _, _, _, specUnit, _, specQuality := jxutils.SplitSkuName(v.Name) @@ -1985,7 +1984,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID if specUnit == model.SpecUnitNames[1] || specUnit == model.SpecUnitNames[2] { skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(specQuality) * 1000))) } else { - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(math.Round(float64(specQuality))))) + skuName.Skus[0].Weight = utils.Float32ToInt(specQuality) } } else { if getNetUpcInfo.SpecQuality != 0 { @@ -1993,7 +1992,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID getNetUpcInfo.SpecUnit == "KG" || getNetUpcInfo.SpecUnit == "l" { skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(getNetUpcInfo.SpecQuality) * 1000))) } else { - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(math.Round(float64(specQuality))))) + skuName.Skus[0].Weight = utils.Float32ToInt(getNetUpcInfo.SpecQuality) } skuName.Skus[0].SpecQuality = getNetUpcInfo.SpecQuality } else { @@ -2061,7 +2060,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID } if v.SpecQuality != 0 { skuName.Skus[0].SpecQuality = v.SpecQuality - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(v.SpecQuality)))) + skuName.Skus[0].Weight = utils.Float32ToInt(v.SpecQuality) skuName.Skus[0].SpecUnit = model.SpecUnitNames[0] } else { _, _, _, specUnit, _, specQuality := jxutils.SplitSkuName(v.Name) @@ -2070,7 +2069,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID if specUnit == model.SpecUnitNames[1] || specUnit == model.SpecUnitNames[2] { skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(specQuality) * 1000))) } else { - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(specQuality)))) + skuName.Skus[0].Weight = utils.Float32ToInt(specQuality) } } else { if productInfo.SpecQuality == 0 { @@ -2079,7 +2078,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID getNetUpcInfo.SpecUnit == "KG" || getNetUpcInfo.SpecUnit == "l" { skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(getNetUpcInfo.SpecQuality) * 1000))) } else { - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(getNetUpcInfo.SpecQuality)))) + skuName.Skus[0].Weight = utils.Float32ToInt(getNetUpcInfo.SpecQuality) } skuName.Skus[0].SpecQuality = getNetUpcInfo.SpecQuality } else { @@ -2090,7 +2089,7 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID if productInfo.Weight != 0 { skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(productInfo.Weight)))) } else { - skuName.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(productInfo.SpecQuality)))) + skuName.Skus[0].Weight = utils.Float32ToInt(v.SpecQuality) } skuName.Skus[0].SpecQuality = productInfo.SpecQuality } @@ -2168,11 +2167,6 @@ func CreateUpcSkuByExcelBin(ctx *jxcontext.Context, reader io.Reader, categoryID skuName.CategoryID = categoryID } skuName.Skus[0].Status = model.SkuStatusNormal - if resBinary, _, err := jxutils.DownloadFileByURL(skuName.Img); err == nil { - if model.ValidMimeTypes[http.DetectContentType(resBinary)] == 0 { - skuName.Img = model.NOSkuNameImg - } - } _, err = AddSkuName(ctx, skuName, ctx.GetUserName()) if err != nil { retVal = []*CreateUpcSkuByExcelErr{buildCreateUpcSkuByExcelErr(v, err.Error())} @@ -2326,8 +2320,8 @@ func AddSkuNameByUpc(ctx *jxcontext.Context, upc string, store *dao.StoreDetail, } //我们商品库中有这个商品,但是upc没有填,则尝试用upc去查一下 productInfos, err2 := GetJdUpcCodeByName(ctx, "", upc) + getNetUpcInfo, err2 := api.AliUpcAPI.GetNetUpcInfo(upc) err = err2 - getNetUpcInfo, err := api.AliUpcAPI.GetNetUpcInfo(upc) //表示用upc也没有找到这个商品 if len(productInfos) == 0 { if getNetUpcInfo == nil || getNetUpcInfo.SpecQuality == 0 || getNetUpcInfo.Unit == "" { @@ -2336,18 +2330,9 @@ func AddSkuNameByUpc(ctx *jxcontext.Context, upc string, store *dao.StoreDetail, if len(v.PictureList) > 0 { skuNameExt.Img = v.PictureList[0] } else { - var result *aliupcapi.GetAliUpcInfoResult - upcDepot, err := dao.GetUpcDepot(db, upc) - if upcDepot == nil { - result, err = api.AliUpcAPI.GetAliUpcInfo(upc) - if err == nil { - err = dao.InsertUpcDepot(db, result) - } - } else { - result = upcDepot - } - if result.Img != "" { - skuNameExt.Img = result.Img + img := getImgFromNet(db, upc) + if img != "" { + skuNameExt.Img = img } else { skuNameExt.Img = model.NOSkuNameImg } @@ -2356,40 +2341,95 @@ func AddSkuNameByUpc(ctx *jxcontext.Context, upc string, store *dao.StoreDetail, skuNameExt.Unit = getNetUpcInfo.Unit skuNameExt.Skus[0].SpecQuality = getNetUpcInfo.SpecQuality skuNameExt.Skus[0].SpecUnit = getNetUpcInfo.SpecUnit - skuNameExt.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(getNetUpcInfo.SpecQuality)))) + skuNameExt.Skus[0].Weight = utils.Float32ToInt(getNetUpcInfo.SpecQuality) } else { productInfo := productInfos[0] - skuNames2, err := dao.GetSkuNames(db, nil, nil, productInfo.Name, false) + if (productInfo.SpecQuality == 0 || productInfo.Unit == "") && (getNetUpcInfo == nil || getNetUpcInfo.SpecQuality == 0 || getNetUpcInfo.Unit == "") { + return fmt.Errorf("此商品无规格无法创建,upc :[%s] , unit: [%s], specQuality : [%v]", upc, getNetUpcInfo.Unit, getNetUpcInfo.SpecQuality) + } + if productInfo.SpecQuality == 0 { + skuNameExt.Skus[0].SpecQuality = getNetUpcInfo.SpecQuality + skuNameExt.Skus[0].Weight = utils.Float32ToInt(getNetUpcInfo.SpecQuality) + } else { + skuNameExt.Skus[0].SpecQuality = productInfo.SpecQuality + skuNameExt.Skus[0].Weight = utils.Float32ToInt(productInfo.SpecQuality) + } + if productInfo.SpecUnit == "" { + skuNameExt.Skus[0].SpecUnit = getNetUpcInfo.SpecUnit + } else { + skuNameExt.Skus[0].SpecUnit = productInfo.SpecUnit + } + if len(productInfo.ImgList) > 0 { + skuNameExt.Img = productInfo.ImgList[0] + } else { + img := getImgFromNet(db, upc) + if img != "" { + skuNameExt.Img = img + } else { + skuNameExt.Img = model.NOSkuNameImg + } + } + } + if flag := checkAndUpdateUpc(ctx, db, skuNameExt, v, store); !flag { + if !strings.Contains(skuNameExt.Img, "image.jxc4.com") { + downloadURL, err := uploadImgStandard(skuNameExt.Img) + if err != nil { + skuNameExt.Img = model.NOSkuNameImg + } else { + skuNameExt.Img = downloadURL + } + } + if resBinary, _, err := jxutils.DownloadFileByURL(skuNameExt.Img); err == nil { + if model.ValidMimeTypes[http.DetectContentType(resBinary)] == 0 { + skuNameExt.Img = model.NOSkuNameImg + } + } + if skuNameExt.SpecUnit == model.SpecUnitNames[1] || skuNameExt.SpecUnit == model.SpecUnitNames[2] || + skuNameExt.SpecUnit == "KG" || skuNameExt.SpecUnit == "l" { + skuNameExt.Skus[0].Weight = skuNameExt.Skus[0].Weight * 1000 + } + outSkuNameExt, err := AddSkuName(ctx, skuNameExt, ctx.GetUserName()) if err != nil { return err } - if len(skuNames2) > 1 { - return fmt.Errorf("此商品名在京西库中查询出了大于1个商品,[%v]", productInfo.Name) - } - //表示查到了,需要把upc更新上去,没查到就要新建 - if len(skuNames2) == 1 && (productInfo.SpecQuality == skuNames2[0].SpecQuality && productInfo.SpecUnit == skuNames2[0].SpecUnit) { - skuNames2[0].Upc = &upc - dao.UpdateEntity(db, skuNames2[0], "Upc") - buildStoreSkuBindInfosAndFocus(ctx, db, store, v, skuNames2[0].ID) - } else { - if len(v.SkuList) > 0 { - skuNameExt.Skus[0].SpecQuality = productInfo.SpecQuality - skuNameExt.Skus[0].SpecUnit = productInfo.SpecUnit - skuNameExt.Skus[0].Weight = int(utils.Str2Int64(utils.Float64ToStr(float64(productInfo.Weight)))) - } - if len(productInfo.ImgList) > 0 { - skuNameExt.Img = productInfo.ImgList[0] - } - } + buildStoreSkuBindInfosAndFocus(ctx, db, store, v, outSkuNameExt.ID) } - outSkuNameExt, err := AddSkuName(ctx, skuNameExt, ctx.GetUserName()) - if err != nil { - return err - } - buildStoreSkuBindInfosAndFocus(ctx, db, store, v, outSkuNameExt.ID) } else { buildStoreSkuBindInfosAndFocus(ctx, db, store, v, skuNames[0].ID) } - return err } + +//该商品名,规格在库中能查询出则更新upc不插入 +func checkAndUpdateUpc(ctx *jxcontext.Context, db *dao.DaoDB, skuNameExt *model.SkuNameExt, v *partner.SkuNameInfo, store *dao.StoreDetail) (flag bool) { + skuNames2, err := dao.GetSkuNames(db, nil, nil, skuNameExt.Name, false) + if err != nil { + return false + } + if len(skuNames2) == 0 { + return false + } + //表示查到了,需要把upc更新上去,没查到就要新建 + if skuNameExt.SpecQuality == skuNames2[0].SpecQuality { + skuNames2[0].Upc = skuNameExt.Upc + dao.UpdateEntity(db, skuNames2[0], "Upc") + buildStoreSkuBindInfosAndFocus(ctx, db, store, v, skuNames2[0].ID) + return true + } + return false +} + +func getImgFromNet(db *dao.DaoDB, upc string) (img string) { + var result *aliupcapi.GetAliUpcInfoResult + upcDepot, err := dao.GetUpcDepot(db, upc) + if upcDepot == nil { + result, err = api.AliUpcAPI.GetAliUpcInfo(upc) + if err == nil { + err = dao.InsertUpcDepot(db, result) + } + } else { + result = upcDepot + } + img = result.Img + return img +} diff --git a/business/jxstore/cms/sync.go b/business/jxstore/cms/sync.go index 0db73ca98..9b89fa5a5 100644 --- a/business/jxstore/cms/sync.go +++ b/business/jxstore/cms/sync.go @@ -916,12 +916,13 @@ func syncStoreSkusFromYb(ctx *jxcontext.Context, storeID, vendorID int, vendorSt db = dao.GetDB() localSkuMap = make(map[string]*dao.StoreSkuSyncInfo) vendorSkuMap = make(map[string]*partner.SkuNameInfo) - addList []*partner.SkuNameInfo - updateList []*partner.SkuNameInfo - deleteList []*dao.StoreSkuSyncInfo + // skuBindInfosDel []*StoreSkuBindInfo + // skuBindInfosUpt []*StoreSkuBindInfo + addList []*partner.SkuNameInfo + updateList []*partner.SkuNameInfo + deleteList []*dao.StoreSkuSyncInfo ) handler, _ := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.ISingleStoreStoreSkuHandler) - store, _ := dao.GetStoreDetail(db, storeID, vendorID) localSkuList, err := dao.GetStoreSkus2(db, vendorID, storeID, nil, false) if err != nil { @@ -949,31 +950,97 @@ func syncStoreSkusFromYb(ctx *jxcontext.Context, storeID, vendorID int, vendorSt deleteList = append(deleteList, v) } } - taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) { - switch step { - case 0: - if len(addList) > 0 { - taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { - var ( - v = batchItemList[0].(*partner.SkuNameInfo) - upc = v.YbBarCode - ) - err = AddSkuNameByUpc(ctx, upc, store, v) - if err != nil { - task.AddFailedList(putils.GetErrMsg2FailedSingleList(nil, err, storeID, model.VendorChineseNames[vendorID], "根据upc创建京西商品")) - } - return retVal, err - } - taskParallel := tasksch.NewParallelTask("创建商品", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, taskFunc, addList) - tasksch.HandleTask(taskParallel, task, true).Run() - _, err = taskParallel.GetResult(0) - } - case 1: - } - return result, err - } - taskSeq := tasksch.NewSeqTask2("同步银豹商品到京西", ctx, true, taskSeqFunc, 3) - tasksch.HandleTask(taskSeq, nil, true).Run() - hint = taskSeq.GetID() + fmt.Println("test111111111111111", storeID, vendorStoreID) + fmt.Println("addList", utils.Format4Output(addList, false)) + fmt.Println("updateList", utils.Format4Output(updateList, false)) + fmt.Println("deleteList", utils.Format4Output(deleteList, false)) + // taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {\ + // store, _ := dao.GetStoreDetail(db, storeID, vendorID) + // switch step { + // case 0: + // if len(addList) > 0 { + // taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + // var ( + // v = batchItemList[0].(*partner.SkuNameInfo) + // upc = v.YbBarCode + // ) + // err = AddSkuNameByUpc(ctx, upc, store, v) + // if err != nil { + // task.AddFailedList(putils.GetErrMsg2FailedSingleList(nil, err, storeID, model.VendorChineseNames[vendorID], "根据upc创建京西商品")) + // } + // return retVal, err + // } + // taskParallel := tasksch.NewParallelTask("创建商品", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, taskFunc, addList) + // tasksch.HandleTask(taskParallel, task, true).Run() + // _, err = taskParallel.GetResult(0) + // } + // case 1: + // if len(deleteList) > 0 { + // taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + // var ( + // v = batchItemList[0].(*dao.StoreSkuSyncInfo) + // ) + // skuBindInfo := &StoreSkuBindInfo{ + // NameID: v.NameID, + // IsFocus: -1, + // } + // retVal = []*StoreSkuBindInfo{skuBindInfo} + // return retVal, err + // } + // taskParallel := tasksch.NewParallelTask("删除商品", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, taskFunc, deleteList) + // tasksch.HandleTask(taskParallel, task, true).Run() + // resultDel, _ := taskParallel.GetResult(0) + // for _, v := range resultDel { + // skuBindInfosDel = append(skuBindInfosDel, v.(*StoreSkuBindInfo)) + // } + // _, err = updateStoresSkusWithoutSync(ctx, db, []int{storeID}, skuBindInfosDel, false) + // } + // case 2: + // if len(updateList) > 0 { + // taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + // var ( + // v = batchItemList[0].(*partner.SkuNameInfo) + // skuBindInfo = &StoreSkuBindInfo{} + // storeSkus []*dao.StoreSkuExt + // pricePercentagePack []*model.PricePercentageItem + // ) + // sql := ` + // SELECT a.*, c.id name_id + // FROM store_sku_bind a + // JOIN sku b ON a.sku_id = b.id + // JOIN sku_name c ON c.id = b.name_id + // WHERE a.store_id = ? AND a.yb_id = ? AND a.deleted_at = ? + // ` + // sqlParams := []interface{}{storeID, v.SkuList[0].VendorSkuID, utils.DefaultTimeValue} + // err = dao.GetRows(db, &storeSkus, sql, sqlParams) + // if len(storeSkus) > 0 { + // if storeSkus[0].YbPrice != int(v.SkuList[0].VendorPrice) { + // err = jxutils.Strings2Objs(store.PricePercentagePackStr, &pricePercentagePack) + // skuBindInfo.UnitPrice = jxutils.CaculateJxPriceByPricePack(pricePercentagePack, 0, int(v.SkuList[0].VendorPrice)) + // } + // } else { + // return retVal, fmt.Errorf("未查询到门店商品,yb_id [%v]", v.SkuList[0].VendorSkuID) + // } + // if v.SkuList[0].Stock < 1 { + // skuBindInfo.IsSale = model.DISABLED + // } + // skuBindInfo.NameID = storeSkus[0].NameID + // retVal = []*StoreSkuBindInfo{skuBindInfo} + // return retVal, err + // } + // taskParallel := tasksch.NewParallelTask("更新商品价格和库存", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, taskFunc, deleteList) + // tasksch.HandleTask(taskParallel, task, true).Run() + // resultUpt, _ := taskParallel.GetResult(0) + // for _, v := range resultUpt { + // skuBindInfosUpt = append(skuBindInfosUpt, v.(*StoreSkuBindInfo)) + // } + // _, err = updateStoresSkusWithoutSync(ctx, db, []int{storeID}, skuBindInfosUpt, false) + // } + // } + // return result, err + // } + // taskSeq := tasksch.NewSeqTask2("同步银豹商品到京西", ctx, true, taskSeqFunc, 3) + // tasksch.HandleTask(taskSeq, nil, true).Run() + // hint = taskSeq.GetID() return hint, err } diff --git a/controllers/cms_sync.go b/controllers/cms_sync.go index 0d823e563..c7303ec66 100644 --- a/controllers/cms_sync.go +++ b/controllers/cms_sync.go @@ -347,3 +347,22 @@ func (c *SyncController) UploadFakeJdThingMap() { }) } } + +// @Title 同步银豹到京西商品 +// @Description 同步银豹到京西商品 +// @Param token header string true "认证token" +// @Param storeIDs formData string true "门店ID列表" +// @Param isAsync formData bool true "是否异步操作" +// @Param isContinueWhenError formData bool false "单个同步失败是否继续,缺省false" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /SyncStoreSkusFromYb [put] +func (c *SyncController) SyncStoreSkusFromYb() { + c.callSyncStoreSkusFromYb(func(params *tSyncSyncStoreSkusFromYbParams) (retVal interface{}, errCode string, err error) { + var storeIDs []int + if err = jxutils.Strings2Objs(params.StoreIDs, &storeIDs); err == nil { + cms.CurVendorSync.SyncStoreSkusFromYb(params.Ctx, storeIDs, params.IsAsync, params.IsContinueWhenError) + } + return retVal, "", err + }) +} diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index 0f2ee4d7e..f03a8aefa 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -2223,6 +2223,15 @@ func init() { Filters: nil, Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SyncController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SyncController"], + beego.ControllerComments{ + Method: "SyncStoreSkusFromYb", + Router: `/SyncStoreSkusFromYb`, + AllowHTTPMethods: []string{"put"}, + MethodParams: param.Make(), + Filters: nil, + Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SyncController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SyncController"], beego.ControllerComments{ Method: "SyncStores",