sports.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package sports
  2. import (
  3. "context"
  4. "encoding/json"
  5. "net/url"
  6. "time"
  7. "go-common/app/interface/main/activity/conf"
  8. "go-common/app/interface/main/activity/dao/sports"
  9. mdlsp "go-common/app/interface/main/activity/model/sports"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _qqNews = 1
  15. _qqMatch = 2
  16. _qqMatchTid = "14"
  17. _qqTeamRank = 3
  18. _qqRankTid = "34"
  19. _qqPlayerRank = 4
  20. _qqRoute = "matchUnion/fetchData"
  21. _newsRoute = "getQQNewsIndexAndItemsVerify"
  22. )
  23. // Service struct
  24. type Service struct {
  25. dao *sports.Dao
  26. }
  27. // New Service
  28. func New(c *conf.Config) (s *Service) {
  29. s = &Service{
  30. dao: sports.New(c),
  31. }
  32. go s.qqNews()
  33. go s.qqMatch()
  34. go s.qqTeamRank()
  35. go s.qqPlayerRank()
  36. return
  37. }
  38. func (s *Service) qqNews() {
  39. var (
  40. params = url.Values{}
  41. rs *mdlsp.QqRes
  42. err error
  43. c = context.Background()
  44. )
  45. for {
  46. for t := 0; t < conf.Conf.Rule.QqTryCount; t++ {
  47. if rs, err = s.dao.QqNews(c, params, _newsRoute); err != nil || rs == nil {
  48. continue
  49. }
  50. s.dao.SetQqCache(c, &rs.IDlist, _qqNews)
  51. break
  52. }
  53. time.Sleep(time.Duration(conf.Conf.Rule.TickQq))
  54. }
  55. }
  56. func (s *Service) qqMatch() {
  57. var (
  58. rs *json.RawMessage
  59. err error
  60. c = context.Background()
  61. params = url.Values{}
  62. )
  63. params.Set("tid", _qqMatchTid)
  64. params.Set("indexName", "col_4")
  65. params.Set("startTime", conf.Conf.Rule.QqStartTime)
  66. params.Set("endTime", conf.Conf.Rule.QqEndTime)
  67. for {
  68. for t := 0; t < conf.Conf.Rule.QqTryCount; t++ {
  69. if rs, err = s.dao.Qq(c, params, _qqRoute); err != nil || rs == nil || len(*rs) == 0 {
  70. continue
  71. }
  72. s.dao.SetQqCache(c, rs, _qqMatch)
  73. break
  74. }
  75. time.Sleep(time.Duration(conf.Conf.Rule.TickQq))
  76. }
  77. }
  78. func (s *Service) qqTeamRank() {
  79. var (
  80. rs *json.RawMessage
  81. err error
  82. c = context.Background()
  83. params = url.Values{}
  84. )
  85. params.Set("tid", _qqRankTid)
  86. params.Set("competitionId", "4")
  87. params.Set("seasonId", conf.Conf.Rule.QqYear)
  88. params.Set("valueType", "teamRank")
  89. params.Set("valueId", "teamRank")
  90. for {
  91. for t := 0; t < conf.Conf.Rule.QqTryCount; t++ {
  92. if rs, err = s.dao.Qq(c, params, _qqRoute); err != nil || rs == nil || len(*rs) == 0 {
  93. continue
  94. }
  95. s.dao.SetQqCache(c, rs, _qqTeamRank)
  96. break
  97. }
  98. time.Sleep(time.Duration(conf.Conf.Rule.TickQq))
  99. }
  100. }
  101. func (s *Service) qqPlayerRank() {
  102. var (
  103. rs *json.RawMessage
  104. err error
  105. c = context.Background()
  106. params = url.Values{}
  107. )
  108. params.Set("tid", _qqRankTid)
  109. params.Set("competitionId", "4")
  110. params.Set("seasonId", conf.Conf.Rule.PlayerYear)
  111. params.Set("valueType", "playerGoalRank")
  112. params.Set("valueId", "playerGoalRank")
  113. for {
  114. for t := 0; t < conf.Conf.Rule.QqTryCount; t++ {
  115. if rs, err = s.dao.Qq(c, params, _qqRoute); err != nil || rs == nil || len(*rs) == 0 {
  116. continue
  117. }
  118. s.dao.SetQqCache(c, rs, _qqPlayerRank)
  119. break
  120. }
  121. time.Sleep(time.Duration(conf.Conf.Rule.TickQq))
  122. }
  123. }
  124. // Qq get qq.
  125. func (s *Service) Qq(c context.Context, params url.Values, p *mdlsp.ParamQq) (rs *json.RawMessage, err error) {
  126. if p.Tp > 0 {
  127. if rs, err = s.dao.QqCache(c, p.Tp); err != nil {
  128. log.Error("s.dao.QqCache tp(%d) error(%v) ", p.Tp, err)
  129. }
  130. } else if rs, err = s.dao.Qq(c, params, p.Route); err != nil {
  131. sports.PromError("QQ接口错误", "s.dao.Qq route(%s) error(%v)", p.Route, err)
  132. }
  133. if rs == nil {
  134. err = ecode.ActivityServerTimeout
  135. }
  136. return
  137. }
  138. // News get qq news.
  139. func (s *Service) News(c context.Context, params url.Values, p *mdlsp.ParamNews) (rs *mdlsp.QqRes, err error) {
  140. if rs, err = s.dao.QqNews(c, params, p.Route); err != nil {
  141. sports.PromError("QQ接口错误", "s.dao.Qq error(%v)", err)
  142. }
  143. if rs == nil {
  144. err = ecode.ActivityServerTimeout
  145. }
  146. return
  147. }