init_base_index.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package services
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/xuri/excelize/v2"
  6. "hongze/hz_eta_data/models"
  7. "hongze/hz_eta_data/utils"
  8. "os"
  9. "path/filepath"
  10. )
  11. // 初始化基础指标数据
  12. func InitBaseIndexData() {
  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/逸诺初始化数据示例.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, indexCode, indexName, frequency, unit, source 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. case 3:
  57. indexCode = colCell
  58. case 4:
  59. indexName = colCell
  60. case 5:
  61. frequency = colCell
  62. case 6:
  63. unit = colCell
  64. case 7:
  65. source = colCell
  66. }
  67. }
  68. if classifyFirst != "" &&
  69. classifySecond != "" &&
  70. classifyThree != "" &&
  71. indexCode != "" &&
  72. indexName != "" &&
  73. unit != "" &&
  74. frequency != "" &&
  75. source != "" {
  76. method := "classify/get_or_add"
  77. classifyFirstMap := make(map[string]interface{})
  78. classifyFirstMap["ClassifyName"] = classifyFirst
  79. classifyFirstMap["ParentId"] = 0
  80. classifyFirstMap["Level"] = 0
  81. classifyFirstMap["ClassifyType"] = 0
  82. result, err := PostEdbLib(classifyFirstMap, method)
  83. if err != nil {
  84. utils.FileLog.Info("初始化分类1失败:" + err.Error())
  85. return
  86. }
  87. resp := new(models.ClassifyResp)
  88. err = json.Unmarshal(result, &resp)
  89. if err != nil {
  90. utils.FileLog.Info("初始化分类1失败:" + err.Error())
  91. return
  92. }
  93. if resp.Ret != 200 {
  94. utils.FileLog.Info("初始化分类1失败:" + resp.Msg + ";" + resp.ErrMsg)
  95. return
  96. }
  97. classifySecondMap := make(map[string]interface{})
  98. classifySecondMap["ClassifyName"] = classifySecond
  99. classifySecondMap["ParentId"] = resp.Data.ClassifyId
  100. classifySecondMap["Level"] = 1
  101. classifySecondMap["ClassifyType"] = 0
  102. result, err = PostEdbLib(classifySecondMap, method)
  103. if err != nil {
  104. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  105. return
  106. }
  107. resp = new(models.ClassifyResp)
  108. err = json.Unmarshal(result, &resp)
  109. if err != nil {
  110. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  111. return
  112. }
  113. if resp.Ret != 200 {
  114. utils.FileLog.Info("初始化分类2失败:" + resp.Msg + ";" + resp.ErrMsg)
  115. return
  116. }
  117. classifyThreeMap := make(map[string]interface{})
  118. classifyThreeMap["ClassifyName"] = classifyThree
  119. classifyThreeMap["ParentId"] = resp.Data.ClassifyId
  120. classifyThreeMap["Level"] = 2
  121. classifyThreeMap["ClassifyType"] = 0
  122. result, err = PostEdbLib(classifyThreeMap, method)
  123. if err != nil {
  124. utils.FileLog.Info("初始化分类3失败:" + err.Error())
  125. return
  126. }
  127. resp = new(models.ClassifyResp)
  128. err = json.Unmarshal(result, &resp)
  129. if err != nil {
  130. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  131. return
  132. }
  133. if resp.Ret != 200 {
  134. utils.FileLog.Info("初始化分类3失败:" + resp.Msg + ";" + resp.ErrMsg)
  135. return
  136. }
  137. method = "edb_info/add"
  138. indexMap := make(map[string]interface{})
  139. indexMap["Source"] = IndexSourceMap[source]
  140. indexMap["EdbCode"] = indexCode
  141. indexMap["EdbName"] = indexName
  142. indexMap["Frequency"] = frequency
  143. indexMap["Unit"] = unit
  144. indexMap["ClassifyId"] = resp.Data.ClassifyId
  145. result, err = PostEdbLib(indexMap, method)
  146. if err != nil {
  147. utils.FileLog.Info("初始化指标失败:" + err.Error())
  148. return
  149. }
  150. indexResp := new(models.BaseResponse)
  151. err = json.Unmarshal(result, &indexResp)
  152. if err != nil {
  153. utils.FileLog.Info("初始化分类2失败:" + err.Error())
  154. return
  155. }
  156. if indexResp.Ret != 200 {
  157. utils.FileLog.Info("初始化指标失败:" + indexResp.Msg + ";" + indexResp.ErrMsg)
  158. return
  159. }
  160. fmt.Println("add index success:" + indexCode)
  161. } else {
  162. fmt.Println("data is empty")
  163. fmt.Println(classifyFirst, classifySecond, classifyThree, indexCode, indexName, frequency, unit, source)
  164. }
  165. }
  166. }
  167. }
  168. /*
  169. // PostEdbLib 调用指标接口
  170. func PostEdbLib(param map[string]interface{}, method string) (resp *models.BaseResponse, err error) {
  171. postUrl := utils.EDB_LIB_URL + method
  172. postData, err := json.Marshal(param)
  173. if err != nil {
  174. return
  175. }
  176. result, err := HttpPost(postUrl, string(postData), "application/json")
  177. if err != nil {
  178. return
  179. }
  180. err = json.Unmarshal(result, &resp)
  181. if err != nil {
  182. return
  183. }
  184. return resp, nil
  185. }
  186. */