base_from_pb_finance.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // PbFinanceController 彭博财务
  13. type PbFinanceController struct {
  14. BaseAuthController
  15. }
  16. // @Title 新增彭博指标接口
  17. // @Description 新增彭博指标接口
  18. // @Success 200 {object} models.AddEdbInfoReq
  19. // @router /add [post]
  20. func (this *PbFinanceController) Add() {
  21. br := new(models.BaseResponse).Init()
  22. var cacheKey string
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. source := utils.DATA_SOURCE_PB_FINANCE
  28. var req models.AddEdbInfoReq
  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.EdbCode == "" {
  36. br.Msg = "请输入指标编码!"
  37. br.ErrMsg = "请输入指标编码,指标编码为空"
  38. return
  39. }
  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. defer func() {
  44. utils.Rc.Delete(cacheKey)
  45. }()
  46. //指标编码处理,将指标编码拆分成 company_code和edb_code
  47. edbCode := req.EdbCode
  48. edbCodeArr := strings.Split(edbCode, "&&")
  49. if len(edbCodeArr) != 2 {
  50. br.Msg = "指标编码异常,请传入客户编码!"
  51. br.ErrMsg = "指标编码异常,请传入客户编码"
  52. return
  53. }
  54. dataItem, err := services.GetEdbDataFromPbFinance(edbCodeArr[0], edbCodeArr[1], utils.BASE_START_DATE_UnSpace, utils.BASE_END_DATE_UnSpace, "")
  55. if err != nil {
  56. br.Msg = "获取指标信息失败!"
  57. br.ErrMsg = "获取指标信息失败 GetEdbDataFromPb,Err:" + err.Error()
  58. return
  59. }
  60. err = models.AddEdbDataFromPbFinance(req.EdbCode, dataItem)
  61. if err != nil {
  62. br.Msg = "获取指标信息失败!"
  63. br.ErrMsg = "获取指标信息失败 AddEdbDataFromPb,Err:" + err.Error()
  64. return
  65. }
  66. br.Ret = 200
  67. br.Success = true
  68. br.Msg = "获取成功"
  69. } else {
  70. br.Ret = 501
  71. br.Success = true
  72. br.Msg = "系统处理中,请稍后重试"
  73. }
  74. }
  75. // @Title 刷新彭博指标接口
  76. // @Description 刷新彭博指标接口
  77. // @Success 200 {object} models.RefreshEdbInfoReq
  78. // @router /refresh [post]
  79. func (this *PbFinanceController) Refresh() {
  80. br := new(models.BaseResponse).Init()
  81. var cacheKey string
  82. defer func() {
  83. this.Data["json"] = br
  84. this.ServeJSON()
  85. }()
  86. source := utils.DATA_SOURCE_PB_FINANCE
  87. var req models.RefreshEdbInfoReq
  88. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  89. if err != nil {
  90. br.Msg = "参数解析异常!"
  91. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  92. return
  93. }
  94. if req.EdbCode == "" {
  95. br.Msg = "请输入指标编码!"
  96. br.ErrMsg = "请输入指标编码,指标编码为空"
  97. return
  98. }
  99. if req.EdbInfoId <= 0 {
  100. br.Msg = "请输入指标ID!"
  101. br.ErrMsg = "请输入指标ID"
  102. return
  103. }
  104. // 获取指标详情
  105. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  106. if err != nil {
  107. br.Msg = "指标不存在!"
  108. br.ErrMsg = "指标不存在"
  109. return
  110. }
  111. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  112. if utils.Rc.IsExist(cacheKey) {
  113. br.Ret = 501
  114. br.Success = true
  115. br.Msg = "系统处理中,请稍后重试"
  116. return
  117. }
  118. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  119. defer func() {
  120. utils.Rc.Delete(cacheKey)
  121. }()
  122. //开始日期格式化
  123. startDateTime, _ := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  124. //指标编码处理,将指标编码拆分成 company_code和edb_code
  125. edbCode := req.EdbCode
  126. edbCodeArr := strings.Split(edbCode, "&&")
  127. if len(edbCodeArr) != 2 {
  128. br.Msg = "指标编码异常,请传入客户编码!"
  129. br.ErrMsg = "指标编码异常,请传入客户编码"
  130. return
  131. }
  132. dataItem, err := services.GetEdbDataFromPbFinance(edbCodeArr[0], edbCodeArr[1], startDateTime.Format(utils.FormatDateUnSpace), utils.BASE_END_DATE_UnSpace, edbInfo.TerminalCode)
  133. if err != nil {
  134. br.Msg = "获取指标信息失败!"
  135. br.ErrMsg = "获取指标信息失败 GetEdbDataFromPb,Err:" + err.Error()
  136. return
  137. }
  138. err = models.RefreshEdbDataFromPbFinance(req.EdbInfoId, req.EdbCode, req.StartDate, dataItem)
  139. if err != nil && err.Error() != utils.ErrNoRow() {
  140. br.Msg = "刷新指标信息失败!"
  141. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromPb,Err:" + err.Error()
  142. return
  143. }
  144. // 更新指标最大最小值
  145. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  146. if err != nil {
  147. br.Msg = errMsg
  148. br.ErrMsg = err.Error()
  149. return
  150. }
  151. // 更新ES
  152. go logic.UpdateEs(edbInfo.EdbInfoId)
  153. br.Ret = 200
  154. br.Success = true
  155. br.Msg = "获取成功"
  156. }