From b74c01e08ad85b70c099c6c1fccbf95650be57a2 Mon Sep 17 00:00:00 2001 From: Leo Date: Mon, 26 Aug 2024 17:44:26 +0800 Subject: [PATCH] feature: init high_opt_loss --- cmd/cus_eng_con_sys.go | 6 +- cmd/high_opt_loss.go | 7 +- high-opt-loss/common/excelkit.go | 80 +++++++++++++++++++ .../service/get_high_opt_loss_list.go | 37 ++++++++- 4 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 high-opt-loss/common/excelkit.go diff --git a/cmd/cus_eng_con_sys.go b/cmd/cus_eng_con_sys.go index 204eafe..434bb82 100644 --- a/cmd/cus_eng_con_sys.go +++ b/cmd/cus_eng_con_sys.go @@ -28,7 +28,7 @@ var engSysCmd = &cobra.Command{ } func run(c *config.Config) { - j, _ := filekit.ReadString("testo.json") + j, _ := filekit.ReadString("test1.json") data := gjson.Get(j, "data").Array() for _, v := range data { name := v.Get("name").String() @@ -36,8 +36,8 @@ func run(c *config.Config) { if name == c.Step && process == c.Process { logkit.Info("***现在开始处理***:" + gjson.Get(v.String(), "tasktitle").String()) _ = commonkit.RecoverFuncWrapper(func() { - //service.ReqAccConfirm(v.Get("processInstanceId").String(), v.Get("id").String()) - service.ReqWithdrawAssign(v.Get("processInstanceId").String()) + service.ReqAccConfirm(v.Get("processInstanceId").String(), v.Get("id").String()) + //service.ReqWithdrawAssign(v.Get("processInstanceId").String()) //service.ReqAccDelivery(v.Get("processInstanceId").String(), v.Get("id").String()) }) time.Sleep(1 * time.Second) diff --git a/cmd/high_opt_loss.go b/cmd/high_opt_loss.go index ed273dc..4c838b8 100644 --- a/cmd/high_opt_loss.go +++ b/cmd/high_opt_loss.go @@ -1,8 +1,10 @@ package cmd import ( + "cu-helper/high-opt-loss/common" "cu-helper/high-opt-loss/service" "github.com/mizuki1412/go-core-kit/init/initkit" + "github.com/mizuki1412/go-core-kit/service/logkit" "github.com/spf13/cobra" ) @@ -16,8 +18,9 @@ var highOpticalLossCmd = &cobra.Command{ Short: "Generate a High Optical Loss Report", Run: func(cmd *cobra.Command, args []string) { initkit.BindFlags(cmd) - service.ReqGetHighOptLossList("1") - + m := service.ReqGetHighOptLossList("2") + logkit.Info("拿到大光衰数据,开始生成...") + common.GenExcelFile(m) }, } diff --git a/high-opt-loss/common/excelkit.go b/high-opt-loss/common/excelkit.go new file mode 100644 index 0000000..2d708c9 --- /dev/null +++ b/high-opt-loss/common/excelkit.go @@ -0,0 +1,80 @@ +package common + +import ( + "cu-helper/high-opt-loss/service" + "fmt" + "github.com/mizuki1412/go-core-kit/service/logkit" + "github.com/spf13/cast" + "github.com/xuri/excelize/v2" + "strconv" +) + +func GenExcelFile(m map[string]*service.HighOptLoss) { + // Open the Excel file + f, err := excelize.OpenFile("/Users/leo/Documents/大光衰通报模板.xlsx") + if err != nil { + fmt.Println(err) + return + } + + // Cut and paste data from E4:E22 to C4:C22 + for i := 4; i <= 22; 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:H22 with data from the map + for i := 4; i <= 22; i++ { + bCell := "B" + strconv.Itoa(i) + key, _ := f.GetCellValue("汇总", bCell) + if data, ok := m[key]; ok { + f.SetCellValue("汇总", "E"+strconv.Itoa(i), data.Count) + f.SetCellValue("汇总", "F"+strconv.Itoa(i), data.DurationOverSeven) + f.SetCellValue("汇总", "G"+strconv.Itoa(i), data.DurationOverFifteen) + f.SetCellValue("汇总", "H"+strconv.Itoa(i), data.Book) + } else { + f.SetCellValue("汇总", "E"+strconv.Itoa(i), 0) + f.SetCellValue("汇总", "F"+strconv.Itoa(i), 0) + f.SetCellValue("汇总", "G"+strconv.Itoa(i), 0) + f.SetCellValue("汇总", "H"+strconv.Itoa(i), 0) + } + } + + // Calculate D4:D22 as C-E + for i := 4; i <= 22; 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) + } + // Sum columns C:H and place the results in row 23 + for col := 'C'; col <= 'H'; col++ { + sumCell := string(col) + "23" + sumFormula := fmt.Sprintf("SUM(%s4:%s22)", 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 <= 'H'; col++ { + sumCell := string(col) + "23" + value, err := f.CalcCellValue("汇总", sumCell) + if err != nil { + fmt.Println(err) + } else { + f.SetCellValue("汇总", sumCell, cast.ToInt(value)) + } + } + + // Save the file + if err := f.SaveAs("/Users/leo/Documents/大光衰通报模板.xlsx"); err != nil { + fmt.Println(err) + } + logkit.Info("生成结束...") +} diff --git a/high-opt-loss/service/get_high_opt_loss_list.go b/high-opt-loss/service/get_high_opt_loss_list.go index 239303d..11a10ea 100644 --- a/high-opt-loss/service/get_high_opt_loss_list.go +++ b/high-opt-loss/service/get_high_opt_loss_list.go @@ -2,18 +2,28 @@ package service import ( "cu-helper/high-opt-loss/model" - "fmt" "github.com/go-resty/resty/v2" "github.com/mizuki1412/go-core-kit/class/exception" + "github.com/tidwall/gjson" "time" ) //获取光衰数据列表 -func ReqGetHighOptLossList(receiveFlag string) { +type HighOptLoss struct { + Count int + CountFlag int //1绿色,2黄色,3红色 + DurationOverSeven int + DurationOverSevenFlag int //1绿色,2黄色,3红色 + DurationOverFifteen int + DurationOverFifteenFlag int //1绿色,2黄色,3红色 + Book int + BookFlag int //1绿色,2黄色,3红色 +} + +func ReqGetHighOptLossList(receiveFlag string) map[string]*HighOptLoss { h := model.NewHeader() header := h.GenReqParam() - fmt.Println(header) f := model.NewGetHighOptLossList(receiveFlag) form := f.GenReqParam() client := resty.New().SetRetryCount(5).SetRetryWaitTime(10 * time.Second) @@ -22,5 +32,24 @@ func ReqGetHighOptLossList(receiveFlag string) { if err != nil { panic(exception.New(err.Error())) } - fmt.Println(resp.String()) + //handle光衰数据 + data := gjson.Get(resp.String(), "list").Array() + m := make(map[string]*HighOptLoss) + for _, v := range data { + if m[v.Get("DEAL_MAN").String()] == nil { + m[v.Get("DEAL_MAN").String()] = &HighOptLoss{} + } + m[v.Get("DEAL_MAN").String()].Count++ + duration := v.Get("TIMELONGS").Int() + if duration >= 604800 { + m[v.Get("DEAL_MAN").String()].DurationOverSeven++ + } + if duration >= 1296000 { + m[v.Get("DEAL_MAN").String()].DurationOverFifteen++ + } + if v.Get("BOOK_TIME").String() != "" { + m[v.Get("DEAL_MAN").String()].Book++ + } + } + return m }