server.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package grpc
  2. import (
  3. "context"
  4. pb "go-common/app/service/main/thumbup/api"
  5. "go-common/app/service/main/thumbup/model"
  6. "go-common/app/service/main/thumbup/service"
  7. "go-common/library/net/rpc/warden"
  8. "github.com/golang/protobuf/ptypes/empty"
  9. )
  10. // New Coin warden rpc server
  11. func New(c *warden.ServerConfig, svr *service.Service) *warden.Server {
  12. ws := warden.NewServer(c)
  13. pb.RegisterThumbupServer(ws.Server(), &server{s: svr})
  14. ws, err := ws.Start()
  15. if err != nil {
  16. panic(err)
  17. }
  18. return ws
  19. }
  20. type server struct {
  21. s *service.Service
  22. }
  23. func (r server) Like(c context.Context, req *pb.LikeReq) (reply *pb.LikeReply, err error) {
  24. res, err := r.s.Like(c, req.Business, req.Mid, req.OriginID, req.MessageID, int8(req.Action), req.UpMid)
  25. reply = &pb.LikeReply{
  26. OriginID: res.OriginID,
  27. MessageID: res.ID,
  28. LikeNumber: res.Likes,
  29. DislikeNumber: res.Dislikes,
  30. }
  31. return
  32. }
  33. func (r server) Stats(c context.Context, req *pb.StatsReq) (reply *pb.StatsReply, err error) {
  34. res, err := r.s.StatsWithLike(c, req.Business, req.Mid, req.OriginID, req.MessageIds)
  35. reply = &pb.StatsReply{Stats: map[int64]*pb.StatState{}}
  36. for name, item := range res {
  37. reply.Stats[name] = &pb.StatState{
  38. OriginID: item.OriginID,
  39. MessageID: item.ID,
  40. LikeNumber: item.Likes,
  41. DislikeNumber: item.Dislikes,
  42. LikeState: pb.State(item.LikeState),
  43. }
  44. }
  45. return
  46. }
  47. func (r server) MultiStats(c context.Context, req *pb.MultiStatsReq) (reply *pb.MultiStatsReply, err error) {
  48. arg := &model.MultiBusiness{
  49. Mid: req.Mid,
  50. Businesses: make(map[string][]*model.MultiBusinessItem),
  51. }
  52. for name, b := range req.Business {
  53. for _, i := range b.Records {
  54. arg.Businesses[name] = append(arg.Businesses[name], &model.MultiBusinessItem{
  55. OriginID: i.OriginID,
  56. MessageID: i.MessageID,
  57. })
  58. }
  59. }
  60. res, err := r.s.MultiStatsWithLike(c, arg)
  61. reply = &pb.MultiStatsReply{}
  62. if res != nil {
  63. reply.Business = make(map[string]*pb.MultiStatsReply_Records)
  64. for k, v := range res {
  65. items := &pb.MultiStatsReply_Records{
  66. Records: make(map[int64]*pb.StatState),
  67. }
  68. for id, state := range v {
  69. items.Records[id] = &pb.StatState{
  70. OriginID: state.OriginID,
  71. MessageID: state.ID,
  72. LikeNumber: state.Likes,
  73. DislikeNumber: state.Dislikes,
  74. LikeState: pb.State(state.LikeState),
  75. }
  76. }
  77. reply.Business[k] = items
  78. }
  79. }
  80. return
  81. }
  82. func (r server) HasLike(c context.Context, req *pb.HasLikeReq) (reply *pb.HasLikeReply, err error) {
  83. _, res, err := r.s.HasLike(c, req.Business, req.Mid, req.MessageIds)
  84. reply = &pb.HasLikeReply{States: res}
  85. return
  86. }
  87. func (r server) UserLikes(c context.Context, req *pb.UserLikesReq) (reply *pb.UserLikesReply, err error) {
  88. res, err := r.s.UserTotalLike(c, req.Business, req.Mid, int(req.Pn), int(req.Ps))
  89. reply = &pb.UserLikesReply{}
  90. if res != nil {
  91. reply.Total = int64(res.Total)
  92. for _, item := range res.List {
  93. reply.Items = append(reply.Items, &pb.ItemRecord{
  94. MessageID: item.MessageID,
  95. Time: item.Time,
  96. })
  97. }
  98. }
  99. return
  100. }
  101. func (r server) ItemLikes(c context.Context, req *pb.ItemLikesReq) (reply *pb.ItemLikesReply, err error) {
  102. res, err := r.s.ItemLikes(c, req.Business, req.OriginID, req.MessageID, int(req.Pn), int(req.Ps), req.LastMid)
  103. reply = &pb.ItemLikesReply{}
  104. for _, item := range res {
  105. reply.Users = append(reply.Users, &pb.UserRecord{
  106. Mid: item.Mid,
  107. Time: item.Time,
  108. })
  109. }
  110. return
  111. }
  112. func (r server) UpdateCount(c context.Context, req *pb.UpdateCountReq) (reply *empty.Empty, err error) {
  113. reply = &empty.Empty{}
  114. err = r.s.UpdateCount(c, req.Business, req.OriginID, req.MessageID, req.LikeChange, req.DislikeChange, req.IP, req.Operator)
  115. return
  116. }
  117. func (r server) RawStat(c context.Context, req *pb.RawStatReq) (reply *pb.RawStatReply, err error) {
  118. res, err := r.s.RawStats(c, req.Business, req.OriginID, req.MessageID)
  119. reply = &pb.RawStatReply{
  120. OriginID: res.OriginID,
  121. MessageID: res.ID,
  122. LikeNumber: res.Likes,
  123. DislikeNumber: res.Dislikes,
  124. LikeChange: res.LikesChange,
  125. DislikeChange: res.DislikesChange,
  126. }
  127. return
  128. }