base_from_ths_ds.go 5.1 KB

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