2025-11-24 22:55:14 +03:30
|
|
|
using CMSMicroservice.Domain.Enums;
|
2025-11-22 22:02:04 +03:30
|
|
|
using CMSMicroservice.Domain.Events;
|
2025-11-24 22:55:14 +03:30
|
|
|
|
2025-11-22 22:02:04 +03:30
|
|
|
namespace CMSMicroservice.Application.UserOrderCQ.Commands.SubmitShopBuyOrder;
|
2025-11-24 22:55:14 +03:30
|
|
|
|
|
|
|
|
public class
|
|
|
|
|
SubmitShopBuyOrderCommandHandler : IRequestHandler<SubmitShopBuyOrderCommand, SubmitShopBuyOrderResponseDto>
|
2025-11-22 22:02:04 +03:30
|
|
|
{
|
|
|
|
|
private readonly IApplicationDbContext _context;
|
|
|
|
|
|
|
|
|
|
public SubmitShopBuyOrderCommandHandler(IApplicationDbContext context)
|
|
|
|
|
{
|
|
|
|
|
_context = context;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-24 22:55:14 +03:30
|
|
|
public async Task<SubmitShopBuyOrderResponseDto> Handle(SubmitShopBuyOrderCommand request,
|
|
|
|
|
CancellationToken cancellationToken)
|
2025-11-22 22:02:04 +03:30
|
|
|
{
|
2025-11-24 22:55:14 +03:30
|
|
|
var user = await _context.Users
|
|
|
|
|
.Include(i => i.UserAddresss)
|
2025-11-25 00:39:31 +03:30
|
|
|
.Include(i => i.UserWallets)
|
|
|
|
|
.ThenInclude(i => i.UserWalletChangeLogs)
|
2025-11-24 22:55:14 +03:30
|
|
|
.Include(i => i.UserCartss)
|
|
|
|
|
.ThenInclude(i => i.Product)
|
|
|
|
|
.FirstOrDefaultAsync(w => w.Id == request.UserId, cancellationToken: cancellationToken);
|
|
|
|
|
if (user.UserCartss.Count == 0)
|
|
|
|
|
throw new NotFoundException("UserCart", request.UserId);
|
2025-11-25 00:39:31 +03:30
|
|
|
|
2025-11-24 22:55:14 +03:30
|
|
|
if (user.UserCartss.Sum(s => s.Count * s.Product.Price) != request.TotalAmount)
|
|
|
|
|
throw new Exception("مبلغ سفارش با مجموع سبد خرید مطابقت ندارد.");
|
|
|
|
|
|
2025-11-25 00:39:31 +03:30
|
|
|
|
|
|
|
|
var userWallet = user.UserWallets.FirstOrDefault();
|
|
|
|
|
if (userWallet == null)
|
|
|
|
|
throw new Exception("کیف پول کاربر یافت نشد.");
|
|
|
|
|
|
2025-11-25 01:58:45 +03:30
|
|
|
if (userWallet.Balance<=0 || userWallet.Balance<request.TotalAmount)
|
|
|
|
|
throw new Exception("موجودی کیف پول کاربر برای انجام این تراکنش کافی نیست.");
|
2025-11-25 00:39:31 +03:30
|
|
|
|
|
|
|
|
var newTransaction = new Transactions()
|
|
|
|
|
{
|
|
|
|
|
Amount = request.TotalAmount,
|
|
|
|
|
Description = "خرید از فروشگاه",
|
|
|
|
|
PaymentStatus = PaymentStatus.Success,
|
|
|
|
|
PaymentDate = DateTime.Now,
|
|
|
|
|
Type = TransactionType.Buy,
|
|
|
|
|
RefId = "localwallet-" + Guid.NewGuid().ToString()
|
|
|
|
|
};
|
|
|
|
|
await _context.Transactionss.AddAsync(newTransaction, cancellationToken);
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
var newWalletLog = new UserWalletChangeLog()
|
|
|
|
|
{
|
|
|
|
|
CurrentBalance = userWallet.Balance,
|
2025-11-25 03:56:45 +03:30
|
|
|
CurrentNetworkBalance = userWallet.NetworkBalance,
|
|
|
|
|
WalletId = userWallet.Id,
|
2025-11-25 00:39:31 +03:30
|
|
|
ChangeValue = -1*request.TotalAmount,
|
|
|
|
|
IsIncrease = false,
|
2025-11-25 03:56:45 +03:30
|
|
|
RefrenceId = newTransaction.Id,
|
2025-11-25 00:39:31 +03:30
|
|
|
};
|
|
|
|
|
userWallet.Balance -= request.TotalAmount;
|
|
|
|
|
|
|
|
|
|
await _context.UserWalletChangeLogs.AddAsync(newWalletLog, cancellationToken);
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
|
|
|
|
|
var newOrder = new UserOrder()
|
2025-11-24 22:55:14 +03:30
|
|
|
{
|
2025-11-25 00:39:31 +03:30
|
|
|
Amount = request.TotalAmount,
|
2025-11-24 22:55:14 +03:30
|
|
|
PaymentStatus = PaymentStatus.Success,
|
|
|
|
|
PaymentMethod = PaymentMethod.Wallet,
|
2025-11-25 00:39:31 +03:30
|
|
|
PaymentDate = DateTime.Now,
|
2025-11-24 22:55:14 +03:30
|
|
|
UserId = request.UserId,
|
2025-11-25 00:39:31 +03:30
|
|
|
UserAddressId = user.UserAddresss.First(f => f.IsDefault).Id,
|
|
|
|
|
TransactionId = newTransaction.Id
|
|
|
|
|
};
|
|
|
|
|
await _context.UserOrders.AddAsync(newOrder, cancellationToken);
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
2025-11-25 01:58:45 +03:30
|
|
|
var factorDetailsList = user.UserCartss.Select(s => new FactorDetails()
|
2025-11-24 22:55:14 +03:30
|
|
|
{
|
|
|
|
|
ProductId = s.ProductId,
|
|
|
|
|
Count = s.Count,
|
|
|
|
|
UnitPrice = s.Product.Price,
|
2025-11-25 00:39:31 +03:30
|
|
|
OrderId = newOrder.Id
|
2025-11-25 01:58:45 +03:30
|
|
|
});
|
|
|
|
|
await _context.FactorDetailss.AddRangeAsync(factorDetailsList, cancellationToken);
|
|
|
|
|
var finalResult = new SubmitShopBuyOrderResponseDto()
|
2025-11-25 00:39:31 +03:30
|
|
|
{
|
|
|
|
|
Id = newOrder.Id,
|
|
|
|
|
PaymentMethod = newOrder.PaymentMethod,
|
|
|
|
|
PaymentStatus = newOrder.PaymentStatus,
|
|
|
|
|
TotalAmount = newOrder.Amount,
|
|
|
|
|
UserAddressText = user.UserAddresss.First(f => f.IsDefault).Address,
|
2025-11-25 03:04:00 +03:30
|
|
|
PaymentDate = newOrder.PaymentDate,
|
2025-11-25 01:58:45 +03:30
|
|
|
FactorDetails = factorDetailsList.Select(s => new SubmitShopBuyOrderFactorDetail()
|
|
|
|
|
{
|
|
|
|
|
Count = s.Count,
|
|
|
|
|
UnitPrice = s.UnitPrice,
|
|
|
|
|
ProductId = s.ProductId,
|
|
|
|
|
ProductThumbnailPath = user.UserCartss.First(f => f.ProductId == s.ProductId).Product.ThumbnailPath,
|
|
|
|
|
ProductTitle = user.UserCartss.First(f => f.ProductId == s.ProductId).Product.Title,
|
|
|
|
|
UnitDiscountPrice = 0,
|
|
|
|
|
}).ToList()
|
2025-11-25 00:39:31 +03:30
|
|
|
};
|
2025-11-25 01:58:45 +03:30
|
|
|
user.UserCartss.Clear();
|
|
|
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
|
return finalResult;
|
2025-11-22 22:02:04 +03:30
|
|
|
}
|
2025-11-24 22:55:14 +03:30
|
|
|
}
|