edb_info_calculate_mapping.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/utils"
  6. "time"
  7. )
  8. // 计算指标
  9. type EdbInfoCalculateController struct {
  10. BaseAuthController
  11. }
  12. // @Title 复制新增来自弘则的计算指标接口
  13. // @Description 复制新增来自弘则的计算指标接口
  14. // @Success 200 {object} models.EdbInfoCalculateMapping
  15. // @router /copy/calculate/from/hz [post]
  16. func (this *EdbInfoController) CopyCalculateFromHz() {
  17. br := new(models.BaseResponse).Init()
  18. var cacheKey string
  19. defer func() {
  20. utils.Rc.Delete(cacheKey)
  21. this.Data["json"] = br
  22. this.ServeJSON()
  23. }()
  24. req := new(models.EdbInfoCalculateMapping)
  25. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  26. if err != nil {
  27. br.Msg = "参数解析异常!"
  28. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  29. return
  30. }
  31. if req.EdbCode == "" {
  32. br.Msg = "请输入指标编码!"
  33. br.ErrMsg = "请输入指标编码,指标编码为空"
  34. return
  35. }
  36. deleteCache := true
  37. cacheKey = "CACHE_EDB_INFO_COPY_CALCULATE_FROM_HZ_" + req.EdbCode
  38. defer func() {
  39. if deleteCache {
  40. utils.Rc.Delete(cacheKey)
  41. }
  42. }()
  43. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  44. deleteCache = false
  45. br.Msg = "系统处理中,请稍后重试!"
  46. br.ErrMsg = "系统处理中,请稍后重试!"
  47. return
  48. }
  49. var isAdd bool
  50. item, err := models.GetEdbInfoCalculateByEdbCode(req.EdbCode, req.FromEdbCode)
  51. if err != nil {
  52. if err.Error() == utils.ErrNoRow() {
  53. isAdd = true
  54. } else {
  55. br.Msg = "判断指标是否存在失败!"
  56. br.ErrMsg = "判断指标是否存在失败"
  57. return
  58. }
  59. }
  60. if item != nil && item.EdbInfoId <= 0 {
  61. isAdd = true
  62. }
  63. if isAdd {
  64. req.EdbInfoCalculateMappingId = 0
  65. err = req.Add()
  66. if err != nil {
  67. br.Msg = "新增指标失败!"
  68. br.ErrMsg = "新增指标失败"
  69. return
  70. }
  71. }
  72. br.Ret = 200
  73. br.Msg = "新增成功"
  74. }