base_edb_lib.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // postRefreshEdbData 刷新指标数据
  110. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  111. postUrl := utils.EDB_LIB_URL + urlStr
  112. postData, err := json.Marshal(param)
  113. if err != nil {
  114. return
  115. }
  116. result, err := HttpPost(postUrl, string(postData), "application/json")
  117. if err != nil {
  118. return
  119. }
  120. err = json.Unmarshal(result, &resp)
  121. if err != nil {
  122. return
  123. }
  124. return resp, nil
  125. }
  126. // SetEdbSourceStat 指标终端更新情况定时汇总
  127. func SetEdbSourceStat() (resp *models.BaseResponse, err error) {
  128. postUrl := utils.EDB_LIB_URL + "edb_stat/source_update"
  129. result, err := HttpPost(postUrl, "", "application/json")
  130. if err != nil {
  131. return
  132. }
  133. err = json.Unmarshal(result, &resp)
  134. if err != nil {
  135. return
  136. }
  137. return resp, nil
  138. }
  139. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  140. body := ioutil.NopCloser(strings.NewReader(postData))
  141. client := &http.Client{}
  142. req, err := http.NewRequest("POST", url, body)
  143. if err != nil {
  144. return nil, err
  145. }
  146. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  147. if len(params) > 0 && params[0] != "" {
  148. contentType = params[0]
  149. }
  150. req.Header.Set("Content-Type", contentType)
  151. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  152. resp, err := client.Do(req)
  153. if err != nil {
  154. return nil, err
  155. }
  156. defer resp.Body.Close()
  157. b, err := ioutil.ReadAll(resp.Body)
  158. fmt.Println("HttpPost:" + string(b))
  159. return b, err
  160. }