base_edb_lib.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. }
  101. if urlStr == "" {
  102. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  103. return
  104. }
  105. resp, err = postRefreshEdbData(param, urlStr)
  106. return
  107. }
  108. // RefreshEdbCalculateData 刷新计算指标数据请求
  109. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  110. param := make(map[string]interface{})
  111. param["EdbCode"] = edbCode
  112. param["EdbInfoId"] = edbInfoId
  113. param["StartDate"] = startDate
  114. resp, err = postRefreshEdbData(param, "calculate/refresh")
  115. return
  116. }
  117. // ExecPythonCode 执行python代码
  118. func ExecPythonCode(pythonCode string) (resp *models.BaseResponse, err error) {
  119. param := make(map[string]interface{})
  120. param["PythonCode"] = pythonCode
  121. urlStr := `/python/exec`
  122. resp, err = postRefreshEdbData(param, urlStr)
  123. return
  124. }
  125. // AddPythonEdbData 新增python指标
  126. func AddPythonEdbData(param string) (resp *models.BaseResponse, err error) {
  127. urlStr := ``
  128. urlStr = "python/add"
  129. resp, err = postAddEdbData(param, urlStr)
  130. return
  131. }
  132. // EditPythonEdbData 编辑python指标
  133. func EditPythonEdbData(param string) (resp *models.BaseResponse, err error) {
  134. urlStr := ``
  135. urlStr = "python/edit"
  136. resp, err = postAddEdbData(param, urlStr)
  137. return
  138. }
  139. // postRefreshEdbData 刷新指标数据
  140. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  141. postUrl := utils.EDB_LIB_URL + urlStr
  142. postData, err := json.Marshal(param)
  143. if err != nil {
  144. return
  145. }
  146. result, err := HttpPost(postUrl, string(postData), "application/json")
  147. if err != nil {
  148. return
  149. }
  150. err = json.Unmarshal(result, &resp)
  151. if err != nil {
  152. return
  153. }
  154. return resp, nil
  155. }
  156. // postAddEdbData 新增指标数据
  157. func postAddEdbData(paramStr string, urlStr string) (resp *models.BaseResponse, err error) {
  158. postUrl := utils.EDB_LIB_URL + urlStr
  159. result, err := HttpPost(postUrl, paramStr, "application/json")
  160. if err != nil {
  161. return
  162. }
  163. err = json.Unmarshal(result, &resp)
  164. if err != nil {
  165. return
  166. }
  167. return resp, nil
  168. }
  169. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  170. body := ioutil.NopCloser(strings.NewReader(postData))
  171. client := &http.Client{}
  172. req, err := http.NewRequest("POST", url, body)
  173. if err != nil {
  174. return nil, err
  175. }
  176. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  177. if len(params) > 0 && params[0] != "" {
  178. contentType = params[0]
  179. }
  180. req.Header.Set("Content-Type", contentType)
  181. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  182. resp, err := client.Do(req)
  183. defer resp.Body.Close()
  184. b, err := ioutil.ReadAll(resp.Body)
  185. fmt.Println("HttpPost:" + string(b))
  186. return b, err
  187. }