125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package bbxt
|
||
|
||
import (
|
||
"ai_scheduler/pkg"
|
||
"fmt"
|
||
"sort"
|
||
"time"
|
||
)
|
||
|
||
type BbxtTools struct {
|
||
cacheDir string
|
||
excelTempDir string
|
||
}
|
||
|
||
func NewBbxtTools() (*BbxtTools, error) {
|
||
cache, err := pkg.GetCacheDir()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
tempDir, err := pkg.GetTmplDir()
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
return &BbxtTools{
|
||
cacheDir: cache,
|
||
excelTempDir: fmt.Sprintf("%s/excel_temp", tempDir),
|
||
}, nil
|
||
}
|
||
|
||
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
|
||
}
|
||
return
|
||
}
|
||
|
||
// OursProductLossSum 负利润分析
|
||
func (b *BbxtTools) StatisOursProductLossSumTotal(ct []string) (err error) {
|
||
data, err := StatisOursProductLossSumApi(&StatisOursProductLossSumReq{
|
||
Ct: ct,
|
||
})
|
||
if err != nil {
|
||
return
|
||
}
|
||
var (
|
||
resellerMap = make(map[int32]*ResellerLoss)
|
||
total [][]string
|
||
gt []*ResellerLoss
|
||
)
|
||
|
||
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
|
||
}
|
||
}
|
||
|
||
// 按经销商总亏损排序
|
||
resellers := make([]*ResellerLoss, 0, len(resellerMap))
|
||
for _, v := range resellerMap {
|
||
resellers = append(resellers, v)
|
||
}
|
||
sort.Slice(resellers, func(i, j int) bool {
|
||
return resellers[i].Total < resellers[j].Total
|
||
})
|
||
|
||
// 构建分组
|
||
for _, v := range resellers {
|
||
if v.Total <= -100 {
|
||
total = append(total, []string{
|
||
fmt.Sprintf("%s", v.ResellerName),
|
||
fmt.Sprintf("%.2f", v.Total),
|
||
})
|
||
}
|
||
if v.Total <= -500 {
|
||
gt = append(gt, v)
|
||
}
|
||
}
|
||
//总量生成excel
|
||
if len(total) > 0 {
|
||
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.resellerDetailFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, gt)
|
||
err = b.resellerDetailFillExcelV2(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, gt)
|
||
}
|
||
return err
|
||
}
|