predict_edb_info_calculate.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/services/data"
  7. "eta/eta_api/utils"
  8. "fmt"
  9. "strings"
  10. "time"
  11. )
  12. // CalculateSave
  13. // @Title 生成预测指标运算
  14. // @Description 生成预测指标运算接口
  15. // @Param request body data_manage.PredictEdbInfoCalculateSaveReq true "type json string"
  16. // @Success Ret=200 返回指标id
  17. // @router /predict_edb_info/calculate/save [post]
  18. func (this *PredictEdbInfoController) CalculateSave() {
  19. br := new(models.BaseResponse).Init()
  20. defer func() {
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. sysUser := this.SysUser
  25. if sysUser == nil {
  26. br.Msg = "请登录"
  27. br.ErrMsg = "请登录,SysUser Is Empty"
  28. br.Ret = 408
  29. return
  30. }
  31. var req data_manage.PredictEdbInfoCalculateSaveReq
  32. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  33. if err != nil {
  34. br.Msg = "参数解析异常!"
  35. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  36. return
  37. }
  38. //传入操作人
  39. req.AdminId = sysUser.AdminId
  40. req.AdminName = sysUser.RealName
  41. // 基础的一些数据校验
  42. req.EdbName = strings.Trim(req.EdbName, " ")
  43. if req.EdbName == "" {
  44. br.Msg = "指标名称不能为空"
  45. return
  46. }
  47. if req.Frequency == "" {
  48. br.Msg = "频率不能为空"
  49. return
  50. }
  51. if req.Unit == "" {
  52. br.Msg = "单位不能为空"
  53. return
  54. }
  55. if req.ClassifyId <= 0 {
  56. br.Msg = "请选择分类"
  57. return
  58. }
  59. if len(req.EdbInfoIdArr) <= 0 {
  60. br.Msg = "请选择指标"
  61. return
  62. }
  63. if req.CalculateFormula == "" {
  64. br.Msg = "请填写指标"
  65. return
  66. }
  67. calculateFormula := req.CalculateFormula
  68. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  69. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  70. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  71. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  72. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  73. req.CalculateFormula = calculateFormula
  74. edbInfoMap := make(map[int]string)
  75. //移除研究员选择指标中的未使用的指标
  76. {
  77. //转大写的计算公式
  78. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  79. //用到的指标
  80. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  81. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  82. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  83. if ok {
  84. br.Msg = "选择指标失败,请勿选择重复指标!"
  85. return
  86. }
  87. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  88. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  89. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  90. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  91. }
  92. }
  93. req.EdbInfoIdArr = newEdbInfoIdArr
  94. }
  95. // 添加计算指标
  96. reqJson, err := json.Marshal(req)
  97. if err != nil {
  98. br.Msg = "参数解析异常!"
  99. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  100. return
  101. }
  102. respItem, err := data.SavePredictEdbData(string(reqJson), this.Lang)
  103. if err != nil {
  104. br.Msg = "获取失败"
  105. br.ErrMsg = "获取失败,Err:" + err.Error()
  106. return
  107. }
  108. if respItem.Ret != 200 {
  109. br.Msg = respItem.Msg
  110. br.ErrMsg = respItem.ErrMsg
  111. return
  112. }
  113. resp := respItem.Data
  114. //添加es
  115. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  116. br.Ret = 200
  117. br.Success = true
  118. br.Msg = "保存成功"
  119. br.Data = resp
  120. br.IsAddLog = true
  121. }
  122. // CalculateBatchSave
  123. // @Title 生成预测指标运算(同比、同差)
  124. // @Description 生成预测指标运算(同比、同差)接口
  125. // @Param request body data_manage.PredictEdbInfoCalculateBatchSaveReq true "type json string"
  126. // @Success Ret=200 返回指标id
  127. // @router /predict_edb_info/calculate/batch/save [post]
  128. func (this *PredictEdbInfoController) CalculateBatchSave() {
  129. br := new(models.BaseResponse).Init()
  130. defer func() {
  131. this.Data["json"] = br
  132. this.ServeJSON()
  133. }()
  134. sysUser := this.SysUser
  135. if sysUser == nil {
  136. br.Msg = "请登录"
  137. br.ErrMsg = "请登录,SysUser Is Empty"
  138. br.Ret = 408
  139. return
  140. }
  141. var req data_manage.PredictEdbInfoCalculateBatchSaveReq
  142. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  143. if err != nil {
  144. br.Msg = "参数解析异常!"
  145. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  146. return
  147. }
  148. //传入操作人
  149. req.AdminId = sysUser.AdminId
  150. req.AdminName = sysUser.RealName
  151. req.EdbName = strings.Trim(req.EdbName, " ")
  152. if req.EdbName == "" {
  153. br.Msg = "指标名称不能为空"
  154. return
  155. }
  156. if req.Frequency == "" {
  157. br.Msg = "频率不能为空"
  158. return
  159. }
  160. if req.Unit == "" {
  161. br.Msg = "单位不能为空"
  162. return
  163. }
  164. if req.ClassifyId <= 0 {
  165. br.Msg = "请选择分类"
  166. return
  167. }
  168. // 添加计算指标
  169. reqJson, err := json.Marshal(req)
  170. if err != nil {
  171. br.Msg = "参数解析异常!"
  172. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  173. return
  174. }
  175. respItem, err := data.BatchSavePredictEdbData(string(reqJson), this.Lang)
  176. if err != nil {
  177. br.Msg = "获取失败"
  178. br.ErrMsg = "获取失败,Err:" + err.Error()
  179. return
  180. }
  181. if respItem.Ret != 200 {
  182. br.Msg = respItem.Msg
  183. br.ErrMsg = respItem.ErrMsg
  184. return
  185. }
  186. resp := respItem.Data
  187. //添加es
  188. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  189. br.Ret = 200
  190. br.Success = true
  191. br.Msg = "保存成功"
  192. br.Data = resp
  193. br.IsAddLog = true
  194. }
  195. // BatchCalculateBatchSave
  196. // @Title 批量添加 生成预测指标运算(同比、同差)
  197. // @Description 批量添加 生成预测指标运算(同比、同差)接口
  198. // @Param request body data_manage.PredictEdbInfoCalculateBatchSaveReq true "type json string"
  199. // @Success Ret=200 返回指标id
  200. // @router /predict_edb_info/calculate/batch/save/batch [post]
  201. func (this *PredictEdbInfoController) BatchCalculateBatchSave() {
  202. br := new(models.BaseResponse).Init()
  203. defer func() {
  204. this.Data["json"] = br
  205. this.ServeJSON()
  206. }()
  207. sysUser := this.SysUser
  208. if sysUser == nil {
  209. br.Msg = "请登录"
  210. br.ErrMsg = "请登录,SysUser Is Empty"
  211. br.Ret = 408
  212. return
  213. }
  214. var reqList []data_manage.BatchPredictEdbInfoCalculateBatchSaveReq
  215. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqList)
  216. if err != nil {
  217. br.Msg = "参数解析异常!"
  218. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  219. return
  220. }
  221. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  222. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  223. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  224. }
  225. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  226. defer func() {
  227. for _, redisKey := range redisKeyList {
  228. utils.Rc.Delete(redisKey)
  229. }
  230. }()
  231. for _, v := range reqList {
  232. req := v.CalculateInfo
  233. req.EdbName = strings.Trim(req.EdbName, " ")
  234. if req.EdbName == "" {
  235. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  236. CalculateId: v.CalculateId,
  237. Msg: "指标名称不能为空",
  238. })
  239. continue
  240. }
  241. if req.Frequency == "" {
  242. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  243. CalculateId: v.CalculateId,
  244. Msg: "频率不能为空",
  245. })
  246. continue
  247. }
  248. if req.Unit == "" {
  249. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  250. CalculateId: v.CalculateId,
  251. Msg: "单位不能为空",
  252. })
  253. continue
  254. }
  255. if req.ClassifyId <= 0 {
  256. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  257. CalculateId: v.CalculateId,
  258. Msg: "请选择分类",
  259. })
  260. continue
  261. }
  262. //fromEdbInfoId := req.FromEdbInfoId
  263. //
  264. //if fromEdbInfoId <= 0 {
  265. // resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  266. // CalculateId: v.CalculateId,
  267. // Msg: "请选择指标",
  268. // })
  269. // continue
  270. //}
  271. req.AdminId = sysUser.AdminId
  272. req.AdminName = sysUser.RealName
  273. //加入缓存机制,避免创建同一个名称的指标 start
  274. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", sysUser.AdminId, ":", req.Source, ":", v.CalculateId)
  275. isExist := utils.Rc.IsExist(redisKey)
  276. if isExist {
  277. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  278. CalculateId: v.CalculateId,
  279. Msg: "指标正在处理,请勿重复提交",
  280. })
  281. continue
  282. } else {
  283. //设置3分钟缓存
  284. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  285. redisKeyList = append(redisKeyList, redisKey)
  286. }
  287. //加入缓存机制,避免创建同一个名称的指标 end
  288. reqJson, err := json.Marshal(req)
  289. if err != nil {
  290. br.Msg = "参数解析异常!"
  291. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  292. return
  293. }
  294. respItem, err := data.BatchSavePredictEdbData(string(reqJson), this.Lang)
  295. if err != nil {
  296. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  297. CalculateId: v.CalculateId,
  298. Msg: "新增失败!",
  299. ErrMsg: "新增失败,Err:" + err.Error(),
  300. })
  301. continue
  302. }
  303. if respItem.Ret != 200 {
  304. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  305. CalculateId: v.CalculateId,
  306. Msg: respItem.Msg,
  307. ErrMsg: respItem.ErrMsg,
  308. })
  309. continue
  310. }
  311. result := respItem.Data
  312. //添加es
  313. data.AddOrEditEdbInfoToEs(result.EdbInfoId)
  314. resp.Success = append(resp.Success, data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp{
  315. CalculateId: v.CalculateId,
  316. EdbInfoId: result.EdbInfoId,
  317. UniqueCode: result.UniqueCode,
  318. ClassifyId: req.ClassifyId,
  319. })
  320. }
  321. br.Ret = 200
  322. br.Success = true
  323. br.Msg = "批量保存成功"
  324. br.Data = resp
  325. br.IsAddLog = true
  326. }
  327. // CalculateComputeCorrelation
  328. // @Title 拟合残差计算相关性(预测指标)
  329. // @Description 拟合残差计算相关性(预测指标)接口
  330. // @Param request body data_manage.PredictEdbInfoCalculateBatchSaveReq true "type json string"
  331. // @Success Ret=200 返回指标id
  332. // @router /predict_edb_info/calculate/compute_correlation [post]
  333. func (this *PredictEdbInfoController) CalculateComputeCorrelation() {
  334. br := new(models.BaseResponse).Init()
  335. defer func() {
  336. this.Data["json"] = br
  337. this.ServeJSON()
  338. }()
  339. sysUser := this.SysUser
  340. if sysUser == nil {
  341. br.Msg = "请登录"
  342. br.ErrMsg = "请登录,SysUser Is Empty"
  343. br.Ret = 408
  344. return
  345. }
  346. var req data_manage.PredictEdbInfoCalculateBatchSaveReq
  347. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  348. if err != nil {
  349. br.Msg = "参数解析异常!"
  350. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  351. return
  352. }
  353. //传入操作人
  354. req.AdminId = sysUser.AdminId
  355. req.AdminName = sysUser.RealName
  356. // 添加计算指标
  357. reqJson, err := json.Marshal(req)
  358. if err != nil {
  359. br.Msg = "参数解析异常!"
  360. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  361. return
  362. }
  363. respItem, err := data.PredictCalculateComputeCorrelation(string(reqJson), this.Lang)
  364. if err != nil {
  365. br.Msg = "获取失败"
  366. br.ErrMsg = "获取失败,Err:" + err.Error()
  367. return
  368. }
  369. if respItem.Ret != 200 {
  370. br.Msg = respItem.Msg
  371. br.ErrMsg = respItem.ErrMsg
  372. return
  373. }
  374. resp := respItem.Data
  375. br.Ret = 200
  376. br.Success = true
  377. br.Msg = "计算成功"
  378. br.Data = resp
  379. br.IsAddLog = true
  380. }