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/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()

View File

@ -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 (
resellerMap = make(map[int32]*ResellerLoss)
total [][]string
gt [][]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
}

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
}