edb_data.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package chart
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_yb/global"
  6. "hongze/hongze_yb/models/tables/edb_source"
  7. "hongze/hongze_yb/utils"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. type RefreshResponse struct {
  13. Ret int
  14. Msg string
  15. ErrMsg string
  16. ErrCode string
  17. Data interface{}
  18. Success bool `description:"true 执行成功,false 执行失败"`
  19. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  20. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  21. }
  22. // RefreshEdbData 刷新指标数据
  23. func RefreshEdbData(edbInfoId, source, subSource int, edbCode, startDate string) (resp *RefreshResponse, err error) {
  24. param := make(map[string]interface{})
  25. param["EdbCode"] = edbCode
  26. param["EdbInfoId"] = edbInfoId
  27. param["StartDate"] = startDate
  28. urlStr := ``
  29. switch source {
  30. case utils.DATA_SOURCE_THS:
  31. if subSource == 0 {
  32. urlStr = "ths/refresh"
  33. } else {
  34. urlStr = "ths/ds/refresh"
  35. }
  36. case utils.DATA_SOURCE_WIND:
  37. if subSource == 0 {
  38. urlStr = "wind/refresh"
  39. } else {
  40. urlStr = "wind/wsd/refresh"
  41. }
  42. case utils.DATA_SOURCE_PB:
  43. urlStr = "/pb/refresh"
  44. case utils.DATA_SOURCE_PB_FINANCE:
  45. urlStr = "/pb_finance/refresh"
  46. case utils.DATA_SOURCE_MANUAL:
  47. urlStr = "/manual/refresh"
  48. case utils.DATA_SOURCE_LZ:
  49. urlStr = "/lz/refresh"
  50. case utils.DATA_SOURCE_YS:
  51. urlStr = "/smm/refresh"
  52. case utils.DATA_SOURCE_GL:
  53. urlStr = "/mysteel/refresh"
  54. case utils.DATA_SOURCE_ZZ:
  55. urlStr = "/zz/refresh"
  56. case utils.DATA_SOURCE_DL:
  57. urlStr = "/dl/refresh"
  58. case utils.DATA_SOURCE_SH:
  59. urlStr = "/sh/refresh"
  60. case utils.DATA_SOURCE_CFFEX:
  61. urlStr = "/cffex/refresh"
  62. case utils.DATA_SOURCE_SHFE:
  63. urlStr = "/shfe/refresh"
  64. case utils.DATA_SOURCE_GIE:
  65. urlStr = "/gie/refresh"
  66. case utils.DATA_SOURCE_LT:
  67. urlStr = "/lt/refresh"
  68. case utils.DATA_SOURCE_COAL:
  69. urlStr = "/coal/refresh"
  70. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  71. urlStr = "google_travel/refresh"
  72. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  73. urlStr = "mysteel_chemical/refresh"
  74. case utils.DATA_SOURCE_EIA_STEO:
  75. urlStr = "eia_steo/refresh"
  76. case utils.DATA_SOURCE_PREDICT:
  77. urlStr = "predict/refresh"
  78. case utils.DATA_SOURCE_COM_TRADE:
  79. urlStr = "com_trade/refresh"
  80. case utils.DATA_SOURCE_SCI:
  81. urlStr = "/sci/refresh"
  82. case utils.DATA_SOURCE_BAIINFO:
  83. urlStr = "/baiinfo/refresh"
  84. case utils.DATA_SOURCE_STOCK_PLANT:
  85. urlStr = "stock_plant/refresh"
  86. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  87. urlStr = "national_statistics/refresh"
  88. default:
  89. edbSource, _ := edb_source.GetEdbSourceBySource(source)
  90. if edbSource != nil {
  91. urlStr = edbSource.EdbRefreshMethod
  92. }
  93. }
  94. if urlStr == "" {
  95. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  96. return
  97. }
  98. resp, err = postRefreshEdbData(param, urlStr)
  99. return
  100. }
  101. // postRefreshEdbData 请求刷新指标数据
  102. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *RefreshResponse, err error) {
  103. edbLibUrl := ""
  104. if global.CONFIG.Serve.RunMode == "release" {
  105. edbLibUrl = "http://172.19.173.232:8300/edbapi/"
  106. } else {
  107. edbLibUrl = "http://8.136.199.33:8300/edbapi/"
  108. }
  109. postUrl := edbLibUrl + urlStr
  110. postData, err := json.Marshal(param)
  111. if err != nil {
  112. return
  113. }
  114. result, err := EdbLibHttpPost(postUrl, string(postData), "application/json")
  115. if err != nil {
  116. return
  117. }
  118. err = json.Unmarshal(result, &resp)
  119. if err != nil {
  120. return
  121. }
  122. return resp, nil
  123. }
  124. // EdbLibHttpPost post请求edb库
  125. func EdbLibHttpPost(url, postData string, params ...string) ([]byte, error) {
  126. body := ioutil.NopCloser(strings.NewReader(postData))
  127. client := &http.Client{}
  128. req, err := http.NewRequest("POST", url, body)
  129. if err != nil {
  130. return nil, err
  131. }
  132. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  133. if len(params) > 0 && params[0] != "" {
  134. contentType = params[0]
  135. }
  136. req.Header.Set("Content-Type", contentType)
  137. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  138. resp, err := client.Do(req)
  139. defer resp.Body.Close()
  140. b, err := ioutil.ReadAll(resp.Body)
  141. fmt.Println("HttpPost:" + string(b))
  142. return b, err
  143. }
  144. // RefreshEdbCalculateData 刷新计算指标数据请求
  145. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *RefreshResponse, err error) {
  146. param := make(map[string]interface{})
  147. param["EdbCode"] = edbCode
  148. param["EdbInfoId"] = edbInfoId
  149. param["StartDate"] = startDate
  150. resp, err = postRefreshEdbData(param, "/calculate/refresh")
  151. return
  152. }
  153. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  154. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *RefreshResponse, err error) {
  155. param := make(map[string]interface{})
  156. param["EdbCode"] = edbCode
  157. param["EdbInfoId"] = edbInfoId
  158. param["StartDate"] = startDate
  159. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  160. return
  161. }