base_edb_lib.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package data
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_task/models"
  6. "hongze/hongze_task/utils"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. // 新增指标数据
  12. func AddEdbData(source int, edbCode string) (resp *models.BaseResponse, err error) {
  13. param := make(map[string]interface{})
  14. param["EdbCode"] = edbCode
  15. urlStr := ``
  16. switch source {
  17. case utils.DATA_SOURCE_THS:
  18. urlStr = "ths/add"
  19. case utils.DATA_SOURCE_WIND:
  20. urlStr = "wind/add"
  21. case utils.DATA_SOURCE_PB:
  22. urlStr = "pb/add"
  23. case utils.DATA_SOURCE_PB_FINANCE:
  24. urlStr = "pb_finance/add"
  25. case utils.DATA_SOURCE_MANUAL:
  26. urlStr = "manual/add"
  27. case utils.DATA_SOURCE_LZ:
  28. urlStr = "lz/add"
  29. case utils.DATA_SOURCE_YS:
  30. urlStr = "smm/add"
  31. case utils.DATA_SOURCE_GL:
  32. urlStr = "mysteel/add"
  33. case utils.DATA_SOURCE_ZZ:
  34. urlStr = "zz/add"
  35. case utils.DATA_SOURCE_DL:
  36. urlStr = "dl/add"
  37. case utils.DATA_SOURCE_SH:
  38. urlStr = "sh/add"
  39. case utils.DATA_SOURCE_CFFEX:
  40. urlStr = "cffex/add"
  41. case utils.DATA_SOURCE_SHFE:
  42. urlStr = "shfe/add"
  43. case utils.DATA_SOURCE_GIE:
  44. urlStr = "gie/add"
  45. case utils.DATA_SOURCE_LT:
  46. urlStr = "lt/add"
  47. case utils.DATA_SOURCE_COAL:
  48. urlStr = "coal/add"
  49. default:
  50. }
  51. if urlStr == "" {
  52. err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
  53. return
  54. }
  55. resp, err = postRefreshEdbData(param, urlStr)
  56. return
  57. }
  58. // RefreshEdbData 刷新指标数据
  59. func RefreshEdbData(edbInfoId, source int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  60. param := make(map[string]interface{})
  61. param["EdbCode"] = edbCode
  62. param["EdbInfoId"] = edbInfoId
  63. param["StartDate"] = startDate
  64. urlStr := ``
  65. switch source {
  66. case utils.DATA_SOURCE_THS:
  67. urlStr = "ths/refresh"
  68. case utils.DATA_SOURCE_WIND:
  69. urlStr = "wind/refresh"
  70. case utils.DATA_SOURCE_PB:
  71. urlStr = "pb/refresh"
  72. case utils.DATA_SOURCE_PB_FINANCE:
  73. urlStr = "pb_finance/refresh"
  74. case utils.DATA_SOURCE_MANUAL:
  75. urlStr = "manual/refresh"
  76. case utils.DATA_SOURCE_LZ:
  77. urlStr = "lz/refresh"
  78. case utils.DATA_SOURCE_YS:
  79. urlStr = "smm/refresh"
  80. case utils.DATA_SOURCE_GL:
  81. urlStr = "mysteel/refresh"
  82. case utils.DATA_SOURCE_ZZ:
  83. urlStr = "zz/refresh"
  84. case utils.DATA_SOURCE_DL:
  85. urlStr = "dl/refresh"
  86. case utils.DATA_SOURCE_SH:
  87. urlStr = "sh/refresh"
  88. case utils.DATA_SOURCE_CFFEX:
  89. urlStr = "cffex/refresh"
  90. case utils.DATA_SOURCE_SHFE:
  91. urlStr = "shfe/refresh"
  92. case utils.DATA_SOURCE_GIE:
  93. urlStr = "gie/refresh"
  94. case utils.DATA_SOURCE_LT:
  95. urlStr = "lt/refresh"
  96. case utils.DATA_SOURCE_COAL:
  97. urlStr = "coal/refresh"
  98. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  99. urlStr = "google_travel/refresh"
  100. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  101. urlStr = "mysteel_chemical/refresh"
  102. case utils.DATA_SOURCE_EIA_STEO:
  103. urlStr = "eia_steo/refresh"
  104. case utils.DATA_SOURCE_PREDICT:
  105. urlStr = "predict/refresh"
  106. case utils.DATA_SOURCE_COM_TRADE:
  107. urlStr = "com_trade/refresh"
  108. case utils.DATA_SOURCE_SCI:
  109. urlStr = "sci/refresh"
  110. case utils.DATA_SOURCE_BAIINFO:
  111. urlStr = "baiinfo/refresh"
  112. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  113. urlStr = "national_statistics/refresh"
  114. }
  115. if urlStr == "" {
  116. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  117. return
  118. }
  119. resp, err = postRefreshEdbData(param, urlStr)
  120. return
  121. }
  122. // RefreshEdbCalculateData 刷新计算指标数据请求
  123. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  124. param := make(map[string]interface{})
  125. param["EdbCode"] = edbCode
  126. param["EdbInfoId"] = edbInfoId
  127. param["StartDate"] = startDate
  128. resp, err = postRefreshEdbData(param, "calculate/refresh")
  129. return
  130. }
  131. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  132. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  133. param := make(map[string]interface{})
  134. param["EdbCode"] = edbCode
  135. param["EdbInfoId"] = edbInfoId
  136. param["StartDate"] = startDate
  137. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  138. return
  139. }
  140. // ExecPythonCode 执行python代码
  141. func ExecPythonCode(pythonCode string) (resp *models.BaseResponse, err error) {
  142. param := make(map[string]interface{})
  143. param["PythonCode"] = pythonCode
  144. urlStr := `/python/exec`
  145. resp, err = postRefreshEdbData(param, urlStr)
  146. return
  147. }
  148. // AddPythonEdbData 新增python指标
  149. func AddPythonEdbData(param string) (resp *models.BaseResponse, err error) {
  150. urlStr := ``
  151. urlStr = "python/add"
  152. resp, err = postAddEdbData(param, urlStr)
  153. return
  154. }
  155. // EditPythonEdbData 编辑python指标
  156. func EditPythonEdbData(param string) (resp *models.BaseResponse, err error) {
  157. urlStr := ``
  158. urlStr = "python/edit"
  159. resp, err = postAddEdbData(param, urlStr)
  160. return
  161. }
  162. var count int64
  163. // postRefreshEdbData 刷新指标数据
  164. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  165. count++
  166. if count%2 == 0 {
  167. utils.EDB_LIB_URL = "http://127.0.0.1:8300/edbapi/"
  168. }
  169. postUrl := utils.EDB_LIB_URL + urlStr
  170. postData, err := json.Marshal(param)
  171. if err != nil {
  172. return
  173. }
  174. result, err := HttpPost(postUrl, string(postData), "application/json")
  175. if err != nil {
  176. return
  177. }
  178. err = json.Unmarshal(result, &resp)
  179. if err != nil {
  180. return
  181. }
  182. return resp, nil
  183. }
  184. // postAddEdbData 新增指标数据
  185. func postAddEdbData(paramStr string, urlStr string) (resp *models.BaseResponse, err error) {
  186. postUrl := utils.EDB_LIB_URL + urlStr
  187. result, err := HttpPost(postUrl, paramStr, "application/json")
  188. if err != nil {
  189. return
  190. }
  191. err = json.Unmarshal(result, &resp)
  192. if err != nil {
  193. return
  194. }
  195. return resp, nil
  196. }
  197. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  198. body := ioutil.NopCloser(strings.NewReader(postData))
  199. client := &http.Client{}
  200. req, err := http.NewRequest("POST", url, body)
  201. if err != nil {
  202. return nil, err
  203. }
  204. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  205. if len(params) > 0 && params[0] != "" {
  206. contentType = params[0]
  207. }
  208. req.Header.Set("Content-Type", contentType)
  209. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  210. resp, err := client.Do(req)
  211. defer resp.Body.Close()
  212. b, err := ioutil.ReadAll(resp.Body)
  213. fmt.Println("HttpPost:" + string(b))
  214. return b, err
  215. }