bnj.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package bnj
  2. import (
  3. "context"
  4. "strings"
  5. "sync/atomic"
  6. "time"
  7. "go-common/library/sync/errgroup.v2"
  8. "go-common/app/interface/main/activity/model/bnj"
  9. arcmdl "go-common/app/service/main/archive/api"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _rewardStepOne = 1
  15. _rewardStepThree = 3
  16. _lastCD = 300
  17. )
  18. // PreviewInfo preview info
  19. func (s *Service) PreviewInfo(c context.Context, mid int64) *bnj.PreviewInfo {
  20. data := &bnj.PreviewInfo{
  21. ActID: s.c.Bnj2019.ActID,
  22. SubID: s.c.Bnj2019.SubID,
  23. }
  24. // TODO del admin check
  25. if err := s.bnjAdminCheck(mid); err != nil {
  26. return data
  27. }
  28. for _, v := range s.c.Bnj2019.Reward {
  29. data.RewardStep = append(data.RewardStep, v.Condition)
  30. }
  31. if s.timeFinish != 0 {
  32. data.TimelinePic = s.c.Bnj2019.TimelinePic
  33. data.H5TimelinePic = s.c.Bnj2019.H5TimelinePic
  34. }
  35. if s.c.Bnj2019.GameCancel != 0 {
  36. data.GameCancel = 1
  37. }
  38. now := time.Now().Unix()
  39. group := errgroup.WithCancel(c)
  40. if mid > 0 && len(s.c.Bnj2019.Reward) > 0 {
  41. for _, v := range s.c.Bnj2019.Reward {
  42. if v.Step > 0 {
  43. step := v.Step
  44. group.Go(func(ctx context.Context) error {
  45. if check, e := s.dao.HasReward(ctx, mid, s.c.Bnj2019.SubID, step); e != nil {
  46. log.Error("Reward s.dao.HasReward(mid:%d,step:%d) error(%v) check(%v)", mid, step, e, check)
  47. } else if check {
  48. switch step {
  49. case _rewardStepOne:
  50. data.HasRewardFirst = 1
  51. case _rewardStepThree:
  52. data.HasRewardSecond = 1
  53. }
  54. }
  55. return nil
  56. })
  57. }
  58. }
  59. }
  60. if err := group.Wait(); err != nil {
  61. log.Error("PreviewInfo group wait error(%v)", err)
  62. }
  63. arcs := s.previewArcs
  64. for _, v := range s.c.Bnj2019.Info {
  65. if v.Publish.Unix() < now {
  66. if arc, ok := arcs[v.Aid]; ok && arc.IsNormal() {
  67. tmp := &bnj.Info{Nav: v.Nav, Pic: v.Pic, H5Pic: v.H5Pic, Detail: v.Detail, H5Detail: v.H5Detail, Arc: &arcmdl.Arc{Aid: v.Aid}}
  68. arc.Pic = strings.Replace(arc.Pic, "http://", "//", 1)
  69. if v.Nickname != "" {
  70. arc.Author.Name = arc.Author.Name + "&" + v.Nickname
  71. }
  72. tmp.Arc = arc
  73. data.Info = append(data.Info, tmp)
  74. }
  75. }
  76. }
  77. if len(data.Info) == 0 {
  78. data.Info = make([]*bnj.Info, 0)
  79. }
  80. return data
  81. }
  82. // Timeline only return timeline and game cancel.
  83. func (s *Service) Timeline(c context.Context, mid int64) *bnj.Timeline {
  84. data := new(bnj.Timeline)
  85. // TODO delete admin check
  86. if err := s.bnjAdminCheck(mid); err != nil {
  87. return data
  88. }
  89. if s.timeFinish != 0 {
  90. data.TimelinePic = s.c.Bnj2019.TimelinePic
  91. data.H5TimelinePic = s.c.Bnj2019.H5TimelinePic
  92. }
  93. if s.c.Bnj2019.GameCancel != 0 {
  94. data.GameCancel = 1
  95. }
  96. data.LikeCount = s.likeCount
  97. return data
  98. }
  99. // TimeReset reset less time.
  100. func (s *Service) TimeReset(c context.Context, mid int64) (ttl int64, err error) {
  101. if time.Now().Unix() < s.c.Bnj2019.Start.Unix() {
  102. err = ecode.ActivityNotStart
  103. return
  104. }
  105. if s.timeFinish != 0 {
  106. err = ecode.ActivityBnjTimeFinish
  107. return
  108. }
  109. var value bool
  110. if value, err = s.dao.CacheResetCD(c, mid, s.resetCD); err != nil {
  111. log.Error("TimeReset s.dao.CacheResetCD(%d) error(%v) value(%v)", mid, err, value)
  112. err = nil
  113. return
  114. }
  115. if !value {
  116. if ttl, err = s.dao.TTLResetCD(c, mid); err != nil {
  117. log.Error("TimeReset s.dao.TTLResetCD(%d) error(%v) value(%v)", mid, err)
  118. err = nil
  119. }
  120. return
  121. }
  122. if s.timeReset == 0 {
  123. atomic.StoreInt64(&s.resetMid, mid)
  124. atomic.StoreInt64(&s.timeReset, 1)
  125. }
  126. return
  127. }
  128. // DelTime .
  129. func (s *Service) DelTime(c context.Context, key string) (err error) {
  130. switch key {
  131. case "time_finish":
  132. if err = s.dao.DelCacheTimeFinish(c); err != nil {
  133. log.Error("DelTime DelCacheTimeFinish error(%v)", err)
  134. }
  135. case "time_less":
  136. if err = s.dao.DelCacheTimeLess(c); err != nil {
  137. log.Error("DelTime DelCacheTimeLess error(%v)", err)
  138. }
  139. }
  140. return
  141. }
  142. func (s *Service) bnjAdminCheck(mid int64) (err error) {
  143. if s.c.Bnj2019.AdminCheck != 0 {
  144. if _, ok := s.bnjAdmins[mid]; !ok {
  145. err = ecode.AccessDenied
  146. }
  147. }
  148. return
  149. }