From 1fbfe8eae29d88c39c6fc9abb4c16f36569d8405 Mon Sep 17 00:00:00 2001 From: renzhiyuan <465386466@qq.com> Date: Mon, 29 Dec 2025 17:58:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=87=8D=E6=9E=84bbxt=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/tools/bbxt/api.go | 26 ------------ internal/tools/bbxt/bbxt.go | 77 ++++++++++++++++++++++++---------- internal/tools/bbxt/entitys.go | 14 +++++++ 3 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 internal/tools/bbxt/entitys.go diff --git a/internal/tools/bbxt/api.go b/internal/tools/bbxt/api.go index 2f62a56..64632cb 100644 --- a/internal/tools/bbxt/api.go +++ b/internal/tools/bbxt/api.go @@ -8,7 +8,6 @@ import ( "net/http" "net/url" "strings" - "time" ) type StatisOursProductLossSumReq struct { @@ -173,31 +172,6 @@ func request(url string, reqData interface{}, resData interface{}) error { return nil } -func formatCtToQueryString(ct []string) string { - if len(ct) != 2 { - return "" - } - - values := url.Values{} - - // 第一个时间(开始时间) - startTime, err := time.Parse("2006-01-02 15:04:05", ct[0]) - if err == nil { - // 保持原样 - values.Add("ct[]", startTime.Format("2006-01-02+15:04:05")) - } - - // 第二个时间(结束时间) - endTime, err := time.Parse("2006-01-02 15:04:05", ct[1]) - if err == nil { - // 添加毫秒 - endTimeWithMs := endTime.Add(999 * time.Millisecond) - values.Add("ct[]", endTimeWithMs.Format("2006-01-02+15:04:05.000")) - } - - return values.Encode() -} - func customEncode(params url.Values) string { encoded := params.Encode() diff --git a/internal/tools/bbxt/bbxt.go b/internal/tools/bbxt/bbxt.go index 1cc5f42..8fe2885 100644 --- a/internal/tools/bbxt/bbxt.go +++ b/internal/tools/bbxt/bbxt.go @@ -12,7 +12,6 @@ import ( type BbxtTools struct { cacheDir string excelTempDir string - ct []string } func NewBbxtTools() (*BbxtTools, error) { @@ -24,19 +23,19 @@ func NewBbxtTools() (*BbxtTools, error) { if err != nil { return nil, err } - now := time.Now() + return &BbxtTools{ cacheDir: cache, excelTempDir: fmt.Sprintf("%s/excel_temp", tempDir), - ct: []string{ - time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location()).Format("2006-01-02 15:04:05"), - time.Date(now.Year(), now.Month(), now.Day(), 23, 59, 59, 0, now.Location()).Format("2006-01-02 15:04:05"), - }, }, nil } -func (b *BbxtTools) DailyReport() (err error) { - err = b.StatisOursProductLossSumTotal() +func (b *BbxtTools) DailyReport(today time.Time) (err error) { + + err = b.StatisOursProductLossSumTotal([]string{ + time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location()).Format("2006-01-02 15:04:05"), + time.Date(today.Year(), today.Month(), today.Day(), 23, 59, 59, 0, today.Location()).Format("2006-01-02 15:04:05"), + }) if err != nil { return } @@ -44,30 +43,61 @@ func (b *BbxtTools) DailyReport() (err error) { } // OursProductLossSum 负利润分析 -func (b *BbxtTools) StatisOursProductLossSumTotal() (err error) { +func (b *BbxtTools) StatisOursProductLossSumTotal(ct []string) (err error) { data, err := StatisOursProductLossSumApi(&StatisOursProductLossSumReq{ - Ct: b.ct, + Ct: ct, }) if err != nil { return } var ( - total [][]string - gt [][]string + resellerMap = make(map[int32]*ResellerLoss) + total [][]string + gt []*ResellerLoss ) - for _, v := range data.List { - if v.Loss <= -100 { + for _, info := range data.List { + // 检查经销商是否已存在 + if _, ok := resellerMap[info.ResellerId]; !ok { + // 创建新的经销商记录 + resellerMap[info.ResellerId] = &ResellerLoss{ + ResellerId: info.ResellerId, + ResellerName: info.ResellerName, + Total: 0, // 初始化为0,后续累加 + ProductLoss: make(map[int32]ProductLoss), // 初始化map + } + } + + // 获取当前经销商 + reseller := resellerMap[info.ResellerId] + + // 累加经销商总亏损 + reseller.Total += info.Loss + + // 检查产品是否已存在 + if _, ok := reseller.ProductLoss[info.OursProductId]; !ok { + // 创建新的产品亏损记录 + reseller.ProductLoss[info.OursProductId] = ProductLoss{ + ProductId: info.OursProductId, + ProductName: info.OursProductName, + Loss: info.Loss, // 初始化为当前产品的亏损 + } + } else { + // 已存在产品记录,累加亏损 + productLoss := reseller.ProductLoss[info.OursProductId] + productLoss.Loss += info.Loss + reseller.ProductLoss[info.OursProductId] = productLoss + } + } + for _, v := range resellerMap { + if v.Total <= -100 { total = append(total, []string{ - v.OursProductName, - fmt.Sprintf("%.2f", v.Loss), + fmt.Sprintf("%d", v.ResellerName), + fmt.Sprintf("%.2f", v.Total), }) } - if v.Loss <= -500 { - gt = append(gt, []string{ - v.OursProductName, - fmt.Sprintf("%.2f", v.Loss), - }) + if v.Total <= -500 { + gt = append(gt, v) } } //总量生成excel @@ -75,6 +105,11 @@ func (b *BbxtTools) StatisOursProductLossSumTotal() (err error) { filePath := b.cacheDir + "/kshj_total" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx" err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_total.xlsx", filePath, total) } + + if len(gt) > 0 { + filePath := b.cacheDir + "/kshj_gt" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx" + err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, total) + } return err } diff --git a/internal/tools/bbxt/entitys.go b/internal/tools/bbxt/entitys.go new file mode 100644 index 0000000..a3dc743 --- /dev/null +++ b/internal/tools/bbxt/entitys.go @@ -0,0 +1,14 @@ +package bbxt + +type ResellerLoss struct { + ResellerId int32 + ResellerName string + Total float64 + ProductLoss map[int32]ProductLoss +} + +type ProductLoss struct { + ProductId int32 + ProductName string + Loss float64 +}