predict_edb_info_calculate.go 9.2 KB

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