base_edb_lib.go 13 KB

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