init_mysteel_index.go 5.4 KB

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