base_from_wind.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_edb_lib/models"
  5. "hongze/hongze_edb_lib/services"
  6. "hongze/hongze_edb_lib/utils"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //wind
  12. type WindController struct {
  13. BaseAuthController
  14. }
  15. // @Title 新增wind指标接口
  16. // @Description 新增wind指标接口
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /add [post]
  19. func (this *WindController) 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. source := utils.DATA_SOURCE_WIND
  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. //期货数据,就默认到今天,特殊处理下
  44. endDate := utils.BASE_END_DATE
  45. if strings.Contains(req.EdbCode, ".") {
  46. endDate = time.Now().Format(utils.FormatDate)
  47. }
  48. dataItem, errCode, err := services.GetEdbDataFromWind(req.EdbCode, utils.BASE_START_DATE, endDate)
  49. if errCode == 421 { //指标超限
  50. br.Ret = 421
  51. }
  52. if err != nil {
  53. br.Msg = "获取指标信息失败!"
  54. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  55. return
  56. }
  57. err = models.AddEdbDataFromWind(req.EdbCode, dataItem)
  58. if err != nil {
  59. br.Msg = "获取指标信息失败!"
  60. br.ErrMsg = "获取指标信息失败 AddEdbDataFromWind,Err:" + err.Error()
  61. return
  62. }
  63. br.Ret = 200
  64. br.Success = true
  65. br.Msg = "获取成功"
  66. } else {
  67. br.Ret = 501
  68. br.Success = true
  69. br.Msg = "系统处理中,请稍后重试"
  70. }
  71. }
  72. // @Title 刷新wind指标接口
  73. // @Description 刷新wind指标接口
  74. // @Success 200 {object} models.RefreshEdbInfoReq
  75. // @router /refresh [post]
  76. func (this *WindController) Refresh() {
  77. br := new(models.BaseResponse).Init()
  78. var cacheKey string
  79. defer func() {
  80. utils.Rc.Delete(cacheKey)
  81. this.Data["json"] = br
  82. this.ServeJSON()
  83. }()
  84. source := utils.DATA_SOURCE_WIND
  85. var req models.RefreshEdbInfoReq
  86. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  87. if err != nil {
  88. br.Msg = "参数解析异常!"
  89. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  90. return
  91. }
  92. if req.EdbCode == "" {
  93. br.Msg = "请输入指标编码!"
  94. br.ErrMsg = "请输入指标编码,指标编码为空"
  95. return
  96. }
  97. if req.EdbInfoId <= 0 {
  98. br.Msg = "请输入指标ID!"
  99. br.ErrMsg = "请输入指标ID"
  100. return
  101. }
  102. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  103. if !utils.Rc.IsExist(cacheKey) {
  104. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  105. //特定的指标过滤(wind不对该指标进行更新了)
  106. {
  107. edbCodeArr := []string{"S6451662", "S6451679", "S6451670", "S6451671", "s6451669", "S6451693", "S6451694"}
  108. has := utils.InArrayByStr(edbCodeArr, req.EdbCode)
  109. if has {
  110. br.Ret = 200
  111. br.Success = true
  112. br.Msg = "该指标不再更新,系统自动过滤了"
  113. return
  114. }
  115. }
  116. //期货数据,就默认到今天,特殊处理下
  117. endDate := utils.BASE_END_DATE
  118. if strings.Contains(req.EdbCode, ".") {
  119. endDate = time.Now().Format(utils.FormatDate)
  120. }
  121. dataItem, errCode, err := services.GetEdbDataFromWind(req.EdbCode, req.StartDate, endDate)
  122. if errCode == 421 { //指标超限
  123. br.Ret = 421
  124. }
  125. if err != nil {
  126. br.Msg = "获取指标信息失败!"
  127. br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error()
  128. return
  129. }
  130. err = models.RefreshEdbDataFromWind(req.EdbInfoId, req.EdbCode, req.StartDate, dataItem)
  131. if err != nil && err.Error() != utils.ErrNoRow() {
  132. br.Msg = "刷新指标信息失败!"
  133. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
  134. return
  135. }
  136. br.Ret = 200
  137. br.Success = true
  138. br.Msg = "获取成功"
  139. } else {
  140. br.Ret = 501
  141. br.Success = true
  142. br.Msg = "系统处理中,请稍后重试"
  143. }
  144. }