From 96323a53059f9af7874d16224ab84dbe31b7c959 Mon Sep 17 00:00:00 2001 From: Leo Date: Thu, 18 Dec 2025 10:35:49 +0800 Subject: [PATCH] feature: gen_con_order --- cmd/gen_con_order.go | 140 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 cmd/gen_con_order.go diff --git a/cmd/gen_con_order.go b/cmd/gen_con_order.go new file mode 100644 index 0000000..3016fc3 --- /dev/null +++ b/cmd/gen_con_order.go @@ -0,0 +1,140 @@ +package cmd + +import ( + "fmt" + "github.com/mizuki1412/go-core-kit/class/exception" + "github.com/mizuki1412/go-core-kit/init/initkit" + "github.com/mizuki1412/go-core-kit/service/configkit" + "github.com/spf13/cobra" + "github.com/xuri/excelize/v2" + "path/filepath" + "strings" + "time" +) + +func init() { + rootCmd.AddCommand(gcoCmd) + defFlagsGco(gcoCmd) +} + +var gcoCmd = &cobra.Command{ + Use: "gco", + Short: "Generate construction order", + Run: func(cmd *cobra.Command, args []string) { + initkit.BindFlags(cmd) + handleGco() + }, +} + +func defFlagsGco(cmd *cobra.Command) { + cmd.Flags().String("gco.template", "", "*Specify the template used to generate construction order") + cmd.Flags().String("gco.file", "", "*Specify the file used to generate construction order") +} + +func handleGco() { + templatePath := configkit.GetString("gco.template", "/Users/leo/Desktop/归档/公众扩容/施工单模板.xlsx") + filePath := configkit.GetString("gco.file", "/Users/leo/Downloads/扩容需求表.xlsx") + src, err := excelize.OpenFile(filePath) + if err != nil { + panic(exception.New(err.Error())) + } + defer src.Close() + rows, err := src.GetRows("扩容需求表") + if err != nil { + panic(exception.New("缺少 Sheet:扩容需求表")) + } + // 今天日期 + today := time.Now().Format("2006.01.02") + today = strings.ReplaceAll(today, "-", ".") + + for i, row := range rows { + if i == 0 { + continue // 跳过表头 + } + + // V 列 index = 21 + if len(row) <= 22 || strings.TrimSpace(row[22]) != "1" { + continue + } + + // 必要字段 + var dVal, eVal, fVal, gVal, dPrefix string + + if len(row) > 3 { + dVal = strings.TrimSpace(row[3]) + + // D列 去掉“新增”及之后部分 + if idx := strings.Index(dVal, "新增"); idx != -1 { + dPrefix = dVal[:idx] + } else { + dPrefix = dVal + } + } + + if len(row) > 4 { + eVal = strings.TrimSpace(row[4]) + } + + if len(row) > 5 { + fVal = strings.TrimSpace(row[5]) + } + if len(row) > 6 { + gVal = strings.TrimSpace(row[6]) + } + + // 打开模板(每次都打开一次,保证生成多份文件不会覆盖) + tpl, err := excelize.OpenFile(templatePath) + if err != nil { + panic(exception.New("缺少 Sheet:扩容需求表")) + } + + sheet := "Sheet1" + + // 写入日期 + tpl.SetCellValue(sheet, "B5", today) + tpl.SetCellValue(sheet, "B15", today) + + // 写入 D 列 + tpl.SetCellValue(sheet, "B2", dVal) + tpl.SetCellValue(sheet, "B12", dVal) + + // 写入 E 列 + tpl.SetCellValue(sheet, "B9", eVal) + tpl.SetCellValue(sheet, "B19", eVal) + + // 写入 D 去掉新增之前部分 + tpl.SetCellValue(sheet, "B6", dPrefix) + tpl.SetCellValue(sheet, "B16", dPrefix) + + // 输出文件名(F列 + D列) + outName := "" + if fVal == "" { + outName = fmt.Sprintf("%s.xlsx", dVal) + } else { + outName = fmt.Sprintf("%s-%s.xlsx", dVal, fVal) + } + finishDate := time.Now() + if gVal == "全覆盖" { + finishDateStr := finishDate.AddDate(0, 0, 2).Format("1月2日") + outName = finishDateStr + "需完成" + outName + } else if gVal == "部分覆盖" { + finishDateStr := finishDate.AddDate(0, 0, 5).Format("1月2日") + outName = finishDateStr + "需完成" + outName + } else if gVal == "未覆盖" { + finishDateStr := finishDate.AddDate(0, 0, 15).Format("1月2日") + outName = finishDateStr + "需完成" + outName + } + thisMonth := time.Now().Format("2006.01") + outPath := filepath.Join(filepath.Dir(templatePath), "施工单", thisMonth, outName) + err = tpl.SaveAs(outPath) + if err != nil { + panic(exception.New(err.Error())) + } + + tpl.Close() + + fmt.Println("生成施工单:", outPath) + } + + fmt.Println("全部生成完成") +}