fix: 1. 增加排序 2.调整title

This commit is contained in:
fuzhongyun 2026-01-05 18:44:32 +08:00
parent a72ec8cfc2
commit 57692a8da3
2 changed files with 42 additions and 14 deletions

View File

@ -684,11 +684,10 @@ func (d *DingTalkBotBiz) rechargeDailyReport(ctx context.Context, now time.Time,
reports = []*bbxt.ReportRes{ reports = []*bbxt.ReportRes{
{ {
ReportName: "我们的商品统计(电商充值系统)", ReportName: "我们的商品统计(电商充值系统)",
Title: fmt.Sprintf("%s 电商充值系统我们的商品统计", now.Format("2006-01-02")), Title: res["title"].(string),
Path: res["path"].(string), Path: res["path"].(string),
Url: res["url"].(string), Url: res["url"].(string),
Data: res["data"].([][]string), Data: res["data"].([][]string),
Desc: res["desc"].(string),
}, },
} }

View File

@ -15,6 +15,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"path/filepath" "path/filepath"
"sort"
"strconv" "strconv"
"time" "time"
@ -91,6 +92,7 @@ type StatisticsOursProductContext struct {
ProductData []statistics_ours_product.StatisticsOursProductItem ProductData []statistics_ours_product.StatisticsOursProductItem
ImgUrl string ImgUrl string
ExcelData [][]string ExcelData [][]string
TotalLoss float64
} }
func (w *statisticsOursProduct) buildWorkflow(ctx context.Context) (compose.Runnable[*StatisticsOursProductWorkflowInput, map[string]any], error) { func (w *statisticsOursProduct) buildWorkflow(ctx context.Context) (compose.Runnable[*StatisticsOursProductWorkflowInput, map[string]any], error) {
@ -122,7 +124,7 @@ func (w *statisticsOursProduct) formatContext(ctx context.Context, input *Statis
Time: time.Now(), Time: time.Now(),
StartTime: startTime, StartTime: startTime,
EndTime: endTime, EndTime: endTime,
Title: fmt.Sprintf("截止 %s 我们的商品统计", endTimeStr), Title: fmt.Sprintf("截止 %s 亏损100以上我们的商品统计", endTimeStr),
}, nil }, nil
} }
@ -156,13 +158,13 @@ func (w *statisticsOursProduct) callStatisticsTool(ctx context.Context, state *S
func (w *statisticsOursProduct) generateExcelAndUpload(ctx context.Context, state *StatisticsOursProductContext) (*StatisticsOursProductContext, error) { func (w *statisticsOursProduct) generateExcelAndUpload(ctx context.Context, state *StatisticsOursProductContext) (*StatisticsOursProductContext, error) {
// 1. 获取模板路径 // 1. 获取模板路径
// cwd, _ := filepath.Abs("../../") // 单元测试用 cwd, _ := filepath.Abs("../../") // 单元测试用
cwd, _ := filepath.Abs(".") // cwd, _ := filepath.Abs(".")
templatePath := filepath.Join(cwd, "tmpl", "excel_temp", "recharge_statistics_ours_product.xlsx") templatePath := filepath.Join(cwd, "tmpl", "excel_temp", "recharge_statistics_ours_product.xlsx")
fileName := fmt.Sprintf("statistics_ours_product_%d%d", time.Now().Unix(), rand.Intn(1000)) fileName := fmt.Sprintf("statistics_ours_product_%d%d", time.Now().Unix(), rand.Intn(1000))
// 2. 转换数据为 [][]string // 2. 转换数据为 [][]string
excelData := w.convertDataToExcelFormat(state.ProductData) excelData, totalLoss := w.convertDataToExcelFormat(state.ProductData)
// 3. 生成 Excel // 3. 生成 Excel
req := &excel_generator.ExcelGeneratorRequest{ req := &excel_generator.ExcelGeneratorRequest{
@ -191,13 +193,20 @@ func (w *statisticsOursProduct) generateExcelAndUpload(ctx context.Context, stat
state.ImgUrl = url state.ImgUrl = url
state.ExcelData = excelData state.ExcelData = excelData
state.TotalLoss = totalLoss
return state, nil return state, nil
} }
// convertDataToExcelFormat 将业务数据转换为 Excel 生成器需要的二维字符串数组 // convertDataToExcelFormat 将业务数据转换为 Excel 生成器需要的二维字符串数组
func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_product.StatisticsOursProductItem) [][]string { func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_product.StatisticsOursProductItem) ([][]string, float64) {
var result [][]string type sortType struct {
Profit float64
cells []string
}
var sortList []sortType
var totalLoss float64
for _, item := range data { for _, item := range data {
var profitVal float64 var profitVal float64
@ -219,6 +228,11 @@ func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_
profitVal = 0 profitVal = 0
} }
// 累加总亏损
if profitVal < 0 {
totalLoss += profitVal
}
// 过滤利润小于 -100 的记录 // 过滤利润小于 -100 的记录
if profitVal > -100 { if profitVal > -100 {
continue continue
@ -236,9 +250,24 @@ func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_
fmt.Sprintf("%v", item.Profit), fmt.Sprintf("%v", item.Profit),
} }
result = append(result, row) sortList = append(sortList, sortType{
Profit: profitVal,
cells: row,
})
} }
return result
// 排序
sort.Slice(sortList, func(i, j int) bool {
return sortList[i].Profit < sortList[j].Profit
})
// 转换为 [][]string
result := make([][]string, 0, len(sortList))
for _, item := range sortList {
result = append(result, item.cells)
}
return result, totalLoss
} }
func (w *statisticsOursProduct) convertToMap(ctx context.Context, state *StatisticsOursProductContext) (map[string]any, error) { func (w *statisticsOursProduct) convertToMap(ctx context.Context, state *StatisticsOursProductContext) (map[string]any, error) {
@ -246,6 +275,6 @@ func (w *statisticsOursProduct) convertToMap(ctx context.Context, state *Statist
"path": "", "path": "",
"url": state.ImgUrl, "url": state.ImgUrl,
"data": state.ExcelData, "data": state.ExcelData,
"desc": state.Title, "title": state.Title + fmt.Sprintf("(总亏损 %.2f", state.TotalLoss),
}, nil }, nil
} }