base_from_predict.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/logic"
  5. "eta/eta_index_lib/models"
  6. "eta/eta_index_lib/services"
  7. "eta/eta_index_lib/utils"
  8. "fmt"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. // PredictController 预测指标
  14. type PredictController struct {
  15. BaseAuthController
  16. }
  17. // Save
  18. // @Title 新增/编辑预测指标运算接口
  19. // @Description 新增预测指标运算接口
  20. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  21. // @router /save [post]
  22. func (this *PredictController) Save() {
  23. br := new(models.BaseResponse).Init()
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. var req models.AddPredictEdbInfoReq
  29. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  30. if err != nil {
  31. br.Msg = "参数解析异常!"
  32. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  33. return
  34. }
  35. if req.EdbInfoId <= 0 {
  36. addPredict(br, req, this.Lang)
  37. } else {
  38. editPredict(br, req, this.Lang)
  39. }
  40. }
  41. // addPredict 添加预测指标运算
  42. func addPredict(br *models.BaseResponse, req models.AddPredictEdbInfoReq, lang string) {
  43. //加入缓存机制,避免创建同一个名称的指标 start
  44. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT, ":", req.EdbName)
  45. isExist := utils.Rc.IsExist(redisKey)
  46. if isExist {
  47. br.Msg = "指标正在处理,请勿重复提交"
  48. return
  49. } else {
  50. //设置3分钟缓存
  51. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  52. defer func() {
  53. _ = utils.Rc.Delete(redisKey)
  54. }()
  55. }
  56. // 添加指标
  57. edbInfo, err, errMsg := logic.AddPredictEdbInfo(req.SourceEdbInfoId, req.ClassifyId, req.EdbName, req.DataDateType, req.EndDateType, req.RuleList, req.MinValue, req.MaxValue, req.AdminId, req.AdminName, lang)
  58. if err != nil {
  59. br.Msg = "保存失败"
  60. if errMsg != `` {
  61. br.Msg = errMsg
  62. }
  63. br.ErrMsg = err.Error()
  64. return
  65. }
  66. resp := models.AddEdbInfoResp{
  67. EdbInfoId: edbInfo.EdbInfoId,
  68. UniqueCode: edbInfo.UniqueCode,
  69. }
  70. br.Ret = 200
  71. br.Success = true
  72. br.Msg = "保存成功"
  73. br.Data = resp
  74. br.IsAddLog = true
  75. }
  76. // editPredict 编辑预测指标运算
  77. func editPredict(br *models.BaseResponse, req models.AddPredictEdbInfoReq, lang string) {
  78. req.EdbName = strings.Trim(req.EdbName, " ")
  79. //加入缓存机制,避免创建同一个名称的指标 start
  80. redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT, ":", req.EdbName)
  81. isExist := utils.Rc.IsExist(redisKey)
  82. if isExist {
  83. br.Msg = "指标正在处理,请勿重复提交"
  84. return
  85. } else {
  86. //设置3分钟缓存
  87. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  88. defer func() {
  89. _ = utils.Rc.Delete(redisKey)
  90. }()
  91. }
  92. // 编辑指标
  93. edbInfo, err, errMsg := logic.EditPredictEdbInfo(req.EdbInfoId, req.ClassifyId, req.EdbName, req.DataDateType, req.EndDateType, req.RuleList, req.MinValue, req.MaxValue, lang)
  94. if err != nil {
  95. br.Msg = "保存失败"
  96. if errMsg != `` {
  97. br.Msg = errMsg
  98. }
  99. br.ErrMsg = err.Error()
  100. return
  101. }
  102. resp := models.AddEdbInfoResp{
  103. EdbInfoId: edbInfo.EdbInfoId,
  104. UniqueCode: edbInfo.UniqueCode,
  105. }
  106. br.Ret = 200
  107. br.Success = true
  108. br.Msg = "保存成功"
  109. br.Data = resp
  110. br.IsAddLog = true
  111. }
  112. // Refresh
  113. // @Title 刷新预测指标接口(有些预测指标里面含有动态数据)
  114. // @Description 刷新预测指标接口(有些预测指标里面含有动态数据)
  115. // @Success 200 {object} models.RefreshEdbInfoReq
  116. // @router /refresh [post]
  117. func (this *PredictController) Refresh() {
  118. br := new(models.BaseResponse).Init()
  119. var cacheKey string
  120. defer func() {
  121. this.Data["json"] = br
  122. this.ServeJSON()
  123. }()
  124. var req models.RefreshEdbInfoReq
  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. if req.EdbCode == "" {
  132. br.Msg = "请输入指标编码!"
  133. br.ErrMsg = "请输入指标编码,指标编码为空"
  134. return
  135. }
  136. if req.EdbInfoId <= 0 {
  137. br.Msg = "请输入指标ID!"
  138. br.ErrMsg = "请输入指标ID"
  139. return
  140. }
  141. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  142. if err != nil {
  143. br.Msg = "指标不存在!"
  144. br.ErrMsg = "指标不存在"
  145. return
  146. }
  147. if edbInfo.IsStaticData == 1 {
  148. //静态数据直接返回
  149. br.Ret = 200
  150. br.Success = true
  151. br.Msg = "获取成功"
  152. return
  153. }
  154. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  155. if utils.Rc.IsExist(cacheKey) {
  156. br.Ret = 501
  157. br.Success = true
  158. br.Msg = "系统处理中,请稍后重试"
  159. br.IsSendEmail = false
  160. return
  161. }
  162. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  163. defer func() {
  164. _ = utils.Rc.Delete(cacheKey)
  165. }()
  166. var errMsg string
  167. _, err, errMsg = logic.RefreshPredictEdbInfo(req.EdbInfoId)
  168. if err != nil {
  169. br.Msg = "刷新指标失败!"
  170. if errMsg != `` {
  171. br.ErrMsg = errMsg
  172. }
  173. br.ErrMsg = "刷新指标失败,err:" + err.Error()
  174. return
  175. }
  176. refreshParams := models.RefreshParams{
  177. EdbInfo: edbInfo,
  178. }
  179. obj := new(models.PredictStandardBase)
  180. latestDateStr, latestValue, err, errMsg := obj.Refresh(refreshParams)
  181. if errMsg != `` {
  182. br.Msg = "刷新指标失败!"
  183. br.ErrMsg = "刷新指标失败,err:" + errMsg
  184. return
  185. }
  186. // 更新指标最大最小值
  187. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  188. if err != nil {
  189. br.Msg = errMsg
  190. br.ErrMsg = err.Error()
  191. return
  192. }
  193. // 更新ES
  194. go logic.UpdateEs(edbInfo.EdbInfoId)
  195. br.Ret = 200
  196. br.Success = true
  197. br.Msg = "获取成功"
  198. }
  199. // CalculateByNinePreview
  200. // @Title 动态环差演算
  201. // @Description 指标运算演算
  202. // @Param request body models.RuleConfig true "type json string"
  203. // @Success Ret=200 返回指标id
  204. // @router /calculate_by_nine/preview [post]
  205. func (this *PredictController) CalculateByNinePreview() {
  206. br := new(models.BaseResponse).Init()
  207. defer func() {
  208. this.Data["json"] = br
  209. this.ServeJSON()
  210. }()
  211. var req models.RuleConfig
  212. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  213. if err != nil {
  214. br.Msg = "参数解析异常!"
  215. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  216. return
  217. }
  218. var errMsg string
  219. // 获取计算参数
  220. formula, edbInfoList, edbInfoIdBytes, err, errMsg := services.GetCalculateByRuleByNineParams(req)
  221. if err != nil {
  222. br.Msg = "计算失败"
  223. if errMsg != "" {
  224. br.Msg = errMsg
  225. }
  226. br.Msg = err.Error()
  227. br.IsSendEmail = false
  228. return
  229. }
  230. // 获取计算数据
  231. dataList, err := services.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes, req.EmptyType, req.MaxEmptyType)
  232. if err != nil {
  233. br.Msg = "数据计算失败"
  234. br.ErrMsg = "数据计算失败:Err:" + err.Error()
  235. br.IsSendEmail = false
  236. return
  237. }
  238. latestDate := time.Now()
  239. for _, v := range edbInfoList {
  240. tmpLatestDate, err := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
  241. if err != nil {
  242. continue
  243. }
  244. if tmpLatestDate.Before(latestDate) {
  245. latestDate = tmpLatestDate
  246. }
  247. }
  248. newDataList := make([]*models.EdbDataList, 0)
  249. lenData := len(dataList)
  250. if lenData > 0 {
  251. for i := lenData - 1; i >= 0; i-- {
  252. newDataList = append(newDataList, dataList[i])
  253. }
  254. }
  255. resp := models.PredictRuleCalculateByNineResp{
  256. LatestDate: latestDate.Format(utils.FormatDate),
  257. DataList: newDataList,
  258. }
  259. br.Ret = 200
  260. br.Success = true
  261. br.Msg = "获取成功"
  262. br.Data = resp
  263. }
  264. // AddStaticEdb
  265. // @Title 新增/编辑预测指标运算接口
  266. // @Description 新增预测指标运算接口
  267. // @Success 200 {object} models.AddStaticPredictEdbInfoReq
  268. // @router /static_edb/add [post]
  269. func (this *PredictController) AddStaticEdb() {
  270. br := new(models.BaseResponse).Init()
  271. defer func() {
  272. this.Data["json"] = br
  273. this.ServeJSON()
  274. }()
  275. var req models.AddStaticPredictEdbInfoReq
  276. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  277. if err != nil {
  278. br.Msg = "参数解析异常!"
  279. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  280. return
  281. }
  282. //加入缓存机制,避免创建同一个名称的指标 start
  283. redisKey := fmt.Sprintf("predict_edb_info:AddStaticEdb:%s", req.EdbName)
  284. isExist := utils.Rc.IsExist(redisKey)
  285. if isExist {
  286. br.Msg = "指标正在处理,请勿重复提交"
  287. return
  288. } else {
  289. //设置3分钟缓存
  290. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  291. defer func() {
  292. _ = utils.Rc.Delete(redisKey)
  293. }()
  294. }
  295. // 添加指标
  296. edbInfo, err, errMsg := logic.AddStaticPredictEdbInfo(req.SourceEdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.AdminId, req.AdminName, this.Lang)
  297. if err != nil {
  298. br.Msg = "保存失败"
  299. if errMsg != `` {
  300. br.Msg = errMsg
  301. }
  302. br.ErrMsg = err.Error()
  303. return
  304. }
  305. resp := models.AddEdbInfoResp{
  306. EdbInfoId: edbInfo.EdbInfoId,
  307. UniqueCode: edbInfo.UniqueCode,
  308. }
  309. br.Ret = 200
  310. br.Success = true
  311. br.Msg = "保存成功"
  312. br.Data = resp
  313. br.IsAddLog = true
  314. }