server.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Package server generate by warden_gen
  2. package server
  3. import (
  4. "context"
  5. "go-common/app/service/main/card/api/grpc/v1"
  6. "go-common/app/service/main/card/model"
  7. service "go-common/app/service/main/card/service"
  8. "go-common/library/net/rpc/warden"
  9. )
  10. // New Card warden rpc server
  11. func New(c *warden.ServerConfig, svr *service.Service) *warden.Server {
  12. ws := warden.NewServer(c)
  13. v1.RegisterCardServer(ws.Server(), &server{svr})
  14. ws, err := ws.Start()
  15. if err != nil {
  16. panic(err)
  17. }
  18. return ws
  19. }
  20. type server struct {
  21. svr *service.Service
  22. }
  23. var _ v1.CardServer = &server{}
  24. // Card get card info.
  25. func (s *server) Card(c context.Context, req *v1.CardReq) (*v1.CardReply, error) {
  26. var res *v1.ModelCard
  27. if cd := s.svr.Card(c, req.Id); cd != nil {
  28. res = convertModelCard(cd)
  29. }
  30. return &v1.CardReply{Data_0: res}, nil
  31. }
  32. // CardHots get card hots.
  33. func (s *server) CardHots(c context.Context, req *v1.CardHotsReq) (*v1.CardHotsReply, error) {
  34. cs := s.svr.CardHots(c)
  35. ls := make([]*v1.ModelCard, len(cs))
  36. for i, v := range cs {
  37. ls[i] = convertModelCard(v)
  38. }
  39. return &v1.CardHotsReply{Data_0: ls}, nil
  40. }
  41. // CardsByGid get card by gid.
  42. func (s *server) CardsByGid(c context.Context, req *v1.CardsByGidReq) (*v1.CardsByGidReply, error) {
  43. cs := s.svr.CardsByGid(c, req.Gid)
  44. ls := make([]*v1.ModelCard, len(cs))
  45. for i, v := range cs {
  46. ls[i] = convertModelCard(v)
  47. }
  48. return &v1.CardsByGidReply{Data_0: ls}, nil
  49. }
  50. // Equip user equip card.
  51. func (s *server) Equip(c context.Context, req *v1.EquipReq) (*v1.EquipReply, error) {
  52. return nil, s.svr.Equip(c, &model.ArgEquip{
  53. Mid: req.Arg.Mid,
  54. CardID: req.Arg.CardId,
  55. })
  56. }
  57. // DemountEquip delete equip.
  58. func (s *server) DemountEquip(c context.Context, req *v1.DemountEquipReq) (*v1.DemountEquipReply, error) {
  59. return nil, s.svr.DemountEquip(c, req.Mid)
  60. }
  61. // AllGroup all group.
  62. func (s *server) AllGroup(c context.Context, req *v1.AllGroupReq) (reply *v1.AllGroupReply, err error) {
  63. var gs *model.AllGroupResp
  64. if gs, err = s.svr.AllGroup(c, req.Mid); err != nil {
  65. return
  66. }
  67. if gs == nil {
  68. return
  69. }
  70. rs := new(v1.ModelAllGroupResp)
  71. if gs.UserCard != nil {
  72. rs.UserCard = convertModelUserCard(gs.UserCard)
  73. }
  74. ls := make([]*v1.ModelGroupInfo, len(gs.List))
  75. for i, v := range gs.List {
  76. cs := make([]*v1.ModelCard, len(v.Cards))
  77. for ci, cv := range v.Cards {
  78. cs[ci] = convertModelCard(cv)
  79. }
  80. ls[i] = &v1.ModelGroupInfo{
  81. GroupId: v.GroupID,
  82. GroupName: v.GroupName,
  83. Cards: cs,
  84. }
  85. }
  86. rs.List = ls
  87. return &v1.AllGroupReply{
  88. Res: rs,
  89. }, nil
  90. }
  91. // UserCard get user card info.
  92. func (s *server) UserCard(c context.Context, req *v1.UserCardReq) (res *v1.UserCardReply, err error) {
  93. var cd *model.UserCard
  94. res = new(v1.UserCardReply)
  95. if cd, err = s.svr.UserCard(c, req.Mid); err != nil {
  96. return
  97. }
  98. if cd == nil {
  99. return
  100. }
  101. res.Res = convertModelUserCard(cd)
  102. return
  103. }
  104. // UserCards get user card infos.
  105. func (s *server) UserCards(c context.Context, req *v1.UserCardsReq) (res *v1.UserCardsReply, err error) {
  106. var cs map[int64]*model.UserCard
  107. res = new(v1.UserCardsReply)
  108. if cs, err = s.svr.UserCards(c, req.Mids); err != nil {
  109. return
  110. }
  111. if len(cs) <= 0 {
  112. return
  113. }
  114. ls := make(map[int64]*v1.ModelUserCard, len(cs))
  115. for k, v := range cs {
  116. ls[k] = convertModelUserCard(v)
  117. }
  118. res.Res = ls
  119. return
  120. }
  121. func convertModelUserCard(v *model.UserCard) *v1.ModelUserCard {
  122. return &v1.ModelUserCard{
  123. Mid: v.Mid,
  124. Id: v.ID,
  125. CardUrl: v.CardURL,
  126. CardType: v.CardType,
  127. Name: v.Name,
  128. ExpireTime: v.ExpireTime,
  129. CardTypeName: v.CardTypeName,
  130. BigCardUrl: v.BigCradURL,
  131. }
  132. }
  133. func convertModelCard(cv *model.Card) *v1.ModelCard {
  134. return &v1.ModelCard{
  135. Id: cv.ID,
  136. Name: cv.Name,
  137. State: cv.State,
  138. Deleted: cv.Deleted,
  139. IsHot: cv.IsHot,
  140. CardUrl: cv.CardURL,
  141. BigCardUrl: cv.BigCradURL,
  142. CardType: cv.CardType,
  143. CardTypeName: cv.CardTypeName,
  144. }
  145. }