base_edb_lib.go 13 KB

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