elec.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package grpc
  2. import (
  3. "context"
  4. "go-common/app/service/main/ugcpay-rank/api/v1"
  5. "go-common/app/service/main/ugcpay-rank/internal/conf"
  6. "go-common/app/service/main/ugcpay-rank/internal/model"
  7. "go-common/app/service/main/ugcpay-rank/internal/service"
  8. "go-common/app/service/main/ugcpay-rank/internal/service/rank"
  9. "go-common/library/log"
  10. "go-common/library/net/rpc/warden"
  11. "github.com/golang/protobuf/ptypes/empty"
  12. )
  13. // New Identify warden rpc server
  14. func New(cfg *warden.ServerConfig, s *service.Service) *warden.Server {
  15. w := warden.NewServer(cfg)
  16. v1.RegisterUGCPayRankServer(w.Server(), &UGCRankServer{s})
  17. ws, err := w.Start()
  18. if err != nil {
  19. panic(err)
  20. }
  21. return ws
  22. }
  23. // UGCRankServer .
  24. type UGCRankServer struct {
  25. svr *service.Service
  26. }
  27. var _ v1.UGCPayRankServer = &UGCRankServer{}
  28. // RankElecAllAV .
  29. func (u *UGCRankServer) RankElecAllAV(ctx context.Context, req *v1.RankElecAVReq) (resp *v1.RankElecAVResp, err error) {
  30. if req.AVID <= 0 {
  31. return
  32. }
  33. if req.RankSize <= 0 || req.RankSize > conf.Conf.Biz.ElecAVRankSize {
  34. req.RankSize = conf.Conf.Biz.ElecAVRankSize
  35. }
  36. r, err := u.svr.ElecTotalRankAV(ctx, req.UPMID, req.AVID, req.RankSize)
  37. if err != nil {
  38. return
  39. }
  40. resp = &v1.RankElecAVResp{
  41. AV: r,
  42. }
  43. return
  44. }
  45. // RankElecMonthAV .
  46. func (u *UGCRankServer) RankElecMonthAV(ctx context.Context, req *v1.RankElecAVReq) (resp *v1.RankElecAVResp, err error) {
  47. if req.AVID <= 0 {
  48. return
  49. }
  50. if req.RankSize <= 0 || req.RankSize > conf.Conf.Biz.ElecAVRankSize {
  51. req.RankSize = conf.Conf.Biz.ElecAVRankSize
  52. }
  53. r, err := u.svr.ElecMonthRankAV(ctx, req.UPMID, req.AVID, req.RankSize)
  54. if err != nil {
  55. return
  56. }
  57. resp = &v1.RankElecAVResp{
  58. AV: r,
  59. }
  60. return
  61. }
  62. // RankElecMonthUP .
  63. func (u *UGCRankServer) RankElecMonthUP(ctx context.Context, req *v1.RankElecUPReq) (resp *v1.RankElecUPResp, err error) {
  64. if req.UPMID <= 0 {
  65. return
  66. }
  67. if req.RankSize <= 0 || req.RankSize > conf.Conf.Biz.ElecUPRankSize {
  68. req.RankSize = conf.Conf.Biz.ElecUPRankSize
  69. }
  70. r, err := u.svr.ElecMonthRankUP(ctx, req.UPMID, req.RankSize)
  71. if err != nil {
  72. return
  73. }
  74. resp = &v1.RankElecUPResp{
  75. UP: r,
  76. }
  77. return
  78. }
  79. // RankElecMonth .
  80. func (u *UGCRankServer) RankElecMonth(ctx context.Context, req *v1.RankElecMonthReq) (resp *v1.RankElecMonthResp, err error) {
  81. var (
  82. up *model.RankElecUPProto
  83. av *model.RankElecAVProto
  84. )
  85. if req.RankSize <= 0 || req.RankSize > conf.Conf.Biz.ElecAVRankSize {
  86. req.RankSize = conf.Conf.Biz.ElecAVRankSize
  87. }
  88. if req.UPMID > 0 {
  89. if up, err = u.svr.ElecMonthRankUP(ctx, req.UPMID, req.RankSize); err != nil {
  90. return
  91. }
  92. }
  93. if req.AVID > 0 {
  94. if av, err = u.svr.ElecMonthRankAV(ctx, req.UPMID, req.AVID, req.RankSize); err != nil {
  95. return
  96. }
  97. }
  98. resp = &v1.RankElecMonthResp{
  99. UP: up,
  100. AV: av,
  101. }
  102. return
  103. }
  104. // RankElecUpdateOrder .
  105. func (u *UGCRankServer) RankElecUpdateOrder(ctx context.Context, req *v1.RankElecUpdateOrderReq) (reply *empty.Empty, err error) {
  106. reply = &empty.Empty{}
  107. var (
  108. prs = make([]rank.PrepRank, 0)
  109. elecMonthlyPrepUPRank = rank.NewElecPrepUPRank(req.UPMID, conf.Conf.Biz.ElecUPRankSize, req.Ver, u.svr.Dao)
  110. // elecTotalPrepUPRank = rank.NewElecPrepUPRank(req.UPMID, conf.Conf.Biz.ElecUPRankSize, 0, u.svr.Dao) 因为敖厂长,所以不能及时更新up总榜,否则很容易db超时
  111. )
  112. prs = append(prs, elecMonthlyPrepUPRank)
  113. if req.AVID != 0 {
  114. elecMonthlyPrepAVRank := rank.NewElecPrepAVRank(req.AVID, conf.Conf.Biz.ElecAVRankSize, req.Ver, u.svr.Dao)
  115. elecTotalPrepAVRank := rank.NewElecPrepAVRank(req.AVID, conf.Conf.Biz.ElecAVRankSize, 0, u.svr.Dao)
  116. prs = append(prs, elecMonthlyPrepAVRank, elecTotalPrepAVRank)
  117. }
  118. // 更新缓存Prep_Rank
  119. u.svr.Asyncer.Do(ctx, func(ctx context.Context) {
  120. for _, pr := range prs {
  121. if theErr := u.svr.UpdateElecPrepRankFromOrder(ctx, pr, req.PayMID, req.Fee); theErr != nil {
  122. log.Error("update prep_rank failed, pr: %s, payMID: %d, fee: %d, err: %+v", pr, req.PayMID, req.Fee, theErr)
  123. continue
  124. }
  125. }
  126. })
  127. return
  128. }
  129. // RankElecUpdateMessage .
  130. func (u *UGCRankServer) RankElecUpdateMessage(ctx context.Context, req *v1.RankElecUpdateMessageReq) (reply *empty.Empty, err error) {
  131. reply = &empty.Empty{}
  132. // 更新缓存Prep_Rank
  133. var (
  134. prs = make([]rank.PrepRank, 0)
  135. )
  136. if req.AVID != 0 {
  137. prs = append(prs, rank.NewElecPrepAVRank(req.AVID, conf.Conf.Biz.ElecAVRankSize, req.Ver, u.svr.Dao))
  138. prs = append(prs, rank.NewElecPrepAVRank(req.AVID, conf.Conf.Biz.ElecAVRankSize, 0, u.svr.Dao))
  139. }
  140. if req.UPMID != 0 {
  141. prs = append(prs, rank.NewElecPrepUPRank(req.UPMID, conf.Conf.Biz.ElecUPRankSize, req.Ver, u.svr.Dao))
  142. }
  143. u.svr.Asyncer.Do(ctx, func(ctx context.Context) {
  144. for _, pr := range prs {
  145. if theErr := u.svr.UpdateElecPrepRankFromMessage(ctx, pr, req.PayMID, req.Message, req.Hidden); theErr != nil {
  146. log.Error("update prep_rank failed, pr: %s, payMID: %d, message: %s, hidden: %t, err: %+v", pr, req.PayMID, req.Message, req.Hidden, theErr)
  147. continue
  148. }
  149. }
  150. })
  151. return
  152. }