fix: 优化销售下滑数据处理和排序逻辑

This commit is contained in:
renzhiyuan 2026-01-05 19:07:53 +08:00
parent e695029238
commit 16c113398a
3 changed files with 21 additions and 15 deletions

View File

@ -18,7 +18,7 @@ const (
) )
var ( var (
DownWardValue int32 = 1000 DownWardValue int32 = 1500
SumFilter int32 = -150 SumFilter int32 = -150
) )
@ -53,11 +53,6 @@ func NewBbxtTools() (*BbxtTools, error) {
func (b *BbxtTools) DailyReport(now time.Time, downWardValue int32, productName []string, sumFilter int32, ossClient *utils_oss.Client) (reports []*ReportRes, err error) { func (b *BbxtTools) DailyReport(now time.Time, downWardValue int32, productName []string, sumFilter int32, ossClient *utils_oss.Client) (reports []*ReportRes, err error) {
reports = make([]*ReportRes, 0, 4) reports = make([]*ReportRes, 0, 4)
statisOfficialProductSumDecline, err := b.GetStatisOfficialProductSumDecline(now, downWardValue, productName, sumFilter)
if err != nil {
return
}
productLossReport, err := b.StatisOursProductLossSum(now) productLossReport, err := b.StatisOursProductLossSum(now)
if err != nil { if err != nil {
return return
@ -70,7 +65,10 @@ func (b *BbxtTools) DailyReport(now time.Time, downWardValue int32, productName
if err != nil { if err != nil {
return return
} }
statisOfficialProductSumDecline, err := b.GetStatisOfficialProductSumDecline(now, downWardValue, productName, sumFilter)
if err != nil {
return
}
reports = append(reports, productLossReport...) reports = append(reports, productLossReport...)
reports = append(reports, profitRankingSum, statisOfficialProductSum, statisOfficialProductSumDecline) reports = append(reports, profitRankingSum, statisOfficialProductSum, statisOfficialProductSumDecline)
@ -357,19 +355,20 @@ func (b *BbxtTools) GetStatisOfficialProductSumDecline(now time.Time, downWardVa
return return
} }
var ( var (
productSumMap = make(map[int32]ProductSumDecline) productSumMap = make(map[string]ProductSumDecline)
) )
for _, v := range data.OfficialProductSumDecline { for _, v := range data.OfficialProductSumDecline {
if _, ex := productSumMap[v.OfficialProductId]; !ex { if _, ex := productSumMap[v.OfficialProductName]; !ex {
productSumMap[v.OfficialProductId] = ProductSumDecline{ productSumMap[v.OfficialProductName] = ProductSumDecline{
OfficialProductName: v.OfficialProductName, OfficialProductName: v.OfficialProductName,
OfficialProductId: v.OfficialProductId, OfficialProductId: v.OfficialProductId,
ProductSumReseller: make(map[int32]ProductSumReseller), ProductSumReseller: make(map[int32]ProductSumReseller),
Index: productMap[v.OfficialProductName],
} }
} }
if v.HistoryOneDiff <= sumFilter || v.HistoryTwoDiff <= sumFilter { if v.HistoryOneDiff <= sumFilter || v.HistoryTwoDiff <= sumFilter {
productSumMap[v.OfficialProductId].ProductSumReseller[v.ResellerId] = ProductSumReseller{ productSumMap[v.OfficialProductName].ProductSumReseller[v.ResellerId] = ProductSumReseller{
ResellerName: v.ResellerName, ResellerName: v.ResellerName,
CurrentNum: v.CurrentNum, CurrentNum: v.CurrentNum,
HistoryOneNum: v.HistoryOneNum, HistoryOneNum: v.HistoryOneNum,
@ -378,8 +377,14 @@ func (b *BbxtTools) GetStatisOfficialProductSumDecline(now time.Time, downWardVa
HistoryTwoDiff: v.HistoryTwoDiff, HistoryTwoDiff: v.HistoryTwoDiff,
} }
} }
} }
var total = make([]ProductSumDecline, 0, len(productSumMap))
for k, _ := range productSumMap {
total = append(total, productSumMap[k])
}
sort.Slice(total, func(i, j int) bool {
return total[i].Index > total[j].Index
})
timeCh := now.Format("1月2日15点") timeCh := now.Format("1月2日15点")
//title := "截至" + timeCh + "销量下滑大于" + fmt.Sprintf("%d", downWardValue) + "明细,分销商仅展示差额大于" + fmt.Sprintf("%d", -sumFilter) //title := "截至" + timeCh + "销量下滑大于" + fmt.Sprintf("%d", downWardValue) + "明细,分销商仅展示差额大于" + fmt.Sprintf("%d", -sumFilter)
title := "截至" + timeCh + "销量下滑较大商品" title := "截至" + timeCh + "销量下滑较大商品"
@ -388,7 +393,7 @@ func (b *BbxtTools) GetStatisOfficialProductSumDecline(now time.Time, downWardVa
return return
} }
filePath := b.cacheDir + "/xlxhmx" + fmt.Sprintf("%d%d", time.Now().Unix(), rand.Intn(1000)) + ".xlsx" filePath := b.cacheDir + "/xlxhmx" + fmt.Sprintf("%d%d", time.Now().Unix(), rand.Intn(1000)) + ".xlsx"
err = b.OfficialProductSumDeclineExcel(b.excelTempDir+"/"+"/xlxhmx.xlsx", filePath, productSumMap, title) err = b.OfficialProductSumDeclineExcel(b.excelTempDir+"/"+"/xlxhmx.xlsx", filePath, total, title)
return &ReportRes{ return &ReportRes{
ReportName: "销售下滑明细", ReportName: "销售下滑明细",
Title: title, Title: title,

View File

@ -26,6 +26,7 @@ type ProductSumDecline struct {
OfficialProductId int32 OfficialProductId int32
OfficialProductName string OfficialProductName string
ProductSumReseller map[int32]ProductSumReseller ProductSumReseller map[int32]ProductSumReseller
Index int
} }
type ProductSumReseller struct { type ProductSumReseller struct {

View File

@ -173,7 +173,7 @@ func (u *Uploader) uploadToOSS(fileName string, fileBytes []byte) string {
// return url // return url
//} //}
func (b *BbxtTools) OfficialProductSumDeclineExcel(templatePath, outputPath string, sumMap map[int32]ProductSumDecline, title string) error { func (b *BbxtTools) OfficialProductSumDeclineExcel(templatePath, outputPath string, sumSlice []ProductSumDecline, title string) error {
// 1. 读取模板 // 1. 读取模板
f, err := excelize.OpenFile(templatePath) f, err := excelize.OpenFile(templatePath)
if err != nil { if err != nil {
@ -227,7 +227,7 @@ func (b *BbxtTools) OfficialProductSumDeclineExcel(templatePath, outputPath stri
currentRow := 3 currentRow := 3
pattern := `\$\{(.*?)\}` pattern := `\$\{(.*?)\}`
re := regexp.MustCompile(pattern) re := regexp.MustCompile(pattern)
for _, product := range sumMap { for _, product := range sumSlice {
// 排序 ProductLoss // 排序 ProductLoss
var reseller []ProductSumReseller var reseller []ProductSumReseller
for _, p := range product.ProductSumReseller { for _, p := range product.ProductSumReseller {