init_base_index.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package services
  2. import (
  3. "fmt"
  4. "github.com/xuri/excelize/v2"
  5. "hongze/hz_eta_data/models"
  6. "hongze/hz_eta_data/utils"
  7. "os"
  8. "path/filepath"
  9. )
  10. // 初始化基础指标数据
  11. func InitBaseIndexData() {
  12. var err error
  13. defer func() {
  14. if err != nil {
  15. fmt.Println("InitBaseIndexData Err:" + err.Error())
  16. }
  17. }()
  18. //读取excel
  19. path, err := filepath.Abs(os.Args[0])
  20. if err != nil {
  21. fmt.Println(err)
  22. }
  23. dir := filepath.Dir(path)
  24. fmt.Println("dir:" + dir)
  25. dataPath := dir + "/docs/逸诺初始化数据示例.xlsx"
  26. fmt.Println("dataPath:" + dataPath)
  27. f, err := excelize.OpenFile(dataPath)
  28. if err != nil {
  29. fmt.Println(err)
  30. return
  31. }
  32. defer func() {
  33. // Close the spreadsheet.
  34. if err := f.Close(); err != nil {
  35. fmt.Println(err)
  36. }
  37. }()
  38. rows, err := f.GetRows("Sheet1")
  39. if err != nil {
  40. fmt.Println(err)
  41. return
  42. }
  43. fmt.Println("rows len:", len(rows))
  44. for rk, row := range rows {
  45. if rk > 0 {
  46. var classifyFirst, classifySecond, classifyThree, indexCode, indexName, unit, frequency, source string
  47. for ck, colCell := range row {
  48. switch ck {
  49. case 0:
  50. classifyFirst = colCell
  51. case 1:
  52. classifySecond = colCell
  53. case 2:
  54. classifySecond = colCell
  55. case 3:
  56. indexCode = colCell
  57. case 4:
  58. indexName = colCell
  59. case 5:
  60. frequency = colCell
  61. case 6:
  62. unit = colCell
  63. case 7:
  64. source = colCell
  65. }
  66. }
  67. if classifyFirst != "" &&
  68. classifySecond != "" &&
  69. classifyThree != "" &&
  70. indexCode != "" &&
  71. indexName != "" &&
  72. unit != "" &&
  73. frequency != "" &&
  74. source != "" {
  75. method := "classify/get_or_add"
  76. classifyFirstMap := make(map[string]interface{})
  77. classifyFirstMap["ClassifyName"] = classifyFirst
  78. classifyFirstMap["ParentId"] = 0
  79. classifyFirstMap["Level"] = 0
  80. classifyFirstMap["ClassifyType"] = 0
  81. resp, err := PostEdbLib(classifyFirstMap, method)
  82. if err != nil {
  83. utils.FileLog.Info("初始化分类1失败:" + err.Error())
  84. return
  85. }
  86. if resp.Ret != 200 {
  87. utils.FileLog.Info("初始化分类1失败:" + resp.Msg + ";" + resp.ErrMsg)
  88. return
  89. }
  90. classifySecondMap := make(map[string]interface{})
  91. classifySecondMap["ClassifyName"] = classifySecond
  92. classifySecondMap["ParentId"] = 0
  93. classifySecondMap["Level"] = 0
  94. classifySecondMap["ClassifyType"] = 0
  95. resp, err = PostEdbLib(classifySecondMap, method)
  96. if err != nil {
  97. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  98. return
  99. }
  100. if resp.Ret != 200 {
  101. utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
  102. return
  103. }
  104. classifyThreeMap := make(map[string]interface{})
  105. classifyThreeMap["ClassifyName"] = classifyThree
  106. classifyThreeMap["ParentId"] = 0
  107. classifyThreeMap["Level"] = 0
  108. classifyThreeMap["ClassifyType"] = 0
  109. resp, err = PostEdbLib(classifyThreeMap, method)
  110. if err != nil {
  111. utils.FileLog.Info("初始化分类3失败:" + err.Error())
  112. return
  113. }
  114. if resp.Ret != 200 {
  115. utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
  116. return
  117. }
  118. classify := resp.Data.(*models.EdbClassify)
  119. var classifyId int
  120. if classify != nil {
  121. classifyId = classify.ClassifyId
  122. }
  123. method = "edb_info/add"
  124. indexMap := make(map[string]interface{})
  125. indexMap["Source"] = IndexSourceMap[source]
  126. indexMap["EdbCode"] = indexCode
  127. indexMap["EdbName"] = indexName
  128. indexMap["Frequency"] = frequency
  129. indexMap["Unit"] = unit
  130. indexMap["ClassifyId"] = classifyId
  131. resp, err = PostEdbLib(indexMap, method)
  132. if err != nil {
  133. utils.FileLog.Info("初始化指标失败:" + err.Error())
  134. return
  135. }
  136. if resp.Ret != 200 {
  137. utils.FileLog.Info("初始化指标失败:" + resp.Msg + ";" + resp.ErrMsg)
  138. return
  139. }
  140. fmt.Println("add index success:" + indexCode)
  141. } else {
  142. fmt.Println("data is empty")
  143. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, unit, frequency, source)
  144. }
  145. }
  146. }
  147. }
  148. /*
  149. // PostEdbLib 调用指标接口
  150. func PostEdbLib(param map[string]interface{}, method string) (resp *models.BaseResponse, err error) {
  151. postUrl := utils.EDB_LIB_URL + method
  152. postData, err := json.Marshal(param)
  153. if err != nil {
  154. return
  155. }
  156. result, err := HttpPost(postUrl, string(postData), "application/json")
  157. if err != nil {
  158. return
  159. }
  160. err = json.Unmarshal(result, &resp)
  161. if err != nil {
  162. return
  163. }
  164. return resp, nil
  165. }
  166. */