init_mysteel_index.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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, isApi bool) {
  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. //dataPath = "./docs/gl_api_test.xlsx"
  30. fmt.Println("dataPath:" + dataPath)
  31. f, err := excelize.OpenFile(dataPath)
  32. if err != nil {
  33. fmt.Println(err)
  34. return
  35. }
  36. defer func() {
  37. // Close the spreadsheet.
  38. if err := f.Close(); err != nil {
  39. fmt.Println(err)
  40. }
  41. }()
  42. rows, err := f.GetRows("Sheet1")
  43. if err != nil {
  44. fmt.Println(err)
  45. return
  46. }
  47. classifyMethod := "mysteel_chemical/mysteel_classify/get_or_add"
  48. indexMethod := "mysteel_chemical/add/mysteel/index"
  49. apiHandleMethod := "mysteel_chemical/handle/api/mysteel/index"
  50. fmt.Println("rows len:", len(rows))
  51. for rk, row := range rows {
  52. if rk > 0 {
  53. var classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source, terminalCode string
  54. for ck, colCell := range row {
  55. switch ck {
  56. case 0:
  57. classifyFirst = colCell
  58. case 1:
  59. classifySecond = colCell
  60. case 2:
  61. classifyThree = colCell
  62. case 3:
  63. indexCode = colCell
  64. case 4:
  65. indexName = colCell
  66. case 5:
  67. frequency = colCell
  68. case 6:
  69. unit = colCell
  70. case 7:
  71. source = colCell
  72. case 8:
  73. terminalCode = colCell //终端号
  74. }
  75. }
  76. if classifySecond != "" &&
  77. classifyThree != "" &&
  78. indexCode != "" &&
  79. indexName != "" &&
  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. // 请求API初始化数据
  176. if !isApi {
  177. continue
  178. }
  179. fmt.Println("请求API刷新数据")
  180. handleReq := make(map[string][]map[string]interface{})
  181. handleMap := make(map[string]interface{})
  182. handleMap["IndexCode"] = indexCode
  183. handleReq["List"] = append(handleReq["List"], handleMap)
  184. res, e := PostHandleMysteelIndexEdbLib(handleReq, apiHandleMethod)
  185. if e != nil {
  186. tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib写入指标失败, code: %s, err: %v, result: %s", indexCode, e, string(res))
  187. fmt.Println(tips)
  188. utils.FileLog.Info(tips)
  189. return
  190. }
  191. handleResp := new(models.EdbInfoResp)
  192. if e = json.Unmarshal(res, &handleResp); e != nil {
  193. tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib解析resp失败, code: %s, err: %v, result: %s", indexCode, e, string(res))
  194. fmt.Println(tips)
  195. utils.FileLog.Info(tips)
  196. return
  197. }
  198. if handleResp.Ret != 200 {
  199. tips := fmt.Sprintf("PostHandleMysteelIndexEdbLib写入指标失败, code: %s, Msg: %s, ErrMsg: %s", indexCode, handleResp.Msg, handleResp.ErrMsg)
  200. fmt.Println(tips)
  201. utils.FileLog.Info(tips)
  202. return
  203. }
  204. fmt.Println("handle index success:" + indexCode)
  205. } else {
  206. fmt.Println("data is empty")
  207. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  208. }
  209. }
  210. }
  211. }