base_from_national_statistics.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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/utils"
  7. "strconv"
  8. "time"
  9. )
  10. // NationalStatisticsController 国家统计局
  11. type NationalStatisticsController struct {
  12. BaseAuthController
  13. }
  14. // Add
  15. // @Title 新增指标
  16. // @Description 新增指标
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /add [post]
  19. func (this *NationalStatisticsController) Add() {
  20. br := new(models.BaseResponse).Init()
  21. var cacheKey string
  22. defer func() {
  23. _ = utils.Rc.Delete(cacheKey)
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req models.AddEdbInfoReq
  28. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. br.Msg = "参数解析异常!"
  31. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  32. return
  33. }
  34. if req.EdbCode == "" {
  35. br.Msg = "请输入指标编码!"
  36. br.ErrMsg = "请输入指标编码,指标编码为空"
  37. return
  38. }
  39. source := utils.DATA_SOURCE_NATIONAL_STATISTICS
  40. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode
  41. if !utils.Rc.IsExist(cacheKey) {
  42. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  43. err = models.AddEdbDataFromNationalStatistics(req.EdbCode)
  44. if err != nil {
  45. br.Msg = "获取指标信息失败!"
  46. br.ErrMsg = "获取指标信息失败 AddEdbDataFromNationalStatistics,Err:" + err.Error()
  47. return
  48. }
  49. br.Ret = 200
  50. br.Success = true
  51. br.Msg = "获取成功"
  52. } else {
  53. br.Ret = 501
  54. br.Success = true
  55. br.Msg = "系统处理中,请稍后重试"
  56. }
  57. }
  58. // Refresh
  59. // @Title 刷新指标
  60. // @Description 刷新指标
  61. // @Success 200 {object} models.RefreshEdbInfoReq
  62. // @router /refresh [post]
  63. func (this *NationalStatisticsController) Refresh() {
  64. br := new(models.BaseResponse).Init()
  65. var cacheKey string
  66. defer func() {
  67. this.Data["json"] = br
  68. this.ServeJSON()
  69. }()
  70. var req models.RefreshEdbInfoReq
  71. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  72. if err != nil {
  73. br.Msg = "参数解析异常!"
  74. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  75. return
  76. }
  77. if req.EdbCode == "" {
  78. br.Msg = "请输入指标编码!"
  79. br.ErrMsg = "请输入指标编码,指标编码为空"
  80. return
  81. }
  82. if req.EdbInfoId < 0 {
  83. br.Msg = "请输入指标ID!"
  84. br.ErrMsg = "请输入指标ID"
  85. return
  86. }
  87. source := utils.DATA_SOURCE_NATIONAL_STATISTICS
  88. // 获取指标详情
  89. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  90. if err != nil {
  91. br.Msg = "指标不存在!"
  92. br.ErrMsg = "指标不存在"
  93. return
  94. }
  95. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  96. if utils.Rc.IsExist(cacheKey) {
  97. br.Ret = 501
  98. br.Success = true
  99. br.Msg = "系统处理中,请稍后重试"
  100. return
  101. }
  102. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  103. defer func() {
  104. _ = utils.Rc.Delete(cacheKey)
  105. }()
  106. if req.EdbInfoId <= 0 {
  107. req.EdbInfoId = edbInfo.EdbInfoId
  108. }
  109. err = models.RefreshEdbDataFromNationalStatistics(req.EdbInfoId, req.EdbCode, req.StartDate)
  110. if err != nil && err.Error() != utils.ErrNoRow() {
  111. br.Msg = "刷新指标信息失败!"
  112. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromNationalStatistics,Err:" + err.Error()
  113. return
  114. }
  115. // 更新指标最大最小值
  116. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  117. if err != nil {
  118. br.Msg = errMsg
  119. br.ErrMsg = err.Error()
  120. return
  121. }
  122. // 更新ES
  123. go logic.UpdateEs(edbInfo.EdbInfoId)
  124. br.Ret = 200
  125. br.Success = true
  126. br.Msg = "获取成功"
  127. }