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 == "" || 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("全部生成完成") }