base_edb_lib.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/models/data_manage"
  6. "eta/eta_chart_lib/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // 新增指标数据
  13. func AddEdbData(source int, edbCode string) (resp *models.BaseResponse, err error) {
  14. param := make(map[string]interface{})
  15. param["EdbCode"] = edbCode
  16. urlStr := ``
  17. switch source {
  18. case utils.DATA_SOURCE_THS:
  19. urlStr = "/ths/add"
  20. case utils.DATA_SOURCE_WIND:
  21. urlStr = "/wind/add"
  22. case utils.DATA_SOURCE_PB:
  23. urlStr = "/pb/add"
  24. case utils.DATA_SOURCE_PB_FINANCE:
  25. urlStr = "/pb_finance/add"
  26. case utils.DATA_SOURCE_MANUAL:
  27. urlStr = "/manual/add"
  28. case utils.DATA_SOURCE_LZ:
  29. urlStr = "/lz/add"
  30. case utils.DATA_SOURCE_YS:
  31. urlStr = "/smm/add"
  32. case utils.DATA_SOURCE_GL:
  33. urlStr = "/mysteel/add"
  34. case utils.DATA_SOURCE_ZZ:
  35. urlStr = "/zz/add"
  36. case utils.DATA_SOURCE_DL:
  37. urlStr = "/dl/add"
  38. case utils.DATA_SOURCE_SH:
  39. urlStr = "/sh/add"
  40. case utils.DATA_SOURCE_CFFEX:
  41. urlStr = "/cffex/add"
  42. case utils.DATA_SOURCE_SHFE:
  43. urlStr = "/shfe/add"
  44. case utils.DATA_SOURCE_GIE:
  45. urlStr = "/gie/add"
  46. case utils.DATA_SOURCE_LT:
  47. urlStr = "/lt/add"
  48. case utils.DATA_SOURCE_COAL:
  49. urlStr = "/coal/add"
  50. case utils.DATA_SOURCE_SCI:
  51. urlStr = "/sci/add"
  52. case utils.DATA_SOURCE_BAIINFO:
  53. urlStr = "/baiinfo/add"
  54. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  55. urlStr = "/national_statistics/add"
  56. default:
  57. }
  58. if urlStr == "" {
  59. err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
  60. return
  61. }
  62. resp, err = postRefreshEdbData(param, urlStr)
  63. return
  64. }
  65. // RefreshEdbData 刷新指标数据
  66. func RefreshEdbData(edbInfoId, source, subSource int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  67. param := make(map[string]interface{})
  68. param["EdbCode"] = edbCode
  69. param["Source"] = source
  70. param["EdbInfoId"] = edbInfoId
  71. param["StartDate"] = startDate
  72. urlStr := ``
  73. switch source {
  74. case utils.DATA_SOURCE_THS:
  75. switch subSource {
  76. case utils.DATA_SUB_SOURCE_DATE:
  77. urlStr = "ths/ds/refresh"
  78. case utils.DATA_SUB_SOURCE_HIGH_FREQUENCY:
  79. urlStr = "ths/hf/edb/refresh"
  80. default:
  81. urlStr = "ths/refresh"
  82. }
  83. case utils.DATA_SOURCE_WIND:
  84. if subSource == 0 {
  85. urlStr = "wind/refresh"
  86. } else {
  87. urlStr = "wind/wsd/refresh"
  88. }
  89. case utils.DATA_SOURCE_PB:
  90. urlStr = "/pb/refresh"
  91. case utils.DATA_SOURCE_PB_FINANCE:
  92. urlStr = "/pb_finance/refresh"
  93. case utils.DATA_SOURCE_MANUAL:
  94. urlStr = "/manual/refresh"
  95. case utils.DATA_SOURCE_LZ:
  96. urlStr = "/lz/refresh"
  97. case utils.DATA_SOURCE_YS:
  98. urlStr = "/smm/refresh"
  99. case utils.DATA_SOURCE_GL:
  100. urlStr = "/mysteel/refresh"
  101. case utils.DATA_SOURCE_ZZ:
  102. urlStr = "/zz/refresh"
  103. case utils.DATA_SOURCE_DL:
  104. urlStr = "/dl/refresh"
  105. case utils.DATA_SOURCE_SH:
  106. urlStr = "/sh/refresh"
  107. case utils.DATA_SOURCE_CFFEX:
  108. urlStr = "/cffex/refresh"
  109. case utils.DATA_SOURCE_SHFE:
  110. urlStr = "/shfe/refresh"
  111. case utils.DATA_SOURCE_GIE:
  112. urlStr = "/gie/refresh"
  113. case utils.DATA_SOURCE_LT:
  114. urlStr = "/lt/refresh"
  115. case utils.DATA_SOURCE_COAL:
  116. urlStr = "/coal/refresh"
  117. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  118. urlStr = "google_travel/refresh"
  119. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  120. urlStr = "mysteel_chemical/refresh"
  121. case utils.DATA_SOURCE_EIA_STEO:
  122. urlStr = "eia_steo/refresh"
  123. case utils.DATA_SOURCE_PREDICT:
  124. urlStr = "predict/refresh"
  125. case utils.DATA_SOURCE_COM_TRADE:
  126. urlStr = "com_trade/refresh"
  127. case utils.DATA_SOURCE_SCI:
  128. urlStr = "/sci/refresh"
  129. case utils.DATA_SOURCE_BAIINFO:
  130. urlStr = "/baiinfo/refresh"
  131. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  132. urlStr = "/national_statistics/refresh"
  133. case utils.DATA_SOURCE_FUBAO:
  134. urlStr = "fubao/refresh"
  135. case utils.DATA_SOURCE_GFEX:
  136. urlStr = "gz/refresh"
  137. case utils.DATA_SOURCE_ICPI:
  138. urlStr = "icpi/refresh"
  139. case utils.DATA_SOURCE_SCI99:
  140. urlStr = "sci99/refresh"
  141. default:
  142. edbSource := data_manage.EdbSourceIdMap[source]
  143. if edbSource != nil {
  144. urlStr = edbSource.EdbRefreshMethod
  145. }
  146. }
  147. if urlStr == "" {
  148. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  149. return
  150. }
  151. resp, err = postRefreshEdbData(param, urlStr)
  152. return
  153. }
  154. // RefreshEdbCalculateData 刷新计算指标数据请求
  155. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  156. param := make(map[string]interface{})
  157. param["EdbCode"] = edbCode
  158. param["EdbInfoId"] = edbInfoId
  159. param["StartDate"] = startDate
  160. resp, err = postRefreshEdbData(param, "/calculate/refresh")
  161. return
  162. }
  163. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  164. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  165. param := make(map[string]interface{})
  166. param["EdbCode"] = edbCode
  167. param["EdbInfoId"] = edbInfoId
  168. param["StartDate"] = startDate
  169. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  170. return
  171. }
  172. // BaseCalculateResp 拟合残差计算相关性的值返回
  173. type BaseCalculateResp struct {
  174. Ret int
  175. Msg string
  176. ErrMsg string
  177. ErrCode string
  178. Data BaseCalculateDataResp
  179. Success bool `description:"true 执行成功,false 执行失败"`
  180. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  181. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  182. }
  183. // BaseCalculateDataResp
  184. // @Description: 基础计算的返回结果
  185. type BaseCalculateDataResp struct {
  186. DataMap map[string]float64
  187. DateList []string
  188. }
  189. // BaseCalculate 基础计算
  190. func BaseCalculate(param, lang string) (resp *BaseCalculateResp, err error) {
  191. urlStr := "calculate/base"
  192. _, resultByte, err := postAddEdbData(param, urlStr, lang)
  193. err = json.Unmarshal(resultByte, &resp)
  194. if err != nil {
  195. return
  196. }
  197. return
  198. }
  199. // postRefreshEdbData 刷新指标数据
  200. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  201. postUrl := utils.EDB_LIB_URL + urlStr
  202. postData, err := json.Marshal(param)
  203. if err != nil {
  204. return
  205. }
  206. result, err := HttpPost(postUrl, string(postData), "application/json")
  207. if err != nil {
  208. return
  209. }
  210. err = json.Unmarshal(result, &resp)
  211. if err != nil {
  212. return
  213. }
  214. return resp, nil
  215. }
  216. // postAddEdbData 新增指标数据
  217. func postAddEdbData(paramStr string, urlStr, lang string) (resp *models.BaseResponse, result []byte, err error) {
  218. postUrl := utils.EDB_LIB_URL + urlStr
  219. result, err = HttpPost(postUrl, paramStr, lang, "application/json")
  220. if err != nil {
  221. return
  222. }
  223. err = json.Unmarshal(result, &resp)
  224. if err != nil {
  225. return
  226. }
  227. return
  228. }
  229. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  230. body := ioutil.NopCloser(strings.NewReader(postData))
  231. client := &http.Client{}
  232. req, err := http.NewRequest("POST", url, body)
  233. if err != nil {
  234. return nil, err
  235. }
  236. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  237. if len(params) > 0 && params[0] != "" {
  238. contentType = params[0]
  239. }
  240. req.Header.Set("Content-Type", contentType)
  241. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  242. resp, err := client.Do(req)
  243. defer resp.Body.Close()
  244. b, err := ioutil.ReadAll(resp.Body)
  245. fmt.Println("HttpPost:" + string(b))
  246. return b, err
  247. }