init_smm_index.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. )
  12. // InitSmmIndexToDataSource 初始化数据源-有色指标数据
  13. func InitSmmIndexToDataSource(filePath string) {
  14. var err error
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("InitBaseIndexData Err:" + err.Error())
  18. }
  19. }()
  20. //读取excel
  21. path, err := filepath.Abs(os.Args[0])
  22. if err != nil {
  23. fmt.Println(err)
  24. }
  25. dir := filepath.Dir(path)
  26. fmt.Println("dir:" + dir)
  27. dataPath := dir + filePath
  28. fmt.Println("dataPath:" + dataPath)
  29. f, err := excelize.OpenFile(dataPath)
  30. if err != nil {
  31. fmt.Println(err)
  32. return
  33. }
  34. defer func() {
  35. // Close the spreadsheet.
  36. if err := f.Close(); err != nil {
  37. fmt.Println(err)
  38. }
  39. }()
  40. rows, err := f.GetRows("Sheet1")
  41. if err != nil {
  42. fmt.Println(err)
  43. return
  44. }
  45. classifyMethod := "smm/smm_classify/get_or_add"
  46. indexMethod := "smm/add/index/to_data_source"
  47. fmt.Println("rows len:", len(rows))
  48. for rk, row := range rows {
  49. if rk > 0 {
  50. var classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source string
  51. for ck, colCell := range row {
  52. switch ck {
  53. case 0:
  54. classifyFirst = colCell
  55. case 1:
  56. classifySecond = colCell
  57. case 2:
  58. classifyThree = colCell
  59. case 3:
  60. indexCode = colCell
  61. case 4:
  62. indexName = colCell
  63. case 5:
  64. frequency = colCell
  65. case 6:
  66. unit = colCell
  67. case 7:
  68. source = colCell
  69. }
  70. }
  71. if classifySecond != "" &&
  72. classifyThree != "" &&
  73. indexCode != "" &&
  74. indexName != "" &&
  75. unit != "" &&
  76. frequency != "" {
  77. // 如果频度只有年月日这种,并没有度,那么需要补充
  78. if !strings.Contains(frequency, "度") {
  79. frequency = frequency + "度"
  80. }
  81. classifySecondMap := make(map[string]interface{})
  82. classifySecondMap["ClassifyName"] = classifySecond
  83. classifySecondMap["ParentId"] = 0
  84. classifySecondMap["Level"] = 0
  85. classifySecondMap["SysUserId"] = utils.OpUserId
  86. classifySecondMap["SysUserRealName"] = utils.OpUserRealName
  87. result, err := PostEdbLib(classifySecondMap, classifyMethod)
  88. if err != nil {
  89. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  90. return
  91. }
  92. resp := new(models.BaseFromSmmClassifyResp)
  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"] = 1
  106. classifyThreeMap["SysUserId"] = utils.OpUserId
  107. classifyThreeMap["SysUserRealName"] = utils.OpUserRealName
  108. result, err = PostEdbLib(classifyThreeMap, classifyMethod)
  109. if err != nil {
  110. utils.FileLog.Info("初始化分类3失败:" + err.Error())
  111. return
  112. }
  113. resp = new(models.BaseFromSmmClassifyResp)
  114. err = json.Unmarshal(result, &resp)
  115. if err != nil {
  116. utils.FileLog.Info("初始化分类3失败:" + err.Error())
  117. return
  118. }
  119. if resp.Ret != 200 {
  120. utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
  121. return
  122. }
  123. indexMap := make(map[string]interface{})
  124. indexMap["EdbCode"] = indexCode
  125. indexMap["ClassifyId"] = resp.Data.ClassifyId
  126. indexMap["SysUserId"] = utils.OpUserId
  127. indexMap["SysUserRealName"] = utils.OpUserRealName
  128. result, err = PostEdbLib(indexMap, indexMethod)
  129. if err != nil {
  130. utils.FileLog.Info("初始化指标失败:" + err.Error() + " result:" + string(result))
  131. return
  132. }
  133. indexResp := new(models.EdbInfoResp)
  134. err = json.Unmarshal(result, &indexResp)
  135. if err != nil {
  136. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  137. return
  138. }
  139. if indexResp.Ret != 200 {
  140. if strings.Contains(indexResp.Msg, "新增指标失败") {
  141. continue
  142. } else {
  143. fmt.Println("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  144. utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  145. return
  146. }
  147. }
  148. fmt.Println("add index success:" + indexCode)
  149. } else {
  150. fmt.Println("data is empty")
  151. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  152. }
  153. }
  154. }
  155. }