server.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package grpc
  2. import (
  3. "context"
  4. pb "go-common/app/service/main/tv/api"
  5. "go-common/app/service/main/tv/internal/model"
  6. "go-common/app/service/main/tv/internal/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/rpc/warden"
  10. )
  11. // New new warden rpc server
  12. func New(c *warden.ServerConfig, svc *service.Service) *warden.Server {
  13. ws := warden.NewServer(c)
  14. pb.RegisterTVServiceServer(ws.Server(), &server{svc})
  15. ws, err := ws.Start()
  16. if err != nil {
  17. panic(err)
  18. }
  19. log.Info("start grpc server")
  20. return ws
  21. }
  22. type server struct {
  23. svr *service.Service
  24. }
  25. var _ pb.TVServiceServer = &server{}
  26. // UserInfo implementation
  27. func (s *server) UserInfo(ctx context.Context, req *pb.UserInfoReq) (resp *pb.UserInfoReply, err error) {
  28. ui, err := s.svr.UserInfo(ctx, req.Mid)
  29. if err != nil {
  30. return
  31. }
  32. if ui == nil {
  33. return nil, ecode.NothingFound
  34. }
  35. resp = &pb.UserInfoReply{}
  36. resp.DeepCopyFromUserInfo(ui)
  37. return
  38. }
  39. // ChangeHistory implementation
  40. func (s *server) ChangeHistory(ctx context.Context, req *pb.ChangeHistoryReq) (resp *pb.ChangeHistoryReply, err error) {
  41. ch, err := s.svr.ChangeHistory(ctx, req.Id)
  42. if err != nil {
  43. return
  44. }
  45. if ch == nil {
  46. return nil, ecode.NothingFound
  47. }
  48. resp = &pb.ChangeHistoryReply{}
  49. resp.DeepCopyFromUserChangeHistory(ch)
  50. return
  51. }
  52. // ChangeHistorys implementation
  53. func (s *server) ChangeHistorys(ctx context.Context, req *pb.ChangeHistorysReq) (resp *pb.ChangeHistorysReply, err error) {
  54. chs, total, err := s.svr.ChangeHistorys(ctx, req.Mid, req.From, req.To, req.Pn, req.Ps)
  55. if err != nil {
  56. return
  57. }
  58. resp = &pb.ChangeHistorysReply{Total: int32(total)}
  59. resp.Historys = make([]*pb.ChangeHistoryReply, 0, len(chs))
  60. for _, ch := range chs {
  61. chr := &pb.ChangeHistoryReply{}
  62. chr.DeepCopyFromUserChangeHistory(ch)
  63. resp.Historys = append(resp.Historys, chr)
  64. }
  65. return
  66. }
  67. func suitType2String(st int8) string {
  68. switch st {
  69. case model.SuitTypeAll:
  70. return "ALL"
  71. case model.SuitTypeMvip:
  72. return "MVIP"
  73. default:
  74. return "ALL"
  75. }
  76. }
  77. // PanelInfo implemention
  78. func (s *server) PanelInfo(ctx context.Context, req *pb.PanelInfoReq) (resp *pb.PanelInfoReply, err error) {
  79. pi, err := s.svr.PanelInfo(ctx, req.Mid)
  80. if err != nil {
  81. return
  82. }
  83. resp = &pb.PanelInfoReply{}
  84. resp.PriceConfigs = make(map[string]*pb.PanelPriceConfigs)
  85. for st, ps := range pi {
  86. ppcs := &pb.PanelPriceConfigs{}
  87. ppcs.PriceConfigs = make([]*pb.PanelPriceConfig, 0)
  88. for _, p := range ps {
  89. item := &pb.PanelPriceConfig{}
  90. item.DeepCopyFromPanelPriceConfig(p)
  91. ppcs.PriceConfigs = append(ppcs.PriceConfigs, item)
  92. }
  93. resp.PriceConfigs[suitType2String(st)] = ppcs
  94. }
  95. return
  96. }
  97. // GuestPanelInfo implemention
  98. func (s *server) GuestPanelInfo(ctx context.Context, req *pb.GuestPanelInfoReq) (resp *pb.GuestPanelInfoReply, err error) {
  99. pi, err := s.svr.GuestPanelInfo(ctx)
  100. if err != nil {
  101. return
  102. }
  103. resp = &pb.GuestPanelInfoReply{}
  104. resp.PriceConfigs = make(map[string]*pb.PanelPriceConfigs)
  105. for st, ps := range pi {
  106. ppcs := &pb.PanelPriceConfigs{}
  107. ppcs.PriceConfigs = make([]*pb.PanelPriceConfig, 0, len(ps))
  108. for _, p := range ps {
  109. item := &pb.PanelPriceConfig{}
  110. item.DeepCopyFromPanelPriceConfig(p)
  111. ppcs.PriceConfigs = append(ppcs.PriceConfigs, item)
  112. }
  113. resp.PriceConfigs[suitType2String(st)] = ppcs
  114. }
  115. return
  116. }
  117. // PayOrder implementation
  118. func (s *server) PayOrder(ctx context.Context, req *pb.PayOrderReq) (resp *pb.PayOrderReply, err error) {
  119. resp = &pb.PayOrderReply{}
  120. return
  121. }
  122. // CreateQr implementation
  123. func (s *server) CreateQr(ctx context.Context, req *pb.CreateQrReq) (resp *pb.CreateQrReply, err error) {
  124. qr, err := s.svr.CreateQr(ctx, req.Mid, req.Pid, req.BuyNum, req.Guid, req.AppChannel)
  125. if err != nil {
  126. return
  127. }
  128. resp = &pb.CreateQrReply{}
  129. resp.DeepCopyFromQR(qr)
  130. return
  131. }
  132. // CreateGuestQr implementation
  133. func (s *server) CreateGuestQr(ctx context.Context, req *pb.CreateGuestQrReq) (resp *pb.CreateGuestQrReply, err error) {
  134. qr, err := s.svr.CreateGuestQr(ctx, req.Pid, req.BuyNum, req.Guid, req.AppChannel)
  135. if err != nil {
  136. return
  137. }
  138. resp = &pb.CreateGuestQrReply{}
  139. resp.DeepCopyFromQR(qr)
  140. return
  141. }
  142. // CreateOrder implementation
  143. func (s *server) CreateOrder(ctx context.Context, req *pb.CreateOrderReq) (resp *pb.CreateOrderReply, err error) {
  144. pi, err := s.svr.CreateOrder(ctx, req.Token, req.Platform, req.PaymentType, req.ClientIp)
  145. if err != nil {
  146. return
  147. }
  148. resp = &pb.CreateOrderReply{}
  149. resp.DeepCopyFromPayInfo(pi)
  150. return
  151. }
  152. // CreateGuestOrder implementation
  153. func (s *server) CreateGuestOrder(ctx context.Context, req *pb.CreateGuestOrderReq) (resp *pb.CreateGuestOrderReply, err error) {
  154. pi, err := s.svr.CreateGuestOrder(ctx, req.Mid, req.Token, req.Platform, req.PaymentType, req.ClientIp)
  155. if err != nil {
  156. return
  157. }
  158. resp = &pb.CreateGuestOrderReply{}
  159. resp.DeepCopyFromPayInfo(pi)
  160. return
  161. }
  162. // RenewVip implementation
  163. func (s *server) TokenInfo(ctx context.Context, req *pb.TokenInfoReq) (resp *pb.TokenInfoReply, err error) {
  164. ti, err := s.svr.TokenInfos(ctx, req.Token)
  165. if err != nil {
  166. return
  167. }
  168. resp = &pb.TokenInfoReply{}
  169. resp.Tokens = make([]*pb.TokenInfo, 0)
  170. for _, v := range ti {
  171. t := &pb.TokenInfo{}
  172. t.DeepCopyFromTokenInfo(v)
  173. resp.Tokens = append(resp.Tokens, t)
  174. }
  175. return
  176. }
  177. // RenewVip implementation
  178. func (s *server) RenewVip(ctx context.Context, req *pb.RenewVipReq) (resp *pb.RenewVipReply, err error) {
  179. err = s.svr.RenewVip(ctx, req.Mid)
  180. if err != nil {
  181. return
  182. }
  183. resp = &pb.RenewVipReply{}
  184. return
  185. }
  186. // YstUserInfo implementation
  187. func (s *server) YstUserInfo(ctx context.Context, req *pb.YstUserInfoReq) (resp *pb.YstUserInfoReply, err error) {
  188. resp = &pb.YstUserInfoReply{}
  189. ui, err := s.svr.YstUserInfo(ctx, req.DeepCopyAsYstUserInfoReq())
  190. if err != nil {
  191. resp.Result = "998"
  192. resp.Msg = err.Error()
  193. return
  194. }
  195. resp.DeepCopyFromUserInfo(ui)
  196. resp.Result = "0"
  197. resp.Msg = "ok"
  198. return
  199. }
  200. // PayCallback implementation
  201. func (s *server) PayCallback(ctx context.Context, req *pb.PayCallbackReq) (resp *pb.PayCallbackReply, err error) {
  202. ystReq := &model.YstPayCallbackReq{}
  203. req.DeepCopyAsIntoYstPayCallbackReq(ystReq)
  204. ystReply := s.svr.PayCallback(ctx, ystReq)
  205. resp = &pb.PayCallbackReply{}
  206. resp.DeepCopyFromYstPayCallbackReply(ystReply)
  207. return
  208. }
  209. // WxContractCallback implementation.
  210. func (s *server) WxContractCallback(ctx context.Context, req *pb.WxContractCallbackReq) (resp *pb.WxContractCallbackReply, err error) {
  211. resp = &pb.WxContractCallbackReply{}
  212. return
  213. }