init_edb_classify.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. )
  11. //初始化指标目录结构
  12. func InitEdbClassify() {
  13. var err error
  14. defer func() {
  15. if err != nil {
  16. fmt.Println("InitBaseIndexData Err:" + err.Error())
  17. }
  18. }()
  19. //读取excel
  20. path, err := filepath.Abs(os.Args[0])
  21. if err != nil {
  22. fmt.Println(err)
  23. }
  24. dir := filepath.Dir(path)
  25. fmt.Println("dir:" + dir)
  26. dataPath := dir + "/docs/ETA数据库目录配置.xlsx"
  27. fmt.Println("dataPath:" + dataPath)
  28. f, err := excelize.OpenFile(dataPath)
  29. if err != nil {
  30. fmt.Println(err)
  31. return
  32. }
  33. defer func() {
  34. // Close the spreadsheet.
  35. if err := f.Close(); err != nil {
  36. fmt.Println(err)
  37. }
  38. }()
  39. rows, err := f.GetRows("Sheet1")
  40. if err != nil {
  41. fmt.Println(err)
  42. return
  43. }
  44. fmt.Println("rows len:", len(rows))
  45. for rk, row := range rows {
  46. if rk > 0 {
  47. var classifyFirst, classifySecond, classifyThree string
  48. for ck, colCell := range row {
  49. switch ck {
  50. case 0:
  51. classifyFirst = colCell
  52. case 1:
  53. classifySecond = colCell
  54. case 2:
  55. classifyThree = colCell
  56. }
  57. }
  58. if classifyFirst != "" &&
  59. classifySecond != "" &&
  60. classifyThree != "" {
  61. method := "classify/get_or_add"
  62. classifyFirstMap := make(map[string]interface{})
  63. classifyFirstMap["ClassifyName"] = classifyFirst
  64. classifyFirstMap["ParentId"] = 0
  65. classifyFirstMap["Level"] = 0
  66. classifyFirstMap["ClassifyType"] = 0
  67. result, err := PostEdbLib(classifyFirstMap, method)
  68. if err != nil {
  69. utils.FileLog.Info("初始化分类1失败:" + err.Error())
  70. return
  71. }
  72. resp := new(models.ClassifyResp)
  73. err = json.Unmarshal(result, &resp)
  74. if err != nil {
  75. utils.FileLog.Info("初始化分类1失败:" + err.Error())
  76. return
  77. }
  78. if resp.Ret != 200 {
  79. utils.FileLog.Info("初始化分类1失败:" + resp.Msg + ";" + resp.ErrMsg)
  80. return
  81. }
  82. classifySecondMap := make(map[string]interface{})
  83. classifySecondMap["ClassifyName"] = classifySecond
  84. classifySecondMap["ParentId"] = resp.Data.ClassifyId
  85. classifySecondMap["Level"] = 1
  86. classifySecondMap["ClassifyType"] = 0
  87. result, err = PostEdbLib(classifySecondMap, method)
  88. if err != nil {
  89. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  90. return
  91. }
  92. resp = new(models.ClassifyResp)
  93. err = json.Unmarshal(result, &resp)
  94. if err != nil {
  95. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  96. return
  97. }
  98. if resp.Ret != 200 {
  99. utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
  100. return
  101. }
  102. classifyThreeMap := make(map[string]interface{})
  103. classifyThreeMap["ClassifyName"] = classifyThree
  104. classifyThreeMap["ParentId"] = resp.Data.ClassifyId
  105. classifyThreeMap["Level"] = 2
  106. classifyThreeMap["ClassifyType"] = 0
  107. result, err = PostEdbLib(classifyThreeMap, method)
  108. if err != nil {
  109. utils.FileLog.Info("初始化分类3失败:" + err.Error())
  110. return
  111. }
  112. resp = new(models.ClassifyResp)
  113. err = json.Unmarshal(result, &resp)
  114. if err != nil {
  115. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  116. return
  117. }
  118. if resp.Ret != 200 {
  119. utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
  120. return
  121. }
  122. } else {
  123. fmt.Println("data is empty")
  124. fmt.Println(classifyFirst, classifySecond, classifyThree)
  125. }
  126. }
  127. }
  128. }