rpc.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package gorpc
  2. import (
  3. "go-common/app/interface/main/history/conf"
  4. "go-common/app/interface/main/history/model"
  5. "go-common/app/interface/main/history/service"
  6. "go-common/library/ecode"
  7. "go-common/library/net/rpc"
  8. "go-common/library/net/rpc/context"
  9. )
  10. // RPC represent rpc server
  11. type RPC struct {
  12. svc *service.Service
  13. }
  14. // New init rpc.
  15. func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
  16. r := &RPC{svc: 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. // Progress report a user hisotry.
  28. func (r *RPC) Progress(c context.Context, arg *model.ArgPro, res *map[int64]*model.History) (err error) {
  29. *res, err = r.svc.Progress(c, arg.Mid, arg.Aids)
  30. return
  31. }
  32. // Position report a user hisotry.
  33. func (r *RPC) Position(c context.Context, arg *model.ArgPos, res *model.History) (err error) {
  34. var v *model.History
  35. tp, err := model.CheckBusiness(arg.Business)
  36. if err != nil {
  37. return
  38. }
  39. if tp > 0 {
  40. arg.TP = tp
  41. }
  42. v, err = r.svc.Position(c, arg.Mid, arg.Aid, arg.TP)
  43. if err == nil {
  44. *res = *v
  45. }
  46. return
  47. }
  48. // Add (c context.Context, mid, src, rtime int64, ip string, h *model.History) (err error) .
  49. func (r *RPC) Add(c context.Context, arg *model.ArgHistory, res *struct{}) (err error) {
  50. if arg.History != nil {
  51. var tp int8
  52. if tp, err = model.CheckBusiness(arg.History.Business); err != nil {
  53. return
  54. } else if tp > 0 {
  55. arg.History.TP = tp
  56. }
  57. }
  58. err = r.svc.AddHistory(c, arg.Mid, arg.Realtime, arg.History)
  59. return
  60. }
  61. // Delete delete histories
  62. func (r *RPC) Delete(c context.Context, arg *model.ArgDelete, res *struct{}) (err error) {
  63. if len(arg.Resources) == 0 {
  64. err = ecode.RequestErr
  65. return
  66. }
  67. histories := make([]*model.History, 0, len(arg.Resources))
  68. for _, history := range arg.Resources {
  69. var tp int8
  70. if tp, err = model.MustCheckBusiness(history.Business); err != nil {
  71. return
  72. }
  73. histories = append(histories, &model.History{
  74. Aid: history.Oid,
  75. TP: tp,
  76. })
  77. }
  78. err = r.svc.Delete(c, arg.Mid, histories)
  79. return
  80. }
  81. // History return all history .
  82. func (r *RPC) History(c context.Context, arg *model.ArgHistories, res *[]*model.Resource) (err error) {
  83. tp, err := model.CheckBusiness(arg.Business)
  84. if err != nil {
  85. return
  86. }
  87. if tp > 0 {
  88. arg.TP = tp
  89. }
  90. *res, err = r.svc.Histories(c, arg.Mid, arg.TP, arg.Pn, arg.Ps)
  91. return
  92. }
  93. // HistoryCursor return all history .
  94. func (r *RPC) HistoryCursor(c context.Context, arg *model.ArgCursor, res *[]*model.Resource) (err error) {
  95. tp, err := model.CheckBusiness(arg.Business)
  96. if err != nil {
  97. return
  98. }
  99. if tp > 0 {
  100. arg.TP = tp
  101. }
  102. var tps []int8
  103. for _, b := range arg.Businesses {
  104. tp, err = model.CheckBusiness(b)
  105. if err != nil {
  106. return
  107. }
  108. tps = append(tps, tp)
  109. }
  110. *res, err = r.svc.HistoryCursor(c, arg.Mid, arg.Max, arg.ViewAt, arg.Ps, arg.TP, tps, arg.RealIP)
  111. return
  112. }
  113. // Clear clear history
  114. func (r *RPC) Clear(c context.Context, arg *model.ArgClear, res *struct{}) (err error) {
  115. var tps []int8
  116. for _, b := range arg.Businesses {
  117. var tp int8
  118. if tp, err = model.MustCheckBusiness(b); err != nil {
  119. return
  120. }
  121. tps = append(tps, tp)
  122. }
  123. err = r.svc.ClearHistory(c, arg.Mid, tps)
  124. return
  125. }