activity.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package service
  2. import (
  3. "context"
  4. "sort"
  5. "time"
  6. "go-common/app/interface/main/growup/model"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. const (
  11. _canEnrol = iota // 未报名,可以报名
  12. _hasEnrol // 已报名
  13. _win // 中奖
  14. _notEnrol // 不能报名
  15. _enrolEnd // 报名已结束
  16. )
  17. // ShowActivity show creative_activity
  18. func (s *Service) ShowActivity(c context.Context, mid, activityID int64) (ac *model.CActivity, err error) {
  19. ac, err = s.dao.GetActivity(c, activityID)
  20. if err != nil {
  21. log.Error("s.dao.GetActivity error(%v)", err)
  22. return
  23. }
  24. enrolNum, winNum, mids, upState, err := s.handleUpActivity(c, mid, activityID)
  25. if err != nil {
  26. log.Error("s.handleUpActivity error(%v)", err)
  27. return
  28. }
  29. // 当前时间是否可以展示
  30. now := xtime.Time(time.Now().Unix())
  31. if ac.UpdatePage == 1 && now >= ac.ProgressStart && now <= ac.ProgressEnd {
  32. ac.ProgressState = 1
  33. ac.Enrollment = enrolNum
  34. ac.WinNum = winNum
  35. // 2: 中奖类型为排序型
  36. if ac.WinType == 2 {
  37. ac.Ranking, err = s.getActUpInfo(c, mids)
  38. if err != nil {
  39. log.Error("s.getActUpInfo error(%v)", err)
  40. return
  41. }
  42. }
  43. }
  44. if !(ac.BonusQuery == 1 && now >= ac.BonusQueryStart && now <= ac.BonusQueryEnd) {
  45. ac.BonusQuery = 0
  46. }
  47. // 获取up主当前状态
  48. ac.SignUpState, err = s.getSignUpState(c, mid, upState, ac)
  49. if err != nil {
  50. log.Error("s.getSignUpState error(%v)", err)
  51. }
  52. return
  53. }
  54. // get mid name and face
  55. func (s *Service) getActUpInfo(c context.Context, mids []int64) (upInfos []*model.ActUpInfo, err error) {
  56. upInfoMap, err := s.dao.AccountInfos(c, mids)
  57. if err != nil {
  58. return
  59. }
  60. upInfos = make([]*model.ActUpInfo, len(upInfoMap))
  61. for i := 0; i < len(mids); i++ {
  62. upInfos[i] = upInfoMap[mids[i]]
  63. }
  64. return
  65. }
  66. func (s *Service) getSignUpState(c context.Context, mid int64, upState int, ac *model.CActivity) (signUpState int, err error) {
  67. now := xtime.Time(time.Now().Unix())
  68. // 报名未开始,不能报名
  69. if now < ac.SignUpStart {
  70. signUpState = _notEnrol
  71. return
  72. }
  73. // 报名已结束并且未中奖
  74. if now > ac.SignUpEnd && upState != _win {
  75. upState = _enrolEnd
  76. }
  77. // 已报名
  78. if upState >= _hasEnrol {
  79. signUpState = upState
  80. return
  81. }
  82. // 签约结束时间 >= 报名结束时间, 任何人都可以报名
  83. if ac.SignedEnd >= ac.SignUpEnd {
  84. signUpState = _canEnrol
  85. } else {
  86. var signedAt xtime.Time
  87. signedAt, err = s.dao.GetUpSignedAt(c, "up_info_video", mid)
  88. if err != nil {
  89. return
  90. }
  91. if signedAt >= ac.SignedStart && signedAt <= ac.SignedEnd {
  92. signUpState = _canEnrol
  93. } else {
  94. signUpState = _notEnrol
  95. }
  96. }
  97. return
  98. }
  99. func (s *Service) handleUpActivity(c context.Context, mid, activityID int64) (enrol, win int, mids []int64, upState int, err error) {
  100. ups, err := s.dao.ListUpActivity(c, activityID)
  101. if err != nil {
  102. return
  103. }
  104. sort.Slice(ups, func(i, j int) bool {
  105. return ups[i].Rank < ups[j].Rank
  106. })
  107. mids = make([]int64, 0)
  108. for _, up := range ups {
  109. if up.State >= _win {
  110. win++
  111. enrol++
  112. mids = append(mids, up.MID)
  113. } else if up.State == _hasEnrol {
  114. enrol++
  115. }
  116. if up.MID == mid {
  117. upState = up.State
  118. // 已发奖
  119. if upState == 3 {
  120. upState = 2
  121. }
  122. }
  123. }
  124. return
  125. }
  126. // SignUpActivity up sign up activity
  127. func (s *Service) SignUpActivity(c context.Context, mid, activityID int64) (err error) {
  128. nickname, _, err := s.dao.CategoryInfo(c, mid)
  129. if err != nil {
  130. log.Error("s.dao.CategoryInfo error(%v)", err)
  131. return
  132. }
  133. upBonus := &model.UpBonus{
  134. MID: mid,
  135. ActivityID: activityID,
  136. Nickname: nickname,
  137. State: 1,
  138. SignUpTime: xtime.Time(time.Now().Unix()),
  139. }
  140. if _, err = s.dao.SignUpActivity(c, upBonus); err != nil {
  141. log.Error("s.dao.SignUpActivity error(%v)", err)
  142. }
  143. return
  144. }