bnj.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package like
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/interface/main/activity/model/bnj"
  6. "go-common/app/interface/main/activity/model/like"
  7. suitmdl "go-common/app/service/main/usersuit/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. // Reward get bnj preview reward.
  12. func (s *Service) Reward(c context.Context, mid int64, step int) (err error) {
  13. reward, ok := s.reward[step]
  14. if !ok {
  15. err = ecode.RequestErr
  16. return
  17. }
  18. var (
  19. likeActs map[int64]int
  20. likeScore map[int64]int64
  21. check bool
  22. )
  23. actID := s.c.Bnj2019.ActID
  24. subID := s.c.Bnj2019.SubID
  25. if likeScore, err = s.dao.LikeActLidCounts(c, []int64{subID}); err != nil {
  26. log.Error("Reward s.dao.LikeActLidCounts(subID:%d) error(%+v)", subID, err)
  27. return
  28. }
  29. if score, ok := likeScore[subID]; !ok || score < reward.Condition {
  30. err = ecode.ActivityBnjSubLow
  31. return
  32. }
  33. if likeActs, err = s.dao.LikeActs(c, actID, mid, []int64{subID}); err != nil {
  34. log.Error("Reward s.dao.LikeActs(subID:%d,actID:%d,mid:%d) error(%+v)", subID, actID, mid, err)
  35. return
  36. }
  37. if isSub, ok := likeActs[subID]; !ok || isSub == 0 {
  38. if _, err = s.LikeAct(c, &like.ParamAddLikeAct{Sid: actID, Lid: subID, Score: 1}, mid); err != nil {
  39. return
  40. }
  41. }
  42. // check has reward
  43. if check, err = s.bnjDao.CacheHasReward(c, mid, subID, step); err != nil || !check {
  44. log.Error("Reward s.dao.CacheHasReward(mid:%d,subID:%d,step:%d) error(%v) check(%v)", mid, subID, step, err, check)
  45. err = ecode.ActivityBnjHasReward
  46. return
  47. }
  48. switch reward.RewardType {
  49. case bnj.RewardTypePendant:
  50. rewardID, e := strconv.ParseInt(reward.RewardID, 10, 64)
  51. if e != nil {
  52. err = ecode.ActivityRewardConfErr
  53. return
  54. }
  55. err = s.suit.GrantByMids(c, &suitmdl.ArgGrantByMids{Mids: []int64{mid}, Pid: rewardID, Expire: reward.Expire})
  56. case bnj.RewardTypeCoupon:
  57. err = s.bnjDao.GrantCoupon(c, mid, reward.RewardID)
  58. // TODO check err code
  59. }
  60. if err != nil {
  61. log.Error("Reward (%+v) error(%+v)", reward, err)
  62. err = ecode.ActivityBnjRewardFail
  63. if e := s.bnjDao.DelCacheHasReward(c, mid, subID, step); e != nil {
  64. log.Error("s.dao.DelCacheHasReward(%+v) error(%v)", reward, e)
  65. }
  66. }
  67. return
  68. }