rpcserver.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package gorpc
  2. import (
  3. "time"
  4. "go-common/app/service/main/up/conf"
  5. "go-common/app/service/main/up/model"
  6. "go-common/app/service/main/up/service"
  7. "go-common/library/ecode"
  8. "go-common/library/log"
  9. "go-common/library/net/rpc"
  10. "go-common/library/net/rpc/context"
  11. )
  12. //RPC rpc server
  13. type RPC struct {
  14. s *service.Service
  15. }
  16. //New create rpc
  17. func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
  18. r := &RPC{s: s}
  19. svr = rpc.NewServer(c.RPCServer)
  20. if err := svr.Register(r); err != nil {
  21. panic(err)
  22. }
  23. return
  24. }
  25. // Ping check connection success.
  26. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  27. return
  28. }
  29. // Special rpc call region
  30. func (r *RPC) Special(c context.Context, arg *model.ArgSpecial, res *[]*model.UpSpecial) (err error) {
  31. *res = r.s.UpsByGroup(c, arg.GroupID)
  32. log.Info("[rpc.Special] arg=%+v, res count=%d", arg, len(*res))
  33. return
  34. }
  35. //Info get up info
  36. func (r *RPC) Info(c context.Context, arg *model.ArgInfo, res *model.UpInfo) (err error) {
  37. if arg.Mid <= 0 {
  38. err = ecode.RequestErr
  39. log.Error("[rpc.Info] error, request mid <= 0, %d", arg.Mid)
  40. return
  41. }
  42. isAuthor, err := r.s.Info(c, arg.Mid, uint8(arg.From))
  43. if err != nil {
  44. log.Error("[rpc.Info] error, mid=%d, from=%d, err=%s", arg.Mid, arg.From, err)
  45. return
  46. }
  47. *res = model.UpInfo{IsAuthor: int32(isAuthor)}
  48. //log.Info("[rpc.Info] mid=%d, from=%d, result=%+v", arg.Mid, arg.From, *res)
  49. return
  50. }
  51. //UpStatBase get up stat
  52. func (r *RPC) UpStatBase(c context.Context, arg *model.ArgMidWithDate, res *model.UpBaseStat) (err error) {
  53. if arg.Date.IsZero() {
  54. // 如果没有填,则取最新的数据,如果有填,则取对应天数的数据,这里不需要做什么操作
  55. arg.Date = time.Now()
  56. // 12点更新数据,数据表为昨天日期,所以在12点以前,要读前天的表
  57. arg.Date = arg.Date.Add(-12*time.Hour).AddDate(0, 0, -1)
  58. }
  59. var data, e = r.s.Data.BaseUpStat(c, arg.Mid, arg.Date.Format("20060102"))
  60. err = e
  61. if err == nil {
  62. data.CopyTo(res)
  63. log.Info("[rpc.UpStatBase] arg=%+v, res=%+v", arg, res)
  64. } else {
  65. log.Error("[rpc.UpStatBase] fail arg=%+v, err=%v", arg, err)
  66. }
  67. return
  68. }
  69. //SetUpSwitch set up switch
  70. func (r *RPC) SetUpSwitch(c context.Context, arg *model.ArgUpSwitch, res *model.PBSetUpSwitchRes) (err error) {
  71. id, err := r.s.SetSwitch(c, arg.Mid, arg.State, uint8(arg.From))
  72. if err == nil {
  73. log.Info("[rpc.SetUpSwitch] arg=%+v, res=%+v", arg, res)
  74. } else {
  75. log.Error("[rpc.SetUpSwitch] fail arg=%+v, err=%v", arg, err)
  76. }
  77. *res = model.PBSetUpSwitchRes{Id: id}
  78. return
  79. }
  80. //UpSwitch get up switch
  81. func (r *RPC) UpSwitch(c context.Context, arg *model.ArgUpSwitch, res *model.PBUpSwitch) (err error) {
  82. if arg.Mid <= 0 {
  83. err = ecode.RequestErr
  84. log.Error("[rpc.UpSwitch] error, request mid <= 0, %d", arg.Mid)
  85. return
  86. }
  87. state, err := r.s.UpSwitchs(c, arg.Mid, uint8(arg.From))
  88. if err != nil {
  89. log.Error("[rpc.UpSwitchs] error, mid=%d, from=%d, err=%s", arg.Mid, arg.From, err)
  90. return
  91. }
  92. *res = model.PBUpSwitch{State: int32(state)}
  93. return
  94. }