diff --git a/business/jxstore/cms/store_sku_check.go b/business/jxstore/cms/store_sku_check.go index 8e7993abe..4c8ff1dd8 100644 --- a/business/jxstore/cms/store_sku_check.go +++ b/business/jxstore/cms/store_sku_check.go @@ -7,18 +7,23 @@ import ( "git.rosy.net.cn/jx-callback/business/jxutils/jxcontext" "git.rosy.net.cn/jx-callback/business/model" "git.rosy.net.cn/jx-callback/business/partner" + "git.rosy.net.cn/jx-callback/business/partner/putils" "git.rosy.net.cn/baseapi" "git.rosy.net.cn/jx-callback/business/jxutils/tasksch" "git.rosy.net.cn/jx-callback/business/jxutils/excel" ) const ( - diffFileName = "export/京西和平台商品差异.xlsx" + canWriteTolocal = true + parallelCount = 10 + fileExt = ".xlsx" + diffFileName = "export/京西和平台商品差异" ) + var ( vendorNameList = map[int]string { - // model.VendorIDMTWM: "美团", - // model.VendorIDEBAI: "饿百", + model.VendorIDMTWM: "美团", + model.VendorIDEBAI: "饿百", model.VendorIDJD: "京东", } titleList = []string { @@ -31,7 +36,8 @@ var ( "平台可售状态", } diffData map[int][]DiffData - JdAllSkuInfoMap map[int]*partner.SkuNameInfo + jdAllSkuInfoMap map[int]*partner.SkuNameInfo + jdAllSkuInfoList []*partner.StoreSkuInfo ) type DiffData struct { @@ -69,53 +75,59 @@ func GetFilterVendorSkuInfoMap(vendorSkuInfoList []*partner.SkuNameInfo) map[int func GetFilterJdSkuInfoMap(jdSkuInfoList []*partner.StoreSkuInfo) map[int]*partner.StoreSkuInfo { filterJdSkuInfoMap := make(map[int]*partner.StoreSkuInfo) for _, value := range jdSkuInfoList { - filterJdSkuInfoMap[value.SkuID] = value + if value.Stock > 0 { + filterJdSkuInfoMap[value.SkuID] = value + } } return filterJdSkuInfoMap } -func GetJdAllSkuInfoMap() map[int]*partner.SkuNameInfo { - if JdAllSkuInfoMap == nil { - JdAllSkuInfoMap = make(map[int]*partner.SkuNameInfo) - ctx := jxcontext.AdminCtx - vendorID := model.VendorIDJD - if multiHandler, ok := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IMultipleStoresHandler); ok { - jdAllSkuInfoList, err := multiHandler.GetSkus(ctx, 0, "", "") - if err != nil { - baseapi.SugarLogger.Errorf("GetJdAllSkuInfoMap error:%v", err) - } else { - for _, value := range jdAllSkuInfoList { - for _, skuInfo := range value.SkuList { - JdAllSkuInfoMap[skuInfo.SkuID] = value +func GetJdAllSkuInfo() { + if _, ok := vendorNameList[model.VendorIDJD]; ok { + if jdAllSkuInfoMap == nil { + jdAllSkuInfoMap = make(map[int]*partner.SkuNameInfo) + ctx := jxcontext.AdminCtx + vendorID := model.VendorIDJD + if multiHandler, ok := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IMultipleStoresHandler); ok { + jdAllSkuNameInfoList, err := multiHandler.GetSkus(ctx, 0, "", "") + if err != nil { + baseapi.SugarLogger.Errorf("GetJdAllSkuInfo error:%v", err) + } else { + jdAllSkuInfoList = putils.StoreSkuFullList2Bare(jdAllSkuNameInfoList) + for _, value := range jdAllSkuNameInfoList { + for _, skuInfo := range value.SkuList { + jdAllSkuInfoMap[skuInfo.SkuID] = value + } } } } } } - - return JdAllSkuInfoMap } func GetSkuStatusName(status int) string { - if status == 1 { + if status == model.SkuStatusNormal { return "可售" - } else { + } else if status == model.SkuStatusDontSale { return "不可售" + } else if status == model.SkuStatusDeleted { + return "删除" } + + return "" } func CompareJxAndJd(vendorID int, storeIDStr, vendorStoreID, storeName string, filterJxSkuInfoMap map[int]*StoreSkuNameExt, filterJdSkuInfoMap map[int]*partner.StoreSkuInfo) { - JdAllSkuInfoMap := GetJdAllSkuInfoMap() for skuID, jxSkuInfo := range filterJxSkuInfoMap { jxSkuDetailName := jxutils.ComposeSkuName(jxSkuInfo.SkuName.Prefix, jxSkuInfo.SkuName.Name, "", jxSkuInfo.SkuName.Unit, jxSkuInfo.SkuName.SpecQuality, jxSkuInfo.SkuName.SpecUnit, 0) jxSkuStatusName := GetSkuStatusName(jxSkuInfo.Skus2[0].SkuStatus) vendorSkuInfo := filterJdSkuInfoMap[skuID] if vendorSkuInfo != nil { - vendorSkuInfo := JdAllSkuInfoMap[vendorSkuInfo.SkuID] + vendorSkuInfo := jdAllSkuInfoMap[vendorSkuInfo.SkuID] vendorSkuDetailName := vendorSkuInfo.SkuList[0].SkuName vendorSkuStatusName := GetSkuStatusName(vendorSkuInfo.SkuList[0].Status) if jxSkuStatusName != vendorSkuStatusName || jxSkuDetailName != vendorSkuDetailName { @@ -128,7 +140,7 @@ func CompareJxAndJd(vendorID int, storeIDStr, vendorStoreID, storeName string, } } for skuID, _ := range filterJdSkuInfoMap { - vendorSkuInfo := JdAllSkuInfoMap[skuID] + vendorSkuInfo := jdAllSkuInfoMap[skuID] vendorSkuDetailName := vendorSkuInfo.SkuList[0].SkuName vendorSkuStatusName := GetSkuStatusName(vendorSkuInfo.SkuList[0].Status) @@ -186,32 +198,59 @@ func CompareJxAndVendor(vendorID int, storeIDStr, vendorStoreID, storeName strin } } -func CheckSkuDiffBetweenJxAndVendor() { +func CheckSkuDiffBetweenJxAndVendor(vendorIDList []string, storeIDList []string) { startProcessTime := time.Now().Unix() + vendorMap := make(map[int]bool) + for _, value := range vendorIDList { + vendorID := int(utils.Str2Int64(value)) + vendorMap[vendorID] = true + } + storeIDMap := make(map[int]bool) + for _, value := range storeIDList { + storeID := int(utils.Str2Int64(value)) + storeIDMap[storeID] = true + } baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor start time: %v", time.Now()) ctx := jxcontext.AdminCtx jxStoreInfoList, err := GetStores(ctx, "", map[string]interface{}{}, 0, -1, utils.DefaultTimeValue, utils.DefaultTimeValue, 0, 0) if err != nil { baseapi.SugarLogger.Errorf("CheckSkuDiffBetweenJxAndVendor GetStores error:%v", err) } else { + GetJdAllSkuInfo() diffData = make(map[int][]DiffData) taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { jxStoreInfoListValue := batchItemList[0].(*StoreExt) storeID := jxStoreInfoListValue.ID + //filter for storeID + if len(storeIDMap) > 0 { + if _, ok := storeIDMap[storeID]; !ok { + return retVal, err + } + } storeIDStr := utils.Int2Str(storeID) storeName := jxStoreInfoListValue.Name if jxStoreInfoListValue.StoreMaps != nil { - jxSkuInfoData, _ := GetStoreSkus(ctx, storeID, []int{}, true, "", true, map[string]interface{}{}, 0, -1) - filterJxSkuInfoMap := GetFilterJxSkuInfoMap(jxSkuInfoData.SkuNames) - for _, vendorListValue := range jxStoreInfoListValue.StoreMaps { + for vendorListIndex, vendorListValue := range jxStoreInfoListValue.StoreMaps { vendorID := int(utils.MustInterface2Int64(vendorListValue["vendorID"])) + //filter for vendorID + if len(vendorMap) > 0 { + if _, ok := vendorMap[vendorID]; !ok { + return retVal, err + } + } if _, ok := vendorNameList[vendorID]; ok { + var filterJxSkuInfoMap map[int]*StoreSkuNameExt + if vendorListIndex == 0 { + jxSkuInfoData, _ := GetStoreSkus(ctx, storeID, []int{}, true, "", true, map[string]interface{}{}, 0, -1) + filterJxSkuInfoMap = GetFilterJxSkuInfoMap(jxSkuInfoData.SkuNames) + } + vendorStoreID := utils.Interface2String(vendorListValue["vendorStoreID"]) baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor storeID:%d vendorID:%d vendorStoreID:%s vendorListValue:%v", storeID, vendorID, vendorStoreID, vendorListValue) if vendorID == model.VendorIDJD { - singleStoreHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.ISingleStoreStoreSkuHandler) - jdSkuInfoList, err := singleStoreHandler.GetStoreSkusBareInfo(ctx, nil, storeID, vendorStoreID, nil) + singleStoreHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IPurchasePlatformStoreSkuHandler) + jdSkuInfoList, err := singleStoreHandler.GetStoreSkusBareInfo(ctx, nil, storeID, vendorStoreID, jdAllSkuInfoList) if err != nil { baseapi.SugarLogger.Errorf("CheckSkuDiffBetweenJxAndVendor GetStoreSkusBareInfo error:%v", err) } else if len(jdSkuInfoList) > 0 { @@ -233,7 +272,7 @@ func CheckSkuDiffBetweenJxAndVendor() { } return retVal, err } - task := tasksch.NewParallelTask("CheckSkuDiffBetweenJxAndVendor", nil, ctx, taskFunc, jxStoreInfoList.Stores) + task := tasksch.NewParallelTask("CheckSkuDiffBetweenJxAndVendor", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc, jxStoreInfoList.Stores) tasksch.HandleTask(task, nil, false).Run() _, err = task.GetResult(0) if err != nil { @@ -261,17 +300,19 @@ func WriteToExcel(data map[int][]DiffData) { } if len(sheetList) > 0 { excelBin := excel.Obj2Excel(sheetList) - err := jxutils.WriteFile(diffFileName, excelBin) - if err != nil { - baseapi.SugarLogger.Errorf("WriteToExcel:save %s failed, error %v", diffFileName, err) - } else { - baseapi.SugarLogger.Debugf("WriteToExcel:save %s success", diffFileName) - downloadURL, err := jxutils.UploadExportContent(excelBin, diffFileName) - if err != nil { - baseapi.SugarLogger.Errorf("WriteToExcel:upload %s failed error:%v", diffFileName, err) - } else { - baseapi.SugarLogger.Debugf("WriteToExcel:upload %s success, downloadURL:%s", diffFileName, downloadURL) - } + timeStr := utils.Int64ToStr(time.Now().Unix()) + diffFullFileName := diffFileName + timeStr + fileExt + if canWriteTolocal { + jxutils.WriteFile(diffFullFileName, excelBin) } + baseapi.SugarLogger.Debugf("WriteToExcel:save %s success", diffFullFileName) + downloadURL, err := jxutils.UploadExportContent(excelBin, diffFullFileName) + if err != nil { + baseapi.SugarLogger.Errorf("WriteToExcel:upload %s failed error:%v", diffFullFileName, err) + } else { + baseapi.SugarLogger.Debugf("WriteToExcel:upload %s success, downloadURL:%s", diffFullFileName, downloadURL) + } + } else { + baseapi.SugarLogger.Debug("WriteToExcel:No diff data!!!") } } \ No newline at end of file diff --git a/business/jxstore/cms/store_sku_check_test.go b/business/jxstore/cms/store_sku_check_test.go index ef06962e8..28250dc25 100644 --- a/business/jxstore/cms/store_sku_check_test.go +++ b/business/jxstore/cms/store_sku_check_test.go @@ -5,5 +5,7 @@ import ( ) func TestCheckSkuDiffBetweenJxAndVendor(t *testing.T) { - CheckSkuDiffBetweenJxAndVendor() + vendorIDList := []string{} + storeIDList := []string{} + CheckSkuDiffBetweenJxAndVendor(vendorIDList, storeIDList) } \ No newline at end of file diff --git a/controllers/temp_op.go b/controllers/temp_op.go index f92a2dcef..a5fc3f47f 100644 --- a/controllers/temp_op.go +++ b/controllers/temp_op.go @@ -1,6 +1,7 @@ package controllers import ( + "strings" "git.rosy.net.cn/baseapi/platformapi/ebaiapi" "git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/jx-callback/business/jxstore/tempop" @@ -295,12 +296,22 @@ func (c *TempOpController) TestStartOrEndOpStore() { // @Title 京西平台和其他平台商品的对比 // @Description 京西平台和其他平台商品的对比 // @Param token header string true "认证token" +// @Param vendorIDList query string false "运营商ID列表(京东0 美团1 饿百3)" +// @Param storeIDList query string false "京西门店ID列表" // @Success 200 {object} controllers.CallResult // @Failure 200 {object} controllers.CallResult // @router /CheckSkuDiffBetweenJxAndVendor [get] func (c *TempOpController) CheckSkuDiffBetweenJxAndVendor() { c.callCheckSkuDiffBetweenJxAndVendor(func(params *tTempopCheckSkuDiffBetweenJxAndVendorParams) (retVal interface{}, errCode string, err error) { - cms.CheckSkuDiffBetweenJxAndVendor() + var vendorIDList []string + if params.VendorIDList != "" { + vendorIDList = strings.Split(params.VendorIDList, ",") + } + var storeIDList []string + if params.StoreIDList != "" { + storeIDList = strings.Split(params.StoreIDList, ",") + } + cms.CheckSkuDiffBetweenJxAndVendor(vendorIDList, storeIDList) return retVal, "", err }) }