predict_edb_info_calculate.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_eta_api/models"
  6. "hongze/hz_eta_api/models/data_manage"
  7. "hongze/hz_eta_api/services/data"
  8. "hongze/hz_eta_api/utils"
  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))
  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))
  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))
  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. })
  319. }
  320. br.Ret = 200
  321. br.Success = true
  322. br.Msg = "批量保存成功"
  323. br.Data = resp
  324. br.IsAddLog = true
  325. }