rpc.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. package server
  2. import (
  3. "go-common/app/service/main/coupon/conf"
  4. "go-common/app/service/main/coupon/model"
  5. "go-common/app/service/main/coupon/service"
  6. "go-common/library/log"
  7. "go-common/library/net/rpc"
  8. "go-common/library/net/rpc/context"
  9. )
  10. // RPC server
  11. type RPC struct {
  12. s *service.Service
  13. }
  14. // New new rpc server.
  15. func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
  16. r := &RPC{s: s}
  17. svr = rpc.NewServer(c.RPCServer)
  18. if err := svr.Register(r); err != nil {
  19. panic(err)
  20. }
  21. return
  22. }
  23. // Ping check connection success.
  24. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  25. return
  26. }
  27. // SalaryCoupon salary coupon.
  28. func (r *RPC) SalaryCoupon(c context.Context, a *model.ArgSalaryCoupon, res *struct{}) (err error) {
  29. return r.s.SalaryCoupon(c, a)
  30. }
  31. // SalaryCouponForThird salary coupon for third.
  32. func (r *RPC) SalaryCouponForThird(c context.Context, a *model.ArgSalaryCoupon, res *model.SalaryCouponForThirdResp) (err error) {
  33. var ct *model.SalaryCouponForThirdResp
  34. if ct, err = r.s.SalaryCouponForThird(c, a); err == nil && ct != nil {
  35. *res = *ct
  36. }
  37. return
  38. }
  39. // CouponPage coupon page.
  40. func (r *RPC) CouponPage(c context.Context, arg *model.ArgRPCPage, res *model.CouponPageRPCResp) (err error) {
  41. var (
  42. cr *model.CouponPageRPCResp
  43. count int64
  44. list []*model.CouponPageResp
  45. )
  46. if count, list, err = r.s.CouponPage(c, arg.Mid, arg.State, arg.Pn, arg.Ps); err != nil {
  47. log.Error("r.s.CouponPage(%d) err(%+v)", arg.Mid, err)
  48. return
  49. }
  50. cr = &model.CouponPageRPCResp{
  51. Count: count,
  52. Res: list,
  53. }
  54. *res = *cr
  55. return
  56. }
  57. // CouponCartoonPage coupon cartoon page.
  58. func (r *RPC) CouponCartoonPage(c context.Context, arg *model.ArgRPCPage, res *model.CouponCartoonPageResp) (err error) {
  59. var p *model.CouponCartoonPageResp
  60. if p, err = r.s.CouponCartoonPage(c, arg.Mid, arg.State, arg.Pn, arg.Ps); err != nil || p == nil {
  61. log.Error("r.s.CouponCartoonPage(%d) err(%+v)", arg.Mid, err)
  62. return
  63. }
  64. *res = *p
  65. return
  66. }
  67. // UsableAllowanceCoupon get usable allowance coupon.
  68. func (r *RPC) UsableAllowanceCoupon(c context.Context, a *model.ArgAllowanceCoupon, res *model.CouponAllowancePanelInfo) (err error) {
  69. var cr *model.CouponAllowancePanelInfo
  70. if cr, err = r.s.UsableAllowanceCoupon(c, a.Mid, a.Pirce, a.Platform, a.ProdLimMonth, a.ProdLimRenewal); err == nil && cr != nil {
  71. *res = *cr
  72. }
  73. if err != nil {
  74. log.Error("rpc.UsableAllowanceCoupon(%+v) err(%+v)", a, err)
  75. }
  76. return
  77. }
  78. // AllowanceCouponPanel get allowance coupon info for pay panel.
  79. func (r *RPC) AllowanceCouponPanel(c context.Context, a *model.ArgAllowanceCoupon, res *model.CouponAllowancePanelResp) (err error) {
  80. var (
  81. cr *model.CouponAllowancePanelResp
  82. us []*model.CouponAllowancePanelInfo
  83. ds []*model.CouponAllowancePanelInfo
  84. ui []*model.CouponAllowancePanelInfo
  85. )
  86. if us, ds, ui, err = r.s.AllowancePanelCoupons(c, a.Mid, a.Pirce, a.Platform, a.ProdLimMonth, a.ProdLimRenewal); err != nil {
  87. log.Error("rpc.AllowancePanelCoupons(%+v) err(%+v)", a, err)
  88. return
  89. }
  90. cr = &model.CouponAllowancePanelResp{
  91. Usables: us,
  92. Disables: ds,
  93. Using: ui,
  94. }
  95. *res = *cr
  96. return
  97. }
  98. // MultiUsableAllowanceCoupon get usable allowance coupon by muti pirce.
  99. func (r *RPC) MultiUsableAllowanceCoupon(c context.Context, a *model.ArgUsablePirces, res *map[float64]*model.CouponAllowancePanelInfo) (err error) {
  100. if *res, err = r.s.MultiUsableAllowanceCoupon(c, a.Mid, a.Pirce, a.Platform, a.ProdLimMonth, a.ProdLimRenewal); err != nil {
  101. log.Error("rpc.MultiUsableAllowanceCoupon(%+v) err(%+v)", a, err)
  102. return
  103. }
  104. return
  105. }
  106. // JudgeCouponUsable judge coupon is usable.
  107. func (r *RPC) JudgeCouponUsable(c context.Context, a *model.ArgJuageUsable, res *model.CouponAllowanceInfo) (err error) {
  108. var cp *model.CouponAllowanceInfo
  109. if cp, err = r.s.JudgeCouponUsable(c, a.Mid, a.Pirce, a.CouponToken, a.Platform, a.ProdLimMonth, a.ProdLimRenewal); err == nil && cp != nil {
  110. *res = *cp
  111. return
  112. }
  113. if err != nil {
  114. log.Error("rpc.JudgeCouponUsable(%+v) err(%+v)", a, err)
  115. }
  116. return
  117. }
  118. // AllowanceInfo allowance info.
  119. func (r *RPC) AllowanceInfo(c context.Context, a *model.ArgAllowance, res *model.CouponAllowanceInfo) (err error) {
  120. var cp *model.CouponAllowanceInfo
  121. if cp, err = r.s.AllowanceInfo(c, a.Mid, a.CouponToken); err == nil && cp != nil {
  122. *res = *cp
  123. return
  124. }
  125. if err != nil {
  126. log.Error("rpc.AllowanceInfo(%+v) err(%+v)", a, err)
  127. }
  128. return
  129. }
  130. // CancelUseCoupon cancel use coupon .
  131. func (r *RPC) CancelUseCoupon(c context.Context, a *model.ArgAllowance, res *struct{}) (err error) {
  132. if err = r.s.CancelUseCoupon(c, a.Mid, a.CouponToken); err != nil {
  133. log.Error("rpc.CancelUseCoupon(%+v) err(%+v)", a, err)
  134. }
  135. return
  136. }
  137. // CouponNotify notify coupon .
  138. func (r *RPC) CouponNotify(c context.Context, a *model.ArgNotify, res *struct{}) (err error) {
  139. if err = r.s.CouponNotify(c, a.Mid, a.OrderNo, a.State); err != nil {
  140. log.Error("rpc.CouponNotify(%+v) err(%+v)", a, err)
  141. }
  142. return
  143. }
  144. // AllowanceList allowance list .
  145. func (r *RPC) AllowanceList(c context.Context, a *model.ArgAllowanceList, res *[]*model.CouponAllowancePanelInfo) (err error) {
  146. if *res, err = r.s.AllowanceList(c, a.Mid, a.State); err != nil {
  147. log.Error("rpc.AllowanceList(%+v) err(%+v)", a, err)
  148. }
  149. return
  150. }
  151. // UseAllowance use allowance .
  152. func (r *RPC) UseAllowance(c context.Context, a *model.ArgUseAllowance, res *struct{}) (err error) {
  153. if err = r.s.UseAllowanceCoupon(c, a); err != nil {
  154. log.Error("rpc.UseAllowanceCoupon(%+v) err(%+v)", a, err)
  155. }
  156. return
  157. }
  158. // AllowanceCount allowance count
  159. func (r *RPC) AllowanceCount(c context.Context, a *model.ArgAllowanceMid, res *int) (err error) {
  160. var rs []*model.CouponAllowanceInfo
  161. if rs, err = r.s.AllowanceCoupon(c, &model.ArgAllowanceCoupons{
  162. Mid: a.Mid,
  163. State: model.NotUsed,
  164. }); err == nil {
  165. *res = len(rs)
  166. }
  167. return
  168. }
  169. //ReceiveAllowance receive allowance.
  170. func (r *RPC) ReceiveAllowance(c context.Context, arg *model.ArgReceiveAllowance, res *string) (err error) {
  171. var couponToken string
  172. if couponToken, err = r.s.ReceiveAllowance(c, arg); err != nil {
  173. log.Error("receive allowance(%+v) err(%+v)", arg, err)
  174. return
  175. }
  176. *res = couponToken
  177. return
  178. }
  179. //PrizeCards .
  180. func (r *RPC) PrizeCards(c context.Context, arg *model.ArgCount, res *[]*model.PrizeCardRep) (err error) {
  181. if *res, err = r.s.PrizeCards(c, arg.Mid); err != nil {
  182. log.Error("r.s.PrizeCards(%+v) err(%+v)", arg, err)
  183. return
  184. }
  185. return
  186. }
  187. //PrizeDraw .
  188. func (r *RPC) PrizeDraw(c context.Context, arg *model.ArgPrizeDraw, res *model.PrizeCardRep) (err error) {
  189. var pc = &model.PrizeCardRep{}
  190. if pc, err = r.s.PrizeDraw(c, arg.Mid, arg.CardType); err != nil {
  191. log.Error("r.s.PrizeDraw(%+v) err(%+v)", arg, err)
  192. return
  193. }
  194. *res = *pc
  195. return
  196. }