hz_edb_classify.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package services
  2. import (
  3. "encoding/json"
  4. "eta/eta_data_init/models"
  5. "eta/eta_data_init/utils"
  6. "fmt"
  7. "github.com/xuri/excelize/v2"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. )
  13. const (
  14. MethodClassifyReset = "edb_info/classify_reset"
  15. )
  16. func ResetHzEdbClassify() {
  17. fmt.Println("start ResetHzEdbClassify")
  18. for _, v := range []string{"ETA基础指标0821整理", "ETA计算指标0821整理"} {
  19. time.Sleep(5 * time.Second)
  20. ResetEdbClassify(v)
  21. }
  22. fmt.Println("end ResetHzEdbClassify")
  23. }
  24. // ResetEdbClassify 重置指标分类
  25. func ResetEdbClassify(fileName string) {
  26. var err error
  27. defer func() {
  28. if err != nil {
  29. fmt.Println("ResetBaseEdbAndClassify Err: " + err.Error())
  30. }
  31. }()
  32. path, e := filepath.Abs(os.Args[0])
  33. if e != nil {
  34. err = fmt.Errorf("path abs err: %s", e.Error())
  35. return
  36. }
  37. dir := filepath.Dir(path)
  38. //dataPath := dir + "/docs/ETA计算指标0821整理.xlsx"
  39. dataPath := fmt.Sprintf("%s/docs/%s.xlsx", dir, fileName)
  40. fmt.Println("dataPath:" + dataPath)
  41. f, e := excelize.OpenFile(dataPath)
  42. if e != nil {
  43. err = fmt.Errorf("open file err: %s", e.Error())
  44. return
  45. }
  46. defer func() {
  47. if e = f.Close(); e != nil {
  48. fmt.Println("file close err: " + e.Error())
  49. }
  50. }()
  51. rows, e := f.GetRows("Sheet1")
  52. if e != nil {
  53. err = fmt.Errorf("get rows err: %s", e.Error())
  54. return
  55. }
  56. if len(rows) == 0 {
  57. err = fmt.Errorf("empty rows")
  58. return
  59. }
  60. // 读取行
  61. utils.FileLog.Info("开始初始化")
  62. for rk, row := range rows {
  63. if rk == 0 {
  64. continue
  65. }
  66. var indexCode, classifyFirst, classifySecond, classifyThird string
  67. // 读取单元格
  68. for ck, cell := range row {
  69. switch ck {
  70. case 2:
  71. indexCode = strings.TrimSpace(cell)
  72. case 7:
  73. classifyFirst = strings.TrimSpace(cell)
  74. case 8:
  75. classifySecond = strings.TrimSpace(cell)
  76. case 9:
  77. classifyThird = strings.TrimSpace(cell)
  78. }
  79. }
  80. if indexCode == "" || classifyFirst == "" || classifySecond == "" || classifyThird == "" {
  81. utils.FileLog.Info("忽略第%d行, IndexCode: %s, ClassifyFirst: %s, ClassifySecond: %s, ClassifyThird: %s", rk+1, indexCode, classifyFirst, classifySecond, classifyThird)
  82. continue
  83. }
  84. utils.FileLog.Info("第%d行, IndexCode: %s", rk+1, indexCode)
  85. params := make(map[string]interface{})
  86. params["IndexCode"] = indexCode
  87. params["ClassifyFirst"] = classifyFirst
  88. params["ClassifySecond"] = classifySecond
  89. params["ClassifyThird"] = classifyThird
  90. result, e := PostEdbLib(params, MethodClassifyReset)
  91. if e != nil {
  92. b, _ := json.Marshal(params)
  93. utils.FileLog.Info("重置指标分类失败, err: %s, params: %s", e.Error(), string(b))
  94. return
  95. }
  96. resp := new(models.EdbInfoResp)
  97. if e := json.Unmarshal(result, &resp); e != nil {
  98. utils.FileLog.Info("MethodClassifyReset Unmarshal err: %s", e.Error())
  99. return
  100. }
  101. if resp.Ret != 200 {
  102. utils.FileLog.Info("重置指标分类失败, Msg: %s, ErrMsg: %s", resp.Msg, resp.ErrMsg)
  103. continue
  104. }
  105. }
  106. utils.FileLog.Info("初始化结束")
  107. }