base_from_predict.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_index_lib/logic"
  5. "eta_gn/eta_index_lib/models"
  6. "eta_gn/eta_index_lib/services"
  7. "eta_gn/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.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.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. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  148. if utils.Rc.IsExist(cacheKey) {
  149. br.Ret = 501
  150. br.Success = true
  151. br.Msg = "系统处理中,请稍后重试"
  152. br.IsSendEmail = false
  153. return
  154. }
  155. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  156. defer func() {
  157. _ = utils.Rc.Delete(cacheKey)
  158. }()
  159. var errMsg string
  160. _, err, errMsg = logic.RefreshPredictEdbInfo(req.EdbInfoId)
  161. if err != nil {
  162. br.Msg = "刷新指标失败!"
  163. if errMsg != `` {
  164. br.ErrMsg = errMsg
  165. }
  166. br.ErrMsg = "刷新指标失败,err:" + err.Error()
  167. return
  168. }
  169. refreshParams := models.RefreshParams{
  170. EdbInfo: edbInfo,
  171. }
  172. obj := new(models.PredictStandardBase)
  173. latestDateStr, latestValue, err, errMsg := obj.Refresh(refreshParams)
  174. if errMsg != `` {
  175. br.Msg = "刷新指标失败!"
  176. br.ErrMsg = "刷新指标失败,err:" + errMsg
  177. return
  178. }
  179. // 更新指标最大最小值
  180. err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
  181. if err != nil {
  182. br.Msg = errMsg
  183. br.ErrMsg = err.Error()
  184. return
  185. }
  186. // 更新ES
  187. go logic.UpdateEs(edbInfo.EdbInfoId)
  188. br.Ret = 200
  189. br.Success = true
  190. br.Msg = "获取成功"
  191. }
  192. // CalculateByNinePreview
  193. // @Title 动态环差演算
  194. // @Description 指标运算演算
  195. // @Param request body models.RuleConfig true "type json string"
  196. // @Success Ret=200 返回指标id
  197. // @router /calculate_by_nine/preview [post]
  198. func (this *PredictController) CalculateByNinePreview() {
  199. br := new(models.BaseResponse).Init()
  200. defer func() {
  201. this.Data["json"] = br
  202. this.ServeJSON()
  203. }()
  204. var req models.RuleConfig
  205. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  206. if err != nil {
  207. br.Msg = "参数解析异常!"
  208. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  209. return
  210. }
  211. var errMsg string
  212. // 获取计算参数
  213. formula, edbInfoList, edbInfoIdBytes, err, errMsg := services.GetCalculateByRuleByNineParams(req)
  214. if err != nil {
  215. br.Msg = "计算失败"
  216. if errMsg != "" {
  217. br.Msg = errMsg
  218. }
  219. br.Msg = err.Error()
  220. br.IsSendEmail = false
  221. return
  222. }
  223. // 获取计算数据
  224. dataList, err := services.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes, req.EmptyType, req.MaxEmptyType)
  225. if err != nil {
  226. br.Msg = "数据计算失败"
  227. br.ErrMsg = "数据计算失败:Err:" + err.Error()
  228. br.IsSendEmail = false
  229. return
  230. }
  231. latestDate := time.Now()
  232. for _, v := range edbInfoList {
  233. tmpLatestDate, err := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
  234. if err != nil {
  235. continue
  236. }
  237. if tmpLatestDate.Before(latestDate) {
  238. latestDate = tmpLatestDate
  239. }
  240. }
  241. newDataList := make([]*models.EdbDataList, 0)
  242. lenData := len(dataList)
  243. if lenData > 0 {
  244. for i := lenData - 1; i >= 0; i-- {
  245. newDataList = append(newDataList, dataList[i])
  246. }
  247. }
  248. resp := models.PredictRuleCalculateByNineResp{
  249. LatestDate: latestDate.Format(utils.FormatDate),
  250. DataList: newDataList,
  251. }
  252. br.Ret = 200
  253. br.Success = true
  254. br.Msg = "获取成功"
  255. br.Data = resp
  256. }