base_edb_lib.go 14 KB

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