feat: 增加分销商负利润详情excel填充
This commit is contained in:
parent
1fbfe8eae2
commit
8deb91dea9
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"ai_scheduler/internal/pkg"
|
"ai_scheduler/internal/pkg"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/xuri/excelize/v2"
|
"github.com/xuri/excelize/v2"
|
||||||
|
|
@ -92,7 +93,7 @@ func (b *BbxtTools) StatisOursProductLossSumTotal(ct []string) (err error) {
|
||||||
for _, v := range resellerMap {
|
for _, v := range resellerMap {
|
||||||
if v.Total <= -100 {
|
if v.Total <= -100 {
|
||||||
total = append(total, []string{
|
total = append(total, []string{
|
||||||
fmt.Sprintf("%d", v.ResellerName),
|
fmt.Sprintf("%s", v.ResellerName),
|
||||||
fmt.Sprintf("%.2f", v.Total),
|
fmt.Sprintf("%.2f", v.Total),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -103,12 +104,13 @@ func (b *BbxtTools) StatisOursProductLossSumTotal(ct []string) (err error) {
|
||||||
//总量生成excel
|
//总量生成excel
|
||||||
if len(total) > 0 {
|
if len(total) > 0 {
|
||||||
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_v2.xlsx", filePath, total)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(gt) > 0 {
|
if len(gt) > 0 {
|
||||||
filePath := b.cacheDir + "/kshj_gt" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx"
|
filePath := b.cacheDir + "/kshj_gt" + fmt.Sprintf("%d", time.Now().Unix()) + ".xlsx"
|
||||||
err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, total)
|
// err = b.SimpleFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, total)
|
||||||
|
err = b.resellerDetailFillExcel(b.excelTempDir+"/"+"kshj_gt.xlsx", filePath, gt)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -166,3 +168,91 @@ func (b *BbxtTools) SimpleFillExcel(templatePath, outputPath string, dataSlice i
|
||||||
// 6. 保存
|
// 6. 保存
|
||||||
return f.SaveAs(outputPath)
|
return f.SaveAs(outputPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 分销商负利润详情填充excel
|
||||||
|
// 1.使用模板文件作为输出文件
|
||||||
|
// 2.分销商总计使用第二行样式(宽高、背景、颜色等)
|
||||||
|
// 3.商品详情使用第三行样式(宽高、背景、颜色等)
|
||||||
|
// 4.保存为新文件
|
||||||
|
func (b *BbxtTools) resellerDetailFillExcel(templatePath, outputPath string, dataSlice []*ResellerLoss) error {
|
||||||
|
// 1. 读取模板
|
||||||
|
f, err := excelize.OpenFile(templatePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
sheet := f.GetSheetName(0)
|
||||||
|
|
||||||
|
// 获取模板样式1:第二行-分销商总计
|
||||||
|
resellerTplRow := 2
|
||||||
|
styleIDReseller, err := f.GetCellStyle(sheet, fmt.Sprintf("A%d", resellerTplRow))
|
||||||
|
if err != nil {
|
||||||
|
// 如果获取失败,就不应用样式,或者记录日志,这里选择忽略错误继续
|
||||||
|
styleIDReseller = 0
|
||||||
|
}
|
||||||
|
rowHeightReseller, err := f.GetRowHeight(sheet, resellerTplRow)
|
||||||
|
if err != nil {
|
||||||
|
rowHeightReseller = 31 // 默认高度
|
||||||
|
}
|
||||||
|
// 获取模板样式2:第三行-产品亏损明细
|
||||||
|
productTplRow := 3
|
||||||
|
styleIDProduct, err := f.GetCellStyle(sheet, fmt.Sprintf("A%d", productTplRow))
|
||||||
|
if err != nil {
|
||||||
|
// 如果获取失败,就不应用样式,或者记录日志,这里选择忽略错误继续
|
||||||
|
styleIDProduct = 0
|
||||||
|
}
|
||||||
|
rowHeightProduct, err := f.GetRowHeight(sheet, productTplRow)
|
||||||
|
if err != nil {
|
||||||
|
rowHeightProduct = 25 // 默认高度
|
||||||
|
}
|
||||||
|
|
||||||
|
currentRow := 2
|
||||||
|
|
||||||
|
for _, reseller := range dataSlice {
|
||||||
|
// 3. 填充经销商数据 (ResellerName, Total)
|
||||||
|
// 设置行高
|
||||||
|
f.SetRowHeight(sheet, currentRow, rowHeightReseller)
|
||||||
|
|
||||||
|
// 设置单元格值
|
||||||
|
f.SetCellValue(sheet, fmt.Sprintf("A%d", currentRow), reseller.ResellerName)
|
||||||
|
f.SetCellValue(sheet, fmt.Sprintf("B%d", currentRow), reseller.Total)
|
||||||
|
|
||||||
|
// 应用样式
|
||||||
|
if styleIDReseller != 0 {
|
||||||
|
f.SetCellStyle(sheet, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("B%d", currentRow), styleIDReseller)
|
||||||
|
}
|
||||||
|
|
||||||
|
currentRow++
|
||||||
|
|
||||||
|
// 4. 填充产品亏损明细
|
||||||
|
// 先对 ProductLoss 进行排序
|
||||||
|
var products []ProductLoss
|
||||||
|
for _, p := range reseller.ProductLoss {
|
||||||
|
products = append(products, p)
|
||||||
|
}
|
||||||
|
// 按 Loss 升序排序 (亏损越多越靠前,负数越小)
|
||||||
|
sort.Slice(products, func(i, j int) bool {
|
||||||
|
return products[i].Loss < products[j].Loss
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, p := range products {
|
||||||
|
// 设置行高
|
||||||
|
f.SetRowHeight(sheet, currentRow, rowHeightProduct)
|
||||||
|
|
||||||
|
// 设置单元格值
|
||||||
|
f.SetCellValue(sheet, fmt.Sprintf("A%d", currentRow), p.ProductName)
|
||||||
|
f.SetCellValue(sheet, fmt.Sprintf("B%d", currentRow), p.Loss)
|
||||||
|
|
||||||
|
// 应用样式
|
||||||
|
if styleIDProduct != 0 {
|
||||||
|
f.SetCellStyle(sheet, fmt.Sprintf("A%d", currentRow), fmt.Sprintf("B%d", currentRow), styleIDProduct)
|
||||||
|
}
|
||||||
|
|
||||||
|
currentRow++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 6. 保存
|
||||||
|
return f.SaveAs(outputPath)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ func Test_StatisOursProductLossSumApiTotal(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
err = o.StatisOursProductLossSumTotal()
|
err = o.StatisOursProductLossSumTotal([]string{"2025-12-28+00:00:00", "2025-12-28+23:59:59.999"})
|
||||||
|
|
||||||
t.Log(err)
|
t.Log(err)
|
||||||
|
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Reference in New Issue