You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
9.8 KiB
Go

package common
import (
"cu-helper/wms/service"
"fmt"
"github.com/mizuki1412/go-core-kit/library/mathkit"
"github.com/mizuki1412/go-core-kit/service/configkit"
"github.com/mizuki1412/go-core-kit/service/logkit"
"github.com/spf13/cast"
"github.com/xuri/excelize/v2"
"path/filepath"
"strconv"
"time"
)
func GenExcelFile(m1 map[string]*service.HighOptLossUnder, m2 map[string]*service.HighOptLossHistory) {
// Open the Excel file
open := configkit.GetString("loss.open", "")
if open == "" {
open = "/Users/leo/Documents/光衰通报/光衰/2025/光衰通报(11月5日).xlsx"
}
f, err := excelize.OpenFile(open)
if err != nil {
fmt.Println(err)
return
}
styleRed, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
Horizontal: "center",
Vertical: "center",
},
Border: []excelize.Border{
{Type: "top", Style: 1, Color: "000000"},
{Type: "bottom", Style: 1, Color: "000000"},
{Type: "left", Style: 1, Color: "000000"},
{Type: "right", Style: 1, Color: "000000"},
},
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"C00000"}, // RGB(192,0,0)
Pattern: 1,
},
Font: &excelize.Font{
Color: "FFFFFF", // 白色
Bold: true,
},
})
styleRedFMT, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
Horizontal: "center",
Vertical: "center",
},
Border: []excelize.Border{
{Type: "top", Style: 1, Color: "000000"},
{Type: "bottom", Style: 1, Color: "000000"},
{Type: "left", Style: 1, Color: "000000"},
{Type: "right", Style: 1, Color: "000000"},
},
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"C00000"}, // RGB(192,0,0)
Pattern: 1,
},
Font: &excelize.Font{
Color: "FFFFFF", // 白色
Bold: true,
},
NumFmt: 10, // 或使用自定义格式代码:"0.00%"
})
styleYellow, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
Horizontal: "center",
Vertical: "center",
},
Border: []excelize.Border{
{Type: "top", Style: 1, Color: "000000"},
{Type: "bottom", Style: 1, Color: "000000"},
{Type: "left", Style: 1, Color: "000000"},
{Type: "right", Style: 1, Color: "000000"},
},
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"FFC000"}, // RGB(255,192,0)
Pattern: 1,
},
Font: &excelize.Font{
Bold: true,
},
})
styleGreen, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
Horizontal: "center",
Vertical: "center",
},
Border: []excelize.Border{
{Type: "top", Style: 1, Color: "000000"},
{Type: "bottom", Style: 1, Color: "000000"},
{Type: "left", Style: 1, Color: "000000"},
{Type: "right", Style: 1, Color: "000000"},
},
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"92D050"}, // RGB(146,208,80)
Pattern: 1,
},
Font: &excelize.Font{
Bold: true,
},
})
styleGreenFMT, err := f.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{
Horizontal: "center",
Vertical: "center",
},
Border: []excelize.Border{
{Type: "top", Style: 1, Color: "000000"},
{Type: "bottom", Style: 1, Color: "000000"},
{Type: "left", Style: 1, Color: "000000"},
{Type: "right", Style: 1, Color: "000000"},
},
Fill: excelize.Fill{
Type: "pattern",
Color: []string{"92D050"}, // RGB(146,208,80)
Pattern: 1,
},
Font: &excelize.Font{
Bold: true,
},
NumFmt: 10, // 或使用自定义格式代码:"0.00%"
})
// Cut and paste data from E4:E25 to C4:C25
//TODO
for i := 4; i <= 25; i++ {
eCell := "E" + strconv.Itoa(i)
cCell := "C" + strconv.Itoa(i)
value, _ := f.GetCellValue("汇总", eCell)
f.SetCellValue("汇总", cCell, cast.ToInt(value))
f.SetCellValue("汇总", eCell, "")
}
// Populate E4:L25 with data from the map
//TODO
for i := 4; i <= 25; i++ {
bCell := "B" + strconv.Itoa(i)
key, _ := f.GetCellValue("汇总", bCell)
if data, ok := m1[key]; ok {
f.SetCellValue("汇总", "E"+strconv.Itoa(i), data.UnderCount)
if data.UnderCount == 0 {
f.SetCellStyle("汇总", "E"+strconv.Itoa(i), "E"+strconv.Itoa(i), styleGreen)
}
if data.UnderCount > 0 && data.UnderCount <= 5 {
f.SetCellStyle("汇总", "E"+strconv.Itoa(i), "E"+strconv.Itoa(i), styleYellow)
}
if data.UnderCount > 5 {
f.SetCellStyle("汇总", "E"+strconv.Itoa(i), "E"+strconv.Itoa(i), styleRed)
}
f.SetCellValue("汇总", "F"+strconv.Itoa(i), data.DurationOverSeven)
if data.DurationOverSeven == 0 {
f.SetCellStyle("汇总", "F"+strconv.Itoa(i), "F"+strconv.Itoa(i), styleGreen)
}
if data.DurationOverSeven > 0 && data.DurationOverSeven <= 5 {
f.SetCellStyle("汇总", "F"+strconv.Itoa(i), "F"+strconv.Itoa(i), styleYellow)
}
if data.DurationOverSeven > 5 {
f.SetCellStyle("汇总", "F"+strconv.Itoa(i), "F"+strconv.Itoa(i), styleRed)
}
f.SetCellValue("汇总", "G"+strconv.Itoa(i), data.DurationOverFifteen)
if data.DurationOverFifteen == 0 {
f.SetCellStyle("汇总", "G"+strconv.Itoa(i), "G"+strconv.Itoa(i), styleGreen)
}
if data.DurationOverFifteen > 0 && data.DurationOverFifteen <= 5 {
f.SetCellStyle("汇总", "G"+strconv.Itoa(i), "G"+strconv.Itoa(i), styleYellow)
}
if data.DurationOverFifteen > 5 {
f.SetCellStyle("汇总", "G"+strconv.Itoa(i), "G"+strconv.Itoa(i), styleRed)
}
f.SetCellValue("汇总", "H"+strconv.Itoa(i), data.Book)
if data.Book == 0 {
f.SetCellStyle("汇总", "H"+strconv.Itoa(i), "H"+strconv.Itoa(i), styleRed)
}
if data.Book > 0 {
f.SetCellStyle("汇总", "H"+strconv.Itoa(i), "H"+strconv.Itoa(i), styleGreen)
}
if data.Book == 0 && data.UnderCount == 0 {
f.SetCellStyle("汇总", "H"+strconv.Itoa(i), "H"+strconv.Itoa(i), styleGreen)
}
f.SetCellValue("汇总", "I"+strconv.Itoa(i), data.CantCount)
f.SetCellValue("汇总", "J"+strconv.Itoa(i), data.ApplyCount)
} else {
f.SetCellValue("汇总", "E"+strconv.Itoa(i), 0)
f.SetCellStyle("汇总", "E"+strconv.Itoa(i), "E"+strconv.Itoa(i), styleGreen)
f.SetCellValue("汇总", "F"+strconv.Itoa(i), 0)
f.SetCellStyle("汇总", "F"+strconv.Itoa(i), "F"+strconv.Itoa(i), styleGreen)
f.SetCellValue("汇总", "G"+strconv.Itoa(i), 0)
f.SetCellStyle("汇总", "G"+strconv.Itoa(i), "G"+strconv.Itoa(i), styleGreen)
f.SetCellValue("汇总", "H"+strconv.Itoa(i), 0)
f.SetCellStyle("汇总", "H"+strconv.Itoa(i), "H"+strconv.Itoa(i), styleGreen)
f.SetCellValue("汇总", "I"+strconv.Itoa(i), 0)
f.SetCellValue("汇总", "J"+strconv.Itoa(i), 0)
}
if data, ok := m2[key]; ok {
f.SetCellValue("汇总", "K"+strconv.Itoa(i), data.Count)
if data1, ok1 := m1[key]; ok1 {
//有归档+有在途
f.SetCellValue("汇总", "L"+strconv.Itoa(i), data.Count+data1.Count)
v := mathkit.FloatRound(float64(data.Count)/(float64(data.Count)+float64(data1.Count)), 4)
f.SetCellValue("汇总", "M"+strconv.Itoa(i), v)
if v < 0.8 {
f.SetCellStyle("汇总", "M"+strconv.Itoa(i), "M"+strconv.Itoa(i), styleRedFMT)
}
if v >= 0.8 {
f.SetCellStyle("汇总", "M"+strconv.Itoa(i), "M"+strconv.Itoa(i), styleGreenFMT)
}
} else {
//有归档+无在途
f.SetCellValue("汇总", "L"+strconv.Itoa(i), data.Count)
v := mathkit.FloatRound(float64(data.Count)/(float64(data.Count)), 4)
f.SetCellValue("汇总", "M"+strconv.Itoa(i), v)
if v < 0.8 {
f.SetCellStyle("汇总", "M"+strconv.Itoa(i), "M"+strconv.Itoa(i), styleRedFMT)
}
if v >= 0.8 {
f.SetCellStyle("汇总", "M"+strconv.Itoa(i), "M"+strconv.Itoa(i), styleGreenFMT)
}
}
} else {
f.SetCellValue("汇总", "K"+strconv.Itoa(i), 0)
if data1, ok1 := m1[key]; ok1 {
//无归档+有在途
f.SetCellValue("汇总", "L"+strconv.Itoa(i), data1.Count)
f.SetCellValue("汇总", "M"+strconv.Itoa(i), 0)
f.SetCellStyle("汇总", "M"+strconv.Itoa(i), "M"+strconv.Itoa(i), styleRedFMT)
} else {
//无归档+无在途
f.SetCellValue("汇总", "L"+strconv.Itoa(i), 0)
f.SetCellValue("汇总", "M"+strconv.Itoa(i), 1)
f.SetCellStyle("汇总", "M"+strconv.Itoa(i), "M"+strconv.Itoa(i), styleGreenFMT)
}
}
}
// Calculate D4:D25 as C-E
//TODO
for i := 4; i <= 25; i++ {
cCell := "C" + strconv.Itoa(i)
eCell := "E" + strconv.Itoa(i)
dCell := "D" + strconv.Itoa(i)
cValue, _ := f.GetCellValue("汇总", cCell)
eValue, _ := f.GetCellValue("汇总", eCell)
cInt, _ := strconv.Atoi(cValue)
eInt, _ := strconv.Atoi(eValue)
f.SetCellValue("汇总", dCell, cInt-eInt)
if cInt-eInt == 0 {
//本周压降为0但是当前遗留量为0
if eInt == 0 {
f.SetCellStyle("汇总", dCell, dCell, styleGreen)
} else {
//当前有遗留但是没压降量
f.SetCellStyle("汇总", dCell, dCell, styleYellow)
}
}
if cInt-eInt < 0 {
f.SetCellStyle("汇总", dCell, dCell, styleRed)
}
if cInt-eInt > 0 {
f.SetCellStyle("汇总", dCell, dCell, styleGreen)
}
}
// Sum columns C:H and place the results in row 26
for col := 'C'; col <= 'L'; col++ {
//TODO
sumCell := string(col) + "26"
//TODO
sumFormula := fmt.Sprintf("SUM(%s4:%s25)", string(col), string(col))
f.SetCellFormula("汇总", sumCell, sumFormula)
}
// Recalculate the sum for each cell to ensure it appears without manual intervention
for col := 'C'; col <= 'L'; col++ {
//TODO
sumCell := string(col) + "26"
value, err := f.CalcCellValue("汇总", sumCell)
if err != nil {
fmt.Println(err)
} else {
f.SetCellValue("汇总", sumCell, cast.ToInt(value))
}
}
//TODO 3行
done, _ := f.GetCellValue("汇总", "K26")
total, _ := f.GetCellValue("汇总", "L26")
f.SetCellValue("汇总", "M26", mathkit.FloatRound(cast.ToFloat64(done)/cast.ToFloat64(total), 4))
//设置表头
content := fmt.Sprintf("西湖智家光衰通报:%s", time.Now().Format("1月2日"))
f.SetCellValue("汇总", "A1", content)
// Save the file
save := filepath.Dir(open) + "/光衰通报(" + time.Now().Format("1月2日") + ").xlsx"
if err := f.SaveAs(save); err != nil {
fmt.Println(err)
}
logkit.Info("生成结束...")
}