base_from_gpr_risk.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "time"
  11. )
  12. // GprRiskController GPR地缘风险指数
  13. type GprRiskController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增GPR地缘风险指数指标接口
  18. // @Description 新增GPR地缘风险指数指标接口
  19. // @Success 200 {object} models.AddEdbInfoReq
  20. // @router /add [post]
  21. func (this *GprRiskController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. var cacheKey string
  24. defer func() {
  25. this.Data["json"] = br
  26. this.ServeJSON()
  27. }()
  28. gprRisk := new(models.BaseFromGprRisk)
  29. source := gprRisk.GetSource()
  30. var req models.AddEdbInfoReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. if req.EdbCode == "" {
  38. br.Msg = "请输入指标编码!"
  39. br.ErrMsg = "请输入指标编码,指标编码为空"
  40. return
  41. }
  42. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  43. if utils.Rc.IsExist(cacheKey) {
  44. br.Ret = 501
  45. br.Success = true
  46. br.Msg = "系统处理中,请稍后重试"
  47. return
  48. }
  49. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  50. defer func() {
  51. utils.Rc.Delete(cacheKey)
  52. }()
  53. err = gprRisk.Add(req.EdbCode)
  54. if err != nil {
  55. br.Msg = "获取指标信息失败!"
  56. br.ErrMsg = "获取指标信息失败 AddEdbDataFromGprRisk,Err:" + err.Error()
  57. return
  58. }
  59. br.Ret = 200
  60. br.Success = true
  61. br.Msg = "获取成功"
  62. return
  63. }
  64. // Refresh
  65. // @Title 刷新GPR地缘风险指数指标接口
  66. // @Description 刷新GPR地缘风险指数指标接口
  67. // @Success 200 {object} models.RefreshEdbInfoReq
  68. // @router /refresh [post]
  69. func (this *GprRiskController) Refresh() {
  70. br := new(models.BaseResponse).Init()
  71. var cacheKey string
  72. defer func() {
  73. this.Data["json"] = br
  74. this.ServeJSON()
  75. }()
  76. GprRisk := new(models.BaseFromGprRisk)
  77. source := GprRisk.GetSource()
  78. var req models.RefreshEdbInfoReq
  79. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  80. if err != nil {
  81. br.Msg = "参数解析异常!"
  82. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  83. return
  84. }
  85. if req.EdbCode == "" {
  86. br.Msg = "请输入指标编码!"
  87. br.ErrMsg = "请输入指标编码,指标编码为空"
  88. return
  89. }
  90. if req.EdbInfoId < 0 {
  91. br.Msg = "请输入指标ID!"
  92. br.ErrMsg = "请输入指标ID"
  93. return
  94. }
  95. // 获取指标详情
  96. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  97. if err != nil {
  98. br.Msg = "指标不存在!"
  99. br.ErrMsg = "指标不存在"
  100. return
  101. }
  102. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  103. if utils.Rc.IsExist(cacheKey) {
  104. br.Ret = 501
  105. br.Success = true
  106. br.Msg = "系统处理中,请稍后重试"
  107. return
  108. }
  109. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  110. defer func() {
  111. utils.Rc.Delete(cacheKey)
  112. }()
  113. if req.EdbInfoId <= 0 {
  114. req.EdbInfoId = edbInfo.EdbInfoId
  115. }
  116. err = GprRisk.Refresh(req.EdbInfoId, req.EdbCode, req.StartDate)
  117. if err != nil && err.Error() != utils.ErrNoRow() {
  118. br.Msg = "刷新指标信息失败!"
  119. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromGprRisk,Err:" + err.Error()
  120. return
  121. }
  122. // 更新指标最大最小值
  123. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  124. if err != nil {
  125. br.Msg = errMsg
  126. br.ErrMsg = err.Error()
  127. return
  128. }
  129. // 更新ES
  130. go logic.UpdateEs(edbInfo.EdbInfoId)
  131. br.Ret = 200
  132. br.Success = true
  133. br.Msg = "获取成功"
  134. }
  135. // HandleExcelData
  136. // @Title 处理GPR地缘风险指数指标的接口
  137. // @Description 处理GPR地缘风险指数指标的接口
  138. // @Success 200 {object} models.HandleGprRiskExcelDataReq
  139. // @router /handle/excel_data [post]
  140. func (this *GprRiskController) HandleExcelData() {
  141. br := new(models.BaseResponse).Init()
  142. defer func() {
  143. this.Data["json"] = br
  144. this.ServeJSON()
  145. }()
  146. var req models.HandleGprRiskExcelDataReq
  147. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  148. if err != nil {
  149. br.Msg = "参数解析异常!"
  150. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  151. return
  152. }
  153. // 处理excel表数据
  154. err = services.HandleGprRiskIndex(&req)
  155. if err != nil {
  156. fmt.Println("HandleMysteelIndex Err:" + err.Error())
  157. br.Msg = "处理失败"
  158. br.ErrMsg = "处理失败,Err:" + err.Error()
  159. return
  160. }
  161. br.Ret = 200
  162. br.Success = true
  163. br.Msg = "处理成功"
  164. }