base_edb_lib.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_task/models"
  5. "eta_gn/eta_task/models/data_manage"
  6. "eta_gn/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.GetEdbSourceRefreshMethodBySourceId(source) // 没有对应的从edb_source中取
  83. //}
  84. urlStr = data_manage.GetEdbSourceRefreshMethodBySourceId(source) // 没有对应的从edb_source中取
  85. if urlStr == "" {
  86. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  87. return
  88. }
  89. resp, err = postRefreshEdbData(param, urlStr)
  90. return
  91. }
  92. // RefreshEdbCalculateData 刷新计算指标数据请求
  93. func RefreshEdbCalculateData(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, "calculate/refresh")
  99. return
  100. }
  101. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  102. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  103. param := make(map[string]interface{})
  104. param["EdbCode"] = edbCode
  105. param["EdbInfoId"] = edbInfoId
  106. param["StartDate"] = startDate
  107. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  108. return
  109. }
  110. // postRefreshEdbData 刷新指标数据
  111. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  112. postUrl := utils.EDB_LIB_URL + urlStr
  113. postData, err := json.Marshal(param)
  114. if err != nil {
  115. return
  116. }
  117. result, err := HttpPost(postUrl, string(postData), "application/json")
  118. if err != nil {
  119. return
  120. }
  121. err = json.Unmarshal(result, &resp)
  122. if err != nil {
  123. return
  124. }
  125. return resp, nil
  126. }
  127. // SetEdbSourceStat 指标终端更新情况定时汇总
  128. func SetEdbSourceStat() (resp *models.BaseResponse, err error) {
  129. postUrl := utils.EDB_LIB_URL + "edb_stat/source_update"
  130. result, err := HttpPost(postUrl, "", "application/json")
  131. if err != nil {
  132. return
  133. }
  134. err = json.Unmarshal(result, &resp)
  135. if err != nil {
  136. return
  137. }
  138. return resp, nil
  139. }
  140. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  141. body := ioutil.NopCloser(strings.NewReader(postData))
  142. client := &http.Client{}
  143. req, err := http.NewRequest("POST", url, body)
  144. if err != nil {
  145. return nil, err
  146. }
  147. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  148. if len(params) > 0 && params[0] != "" {
  149. contentType = params[0]
  150. }
  151. req.Header.Set("Content-Type", contentType)
  152. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  153. resp, err := client.Do(req)
  154. if err != nil {
  155. return nil, err
  156. }
  157. defer resp.Body.Close()
  158. b, err := ioutil.ReadAll(resp.Body)
  159. //fmt.Println("HttpPost:" + string(b))
  160. return b, err
  161. }