From 7182ea87d5ea352711128eae2298ba4f9a89a5f2 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, 11 Dec 2019 15:28:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B0=83=E4=BB=B7=E5=8C=85?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- business/jxstore/cms/cms.go | 2 +- business/jxstore/cms/store.go | 4 +- business/jxstore/cms/store_sku.go | 131 ++++++++++++++++++++++++++---- 3 files changed, 119 insertions(+), 18 deletions(-) diff --git a/business/jxstore/cms/cms.go b/business/jxstore/cms/cms.go index 87f88d2c6..59f0ecda8 100644 --- a/business/jxstore/cms/cms.go +++ b/business/jxstore/cms/cms.go @@ -359,7 +359,7 @@ func UpdateConfig(ctx *jxcontext.Context, key, configType, value string) (hint s if vendorID != model.VendorIDJX { dao.SetStoreSkuSyncStatus(db, vendorID, storeIDs, nil, model.SyncFlagPriceMask) } else { - ReCalculateJxPrice(db, ctx, storeIDs) + hint, err = ReCalculateJxPrice(db, ctx, storeIDs) } } case model.ConfigTypeFreightPack: diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index 8f99c24aa..93a587f15 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -1058,7 +1058,7 @@ func AddStoreVendorMap(ctx *jxcontext.Context, db *dao.DaoDB, vendorID int, vend err = ErrCanNotFindVendor } } else { - ReCalculateJxPrice(db, ctx, []int{storeID}) + ReCalculateJxPriceLight(db, ctx, storeID) } if err == nil { dao.WrapAddIDCULDEntity(storeMap, userName) @@ -1201,7 +1201,7 @@ func UpdateStoreVendorMap(ctx *jxcontext.Context, db *dao.DaoDB, storeID, vendor } } else { if valid["pricePercentage"] != nil || valid["pricePercentagePack"] != nil { - ReCalculateJxPrice(db, ctx, []int{storeID}) + ReCalculateJxPriceLight(db, ctx, storeID) } } } diff --git a/business/jxstore/cms/store_sku.go b/business/jxstore/cms/store_sku.go index 779a67c96..6358981de 100644 --- a/business/jxstore/cms/store_sku.go +++ b/business/jxstore/cms/store_sku.go @@ -10,6 +10,9 @@ import ( "sync" "time" + "git.rosy.net.cn/baseapi" + + "git.rosy.net.cn/jx-callback/business/jxutils/excel" "git.rosy.net.cn/jx-callback/business/jxutils/tasksch" "git.rosy.net.cn/jx-callback/business/partner/purchase/jd" @@ -112,15 +115,16 @@ type SheetParam struct { } type DataSuccess struct { - NameID int `json:"商品编码"` + NameID int `json:"商品NameID"` Name string `json:"商品名称"` + Unit string `json:"单位"` OrgPrice float64 `json:"原价"` NowPrice float64 `json:"现价"` MixPrice float64 `json:"涨跌"` } type DataFailed struct { - NameID string `json:"商品ID"` + NameID int `json:"商品NameID"` Name string `json:"商品名称"` Comment string `json:"备注"` } @@ -144,7 +148,20 @@ var ( "18180948107": 1, // 徐 // "13684045763": 1, // 周 } - dataLock DataLock + dataLock DataLock + titleListSuccess = []string{ + "商品NameID", + "商品名称", + "单位", + "原价", + "现价", + "涨跌", + } + titleListFailed = []string{ + "商品NameID", + "商品名称", + "备注", + } ) func GetStoreSkus(ctx *jxcontext.Context, storeID int, skuIDs []int, isFocus bool, keyword string, isBySku, isAct bool, params map[string]interface{}, offset, pageSize int) (skuNamesInfo *dao.StoreSkuNamesInfo, err error) { @@ -680,8 +697,6 @@ func GetStoresSkusSaleInfo(ctx *jxcontext.Context, storeIDs []int, skuIDs []int, HAVING count >= ? AND count <= ? ` sqlParams = append(sqlParams, fromCount, toCount) - // fmt.Println(sql) - // fmt.Println(utils.Format4Output(sqlParams, false)) if err = dao.GetRows(db, &saleInfoList, sql, sqlParams...); err == nil { // globals.SugarLogger.Debug(utils.Format4Output(saleInfoList, false)) return saleInfoList, nil @@ -2058,8 +2073,31 @@ func AutoSaleStoreSku(ctx *jxcontext.Context, storeIDs []int, isNeedSync bool) ( return err } -func ReCalculateJxPrice(db *dao.DaoDB, ctx *jxcontext.Context, storeIDs []int) (err error) { - for _, storeID := range storeIDs { +func ReCalculateJxPrice(db *dao.DaoDB, ctx *jxcontext.Context, storeIDs []int) (hint string, err error) { + task := tasksch.NewParallelTask("刷新京西平台价格", tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, + func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { + storeID := batchItemList[0].(int) + if storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDJX); err == nil { + if storeSkuList, err := dao.GetStoresSkusInfo(db, []int{storeID}, nil); err == nil { + for _, skuBind := range storeSkuList { + skuBind.JxPrice = jxutils.CaculatePriceByPricePack(storeDetail.PricePercentagePackObj, int(storeDetail.PricePercentage), skuBind.Price) + dao.UpdateEntity(db, skuBind) + } + } else { + return "", err + } + } else { + return "", err + } + return retVal, err + }, storeIDs) + tasksch.HandleTask(task, nil, true).Run() + hint = task.GetID() + return hint, err +} + +func ReCalculateJxPriceLight(db *dao.DaoDB, ctx *jxcontext.Context, storeID int) (err error) { + if storeID != 0 { if storeDetail, err := dao.GetStoreDetail(db, storeID, model.VendorIDJX); err == nil { if storeSkuList, err := dao.GetStoresSkusInfo(db, []int{storeID}, nil); err == nil { for _, skuBind := range storeSkuList { @@ -2172,19 +2210,42 @@ func RefreshJxPriceByExcelBin(ctx *jxcontext.Context, storeIDs []int, reader str for _, v := range nameIDGroup { if v != "" { nameID := int(utils.Str2Int64(v)) - storeSkuBindInfo := &StoreSkuBindInfo{ - NameID: nameID, - UnitPrice: storeSkuNamePrice.Price, - } - skuBindInfos = append(skuBindInfos, storeSkuBindInfo) for _, vv := range storeIDs { skuList, err2 := dao.GetStoreSkusByNameIDs(db, []int{vv}, nameID) err = err2 if len(skuList) > 0 { - outSuccess := DataSuccess{NameID: nameID} + storeSkuBindInfo := &StoreSkuBindInfo{ + NameID: nameID, + UnitPrice: storeSkuNamePrice.Price, + } + skuBindInfos = append(skuBindInfos, storeSkuBindInfo) + outSuccess := DataSuccess{ + NameID: nameID, + Name: skuList[0].Name, + Unit: storeSkuNamePrice.Unit, + OrgPrice: utils.Str2Float64(utils.Int64ToStr(skuList[0].UnitPrice / 100)), + NowPrice: utils.Str2Float64(utils.Int64ToStr(int64(storeSkuNamePrice.Price / 100))), + MixPrice: utils.Str2Float64(utils.Int64ToStr(int64(storeSkuNamePrice.Price) - skuList[0].UnitPrice)), + } dataLock.AppendDataSuccess(outSuccess) + } else { + //京西xx门店没有关注该商品 + outFailed := DataFailed{ + NameID: nameID, + Name: storeSkuNamePrice.Name, + Comment: fmt.Sprintf("京西[%v]门店没有关注该商品,商品nameID[%v],Excel上商品名[%v]", vv, nameID, storeSkuNamePrice.Name), + } + dataLock.AppendDataFailed(outFailed) } } + } else { + //nameID为空,还未填写nameID + outFailed := DataFailed{ + NameID: 0, + Name: storeSkuNamePrice.Name, + Comment: fmt.Sprintf("商品nameID为空!,还未填写商品nameID,Excel上商品名[%v]", storeSkuNamePrice.Name), + } + dataLock.AppendDataFailed(outFailed) } } retVal = skuBindInfos @@ -2194,14 +2255,18 @@ func RefreshJxPriceByExcelBin(ctx *jxcontext.Context, storeIDs []int, reader str tasksch.HandleTask(taskParallel, task, true).Run() skuBindInfosInter, err = taskParallel.GetResult(0) case 2: + //更新京西价 for _, v := range skuBindInfosInter { skuBindInfoList = append(skuBindInfoList, v.(*StoreSkuBindInfo)) } - // UpdateStoresSkus(ctx, storeIDs, skuBindInfoList, false, isAsync, isContinueWhenError) + UpdateStoresSkus(ctx, storeIDs, skuBindInfoList, false, isAsync, isContinueWhenError) + case 3: + //写Excel + WriteToExcelJx(task, dataLock.dataSuccessList, dataLock.dataFailedList) } return result, err } - taskSeq := tasksch.NewSeqTask2("根据Excel刷新京西价", ctx, isContinueWhenError, taskSeqFunc, 3) + taskSeq := tasksch.NewSeqTask2("根据Excel刷新京西价", ctx, isContinueWhenError, taskSeqFunc, 4) tasksch.HandleTask(taskSeq, nil, true).Run() if !isAsync { _, err = taskSeq.GetResult(0) @@ -2212,6 +2277,42 @@ func RefreshJxPriceByExcelBin(ctx *jxcontext.Context, storeIDs []int, reader str return hint, err } +func WriteToExcelJx(task *tasksch.SeqTask, dataSuccess []DataSuccess, dataFailed []DataFailed) (err error) { + var sheetList1 []*excel.Obj2ExcelSheetConfig + var sheetList2 []*excel.Obj2ExcelSheetConfig + var downloadURL1, downloadURL2, fileName1, fileName2 string + excelConf1 := &excel.Obj2ExcelSheetConfig{ + Title: "sheet1", + Data: dataSuccess, + CaptionList: titleListSuccess, + } + sheetList1 = append(sheetList1, excelConf1) + excelConf2 := &excel.Obj2ExcelSheetConfig{ + Title: "sheet1", + Data: dataFailed, + CaptionList: titleListFailed, + } + sheetList2 = append(sheetList2, excelConf2) + if excelConf1 != nil { + downloadURL1, fileName1, err = jxutils.UploadExeclAndPushMsg(sheetList1, "京西已更新商品") + } else { + baseapi.SugarLogger.Debug("WriteToExcel: dataSuccess is nil!") + } + if excelConf2 != nil { + downloadURL2, fileName2, err = jxutils.UploadExeclAndPushMsg(sheetList2, "未更新商品") + } else { + baseapi.SugarLogger.Debug("WriteToExcel: dataFailed is nil!") + } + if err != nil { + baseapi.SugarLogger.Errorf("WriteToExcel:upload %s , %s failed error:%v", fileName1, fileName2, err) + } else { + noticeMsg := fmt.Sprintf("[详情点我]path1=%s, path2=%s \n", downloadURL1, downloadURL2) + task.SetNoticeMsg(noticeMsg) + baseapi.SugarLogger.Debugf("WriteToExcel:upload %s ,%s success, downloadURL1:%s ,downloadURL2:%s", fileName1, fileName2, downloadURL1, downloadURL2) + } + return err +} + func CreateOrUpdateStoreSkuNamePriceByExcel(db *dao.DaoDB, ctx *jxcontext.Context, storeSkuNamePriceList []*model.StoreSkuNamePrice, storeSkuNamePriceListOrg []*model.StoreSkuNamePrice) (err error) { storeSkuNamePriceMap := StoreSkuNamePriceList2Map(ctx, storeSkuNamePriceListOrg) dao.Begin(db)