Files
jx-callback/business/jxstore/cms/store_sku_check.go
苏尹岚 e9607ac921 tijiao
2019-10-31 13:55:29 +08:00

513 lines
19 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cms
import (
"fmt"
"strings"
"sync"
"time"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/jxutils/excel"
"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"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/business/partner/putils"
"git.rosy.net.cn/jx-callback/globals"
)
const (
canWriteTolocal = false
needStatistic = true
isFilterToBeCreateAndNotSale = true
parallelCount = 5
fileExt = ".xlsx"
)
const (
DatAanalyse1 = "京东商品库直接创建,正常数据"
DatAanalyse2 = "(可能无经营许可)"
)
var (
diffFileName = map[bool]string{
true: "export/JXCSAndVendorSkuDiff",
false: "export/JXGYAndVendorSkuDiff",
}
vendorNameList = map[int]string{
model.VendorIDMTWM: model.VendorChineseNames[model.VendorIDMTWM],
model.VendorIDEBAI: model.VendorChineseNames[model.VendorIDEBAI],
model.VendorIDJD: model.VendorChineseNames[model.VendorIDJD],
}
titleList = []string{
"京西门店ID",
"平台门店ID",
"门店名",
"SkuID",
"同步状态",
"待创建",
"待删除",
"京西商品名",
"平台商品名",
"京西可售状态",
"平台可售状态",
"数据分析",
}
statisticTitleList = []string{
"京西和平台商品状态",
"待创建",
"京西可售状态",
"数量",
"占比",
}
statisticDataList = [][]string{
{"京西有,平台无", "是", "下架"},
{"京西有,平台无", "是", "上架"},
{"京西有,平台无", "否", "下架"},
{"京西有,平台无", "否", "上架"},
{"京西无,平台有", "N/A", "N/A"},
{"京西有,平台有", "是", "下架"},
{"京西有,平台有", "是", "上架"},
{"京西有,平台有", "否", "下架"},
{"京西有,平台有", "否", "上架"},
}
diffData DiffDataLock
multiStoreAllSkuInfoMap map[int]map[int]*partner.SkuNameInfo
multiStoreAllSkuInfoList map[int][]*partner.StoreSkuInfo
filterCloseSkuIds []int
)
type DiffDataLock struct {
diffDataMap map[int][]DiffData
locker sync.RWMutex
}
type DiffData struct {
JxStoreID string `json:"京西门店ID"`
VendorStoreID string `json:"平台门店ID"`
VendorStoreName string `json:"门店名"`
SkuID string `json:"SkuID"`
SyncStatus string `json:"同步状态"`
ToBeCreate string `json:"待创建"`
ToBeDel string `json:"待删除"`
JxSkuName string `json:"京西商品名"`
VendorSkuName string `json:"平台商品名"`
JxStatus string `json:"京西可售状态"`
VendorStatus string `json:"平台可售状态"`
DatAanalyse string `json:"数据分析"`
}
type StatisticData struct {
JxVendorStatus string `json:"京西和平台商品状态"`
ToBeCreate string `json:"待创建"`
JxSaleStatus string `json:"京西可售状态"`
Count string `json:"数量"`
Percent string `json:"占比"`
}
func (d *DiffDataLock) AppendData(vendorID int, diffData DiffData) {
d.locker.Lock()
defer d.locker.Unlock()
d.diffDataMap[vendorID] = append(d.diffDataMap[vendorID], diffData)
}
func (d *DiffDataLock) InitData() {
d.diffDataMap = make(map[int][]DiffData)
}
func InitMultiStoreData() {
multiStoreAllSkuInfoMap = make(map[int]map[int]*partner.SkuNameInfo)
multiStoreAllSkuInfoList = make(map[int][]*partner.StoreSkuInfo)
}
func GetMultiStoreAllSkuInfoMap(vendorID int) map[int]*partner.SkuNameInfo {
return multiStoreAllSkuInfoMap[vendorID]
}
func GetMultiStoreAllSkuInfoList(vendorID int) []*partner.StoreSkuInfo {
return multiStoreAllSkuInfoList[vendorID]
}
func GetMultiStoreAllSkuInfo(ctx *jxcontext.Context, vendorMap map[int]bool) {
InitMultiStoreData()
filterCloseSkuIds = filterCloseSkuIds[0:0]
for vendorID, _ := range vendorNameList {
//filter for vendorID
if len(vendorMap) > 0 {
if _, ok := vendorMap[vendorID]; !ok {
continue
}
}
if partner.IsMultiStore(vendorID) {
multiHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IMultipleStoresHandler)
allSkuNameInfoList, err := multiHandler.GetSkusForCheck(ctx, 0, "", "")
if err != nil {
baseapi.SugarLogger.Errorf("GetMultiStoreAllSkuInfo error:%v", err)
} else {
multiStoreAllSkuInfoList[vendorID] = putils.StoreSkuFullList2Bare(allSkuNameInfoList) //map[平台ID[]StoreSkuInfo1,StoreSkuInfo2...]
tempMap := make(map[int]*partner.SkuNameInfo)
for _, value := range allSkuNameInfoList {
for _, skuInfo := range value.SkuList {
filterCloseSkuIds = append(filterCloseSkuIds, skuInfo.SkuID)
tempMap[skuInfo.SkuID] = value
}
}
multiStoreAllSkuInfoMap[vendorID] = tempMap //map[平台ID][map[skuID1SkuNameInfo1skuID2SkuNameInfo2]...]
}
}
}
}
func GetFilterJxSkuInfoMap(jxSkuInfoList []*StoreSkuNameExt) map[int]*StoreSkuNameExt {
filterJxSkuInfoMap := make(map[int]*StoreSkuNameExt)
for _, value := range jxSkuInfoList {
for _, skuInfo := range value.Skus2 {
filterJxSkuInfoMap[skuInfo.SkuID] = value
}
}
return filterJxSkuInfoMap
}
func GetFilterVendorSkuInfoMap(vendorSkuInfoList []*partner.SkuNameInfo) map[int]*partner.SkuNameInfo {
filterVendorSkuInfoMap := make(map[int]*partner.SkuNameInfo)
for _, value := range vendorSkuInfoList {
for _, skuInfo := range value.SkuList {
filterVendorSkuInfoMap[skuInfo.SkuID] = value
}
}
return filterVendorSkuInfoMap
}
func GetFilterMultiStoreSkuInfoMap(vendorID int, skuInfoList []*partner.StoreSkuInfo) map[int]*partner.SkuNameInfo {
allSkuInfoMap := GetMultiStoreAllSkuInfoMap(vendorID)
filterSkuInfoMap := make(map[int]*partner.SkuNameInfo)
for _, value := range skuInfoList {
if value.Stock > 0 {
filterSkuInfoMap[value.SkuID] = allSkuInfoMap[value.SkuID]
}
}
return filterSkuInfoMap
}
func GetFilterStoreList(storeList []*StoreExt, vendorMap, storeIDMap map[int]bool) (outStoreList []*StoreExt) {
for _, storeInfo := range storeList {
storeID := storeInfo.ID
//filter for storeID
if len(storeIDMap) > 0 {
if _, ok := storeIDMap[storeID]; !ok {
continue
}
}
if storeInfo.StoreMaps != nil {
var tempStoreMaps []map[string]interface{}
for _, vendorStoreInfo := range storeInfo.StoreMaps {
vendorID := int(utils.MustInterface2Int64(vendorStoreInfo["vendorID"]))
isSyncStoreSku := int(utils.MustInterface2Int64(vendorStoreInfo["isSync"]))
if isSyncStoreSku == 0 {
continue
}
//filter for vendorID
if len(vendorMap) > 0 {
if _, ok := vendorMap[vendorID]; !ok {
continue
}
}
if _, ok := vendorNameList[vendorID]; !ok {
continue
}
tempStoreMaps = append(tempStoreMaps, vendorStoreInfo)
}
if len(tempStoreMaps) > 0 {
storeInfo.StoreMaps = tempStoreMaps
outStoreList = append(outStoreList, storeInfo)
}
}
}
return outStoreList
}
func GetSkuSaleStatusName(status int) string {
return model.SkuStatusName[status]
}
func GetBoolName(flag bool) string {
if flag {
return "是"
} else {
return "否"
}
}
func IsSkuCanSale(saleStatus int) bool {
return saleStatus == model.SkuStatusNormal
}
//京西平台和其他平台商品的对比
//storeIDStr 京西商家id vendorStoreID 平台商家id
func CompareJxAndVendor(vendorID int, storeIDStr, vendorStoreID, storeName string, filterJxSkuInfoMap map[int]*StoreSkuNameExt, filterVendorSkuInfoMap map[int]*partner.SkuNameInfo) {
for skuID, jxSkuInfo := range filterJxSkuInfoMap {
skuIDStr := utils.Int2Str(skuID)
//jxSkuDetailName : 前缀 ([荐]) + 分类名(xxx水饺) + 数量单位(约..g/份) + 注释 (补充..)
jxSkuDetailName := jxutils.ComposeSkuName(jxSkuInfo.SkuName.Prefix, jxSkuInfo.SkuName.Name, jxSkuInfo.Skus2[0].Comment, jxSkuInfo.SkuName.Unit, jxSkuInfo.Skus2[0].SkuSpecQuality, jxSkuInfo.Skus2[0].SkuSpecUnit, 0)
//jxSkuSaleStatus : 商品状态 skustatus 优先级高于 StoreSkuStatus
jxSkuSaleStatus := jxutils.MergeSkuStatus(jxSkuInfo.Skus2[0].SkuStatus, jxSkuInfo.Skus2[0].StoreSkuStatus)
jxSkuSaleStatusName := GetSkuSaleStatusName(jxSkuSaleStatus)
vendorSkuInfo := filterVendorSkuInfoMap[skuID]
var status int8
if vendorID == model.VendorIDMTWM {
status = jxSkuInfo.Skus2[0].MtwmSyncStatus
} else if vendorID == model.VendorIDEBAI {
status = jxSkuInfo.Skus2[0].EbaiSyncStatus
} else if vendorID == model.VendorIDJD {
status = jxSkuInfo.Skus2[0].JdSyncStatus
}
syncStatus := utils.Int2Str(int(status))
toBeCreate := GetBoolName(model.IsSyncStatusNeedCreate(status))
toBeDel := GetBoolName(model.IsSyncStatusNeedDelete(status))
if vendorSkuInfo != nil {
vendorSkuDetailName := vendorSkuInfo.SkuList[0].SkuName
vendorSkuSaleStatusName := GetSkuSaleStatusName(vendorSkuInfo.SkuList[0].Status)
isSaleStatusDiff := jxSkuSaleStatusName != vendorSkuSaleStatusName
isNameDiff := jxSkuDetailName != vendorSkuDetailName
if jxSkuDetailName != "" && vendorSkuDetailName != "" && strings.Contains(jxSkuDetailName, vendorSkuDetailName) {
isNameDiff = false
}
if isSaleStatusDiff || isNameDiff {
outPutData := DiffData{storeIDStr, vendorStoreID, storeName, skuIDStr, syncStatus, toBeCreate, toBeDel, jxSkuDetailName, vendorSkuDetailName, jxSkuSaleStatusName, vendorSkuSaleStatusName, ""}
diffData.AppendData(vendorID, outPutData)
}
} else {
if isFilterToBeCreateAndNotSale && model.IsSyncStatusNeedCreate(status) && !IsSkuCanSale(jxSkuSaleStatus) {
continue
}
outPutData := DiffData{storeIDStr, vendorStoreID, storeName, skuIDStr, syncStatus, toBeCreate, toBeDel, jxSkuDetailName, "", jxSkuSaleStatusName, "", ""}
diffData.AppendData(vendorID, outPutData)
}
}
for skuID, vendorSkuInfo := range filterVendorSkuInfoMap {
if vendorSkuInfo != nil {
if len(vendorSkuInfo.SkuList) > 0 {
skuIDStr := utils.Int2Str(skuID)
vendorSkuDetailName := vendorSkuInfo.SkuList[0].SkuName
vendorSkuSaleStatusName := GetSkuSaleStatusName(vendorSkuInfo.SkuList[0].Status)
jxSkuInfo := filterJxSkuInfoMap[skuID]
if jxSkuInfo == nil {
outPutData := DiffData{storeIDStr, vendorStoreID, storeName, skuIDStr, "", "", "", "", vendorSkuDetailName, "", vendorSkuSaleStatusName, DatAanalyse1}
diffData.AppendData(vendorID, outPutData)
}
} else {
globals.SugarLogger.Warnf("CompareJxAndVendor vendorSkuInfo.SkuList:%d is nil", skuID)
}
} else {
globals.SugarLogger.Warnf("CompareJxAndVendor skuID:%d is nil", skuID)
}
}
}
//入口函数,校验本地商品京西和其他平台的差异
func CheckSkuDiffBetweenJxAndVendor(ctx *jxcontext.Context, vendorIDList []int, storeIDList []int) {
startProcessTime := time.Now().Unix()
baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor start time: %v", time.Now())
vendorMap := make(map[int]bool)
for _, vendorID := range vendorIDList {
vendorMap[vendorID] = true //map[平台ID:true]
}
storeIDMap := make(map[int]bool)
for _, storeID := range storeIDList {
storeIDMap[storeID] = true //map[门店ID:true]
}
taskSeqFunc := func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
switch step {
case 0:
//获取所有多门店平台的商品信息
//1.循环传进来的平台id如果是多门店的平台查询该门店京东到家平台的所有商品
//2.为multiStoreAllSkuInfoList 和 multiStoreAllSkuInfoMap 赋值
GetMultiStoreAllSkuInfo(ctx, vendorMap)
case 1:
//1.获取京西本地所有门店信息 store、place、user表联查 jxStoreInfoList
//2.过滤所有门店信息只留下传进来的vendorMapstoreIDMap中对应的平台和门台信息 filterStoreList
//3.判断是否多平台门店,两种情况两种处理
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 {
filterStoreList := GetFilterStoreList(jxStoreInfoList.Stores, vendorMap, storeIDMap)
diffData.InitData()
taskFunc := func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
jxStoreInfoListValue := batchItemList[0].(*StoreExt)
storeID := jxStoreInfoListValue.ID
storeIDStr := utils.Int2Str(storeID)
storeName := jxStoreInfoListValue.Name
if jxStoreInfoListValue.StoreMaps != nil {
isGetJxSkuInfoData := false
var filterJxSkuInfoMap map[int]*StoreSkuNameExt
for _, vendorListValue := range jxStoreInfoListValue.StoreMaps {
vendorID := int(utils.MustInterface2Int64(vendorListValue["vendorID"]))
if isGetJxSkuInfoData == false { //only get once jx sku info list every store id
isGetJxSkuInfoData = true
jxSkuInfoData, _ := GetStoreSkus(ctx, storeID, filterCloseSkuIds, true, "", true, false, map[string]interface{}{}, 0, -1)
filterJxSkuInfoMap = GetFilterJxSkuInfoMap(jxSkuInfoData.SkuNames) //map[京西商品ID:StoreSkuNameExt]
}
vendorStoreID := utils.Interface2String(vendorListValue["vendorStoreID"])
baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor storeID:%d vendorID:%d vendorStoreID:%s vendorListValue:%v", storeID, vendorID, vendorStoreID, vendorListValue)
if partner.IsMultiStore(vendorID) {
singleStoreHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.IPurchasePlatformStoreSkuHandler)
allSkuInfoList := GetMultiStoreAllSkuInfoList(vendorID)
skuBareInfoList, err := singleStoreHandler.GetStoreSkusBareInfo(ctx, task, storeID, vendorStoreID, allSkuInfoList)
if err != nil {
baseapi.SugarLogger.Infof("CheckSkuDiffBetweenJxAndVendor GetStoreSkusBareInfo error:%v", err)
} else if len(skuBareInfoList) > 0 {
filterSkuInfoMap := GetFilterMultiStoreSkuInfoMap(vendorID, skuBareInfoList) //map[京东商品ID:SkuNameInfo]
CompareJxAndVendor(vendorID, storeIDStr, vendorStoreID, storeName, filterJxSkuInfoMap, filterSkuInfoMap)
}
} else {
singleStoreHandler := partner.GetPurchasePlatformFromVendorID(vendorID).(partner.ISingleStoreStoreSkuHandler)
vendorSkuInfoList, err := singleStoreHandler.GetStoreSkusFullInfo(ctx, task, storeID, vendorStoreID, nil)
if err != nil {
baseapi.SugarLogger.Infof("CheckSkuDiffBetweenJxAndVendor GetStoreSkusFullInfo error:%v", err)
} else if len(vendorSkuInfoList) > 0 {
filterVendorSkuInfoMap := GetFilterVendorSkuInfoMap(vendorSkuInfoList) //map[平台商品ID:SkuNameInfo]
CompareJxAndVendor(vendorID, storeIDStr, vendorStoreID, storeName, filterJxSkuInfoMap, filterVendorSkuInfoMap)
}
}
}
}
return retVal, err
}
taskParallel := tasksch.NewParallelTask("京西和平台商品差异对比-门店商品对比", tasksch.NewParallelConfig().SetParallelCount(parallelCount), ctx, taskFunc, filterStoreList)
tasksch.HandleTask(taskParallel, task, true).Run()
_, err = taskParallel.GetResult(0)
if err != nil {
baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor taskParallel error:%v", err)
}
}
case 2:
WriteToExcel(task, diffData.diffDataMap)
endProcessTime := time.Now().Unix()
diff := endProcessTime - startProcessTime
baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor end time: %v", time.Now())
baseapi.SugarLogger.Debugf("CheckSkuDiffBetweenJxAndVendor cost time: %d sec", diff)
}
return result, err
}
taskSeq := tasksch.NewSeqTask("京西和平台商品差异对比-序列任务", ctx, taskSeqFunc, 3)
tasksch.HandleTask(taskSeq, nil, true).Run()
}
func IsJXCS() bool {
return globals.IsMainProductEnv()
}
func AddStatisticSheet(sheetName string, data []DiffData) (sheet *excel.Obj2ExcelSheetConfig) {
statisticData := make([]StatisticData, 9)
var count [9]int
var percent [9]float32
totalCount := len(data)
for _, rowData := range data {
if rowData.JxSkuName != "" && rowData.VendorSkuName == "" {
if rowData.ToBeCreate == "是" {
if rowData.JxStatus == "下架" {
count[0]++
} else if rowData.JxStatus == "上架" {
count[1]++
}
} else if rowData.ToBeCreate == "否" {
if rowData.JxStatus == "下架" {
count[2]++
} else if rowData.JxStatus == "上架" {
count[3]++
}
}
} else if rowData.JxSkuName == "" && rowData.VendorSkuName != "" {
if rowData.ToBeCreate == "" && rowData.JxStatus == "" {
count[4]++
}
} else if rowData.JxSkuName != "" && rowData.VendorSkuName != "" {
if rowData.ToBeCreate == "是" {
if rowData.JxStatus == "下架" {
count[5]++
} else if rowData.JxStatus == "上架" {
count[6]++
}
} else if rowData.ToBeCreate == "否" {
if rowData.JxStatus == "下架" {
count[7]++
} else if rowData.JxStatus == "上架" {
count[8]++
}
}
}
}
for index, value := range count {
percent[index] = float32(value*100) / float32(totalCount)
countStr := utils.Int2Str(value)
percentStr := fmt.Sprintf("%.2f%%", percent[index])
subStatisticData := statisticDataList[index]
statisticData[index] = StatisticData{subStatisticData[0], subStatisticData[1], subStatisticData[2], countStr, percentStr}
}
sheetName = sheetName + "统计"
sheet = &excel.Obj2ExcelSheetConfig{
Title: sheetName,
Data: statisticData,
CaptionList: statisticTitleList,
}
return sheet
}
func WriteToExcel(task *tasksch.SeqTask, data map[int][]DiffData) {
var sheetList []*excel.Obj2ExcelSheetConfig
for key, value := range data {
sheetName := vendorNameList[key]
excelConf := &excel.Obj2ExcelSheetConfig{
Title: sheetName,
Data: value,
CaptionList: titleList,
}
sheetList = append(sheetList, excelConf)
if needStatistic {
statisticSheet := AddStatisticSheet(sheetName, value)
sheetList = append(sheetList, statisticSheet)
}
baseapi.SugarLogger.Debugf("WriteToExcel:append:%s count:%d", sheetName, len(value))
}
if len(sheetList) > 0 {
excelBin := excel.Obj2Excel(sheetList)
sheetList = nil
timeStr := utils.Int64ToStr(time.Now().Unix())
diffFullFileName := diffFileName[IsJXCS()] + 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 {
noticeMsg := fmt.Sprintf("[详情点我]%s/billshow/?normal=true&path=%s \n", globals.BackstageHost, downloadURL)
task.SetNoticeMsg(noticeMsg)
baseapi.SugarLogger.Debugf("WriteToExcel:upload %s success, downloadURL:%s", diffFullFileName, downloadURL)
}
} else {
baseapi.SugarLogger.Debug("WriteToExcel:No diff data!!!")
}
}