achievement.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package bws
  2. import (
  3. "context"
  4. "time"
  5. bwsmdl "go-common/app/interface/main/activity/model/bws"
  6. suitmdl "go-common/app/service/main/usersuit/model"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. )
  11. // Award achievement award
  12. func (s *Service) Award(c context.Context, loginMid int64, p *bwsmdl.ParamAward) (err error) {
  13. var (
  14. userAchieves []*bwsmdl.UserAchieve
  15. userAward int64 = -1
  16. )
  17. if _, ok := s.awardMids[loginMid]; !ok {
  18. err = ecode.ActivityNotAwardAdmin
  19. return
  20. }
  21. if p.Key == "" {
  22. if p.Key, err = s.midToKey(c, p.Mid); err != nil {
  23. return
  24. }
  25. }
  26. if userAchieves, err = s.dao.UserAchieves(c, p.Bid, p.Key); err != nil {
  27. err = ecode.ActivityAchieveFail
  28. return
  29. }
  30. if len(userAchieves) == 0 {
  31. err = ecode.ActivityNoAchieve
  32. return
  33. }
  34. for _, v := range userAchieves {
  35. if v.Aid == p.Aid {
  36. userAward = v.Award
  37. break
  38. }
  39. }
  40. if userAward == -1 {
  41. err = ecode.ActivityNoAchieve
  42. return
  43. } else if userAward == _noAward {
  44. err = ecode.ActivityNoAward
  45. return
  46. } else if userAward == _awardAlready {
  47. err = ecode.ActivityAwardAlready
  48. return
  49. }
  50. if err = s.dao.Award(c, p.Key, p.Aid); err != nil {
  51. log.Error("s.dao.Award key(%s) error(%v)", p.Key, err)
  52. }
  53. s.dao.DelCacheUserAchieves(c, p.Bid, p.Key)
  54. return
  55. }
  56. // Achievements achievements list
  57. func (s *Service) Achievements(c context.Context, p *bwsmdl.ParamID) (rs *bwsmdl.Achievements, err error) {
  58. var mapCnt map[int64]int64
  59. if rs, err = s.dao.Achievements(c, p.Bid); err != nil || rs == nil || len(rs.Achievements) == 0 {
  60. log.Error("s.dao.Achievements error(%v)", err)
  61. err = ecode.ActivityAchieveFail
  62. return
  63. }
  64. if mapCnt, err = s.countAchieves(c, p.Bid, p.Day); err != nil || len(mapCnt) == 0 {
  65. err = nil
  66. return
  67. }
  68. for _, achieve := range rs.Achievements {
  69. achieve.UserCount = mapCnt[achieve.ID]
  70. }
  71. return
  72. }
  73. func (s *Service) countAchieves(c context.Context, bid int64, day string) (rs map[int64]int64, err error) {
  74. var countAchieves []*bwsmdl.CountAchieves
  75. if day == "" {
  76. day = today()
  77. }
  78. if countAchieves, err = s.dao.AchieveCounts(c, bid, day); err != nil {
  79. log.Error("s.dao.RawCountAchieves error(%v)", err)
  80. return
  81. }
  82. rs = make(map[int64]int64, len(countAchieves))
  83. for _, countAchieve := range countAchieves {
  84. rs[countAchieve.Aid] = countAchieve.Count
  85. }
  86. return
  87. }
  88. // Achievement Achievement
  89. func (s *Service) Achievement(c context.Context, p *bwsmdl.ParamID) (rs *bwsmdl.Achievement, err error) {
  90. var (
  91. achieves *bwsmdl.Achievements
  92. )
  93. if achieves, err = s.dao.Achievements(c, p.Bid); err != nil || achieves == nil || len(achieves.Achievements) == 0 {
  94. log.Error("s.dao.Achievements error(%v)", err)
  95. err = ecode.ActivityAchieveFail
  96. return
  97. }
  98. for _, Achievement := range achieves.Achievements {
  99. if Achievement.ID == p.ID {
  100. rs = Achievement
  101. break
  102. }
  103. }
  104. if rs == nil {
  105. err = ecode.ActivityIDNotExists
  106. }
  107. return
  108. }
  109. func (s *Service) userAchieves(c context.Context, bid int64, key string) (res []*bwsmdl.UserAchieveDetail, err error) {
  110. var (
  111. usAchieves []*bwsmdl.UserAchieve
  112. achieves *bwsmdl.Achievements
  113. )
  114. if usAchieves, err = s.dao.UserAchieves(c, bid, key); err != nil {
  115. err = ecode.ActivityUserAchieveFail
  116. return
  117. }
  118. if len(usAchieves) == 0 {
  119. return
  120. }
  121. if achieves, err = s.dao.Achievements(c, bid); err != nil || achieves == nil || len(achieves.Achievements) == 0 {
  122. err = ecode.ActivityAchieveFail
  123. return
  124. }
  125. achievesMap := make(map[int64]*bwsmdl.Achievement, len(achieves.Achievements))
  126. for _, v := range achieves.Achievements {
  127. achievesMap[v.ID] = v
  128. }
  129. for _, v := range usAchieves {
  130. detail := &bwsmdl.UserAchieveDetail{UserAchieve: v}
  131. if achieve, ok := achievesMap[v.Aid]; ok {
  132. detail.Name = achieve.Name
  133. detail.Icon = achieve.Icon
  134. detail.Dic = achieve.Dic
  135. detail.LockType = achieve.LockType
  136. detail.Unlock = achieve.Unlock
  137. detail.Bid = achieve.Bid
  138. detail.IconBig = achieve.IconBig
  139. detail.IconActive = achieve.IconActive
  140. detail.IconActiveBig = achieve.IconActiveBig
  141. detail.SuitID = achieve.SuitID
  142. }
  143. res = append(res, detail)
  144. }
  145. return
  146. }
  147. func (s *Service) addAchieve(c context.Context, mid int64, achieve *bwsmdl.Achievement, key string) (err error) {
  148. var uaID int64
  149. if uaID, err = s.dao.AddUserAchieve(c, achieve.Bid, achieve.ID, achieve.Award, key); err != nil {
  150. err = ecode.ActivityAddAchieveFail
  151. return
  152. }
  153. if err = s.dao.AppendUserAchievesCache(c, achieve.Bid, key, &bwsmdl.UserAchieve{ID: uaID, Aid: achieve.ID, Award: achieve.Award, Ctime: xtime.Time(time.Now().Unix())}); err != nil {
  154. return
  155. }
  156. s.cache.Do(c, func(c context.Context) {
  157. s.dao.IncrCacheAchieveCounts(c, achieve.Bid, achieve.ID, today())
  158. var (
  159. keyID int64
  160. e error
  161. )
  162. if mid == 0 {
  163. if mid, keyID, e = s.keyToMid(c, key); e != nil || mid == 0 {
  164. log.Warn("Lottery keyID(%d) key(%s) error(%v)", keyID, key, e)
  165. }
  166. }
  167. if mid > 0 {
  168. if achieve.SuitID > 0 {
  169. arg := &suitmdl.ArgGrantByMids{Mids: []int64{mid}, Pid: achieve.SuitID, Expire: s.c.Rule.BwsSuitExpire}
  170. if e := s.suitRPC.GrantByMids(c, arg); e != nil {
  171. log.Error("addAchieve s.suit.GrantByMids(%d,%d) error(%v)", mid, achieve.SuitID, e)
  172. }
  173. log.Warn("Suit mid(%d) suitID(%d)", mid, achieve.SuitID)
  174. }
  175. if _, ok := s.lotteryAids[achieve.ID]; ok {
  176. s.dao.AddLotteryMidCache(c, achieve.ID, mid)
  177. }
  178. }
  179. })
  180. return
  181. }