fix: 重构bbxt数据处理逻辑

This commit is contained in:
renzhiyuan 2025-12-29 17:58:13 +08:00
parent 0d8ef7056c
commit 1fbfe8eae2
3 changed files with 70 additions and 47 deletions

View File

@ -8,7 +8,6 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
"time"
) )
type StatisOursProductLossSumReq struct { type StatisOursProductLossSumReq struct {
@ -173,31 +172,6 @@ func request(url string, reqData interface{}, resData interface{}) error {
return nil 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 { func customEncode(params url.Values) string {
encoded := params.Encode() encoded := params.Encode()

View File

@ -12,7 +12,6 @@ import (
type BbxtTools struct { type BbxtTools struct {
cacheDir string cacheDir string
excelTempDir string excelTempDir string
ct []string
} }
func NewBbxtTools() (*BbxtTools, error) { func NewBbxtTools() (*BbxtTools, error) {
@ -24,19 +23,19 @@ func NewBbxtTools() (*BbxtTools, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
now := time.Now()
return &BbxtTools{ return &BbxtTools{
cacheDir: cache, cacheDir: cache,
excelTempDir: fmt.Sprintf("%s/excel_temp", tempDir), 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 }, nil
} }
func (b *BbxtTools) DailyReport() (err error) { func (b *BbxtTools) DailyReport(today time.Time) (err error) {
err = b.StatisOursProductLossSumTotal()
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 { if err != nil {
return return
} }
@ -44,30 +43,61 @@ func (b *BbxtTools) DailyReport() (err error) {
} }
// OursProductLossSum 负利润分析 // OursProductLossSum 负利润分析
func (b *BbxtTools) StatisOursProductLossSumTotal() (err error) { func (b *BbxtTools) StatisOursProductLossSumTotal(ct []string) (err error) {
data, err := StatisOursProductLossSumApi(&StatisOursProductLossSumReq{ data, err := StatisOursProductLossSumApi(&StatisOursProductLossSumReq{
Ct: b.ct, Ct: ct,
}) })
if err != nil { if err != nil {
return return
} }
var ( var (
total [][]string resellerMap = make(map[int32]*ResellerLoss)
gt [][]string total [][]string
gt []*ResellerLoss
) )
for _, v := range data.List { for _, info := range data.List {
if v.Loss <= -100 { // 检查经销商是否已存在
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{ total = append(total, []string{
v.OursProductName, fmt.Sprintf("%d", v.ResellerName),
fmt.Sprintf("%.2f", v.Loss), fmt.Sprintf("%.2f", v.Total),
}) })
} }
if v.Loss <= -500 { if v.Total <= -500 {
gt = append(gt, []string{ gt = append(gt, v)
v.OursProductName,
fmt.Sprintf("%.2f", v.Loss),
})
} }
} }
//总量生成excel //总量生成excel
@ -75,6 +105,11 @@ func (b *BbxtTools) StatisOursProductLossSumTotal() (err error) {
filePath := b.cacheDir + "/kshj_total" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx" filePath := b.cacheDir + "/kshj_total" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx"
err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_total.xlsx", filePath, total) 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 return err
} }

View File

@ -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
}