base_edb_lib.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. urlStr = "ths/refresh"
  23. case utils.DATA_SOURCE_WIND:
  24. if subSource == 0 {
  25. urlStr = "wind/refresh"
  26. } else {
  27. urlStr = "wind/wsd/refresh"
  28. }
  29. case utils.DATA_SOURCE_PB:
  30. urlStr = "pb/refresh"
  31. case utils.DATA_SOURCE_PB_FINANCE:
  32. urlStr = "pb_finance/refresh"
  33. case utils.DATA_SOURCE_MANUAL:
  34. urlStr = "manual/refresh"
  35. case utils.DATA_SOURCE_LZ:
  36. urlStr = "lz/refresh"
  37. case utils.DATA_SOURCE_YS:
  38. urlStr = "smm/refresh"
  39. case utils.DATA_SOURCE_GL:
  40. urlStr = "mysteel/refresh"
  41. case utils.DATA_SOURCE_ZZ:
  42. urlStr = "zz/refresh"
  43. case utils.DATA_SOURCE_DL:
  44. urlStr = "dl/refresh"
  45. case utils.DATA_SOURCE_SH:
  46. urlStr = "sh/refresh"
  47. case utils.DATA_SOURCE_CFFEX:
  48. urlStr = "cffex/refresh"
  49. case utils.DATA_SOURCE_SHFE:
  50. urlStr = "shfe/refresh"
  51. case utils.DATA_SOURCE_GIE:
  52. urlStr = "gie/refresh"
  53. case utils.DATA_SOURCE_LT:
  54. urlStr = "lt/refresh"
  55. case utils.DATA_SOURCE_COAL:
  56. urlStr = "coal/refresh"
  57. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  58. urlStr = "google_travel/refresh"
  59. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  60. urlStr = "mysteel_chemical/refresh"
  61. case utils.DATA_SOURCE_EIA_STEO:
  62. urlStr = "eia_steo/refresh"
  63. case utils.DATA_SOURCE_PREDICT:
  64. urlStr = "predict/refresh"
  65. case utils.DATA_SOURCE_COM_TRADE:
  66. urlStr = "com_trade/refresh"
  67. case utils.DATA_SOURCE_SCI:
  68. urlStr = "sci/refresh"
  69. case utils.DATA_SOURCE_BAIINFO:
  70. urlStr = "baiinfo/refresh"
  71. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  72. urlStr = "national_statistics/refresh"
  73. default:
  74. urlStr = data_manage.EdbDataRefreshMethodMap[source] // 没有对应的从edb_source中取
  75. }
  76. if urlStr == "" {
  77. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  78. return
  79. }
  80. resp, err = postRefreshEdbData(param, urlStr)
  81. return
  82. }
  83. // RefreshEdbCalculateData 刷新计算指标数据请求
  84. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  85. param := make(map[string]interface{})
  86. param["EdbCode"] = edbCode
  87. param["EdbInfoId"] = edbInfoId
  88. param["StartDate"] = startDate
  89. resp, err = postRefreshEdbData(param, "calculate/refresh")
  90. return
  91. }
  92. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  93. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  94. param := make(map[string]interface{})
  95. param["EdbCode"] = edbCode
  96. param["EdbInfoId"] = edbInfoId
  97. param["StartDate"] = startDate
  98. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  99. return
  100. }
  101. // postRefreshEdbData 刷新指标数据
  102. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  103. postUrl := utils.EDB_LIB_URL + urlStr
  104. postData, err := json.Marshal(param)
  105. if err != nil {
  106. return
  107. }
  108. result, err := HttpPost(postUrl, string(postData), "application/json")
  109. if err != nil {
  110. return
  111. }
  112. err = json.Unmarshal(result, &resp)
  113. if err != nil {
  114. return
  115. }
  116. return resp, nil
  117. }
  118. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  119. body := ioutil.NopCloser(strings.NewReader(postData))
  120. client := &http.Client{}
  121. req, err := http.NewRequest("POST", url, body)
  122. if err != nil {
  123. return nil, err
  124. }
  125. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  126. if len(params) > 0 && params[0] != "" {
  127. contentType = params[0]
  128. }
  129. req.Header.Set("Content-Type", contentType)
  130. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  131. resp, err := client.Do(req)
  132. if err != nil {
  133. return nil, err
  134. }
  135. defer resp.Body.Close()
  136. b, err := ioutil.ReadAll(resp.Body)
  137. fmt.Println("HttpPost:" + string(b))
  138. return b, err
  139. }