rpc.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package rpc
  2. import (
  3. "time"
  4. pb "go-common/app/service/main/coin/api"
  5. "go-common/app/service/main/coin/conf"
  6. coin "go-common/app/service/main/coin/model"
  7. "go-common/app/service/main/coin/service"
  8. "go-common/library/net/rpc"
  9. "go-common/library/net/rpc/context"
  10. )
  11. // RPC define rpc.
  12. type RPC struct {
  13. s *service.Service
  14. }
  15. // New new rpc server.
  16. func New(c *conf.Config, s *service.Service) (svr *rpc.Server) {
  17. r := &RPC{s: s}
  18. svr = rpc.NewServer(c.RPCServer)
  19. if err := svr.Register(r); err != nil {
  20. panic(err)
  21. }
  22. return
  23. }
  24. // Ping check connection success.
  25. func (r *RPC) Ping(c context.Context, arg *struct{}, res *struct{}) (err error) {
  26. return
  27. }
  28. // AddCoins add coin to archive.
  29. func (r *RPC) AddCoins(c context.Context, ac *coin.ArgAddCoin, res *struct{}) (err error) {
  30. tp, err := r.s.CheckBusiness(ac.Business)
  31. if err != nil {
  32. return
  33. }
  34. if tp > 0 {
  35. ac.AvType = tp
  36. }
  37. if ac.AvType == 0 {
  38. ac.AvType = 1
  39. }
  40. b, err := r.s.GetBusinessName(ac.AvType)
  41. if err != nil {
  42. return
  43. }
  44. arg := &pb.AddCoinReq{
  45. IP: ac.RealIP,
  46. Mid: ac.Mid,
  47. Upmid: ac.UpMid,
  48. MaxCoin: ac.MaxCoin,
  49. Aid: ac.Aid,
  50. Business: b,
  51. Number: ac.Multiply,
  52. Typeid: int32(ac.TypeID),
  53. PubTime: ac.PubTime,
  54. }
  55. _, err = r.s.AddCoin(c, arg)
  56. return
  57. }
  58. // ArchiveUserCoins archive coins.
  59. func (r *RPC) ArchiveUserCoins(c context.Context, m *coin.ArgCoinInfo, res *coin.ArchiveUserCoins) (err error) {
  60. tp, err := r.s.CheckBusiness(m.Business)
  61. if err != nil {
  62. return
  63. }
  64. if tp > 0 {
  65. m.AvType = tp
  66. }
  67. if m.AvType == 0 {
  68. m.AvType = 1
  69. }
  70. b, err := r.s.GetBusinessName(m.AvType)
  71. if err != nil {
  72. return
  73. }
  74. arg := pb.ItemUserCoinsReq{
  75. Mid: m.Mid,
  76. Aid: m.Aid,
  77. Business: b,
  78. }
  79. var rr *pb.ItemUserCoinsReply
  80. if rr, err = r.s.ItemUserCoins(c, &arg); err == nil && rr != nil {
  81. *res = coin.ArchiveUserCoins{Multiply: rr.Number}
  82. }
  83. return
  84. }
  85. // UserCoins get user coins.
  86. func (r *RPC) UserCoins(c context.Context, arg *coin.ArgCoinInfo, res *float64) (err error) {
  87. reply, err := r.s.UserCoins(c, &pb.UserCoinsReq{Mid: arg.Mid})
  88. if reply != nil {
  89. *res = reply.Count
  90. }
  91. return
  92. }
  93. // ModifyCoin modify user coin.
  94. func (r *RPC) ModifyCoin(c context.Context, arg *coin.ArgModifyCoin, res *float64) (err error) {
  95. req := &pb.ModifyCoinsReq{
  96. Mid: arg.Mid,
  97. Count: arg.Count,
  98. Reason: arg.Reason,
  99. IP: arg.IP,
  100. Operator: arg.Operator,
  101. CheckZero: int32(arg.CheckZero),
  102. Ts: time.Now().Unix(),
  103. }
  104. reply, err := r.s.ModifyCoins(c, req)
  105. if err != nil {
  106. return
  107. }
  108. *res = reply.Result
  109. return
  110. }
  111. // List coin added list.
  112. func (r *RPC) List(c context.Context, arg *coin.ArgList, res *[]*coin.List) (err error) {
  113. tp, err := r.s.CheckBusiness(arg.Business)
  114. if err != nil {
  115. return
  116. }
  117. if tp > 0 {
  118. arg.TP = tp
  119. }
  120. b, err := r.s.GetBusinessName(arg.TP)
  121. if err != nil {
  122. return
  123. }
  124. req := &pb.ListReq{
  125. Mid: arg.Mid,
  126. Business: b,
  127. Ts: time.Now().Unix(),
  128. }
  129. reply, err := r.s.List(c, req)
  130. if err != nil {
  131. return
  132. }
  133. lists := []*coin.List{}
  134. for _, r := range reply.List {
  135. lists = append(lists, &coin.List{
  136. Aid: r.Aid,
  137. Multiply: r.Number,
  138. Ts: r.Ts,
  139. IP: r.IP,
  140. })
  141. }
  142. *res = lists
  143. return
  144. }
  145. // UserLog user log
  146. func (r *RPC) UserLog(c context.Context, arg *coin.ArgLog, res *[]*coin.Log) (err error) {
  147. req := &pb.CoinsLogReq{
  148. Mid: arg.Mid,
  149. Recent: arg.Recent,
  150. Translate: arg.Translate,
  151. }
  152. reply, err := r.s.CoinsLog(c, req)
  153. lists := []*coin.Log{}
  154. for _, r := range reply.List {
  155. lists = append(lists, &coin.Log{
  156. From: r.From,
  157. To: r.To,
  158. IP: r.IP,
  159. Desc: r.Desc,
  160. TimeStamp: r.TimeStamp,
  161. })
  162. }
  163. *res = lists
  164. return
  165. }
  166. // AddUserCoinExp add user coin exp for job
  167. func (r *RPC) AddUserCoinExp(c context.Context, arg *coin.ArgAddUserCoinExp, res *struct{}) (err error) {
  168. tp, err := r.s.CheckBusiness(arg.BusinessName)
  169. if err != nil {
  170. return
  171. }
  172. if tp > 0 {
  173. arg.Business = tp
  174. }
  175. b, err := r.s.GetBusinessName(arg.Business)
  176. if err != nil {
  177. return
  178. }
  179. req := &pb.AddUserCoinExpReq{
  180. IP: arg.RealIP,
  181. Mid: arg.Mid,
  182. Business: b,
  183. Number: arg.Number,
  184. }
  185. _, err = r.s.AddUserCoinExp(c, req)
  186. return
  187. }
  188. // UpdateAddCoin update db after add coin for job.
  189. func (r *RPC) UpdateAddCoin(c context.Context, arg *coin.Record, res *struct{}) (err error) {
  190. tp, err := r.s.CheckBusiness(arg.Business)
  191. if err != nil {
  192. return
  193. }
  194. if tp > 0 {
  195. arg.AvType = tp
  196. }
  197. b, err := r.s.GetBusinessName(arg.AvType)
  198. if err != nil {
  199. return
  200. }
  201. req := &pb.UpdateAddCoinReq{
  202. Aid: arg.Aid,
  203. Mid: arg.Mid,
  204. Up: arg.Up,
  205. Timestamp: arg.Timestamp,
  206. Number: arg.Multiply,
  207. Business: b,
  208. IPV6: arg.IPV6,
  209. }
  210. _, err = r.s.UpdateAddCoin(c, req)
  211. return
  212. }
  213. // TodayExp .
  214. func (r *RPC) TodayExp(c context.Context, arg *coin.ArgMid, res *int64) (err error) {
  215. req := &pb.TodayExpReq{
  216. Mid: arg.Mid,
  217. }
  218. reply, err := r.s.TodayExp(c, req)
  219. if reply != nil {
  220. *res = reply.Exp
  221. }
  222. return
  223. }