base_from_stock_plant.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/models/supply_analysis"
  7. "eta/eta_index_lib/utils"
  8. "strconv"
  9. "time"
  10. )
  11. // StockPlantController 存量装置
  12. type StockPlantController struct {
  13. BaseAuthController
  14. }
  15. // Calculate
  16. // @Title 计算减产数
  17. // @Description 计算减产数
  18. // @Success 200 {object} supply_analysis.CalculateVarietyReq
  19. // @router /calculate [post]
  20. func (this *StockPlantController) Calculate() {
  21. br := new(models.BaseResponse).Init()
  22. var cacheKey string
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req supply_analysis.CalculateVarietyReq
  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.VarietyId <= 0 {
  35. br.Msg = "请选择品种!"
  36. br.ErrMsg = "请选择品种"
  37. return
  38. }
  39. //cacheKey = utils.CACHE_STOCK_PLANT_CALCULATE + strconv.Itoa(req.VarietyId)
  40. //if utils.Rc.IsExist(cacheKey) {
  41. // br.Ret = 501
  42. // br.Success = true
  43. // br.Msg = "系统处理中,请稍后重试"
  44. // return
  45. //}
  46. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  47. defer func() {
  48. utils.Rc.Delete(cacheKey)
  49. }()
  50. err = supply_analysis.Calculate(req.VarietyId, req.AdminId, req.AdminName)
  51. if err != nil {
  52. br.Msg = "获取指标信息失败!"
  53. br.ErrMsg = "获取指标信息失败 AddEdbDataFromManual,Err:" + err.Error()
  54. return
  55. }
  56. br.Ret = 200
  57. br.Success = true
  58. br.Msg = "获取成功"
  59. }
  60. // @Title 刷新手工指标接口
  61. // @Description 刷新手工指标接口
  62. // @Success 200 {object} models.RefreshEdbInfoReq
  63. // @router /refresh [post]
  64. func (this *StockPlantController) Refresh() {
  65. br := new(models.BaseResponse).Init()
  66. defer func() {
  67. this.Data["json"] = br
  68. this.ServeJSON()
  69. }()
  70. source := utils.DATA_SOURCE_STOCK_PLANT
  71. var req models.RefreshEdbInfoReq
  72. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  73. if err != nil {
  74. br.Msg = "参数解析异常!"
  75. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  76. return
  77. }
  78. if req.EdbCode == "" {
  79. br.Msg = "请输入指标编码!"
  80. br.ErrMsg = "请输入指标编码,指标编码为空"
  81. return
  82. }
  83. if req.EdbInfoId <= 0 {
  84. br.Msg = "请输入指标ID!"
  85. br.ErrMsg = "请输入指标ID"
  86. return
  87. }
  88. cacheKey := utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  89. if utils.Rc.IsExist(cacheKey) {
  90. br.Ret = 501
  91. br.Success = true
  92. br.Msg = "系统处理中,请稍后重试"
  93. return
  94. }
  95. // 设置缓存
  96. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  97. defer func() {
  98. utils.Rc.Delete(cacheKey)
  99. }()
  100. // 获取指标详情
  101. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  102. if err != nil {
  103. br.Msg = "指标不存在!"
  104. br.ErrMsg = "指标不存在"
  105. return
  106. }
  107. req.StartDate = `` // 开始日期也给过滤吧,万一研究员补充了之前的数据呢,还是过滤掉吧
  108. err = models.RefreshEdbDataFromStockPlant(req.EdbInfoId, req.EdbCode, req.StartDate)
  109. if err != nil && err.Error() != utils.ErrNoRow() {
  110. br.Msg = "刷新指标信息失败!"
  111. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromManual,Err:" + err.Error()
  112. return
  113. }
  114. // 更新指标最大最小值
  115. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  116. if err != nil {
  117. br.Msg = errMsg
  118. br.ErrMsg = err.Error()
  119. return
  120. }
  121. // 更新ES
  122. go logic.UpdateEs(edbInfo.EdbInfoId)
  123. br.Ret = 200
  124. br.Success = true
  125. br.Msg = "获取成功"
  126. }