base_edb_lib.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package data
  2. import (
  3. "encoding/json"
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/data_manage"
  6. "eta/eta_mobile/utils"
  7. "fmt"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // AddEdbData 新增指标数据
  13. func AddEdbData(source int, edbCode string) (resp *models.BaseResponse, err error) {
  14. param := make(map[string]interface{})
  15. param["EdbCode"] = edbCode
  16. param["Source"] = source
  17. urlStr := ``
  18. switch source {
  19. case utils.DATA_SOURCE_THS:
  20. urlStr = "ths/add"
  21. case utils.DATA_SOURCE_WIND:
  22. urlStr = "wind/add"
  23. case utils.DATA_SOURCE_PB:
  24. urlStr = "pb/add"
  25. case utils.DATA_SOURCE_PB_FINANCE:
  26. urlStr = "pb_finance/add"
  27. case utils.DATA_SOURCE_MANUAL:
  28. urlStr = "manual/add"
  29. case utils.DATA_SOURCE_LZ:
  30. urlStr = "lz/add"
  31. case utils.DATA_SOURCE_YS:
  32. urlStr = "smm/add"
  33. case utils.DATA_SOURCE_GL:
  34. urlStr = "mysteel/add"
  35. case utils.DATA_SOURCE_ZZ:
  36. urlStr = "zz/add"
  37. case utils.DATA_SOURCE_DL:
  38. urlStr = "dl/add"
  39. case utils.DATA_SOURCE_SH:
  40. urlStr = "sh/add"
  41. case utils.DATA_SOURCE_CFFEX:
  42. urlStr = "cffex/add"
  43. case utils.DATA_SOURCE_SHFE:
  44. urlStr = "shfe/add"
  45. case utils.DATA_SOURCE_GIE:
  46. urlStr = "gie/add"
  47. case utils.DATA_SOURCE_LT:
  48. urlStr = "lt/add"
  49. case utils.DATA_SOURCE_COAL:
  50. urlStr = "coal/add"
  51. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  52. urlStr = "google_travel/add"
  53. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  54. urlStr = "mysteel_chemical/add"
  55. case utils.DATA_SOURCE_EIA_STEO:
  56. urlStr = "eia_steo/add"
  57. case utils.DATA_SOURCE_COM_TRADE:
  58. urlStr = "com_trade/add"
  59. case utils.DATA_SOURCE_SCI:
  60. urlStr = "sci/add"
  61. case utils.DATA_SOURCE_BAIINFO:
  62. urlStr = "baiinfo/add"
  63. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  64. urlStr = "national_statistics/add"
  65. case utils.DATA_SOURCE_FUBAO:
  66. urlStr = "fubao/add"
  67. case utils.DATA_SOURCE_GFEX:
  68. urlStr = "gz/add"
  69. default:
  70. edbSource := data_manage.EdbSourceIdMap[source]
  71. if edbSource != nil {
  72. urlStr = edbSource.EdbAddMethod
  73. }
  74. }
  75. if urlStr == "" {
  76. err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
  77. return
  78. }
  79. resp, err = postRefreshEdbData(param, urlStr)
  80. return
  81. }
  82. type AddPredictEdbDataResponse struct {
  83. Ret int
  84. Msg string
  85. ErrMsg string
  86. ErrCode string
  87. Data data_manage.AddEdbInfoResp
  88. Success bool `description:"true 执行成功,false 执行失败"`
  89. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  90. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  91. }
  92. // RefreshEdbData 刷新指标数据
  93. func RefreshEdbData(edbInfoId, source 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. param["Source"] = source
  99. urlStr := ``
  100. switch source {
  101. case utils.DATA_SOURCE_THS:
  102. urlStr = "ths/refresh"
  103. case utils.DATA_SOURCE_WIND:
  104. urlStr = "wind/refresh"
  105. case utils.DATA_SOURCE_PB:
  106. urlStr = "pb/refresh"
  107. case utils.DATA_SOURCE_PB_FINANCE:
  108. urlStr = "pb_finance/refresh"
  109. case utils.DATA_SOURCE_MANUAL:
  110. urlStr = "manual/refresh"
  111. case utils.DATA_SOURCE_LZ:
  112. urlStr = "lz/refresh"
  113. case utils.DATA_SOURCE_YS:
  114. urlStr = "smm/refresh"
  115. case utils.DATA_SOURCE_GL:
  116. urlStr = "mysteel/refresh"
  117. case utils.DATA_SOURCE_ZZ:
  118. urlStr = "zz/refresh"
  119. case utils.DATA_SOURCE_DL:
  120. urlStr = "dl/refresh"
  121. case utils.DATA_SOURCE_SH:
  122. urlStr = "sh/refresh"
  123. case utils.DATA_SOURCE_CFFEX:
  124. urlStr = "cffex/refresh"
  125. case utils.DATA_SOURCE_SHFE:
  126. urlStr = "shfe/refresh"
  127. case utils.DATA_SOURCE_GIE:
  128. urlStr = "gie/refresh"
  129. case utils.DATA_SOURCE_LT:
  130. urlStr = "lt/refresh"
  131. case utils.DATA_SOURCE_COAL:
  132. urlStr = "coal/refresh"
  133. case utils.DATA_SOURCE_GOOGLE_TRAVEL:
  134. urlStr = "google_travel/refresh"
  135. case utils.DATA_SOURCE_MYSTEEL_CHEMICAL:
  136. urlStr = "mysteel_chemical/refresh"
  137. case utils.DATA_SOURCE_EIA_STEO:
  138. urlStr = "eia_steo/refresh"
  139. case utils.DATA_SOURCE_PREDICT:
  140. urlStr = "predict/refresh"
  141. case utils.DATA_SOURCE_COM_TRADE:
  142. urlStr = "com_trade/refresh"
  143. case utils.DATA_SOURCE_SCI:
  144. urlStr = "sci/refresh"
  145. case utils.DATA_SOURCE_BAIINFO:
  146. urlStr = "baiinfo/refresh"
  147. case utils.DATA_SOURCE_STOCK_PLANT:
  148. urlStr = "stock_plant/refresh"
  149. case utils.DATA_SOURCE_NATIONAL_STATISTICS:
  150. urlStr = "national_statistics/refresh"
  151. case utils.DATA_SOURCE_FUBAO:
  152. urlStr = "fubao/refresh"
  153. case utils.DATA_SOURCE_GFEX:
  154. urlStr = "gz/refresh"
  155. default:
  156. edbSource := data_manage.EdbSourceIdMap[source]
  157. if edbSource != nil {
  158. urlStr = edbSource.EdbRefreshMethod
  159. }
  160. }
  161. if urlStr == "" {
  162. err = fmt.Errorf(fmt.Sprint("source:", source, ";未实现该指标的刷新接口,请联系管理员"))
  163. return
  164. }
  165. resp, err = postRefreshEdbData(param, urlStr)
  166. return
  167. }
  168. // RefreshEdbCalculateData 刷新普通计算指标数据请求
  169. func RefreshEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  170. param := make(map[string]interface{})
  171. param["EdbCode"] = edbCode
  172. param["EdbInfoId"] = edbInfoId
  173. param["StartDate"] = startDate
  174. resp, err = postRefreshEdbData(param, "calculate/refresh")
  175. return
  176. }
  177. // RefreshPredictEdbCalculateData 刷新 预测计算指标 数据请求
  178. func RefreshPredictEdbCalculateData(edbInfoId int, edbCode, startDate string) (resp *models.BaseResponse, err error) {
  179. param := make(map[string]interface{})
  180. param["EdbCode"] = edbCode
  181. param["EdbInfoId"] = edbInfoId
  182. param["StartDate"] = startDate
  183. resp, err = postRefreshEdbData(param, "predict_calculate/refresh")
  184. return
  185. }
  186. // AddEdbCalculateData 新增 累计值转月-同比值-同差等计算新增
  187. func AddEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  188. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/add")
  189. err = json.Unmarshal(resultByte, &resp)
  190. if err != nil {
  191. return
  192. }
  193. return
  194. }
  195. // EditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增
  196. func EditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  197. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/edit")
  198. err = json.Unmarshal(resultByte, &resp)
  199. if err != nil {
  200. return
  201. }
  202. return
  203. }
  204. // BatchSaveEdbCalculateData 新增 累计值转月-同比值-同差等计算新增
  205. func BatchSaveEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  206. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/save")
  207. err = json.Unmarshal(resultByte, &resp)
  208. if err != nil {
  209. return
  210. }
  211. return
  212. }
  213. // BatchEditEdbCalculateData 编辑 累计值转月-同比值-同差等计算新增
  214. func BatchEditEdbCalculateData(edbInfoCalculateBatchSaveReqStr string) (resp *AddPredictEdbDataResponse, err error) {
  215. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/batch/edit")
  216. err = json.Unmarshal(resultByte, &resp)
  217. if err != nil {
  218. return
  219. }
  220. return
  221. }
  222. // CalculateComputeCorrelationResp 拟合残差计算相关性的值返回
  223. type CalculateComputeCorrelationResp struct {
  224. Ret int
  225. Msg string
  226. ErrMsg string
  227. ErrCode string
  228. Data float64
  229. Success bool `description:"true 执行成功,false 执行失败"`
  230. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  231. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  232. }
  233. // CalculateComputeCorrelation 拟合残差计算相关性的值
  234. func CalculateComputeCorrelation(edbInfoCalculateBatchSaveReqStr string) (resp *CalculateComputeCorrelationResp, err error) {
  235. _, resultByte, err := postAddEdbData(edbInfoCalculateBatchSaveReqStr, "calculate/compute_correlation")
  236. err = json.Unmarshal(resultByte, &resp)
  237. if err != nil {
  238. return
  239. }
  240. return
  241. }
  242. // postRefreshEdbData 刷新指标数据
  243. func postRefreshEdbData(param map[string]interface{}, urlStr string) (resp *models.BaseResponse, err error) {
  244. postUrl := utils.EDB_LIB_URL + urlStr
  245. postData, err := json.Marshal(param)
  246. if err != nil {
  247. return
  248. }
  249. result, err := HttpPost(postUrl, string(postData), "application/json")
  250. if err != nil {
  251. return
  252. }
  253. utils.FileLog.Info("postRefreshEdbData:" + postUrl + ";" + string(postData) + ";result:" + string(result))
  254. err = json.Unmarshal(result, &resp)
  255. if err != nil {
  256. return
  257. }
  258. return resp, nil
  259. }
  260. // postAddEdbData 新增指标数据
  261. func postAddEdbData(paramStr string, urlStr string) (resp *models.BaseResponse, result []byte, err error) {
  262. postUrl := utils.EDB_LIB_URL + urlStr
  263. result, err = HttpPost(postUrl, paramStr, "application/json")
  264. if err != nil {
  265. return
  266. }
  267. err = json.Unmarshal(result, &resp)
  268. if err != nil {
  269. return
  270. }
  271. return
  272. }
  273. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  274. body := ioutil.NopCloser(strings.NewReader(postData))
  275. client := &http.Client{}
  276. req, err := http.NewRequest("POST", url, body)
  277. if err != nil {
  278. return nil, err
  279. }
  280. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  281. if len(params) > 0 && params[0] != "" {
  282. contentType = params[0]
  283. }
  284. req.Header.Set("Content-Type", contentType)
  285. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  286. resp, err := client.Do(req)
  287. defer resp.Body.Close()
  288. b, err := ioutil.ReadAll(resp.Body)
  289. fmt.Println("HttpPost:" + string(b))
  290. return b, err
  291. }
  292. // AddEdbData 新增指标数据
  293. func AddEdbDataWindWsd(source int, stockCode, edbCode string) (resp *models.BaseResponse, err error) {
  294. param := make(map[string]interface{})
  295. param["EdbCode"] = edbCode
  296. param["StockCode"] = stockCode
  297. param["Source"] = source
  298. urlStr := `wind/wsd/add`
  299. if urlStr == "" {
  300. err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
  301. return
  302. }
  303. resp, err = postRefreshEdbData(param, urlStr)
  304. return
  305. }
  306. // AddEdbDataThsDs 新增指标数据
  307. func AddEdbDataThsDs(source int, stockCode, edbCode string) (resp *models.BaseResponse, err error) {
  308. param := make(map[string]interface{})
  309. param["EdbCode"] = edbCode
  310. param["StockCode"] = stockCode
  311. param["Source"] = source
  312. urlStr := `ths/ds/add`
  313. if urlStr == "" {
  314. err = fmt.Errorf("未实现该指标的刷新接口,请联系管理员")
  315. return
  316. }
  317. resp, err = postRefreshEdbData(param, urlStr)
  318. return
  319. }