public async Task <WalletAction> RechargeAsync( Guid?tenantId, Guid userId, Guid rechargeProductId, int productQuantity, string orderId, int creditAmount, decimal paidAmount, string title, string description ) { using (CurrentTenant.Change(tenantId)) { var coinPurchased = new WalletAction(GuidGenerator.Create(), tenantId, userId, rechargeProductId, productQuantity, orderId, creditAmount, paidAmount); await _repository.InsertAsync(coinPurchased); // 修改销售数量 var product = await _productRepository.GetAsync(rechargeProductId); product.SetSoldQuantity(product.SoldQuantity + productQuantity); // 钱包 var wallet = await _walletRepository.GetByUserIdAsync(userId); wallet.IncBalance(creditAmount); // 写日志 var log = new WalletLog(GuidGenerator.Create(), tenantId, userId, "Recharge", false, creditAmount, wallet.Balance, title, description); await _walletLogRepository.InsertAsync(log); return(coinPurchased); } }
public async Task <CreateWalletDto> Handle(CreateWalletCommand request, CancellationToken cancellationToken) { var entity = mapper.Map <Wallet>(request.Dto); var wallet = await context.Wallets.SingleOrDefaultAsync(o => o.RiderId == entity.RiderId); if (wallet != null) { throw new RiderWalletAlreadyExist(); } // log wallet transaction var walletLog = new WalletLog(); walletLog.RiderId = entity.RiderId; walletLog.LogDate = DateTime.UtcNow; walletLog.Points = entity.PointsLoaded; walletLog.CurrentPoints = entity.CurrentPoints + entity.PointsLoaded; entity.CurrentPoints = entity.CurrentPoints + entity.PointsLoaded; entity.DateCreated = DateTime.UtcNow; context.Wallets.Add(entity); await context.SaveChangesAsync(); if (entity.WalletStatusId > 0) { var walletStatus = await context.WalletStatus.FindAsync(entity.WalletStatusId); if (walletStatus != null) { walletLog.CurrentStatus = walletStatus.Status; } } context.WalletLogs.Add(walletLog); await context.SaveChangesAsync(); var result = mapper.Map <CreateWalletDto>(entity); return(result); }
/// <summary> /// Method for building ObjectResult from WalletLog's Memento /// </summary> /// <param name="logItem"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static IActionResult GetRepeatedResult(WalletLog logItem) { switch (logItem.ResultType) { case ResultType.Created: var createdResult = JsonSerializer.Deserialize <WalletOperationResult>(logItem.Memento); createdResult.Repeated = true; // TODO: Fill in uri to Get method where the transaction can be found return(new CreatedResult("", createdResult)); case ResultType.UnprocessableEntity: return(new UnprocessableEntityObjectResult(WalletController.UnprocessableWalletLogResponseObject)); case ResultType.BadRequest: var badRequestResult = JsonSerializer.Deserialize <ValidationProblemDetails>(logItem.Memento); return(new BadRequestObjectResult(badRequestResult)); default: // throws Exception which returns 500 Internal Server Error throw new ArgumentOutOfRangeException(); } }
public async Task <decimal> PayAsync( Guid?tenantId, Guid userId, int creditAmount, string title, string description, string typeName = "Pay" ) { using (CurrentTenant.Change(tenantId)) { // 钱包 var wallet = await _walletRepository.GetByUserIdAsync(userId); wallet.DecBalance(creditAmount); // 写日志 var log = new WalletLog(GuidGenerator.Create(), tenantId, userId, typeName, true, creditAmount, wallet.Balance, title, description); await _walletLogRepository.InsertAsync(log); return(wallet.Balance); } }