From 57692a8da3a3a25a42cd98c9590c9abb2338b319 Mon Sep 17 00:00:00 2001 From: fuzhongyun <15339891972@163.com> Date: Mon, 5 Jan 2026 18:44:32 +0800 Subject: [PATCH] =?UTF-8?q?fix:=201.=20=E5=A2=9E=E5=8A=A0=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=202.=E8=B0=83=E6=95=B4title?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/biz/ding_talk_bot.go | 3 +- .../recharge/statistics_ours_product.go | 53 ++++++++++++++----- 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/internal/biz/ding_talk_bot.go b/internal/biz/ding_talk_bot.go index 726249f..dc4e2b6 100644 --- a/internal/biz/ding_talk_bot.go +++ b/internal/biz/ding_talk_bot.go @@ -684,11 +684,10 @@ func (d *DingTalkBotBiz) rechargeDailyReport(ctx context.Context, now time.Time, reports = []*bbxt.ReportRes{ { ReportName: "我们的商品统计(电商充值系统)", - Title: fmt.Sprintf("%s 电商充值系统我们的商品统计", now.Format("2006-01-02")), + Title: res["title"].(string), Path: res["path"].(string), Url: res["url"].(string), Data: res["data"].([][]string), - Desc: res["desc"].(string), }, } diff --git a/internal/domain/workflow/recharge/statistics_ours_product.go b/internal/domain/workflow/recharge/statistics_ours_product.go index 13a644e..904f7c5 100644 --- a/internal/domain/workflow/recharge/statistics_ours_product.go +++ b/internal/domain/workflow/recharge/statistics_ours_product.go @@ -15,6 +15,7 @@ import ( "fmt" "math/rand" "path/filepath" + "sort" "strconv" "time" @@ -91,6 +92,7 @@ type StatisticsOursProductContext struct { ProductData []statistics_ours_product.StatisticsOursProductItem ImgUrl string ExcelData [][]string + TotalLoss float64 } 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(), StartTime: startTime, EndTime: endTime, - Title: fmt.Sprintf("截止 %s 我们的商品统计", endTimeStr), + Title: fmt.Sprintf("截止 %s 亏损100以上我们的商品统计", endTimeStr), }, 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) { // 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") fileName := fmt.Sprintf("statistics_ours_product_%d%d", time.Now().Unix(), rand.Intn(1000)) // 2. 转换数据为 [][]string - excelData := w.convertDataToExcelFormat(state.ProductData) + excelData, totalLoss := w.convertDataToExcelFormat(state.ProductData) // 3. 生成 Excel req := &excel_generator.ExcelGeneratorRequest{ @@ -191,13 +193,20 @@ func (w *statisticsOursProduct) generateExcelAndUpload(ctx context.Context, stat state.ImgUrl = url state.ExcelData = excelData + state.TotalLoss = totalLoss return state, nil } // convertDataToExcelFormat 将业务数据转换为 Excel 生成器需要的二维字符串数组 -func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_product.StatisticsOursProductItem) [][]string { - var result [][]string +func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_product.StatisticsOursProductItem) ([][]string, float64) { + type sortType struct { + Profit float64 + cells []string + } + + var sortList []sortType + var totalLoss float64 for _, item := range data { var profitVal float64 @@ -219,6 +228,11 @@ func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_ profitVal = 0 } + // 累加总亏损 + if profitVal < 0 { + totalLoss += profitVal + } + // 过滤利润小于 -100 的记录 if profitVal > -100 { continue @@ -236,16 +250,31 @@ func (w *statisticsOursProduct) convertDataToExcelFormat(data []statistics_ours_ 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) { return map[string]any{ - "path": "", - "url": state.ImgUrl, - "data": state.ExcelData, - "desc": state.Title, + "path": "", + "url": state.ImgUrl, + "data": state.ExcelData, + "title": state.Title + fmt.Sprintf("(总亏损 %.2f)", state.TotalLoss), }, nil }