base_edb_lib.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_task/models"
  5. "eta/eta_task/models/data_manage"
  6. "eta/eta_task/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // RefreshEdbData 刷新指标数据
  13. func RefreshEdbData(edbInfoId, source, subSource int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  14. param := make(map[string]interface{})
  15. param["EdbCode"] = edbCode
  16. param["EdbInfoId"] = edbInfoId
  17. param["StartDate"] = startDate
  18. param["Source"] = source
  19. urlStr := ``
  20. switch source {
  21. case utils.DATA_SOURCE_THS:
  22. if subSource == 0 {
  23. urlStr = "ths/refresh"
  24. } else {
  25. urlStr = "ths/ds/refresh"
  26. }
  27. case utils.DATA_SOURCE_WIND:
  28. if subSource == 0 {
  29. urlStr = "wind/refresh"
  30. } else {
  31. urlStr = "wind/wsd/refresh"
  32. }
  33. case utils.DATA_SOURCE_PB:
  34. urlStr = "pb/refresh"
  35. case utils.DATA_SOURCE_PB_FINANCE:
  36. urlStr = "pb_finance/refresh"
  37. case utils.DATA_SOURCE_MANUAL:
  38. urlStr = "manual/refresh"
  39. case utils.DATA_SOURCE_LZ:
  40. urlStr = "lz/refresh"
  41. case utils.DATA_SOURCE_YS:
  42. urlStr = "smm/refresh"
  43. case utils.DATA_SOURCE_GL:
  44. urlStr = "mysteel/refresh"
  45. case utils.DATA_SOURCE_ZZ:
  46. urlStr = "zz/refresh"
  47. case utils.DATA_SOURCE_DL:
  48. urlStr = "dl/refresh"
  49. case utils.DATA_SOURCE_SH:
  50. urlStr = "sh/refresh"
  51. case utils.DATA_SOURCE_CFFEX:
  52. urlStr = "cffex/refresh"
  53. case utils.DATA_SOURCE_SHFE:
  54. urlStr = "shfe/refresh"
  55. case utils.DATA_SOURCE_GIE:
  56. urlStr = "gie/refresh"
  57. case utils.DATA_SOURCE_LT:
  58. urlStr = "lt/refresh"
  59. case utils.DATA_SOURCE_COAL:
  60. urlStr = "coal/refresh"
  61. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  62. urlStr = "google_travel/refresh"
  63. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  64. urlStr = "mysteel_chemical/refresh"
  65. case utils.DATA_SOURCE_EIA_STEO:
  66. urlStr = "eia_steo/refresh"
  67. case utils.DATA_SOURCE_PREDICT:
  68. urlStr = "predict/refresh"
  69. case utils.DATA_SOURCE_COM_TRADE:
  70. urlStr = "com_trade/refresh"
  71. case utils.DATA_SOURCE_SCI:
  72. urlStr = "sci/refresh"
  73. case utils.DATA_SOURCE_BAIINFO:
  74. urlStr = "baiinfo/refresh"
  75. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  76. urlStr = "national_statistics/refresh"
  77. case utils.DATA_SOURCE_FUBAO:
  78. urlStr = "fubao/refresh"
  79. case utils.DATA_SOURCE_GFEX:
  80. urlStr = "gz/refresh"
  81. default:
  82. urlStr = data_manage.EdbDataRefreshMethodMap[source] // 没有对应的从edb_source中取
  83. }
  84. if urlStr == "" {
  85. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  86. return
  87. }
  88. resp, err = postRefreshEdbData(param, urlStr)
  89. return
  90. }
  91. // RefreshEdbCalculateData 刷新计算指标数据请求
  92. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  93. param := make(map[string]interface{})
  94. param["EdbCode"] = edbCode
  95. param["EdbInfoId"] = edbInfoId
  96. param["StartDate"] = startDate
  97. resp, err = postRefreshEdbData(param, "calculate/refresh")
  98. return
  99. }
  100. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  101. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  102. param := make(map[string]interface{})
  103. param["EdbCode"] = edbCode
  104. param["EdbInfoId"] = edbInfoId
  105. param["StartDate"] = startDate
  106. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  107. return
  108. }
  109. var step int //计数器
  110. // postRefreshEdbData 刷新指标数据
  111. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  112. var postUrl string
  113. edbLibUrlListLen := len(utils.EDB_LIB_URL_LIST)
  114. if edbLibUrlListLen <= 0 {
  115. postUrl = utils.EDB_LIB_URL
  116. } else {
  117. rn := step % edbLibUrlListLen
  118. postUrl = utils.EDB_LIB_URL_LIST[rn].Url
  119. }
  120. postUrl = postUrl + urlStr
  121. step++
  122. postData, err := json.Marshal(param)
  123. if err != nil {
  124. return
  125. }
  126. result, err := HttpPost(postUrl, string(postData), "application/json")
  127. if err != nil {
  128. return
  129. }
  130. err = json.Unmarshal(result, &resp)
  131. if err != nil {
  132. return
  133. }
  134. return resp, nil
  135. }
  136. // SetEdbSourceStat 指标终端更新情况定时汇总
  137. func SetEdbSourceStat() (resp *models.BaseResponse, err error) {
  138. postUrl := utils.EDB_LIB_URL + "edb_stat/source_update"
  139. result, err := HttpPost(postUrl, "", "application/json")
  140. if err != nil {
  141. return
  142. }
  143. err = json.Unmarshal(result, &resp)
  144. if err != nil {
  145. return
  146. }
  147. return resp, nil
  148. }
  149. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  150. body := ioutil.NopCloser(strings.NewReader(postData))
  151. client := &http.Client{}
  152. req, err := http.NewRequest("POST", url, body)
  153. if err != nil {
  154. return nil, err
  155. }
  156. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  157. if len(params) > 0 && params[0] != "" {
  158. contentType = params[0]
  159. }
  160. req.Header.Set("Content-Type", contentType)
  161. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  162. resp, err := client.Do(req)
  163. if err != nil {
  164. return nil, err
  165. }
  166. defer resp.Body.Close()
  167. b, err := ioutil.ReadAll(resp.Body)
  168. fmt.Println("HttpPost:" + string(b))
  169. return b, err
  170. }