base_from_ths_ds.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. "time"
  10. )
  11. // 同花顺
  12. type ThsDsController struct {
  13. BaseAuthController
  14. }
  15. // @Title 新增同花顺指标接口
  16. // @Description 新增同花顺指标接口
  17. // @Success 200 {object} models.AddEdbInfoReq
  18. // @router /ds/add [post]
  19. func (this *ThsDsController) 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_THS
  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. //期货数据,就默认到今天,特殊处理下
  41. endDate := time.Now().Format(utils.FormatDate)
  42. cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.StockCode + req.EdbCode
  43. if !utils.Rc.IsExist(cacheKey) {
  44. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  45. dataItem, err := services.GetEdbDataFromThsDs(req.StockCode, req.EdbCode, utils.BASE_START_DATE, endDate, "")
  46. if err != nil {
  47. br.Msg = "获取指标信息失败!"
  48. br.ErrMsg = "获取指标信息失败 GetEdbDataFromThsDs,Err:" + err.Error()
  49. return
  50. }
  51. err = models.AddEdbDataFromThsDs(req.StockCode, req.EdbCode, dataItem)
  52. if err != nil {
  53. br.Msg = "获取指标信息失败!"
  54. br.ErrMsg = "获取指标信息失败 AddEdbDataFromThs,Err:" + err.Error()
  55. return
  56. }
  57. br.Ret = 200
  58. br.Success = true
  59. br.Msg = "获取成功"
  60. } else {
  61. br.Ret = 501
  62. br.Success = true
  63. br.Msg = "系统处理中,请稍后重试"
  64. }
  65. }
  66. // @Title 刷新同花顺指标接口
  67. // @Description 刷新同花顺指标接口
  68. // @Success 200 {object} models.RefreshEdbInfoReq
  69. // @router /ds/refresh [post]
  70. func (this *ThsDsController) Refresh() {
  71. br := new(models.BaseResponse).Init()
  72. var cacheKey string
  73. defer func() {
  74. this.Data["json"] = br
  75. this.ServeJSON()
  76. }()
  77. source := utils.DATA_SOURCE_THS
  78. var req models.RefreshEdbInfoReq
  79. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  80. if err != nil {
  81. br.Msg = "参数解析异常!"
  82. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  83. return
  84. }
  85. if req.EdbCode == "" {
  86. br.Msg = "请输入指标编码!"
  87. br.ErrMsg = "请输入指标编码,指标编码为空"
  88. return
  89. }
  90. if req.EdbInfoId <= 0 {
  91. br.Msg = "请输入指标ID!"
  92. br.ErrMsg = "请输入指标ID"
  93. return
  94. }
  95. //期货数据,就默认到今天,特殊处理下
  96. endDate := time.Now().Format(utils.FormatDate)
  97. // 获取指标详情
  98. edbInfo, err := models.GetEdbInfoByEdbCode(source, req.EdbCode)
  99. if err != nil {
  100. br.Msg = "指标不存在!"
  101. br.ErrMsg = "指标不存在"
  102. return
  103. }
  104. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode
  105. if utils.Rc.IsExist(cacheKey) {
  106. br.Ret = 501
  107. br.Success = true
  108. br.Msg = "系统处理中,请稍后重试"
  109. return
  110. }
  111. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  112. defer func() {
  113. utils.Rc.Delete(cacheKey)
  114. }()
  115. dataItem, err := services.GetEdbDataFromThsDs(edbInfo.StockCode, edbInfo.IndicatorCode, utils.GetEdbRefreshStartDate(req.StartDate), endDate, edbInfo.TerminalCode)
  116. if err != nil {
  117. br.Msg = "获取指标信息失败!"
  118. br.ErrMsg = "获取指标信息失败 GetEdbDataFromThsDs,Err:" + err.Error()
  119. return
  120. }
  121. err = models.RefreshEdbDataFromThsDs(req.EdbInfoId, req.EdbCode, utils.GetEdbRefreshStartDate(req.StartDate), dataItem)
  122. if err != nil && err.Error() != utils.ErrNoRow() {
  123. br.Msg = "刷新指标信息失败!"
  124. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromThsDs,Err:" + err.Error()
  125. return
  126. }
  127. // 更新指标最大最小值
  128. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  129. if err != nil {
  130. br.Msg = errMsg
  131. br.ErrMsg = err.Error()
  132. return
  133. }
  134. // 更新ES
  135. go logic.UpdateEs(edbInfo.EdbInfoId)
  136. br.Ret = 200
  137. br.Success = true
  138. br.Msg = "获取成功"
  139. }
  140. //func init() {
  141. // //?EdbCode=s005696248&StartDate=2023-02-03&EndDate=2027-03-23
  142. // //edbCode := `s005696248`
  143. // //startDate := `2023-02-03`
  144. // //endDate := `2027-03-23`
  145. // //EdbCode=S011292460&StartDate=1993-03-23&EndDate=2027-03-23
  146. // edbCode := `S011292460`
  147. // startDate := `1993-03-23`
  148. // endDate := `2027-03-23`
  149. // //edbCode := `@CL0W.NMX`
  150. // //startDate := `20221218`
  151. // //endDate := `20230118`
  152. // list, err := services.GetEdbDataFromThsHttp(edbCode, startDate, endDate, 0)
  153. // //list, err := services.GetFutureGoodDataFromThsHttp(edbCode, startDate, endDate)
  154. //
  155. // //token, err := services.GetAccessToken()
  156. // if err != nil {
  157. // fmt.Println("err:", err)
  158. // return
  159. // }
  160. // fmt.Println(list)
  161. // //fmt.Println(token)
  162. //}