asset.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package grpc
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/ugcpay/api/grpc/v1"
  6. "go-common/app/service/main/ugcpay/model"
  7. "go-common/app/service/main/ugcpay/service"
  8. "go-common/library/log"
  9. "go-common/library/net/metadata"
  10. "go-common/library/net/rpc/warden"
  11. "google.golang.org/grpc"
  12. )
  13. // New Identify warden rpc server
  14. func New(cfg *warden.ServerConfig, s *service.Service) *warden.Server {
  15. w := warden.NewServer(cfg)
  16. w.Use(func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  17. if resp, err = handler(ctx, req); err == nil {
  18. log.Infov(ctx,
  19. log.KV("path", info.FullMethod),
  20. log.KV("caller", metadata.String(ctx, metadata.Caller)),
  21. log.KV("remote_ip", metadata.String(ctx, metadata.RemoteIP)),
  22. log.KV("args", fmt.Sprintf("%s", req)),
  23. log.KV("retVal", fmt.Sprintf("%s", resp)))
  24. }
  25. return
  26. })
  27. v1.RegisterUGCPayServer(w.Server(), &UGCPayServer{s})
  28. ws, err := w.Start()
  29. if err != nil {
  30. panic(err)
  31. }
  32. return ws
  33. }
  34. // UGCPayServer .
  35. type UGCPayServer struct {
  36. svr *service.Service
  37. }
  38. var _ v1.UGCPayServer = &UGCPayServer{}
  39. // AssetRegister .
  40. func (u *UGCPayServer) AssetRegister(ctx context.Context, req *v1.AssetRegisterReq) (*v1.EmptyStruct, error) {
  41. err := u.svr.AssetRegister(ctx, req.Mid, req.Oid, req.Otype, req.Currency, req.Price)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return &v1.EmptyStruct{}, nil
  46. }
  47. // AssetQuery .
  48. func (u *UGCPayServer) AssetQuery(ctx context.Context, req *v1.AssetQueryReq) (*v1.AssetQueryResp, error) {
  49. res, pp, err := u.svr.AssetQuery(ctx, req.Oid, req.Otype, req.Currency)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &v1.AssetQueryResp{
  54. Price: res.Price,
  55. PlatformPrice: pp,
  56. }, nil
  57. }
  58. // AssetRelation .
  59. func (u *UGCPayServer) AssetRelation(ctx context.Context, req *v1.AssetRelationReq) (*v1.AssetRelationResp, error) {
  60. res, err := u.svr.AssetRelation(ctx, req.Mid, req.Oid, req.Otype)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return &v1.AssetRelationResp{
  65. State: res,
  66. }, nil
  67. }
  68. // AssetRelationDetail .
  69. func (u *UGCPayServer) AssetRelationDetail(ctx context.Context, req *v1.AssetRelationDetailReq) (*v1.AssetRelationDetailResp, error) {
  70. state, err := u.svr.AssetRelation(ctx, req.Mid, req.Oid, req.Otype)
  71. if err != nil {
  72. return nil, err
  73. }
  74. res, pp, err := u.svr.AssetQuery(ctx, req.Oid, req.Otype, req.Currency)
  75. if err != nil {
  76. return nil, err
  77. }
  78. return &v1.AssetRelationDetailResp{
  79. RelationState: state,
  80. AssetPrice: res.Price,
  81. AssetPlatformPrice: pp,
  82. }, nil
  83. }
  84. // TradeCreate .
  85. func (u *UGCPayServer) TradeCreate(ctx context.Context, req *v1.TradeCreateReq) (*v1.TradeCreateResp, error) {
  86. orderID, payData, err := u.svr.TradeCreate(ctx, req.Platform, req.Mid, req.Oid, req.Otype, req.Currency)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &v1.TradeCreateResp{
  91. OrderId: orderID,
  92. PayData: payData,
  93. }, nil
  94. }
  95. // TradeQuery .
  96. func (u *UGCPayServer) TradeQuery(ctx context.Context, req *v1.TradeOrderReq) (*v1.TradeOrderResp, error) {
  97. order, err := u.svr.TradeQuery(ctx, req.Id)
  98. if err != nil {
  99. return nil, err
  100. }
  101. order.State = order.ReturnState()
  102. return &v1.TradeOrderResp{
  103. OrderId: order.OrderID,
  104. Mid: order.MID,
  105. Biz: order.Biz,
  106. Platform: order.Platform,
  107. Oid: order.OID,
  108. Otype: order.OType,
  109. Fee: order.Fee,
  110. Currency: order.Currency,
  111. PayId: order.PayID,
  112. State: order.State,
  113. Reason: order.PayReason,
  114. }, nil
  115. }
  116. // TradeCancel .
  117. func (u *UGCPayServer) TradeCancel(ctx context.Context, req *v1.TradeOrderReq) (*v1.EmptyStruct, error) {
  118. err := u.svr.TradeCancel(ctx, req.Id)
  119. if err != nil {
  120. return nil, err
  121. }
  122. return &v1.EmptyStruct{}, nil
  123. }
  124. // TradeConfirm .
  125. func (u *UGCPayServer) TradeConfirm(ctx context.Context, req *v1.TradeOrderReq) (*v1.TradeOrderResp, error) {
  126. order, err := u.svr.TradeConfirm(ctx, req.Id)
  127. if err != nil {
  128. return nil, err
  129. }
  130. order.State = order.ReturnState()
  131. return &v1.TradeOrderResp{
  132. OrderId: order.OrderID,
  133. Mid: order.MID,
  134. Biz: order.Biz,
  135. Platform: order.Platform,
  136. Oid: order.OID,
  137. Otype: order.OType,
  138. Fee: order.Fee,
  139. Currency: order.Currency,
  140. PayId: order.PayID,
  141. State: order.State,
  142. Reason: order.PayReason,
  143. }, nil
  144. }
  145. // TradeRefund .
  146. func (u *UGCPayServer) TradeRefund(ctx context.Context, req *v1.TradeOrderReq) (*v1.EmptyStruct, error) {
  147. err := u.svr.TradeRefund(ctx, req.Id)
  148. if err != nil {
  149. return nil, err
  150. }
  151. return &v1.EmptyStruct{}, nil
  152. }
  153. // IncomeUserAssetOverview .
  154. func (u *UGCPayServer) IncomeUserAssetOverview(ctx context.Context, req *v1.IncomeUserAssetOverviewReq) (*v1.IncomeUserAssetOverviewResp, error) {
  155. overview, monthReady, newDailyBill, err := u.svr.IncomeUserAssetOverview(ctx, req.Mid, "bp")
  156. if err != nil {
  157. return nil, err
  158. }
  159. // 总计收入
  160. resp := &v1.IncomeUserAssetOverviewResp{}
  161. if overview != nil {
  162. resp.Total = overview.TotalIn - overview.TotalOut
  163. resp.TotalBuyTimes = overview.PaySuccess - overview.PayError
  164. }
  165. // 本月新增收入
  166. resp.MonthNew = monthReady
  167. // 前日新增收入
  168. if newDailyBill != nil {
  169. resp.DayNew = newDailyBill.In - newDailyBill.Out
  170. }
  171. log.Info("IncomeUserAssetOverview grpc resp: %+v", resp)
  172. return resp, nil
  173. }
  174. // IncomeUserAssetList .
  175. func (u *UGCPayServer) IncomeUserAssetList(ctx context.Context, req *v1.IncomeUserAssetListReq) (resp *v1.IncomeUserAssetListResp, err error) {
  176. // 月度收入
  177. if req.Ver > 0 {
  178. return u.incomeUserAssetListByVer(ctx, req.Mid, "bp", req.Ver, req.Pn, req.Ps)
  179. }
  180. // 总计收入
  181. return u.incomeUserAssetListByAll(ctx, req.Mid, "bp", req.Pn, req.Ps)
  182. }
  183. func (u *UGCPayServer) incomeUserAssetListByAll(ctx context.Context, mid int64, currency string, pn, ps int64) (resp *v1.IncomeUserAssetListResp, err error) {
  184. var (
  185. allList *model.AggrIncomeUserAssetList
  186. allPage *model.Page
  187. )
  188. // 获得总计收入
  189. if allList, allPage, err = u.svr.IncomeUserAssetList(ctx, mid, currency, 0, pn, ps); err != nil {
  190. return
  191. }
  192. // 查询 asset price
  193. assetPriceMap := make(map[string]int64) // assetPriceMap map[assetKey]price
  194. for _, a := range allList.Assets {
  195. as, _, err := u.svr.AssetQuery(ctx, a.OID, a.OType, a.Currency)
  196. if err != nil {
  197. log.Error("IncomeUserAssetList found invalid asset oid: %d, otype: %s, err: %+v", a.OID, a.OType, err)
  198. err = nil
  199. continue
  200. }
  201. assetPriceMap[assetKey(a.OID, a.OType, a.Currency)] = as.Price
  202. }
  203. // 写入返回值
  204. resp = &v1.IncomeUserAssetListResp{
  205. Page: &v1.Page{
  206. Num: allPage.Num,
  207. Size_: allPage.Size,
  208. Total: allPage.Total,
  209. },
  210. }
  211. for _, a := range allList.Assets {
  212. asset := &v1.IncomeUserAsset{
  213. Oid: a.OID,
  214. Otype: a.OType,
  215. Currency: a.Currency,
  216. Price: assetPriceMap[assetKey(a.OID, a.OType, a.Currency)],
  217. TotalBuyTimes: a.PaySuccess,
  218. NewBuyTimes: 0,
  219. TotalErrTimes: a.PayError,
  220. NewErrTimes: 0,
  221. }
  222. resp.List = append(resp.List, asset)
  223. }
  224. return resp, nil
  225. }
  226. func (u *UGCPayServer) incomeUserAssetListByVer(ctx context.Context, mid int64, currency string, ver int64, pn, ps int64) (resp *v1.IncomeUserAssetListResp, err error) {
  227. var (
  228. monthList *model.AggrIncomeUserAssetList
  229. monthPage *model.Page
  230. )
  231. // 获得月份收入
  232. if monthList, monthPage, err = u.svr.IncomeUserAssetList(ctx, mid, currency, ver, pn, ps); err != nil {
  233. return
  234. }
  235. // 查询 asset price
  236. assetPriceMap := make(map[string]int64) // assetPriceMap map[assetKey]price
  237. for _, a := range monthList.Assets {
  238. as, _, err := u.svr.AssetQuery(ctx, a.OID, a.OType, a.Currency)
  239. if err != nil {
  240. log.Error("IncomeUserAssetList found invalid asset oid: %d, otype: %s, err: %+v", a.OID, a.OType, err)
  241. err = nil
  242. continue
  243. }
  244. assetPriceMap[assetKey(a.OID, a.OType, a.Currency)] = as.Price
  245. }
  246. // 写入返回值
  247. resp = &v1.IncomeUserAssetListResp{
  248. Page: &v1.Page{
  249. Num: monthPage.Num,
  250. Size_: monthPage.Size,
  251. Total: monthPage.Total,
  252. },
  253. }
  254. for _, a := range monthList.Assets {
  255. var (
  256. asset = &v1.IncomeUserAsset{
  257. Oid: a.OID,
  258. Otype: a.OType,
  259. Currency: a.Currency,
  260. Price: 0,
  261. TotalBuyTimes: 0,
  262. NewBuyTimes: a.PaySuccess,
  263. TotalErrTimes: 0,
  264. NewErrTimes: a.PayError,
  265. }
  266. )
  267. asset.Price = assetPriceMap[assetKey(a.OID, a.OType, a.Currency)]
  268. allAsset, err := u.svr.IncomeUserAsset(ctx, mid, a.OID, a.OType, a.Currency, 0)
  269. if err != nil {
  270. log.Error("u.svr.IncomeUserAsset mid: %d, oid: %d, otype: %s, currency: %s, err: %+v", mid, a.OID, a.OType, a.Currency, err)
  271. err = nil
  272. continue
  273. }
  274. if allAsset == nil {
  275. log.Error("u.svr.IncomeUserAsset got nil asset, mid: %d, oid: %d, otype: %s, currency: %s", mid, a.OID, a.OType, a.Currency)
  276. err = nil
  277. continue
  278. }
  279. asset.TotalBuyTimes = allAsset.PaySuccess
  280. asset.TotalErrTimes = allAsset.PayError
  281. resp.List = append(resp.List, asset)
  282. }
  283. return
  284. }
  285. func assetKey(oid int64, otype, currency string) string {
  286. return fmt.Sprintf("%d_%s_%s", oid, otype, currency)
  287. }