feat : complete calculate discount , add ver 0.6.12.20
parent
574bba81c1
commit
d13a95c9b6
|
@ -1,8 +1,9 @@
|
||||||
using NetinaShop.Domain.Dtos.SmallDtos;
|
using NetinaShop.Domain.Dtos.LargDtos;
|
||||||
|
using NetinaShop.Domain.Dtos.SmallDtos;
|
||||||
|
|
||||||
namespace NetinaShop.Core.EntityServices.DiscountHandlers;
|
namespace NetinaShop.Core.EntityServices.DiscountHandlers;
|
||||||
|
|
||||||
public class CalculateProductDiscountCommandHandler : IRequestHandler<CalculateProductDiscountCommand, ProductSDto>
|
public class CalculateProductDiscountCommandHandler : IRequestHandler<CalculateProductDiscountCommand, bool>
|
||||||
{
|
{
|
||||||
private readonly IRepositoryWrapper _repositoryWrapper;
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
||||||
|
|
||||||
|
@ -10,11 +11,27 @@ public class CalculateProductDiscountCommandHandler : IRequestHandler<CalculateP
|
||||||
{
|
{
|
||||||
_repositoryWrapper = repositoryWrapper;
|
_repositoryWrapper = repositoryWrapper;
|
||||||
}
|
}
|
||||||
public async Task<ProductSDto> Handle(CalculateProductDiscountCommand request, CancellationToken cancellationToken)
|
|
||||||
|
|
||||||
|
public async Task<bool> Handle(CalculateProductDiscountCommand request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
if (request.Product is ProductSDto product)
|
||||||
|
{
|
||||||
|
await CalculateAsync(product, cancellationToken);
|
||||||
|
}else if (request.Product is ProductLDto productLDto)
|
||||||
|
{
|
||||||
|
await CalculateAsync(productLDto, cancellationToken);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw new AppException("Model is wrong , ProductSDto or ProductLDto");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ProductSDto> CalculateAsync(ProductSDto request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
double discountPrice = 0;
|
double discountPrice = 0;
|
||||||
request.ProductSDto.CostWithDiscount = request.ProductSDto.Cost;
|
request.CostWithDiscount = request.Cost;
|
||||||
double totalPrice = request.ProductSDto.Cost;
|
double totalPrice = request.Cost;
|
||||||
|
|
||||||
var allDiscount = await _repositoryWrapper.SetRepository<Discount>()
|
var allDiscount = await _repositoryWrapper.SetRepository<Discount>()
|
||||||
.TableNoTracking
|
.TableNoTracking
|
||||||
|
@ -25,23 +42,23 @@ public class CalculateProductDiscountCommandHandler : IRequestHandler<CalculateP
|
||||||
|
|
||||||
discountPrice = allDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - allDiscount.DiscountAmount :
|
discountPrice = allDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - allDiscount.DiscountAmount :
|
||||||
totalPrice - totalPrice / 100 * allDiscount.DiscountPercent;
|
totalPrice - totalPrice / 100 * allDiscount.DiscountPercent;
|
||||||
request.ProductSDto.HasDiscount = true;
|
request.HasDiscount = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var categoryDiscount = await _repositoryWrapper.SetRepository<CategoryDiscount>()
|
var categoryDiscount = await _repositoryWrapper.SetRepository<CategoryDiscount>()
|
||||||
.TableNoTracking
|
.TableNoTracking
|
||||||
.FirstOrDefaultAsync(d => d.CategoryId == request.ProductSDto.CategoryId && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken);
|
.FirstOrDefaultAsync(d => d.CategoryId == request.CategoryId && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken);
|
||||||
|
|
||||||
if (categoryDiscount != null && !categoryDiscount.IsExpired())
|
if (categoryDiscount != null && !categoryDiscount.IsExpired())
|
||||||
{
|
{
|
||||||
discountPrice = categoryDiscount.AmountType == DiscountAmountType.Amount
|
discountPrice = categoryDiscount.AmountType == DiscountAmountType.Amount
|
||||||
? totalPrice - categoryDiscount.DiscountAmount : totalPrice - totalPrice / 100 * categoryDiscount.DiscountPercent;
|
? totalPrice - categoryDiscount.DiscountAmount : totalPrice - totalPrice / 100 * categoryDiscount.DiscountPercent;
|
||||||
request.ProductSDto.HasDiscount = true;
|
request.HasDiscount = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
var productDiscount = await _repositoryWrapper.SetRepository<ProductDiscount>()
|
var productDiscount = await _repositoryWrapper.SetRepository<ProductDiscount>()
|
||||||
.TableNoTracking
|
.TableNoTracking
|
||||||
.Where(d => d.HasCode == false && d.ProductId == request.ProductSDto.Id && d.ExpireDate.Date >= DateTime.Today.Date)
|
.Where(d => d.HasCode == false && d.ProductId == request.Id && d.ExpireDate.Date >= DateTime.Today.Date)
|
||||||
.OrderByDescending(d => d.CreatedAt)
|
.OrderByDescending(d => d.CreatedAt)
|
||||||
.FirstOrDefaultAsync(cancellationToken);
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
@ -50,17 +67,75 @@ public class CalculateProductDiscountCommandHandler : IRequestHandler<CalculateP
|
||||||
discountPrice = productDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - productDiscount.DiscountAmount
|
discountPrice = productDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - productDiscount.DiscountAmount
|
||||||
: totalPrice - totalPrice / 100 * productDiscount.DiscountPercent;
|
: totalPrice - totalPrice / 100 * productDiscount.DiscountPercent;
|
||||||
|
|
||||||
request.ProductSDto.HasDiscount = true;
|
request.HasDiscount = true;
|
||||||
if (productDiscount.IsSpecialOffer)
|
if (productDiscount.IsSpecialOffer)
|
||||||
request.ProductSDto.IsSpecial = true;
|
request.IsSpecial = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.ProductSDto.HasDiscount)
|
if (request.HasDiscount)
|
||||||
{
|
{
|
||||||
request.ProductSDto.CostWithDiscount = discountPrice;
|
request.CostWithDiscount = discountPrice;
|
||||||
request.ProductSDto.DiscountPercent = request.ProductSDto.Cost == 0 ? 0 : 100 - 100 * request.ProductSDto.CostWithDiscount / request.ProductSDto.Cost;
|
request.DiscountPercent = request.Cost == 0 ? 0 : 100 - 100 * request.CostWithDiscount / request.Cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.ProductSDto;
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<ProductLDto> CalculateAsync(ProductLDto request, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
double discountPrice = 0;
|
||||||
|
request.CostWithDiscount = request.Cost;
|
||||||
|
double totalPrice = request.Cost;
|
||||||
|
|
||||||
|
var allDiscount = await _repositoryWrapper.SetRepository<Discount>()
|
||||||
|
.TableNoTracking
|
||||||
|
.FirstOrDefaultAsync(d => d.Type == DiscountType.All && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken);
|
||||||
|
|
||||||
|
if (allDiscount != null && !allDiscount.IsExpired())
|
||||||
|
{
|
||||||
|
|
||||||
|
discountPrice = allDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - allDiscount.DiscountAmount :
|
||||||
|
totalPrice - totalPrice / 100 * allDiscount.DiscountPercent;
|
||||||
|
request.HasDiscount = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var categoryDiscount = await _repositoryWrapper.SetRepository<CategoryDiscount>()
|
||||||
|
.TableNoTracking
|
||||||
|
.FirstOrDefaultAsync(d => d.CategoryId == request.CategoryId && d.HasCode == false && d.ExpireDate.Date >= DateTime.Today.Date, cancellationToken);
|
||||||
|
|
||||||
|
if (categoryDiscount != null && !categoryDiscount.IsExpired())
|
||||||
|
{
|
||||||
|
discountPrice = categoryDiscount.AmountType == DiscountAmountType.Amount
|
||||||
|
? totalPrice - categoryDiscount.DiscountAmount : totalPrice - totalPrice / 100 * categoryDiscount.DiscountPercent;
|
||||||
|
request.HasDiscount = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
var productDiscount = await _repositoryWrapper.SetRepository<ProductDiscount>()
|
||||||
|
.TableNoTracking
|
||||||
|
.Where(d => d.HasCode == false && d.ProductId == request.Id && d.ExpireDate.Date >= DateTime.Today.Date)
|
||||||
|
.OrderByDescending(d => d.CreatedAt)
|
||||||
|
.FirstOrDefaultAsync(cancellationToken);
|
||||||
|
|
||||||
|
if (productDiscount != null && !productDiscount.IsExpired())
|
||||||
|
{
|
||||||
|
discountPrice = productDiscount.AmountType == DiscountAmountType.Amount ? totalPrice - productDiscount.DiscountAmount
|
||||||
|
: totalPrice - totalPrice / 100 * productDiscount.DiscountPercent;
|
||||||
|
|
||||||
|
request.HasDiscount = true;
|
||||||
|
if (productDiscount.IsSpecialOffer)
|
||||||
|
{
|
||||||
|
request.IsSpecial = true;
|
||||||
|
request.SpecialOffer = productDiscount.AdaptToSDto();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.HasDiscount)
|
||||||
|
{
|
||||||
|
request.CostWithDiscount = discountPrice;
|
||||||
|
request.DiscountPercent = request.Cost == 0 ? 0 : 100 - 100 * request.CostWithDiscount / request.Cost;
|
||||||
|
}
|
||||||
|
|
||||||
|
return request;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -43,4 +43,4 @@ public sealed record UpdateProductCommand(
|
||||||
|
|
||||||
public sealed record DeleteProductCommand(Guid Id) : IRequest<bool>;
|
public sealed record DeleteProductCommand(Guid Id) : IRequest<bool>;
|
||||||
|
|
||||||
public sealed record CalculateProductDiscountCommand(ProductSDto ProductSDto) : IRequest<ProductSDto>;
|
public sealed record CalculateProductDiscountCommand(object Product) : IRequest<bool>;
|
|
@ -8,7 +8,11 @@ public class ProductLDto : BaseDto<ProductLDto,Product>
|
||||||
public string ExpertCheck { get; set; } = string.Empty;
|
public string ExpertCheck { get; set; } = string.Empty;
|
||||||
public string Tags { get; set; } = string.Empty;
|
public string Tags { get; set; } = string.Empty;
|
||||||
public string Warranty { get; set; } = string.Empty;
|
public string Warranty { get; set; } = string.Empty;
|
||||||
|
public double CostWithDiscount { get; set; }
|
||||||
|
public double DiscountPercent { get; set; }
|
||||||
public bool BeDisplayed { get; set; }
|
public bool BeDisplayed { get; set; }
|
||||||
|
public bool IsSpecial { get; set; }
|
||||||
|
public bool HasDiscount { get; set; }
|
||||||
public bool HasExpressDelivery { get; set; }
|
public bool HasExpressDelivery { get; set; }
|
||||||
public double Cost { get; set; }
|
public double Cost { get; set; }
|
||||||
public double PackingCost { get; set; }
|
public double PackingCost { get; set; }
|
||||||
|
|
|
@ -5,10 +5,12 @@ namespace NetinaShop.Repository.Handlers.Products;
|
||||||
public class GetProductQueryHandler : IRequestHandler<GetProductQuery, GetProductResponseDto>
|
public class GetProductQueryHandler : IRequestHandler<GetProductQuery, GetProductResponseDto>
|
||||||
{
|
{
|
||||||
private readonly IRepositoryWrapper _repositoryWrapper;
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
||||||
|
private readonly IMediator _mediator;
|
||||||
|
|
||||||
public GetProductQueryHandler(IRepositoryWrapper repositoryWrapper)
|
public GetProductQueryHandler(IRepositoryWrapper repositoryWrapper,IMediator mediator)
|
||||||
{
|
{
|
||||||
_repositoryWrapper = repositoryWrapper;
|
_repositoryWrapper = repositoryWrapper;
|
||||||
|
_mediator = mediator;
|
||||||
}
|
}
|
||||||
public async Task<GetProductResponseDto> Handle(GetProductQuery request, CancellationToken cancellationToken)
|
public async Task<GetProductResponseDto> Handle(GetProductQuery request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
@ -20,18 +22,7 @@ public class GetProductQueryHandler : IRequestHandler<GetProductQuery, GetProduc
|
||||||
if (ent == null)
|
if (ent == null)
|
||||||
throw new AppException("Product not found", ApiResultStatusCode.NotFound);
|
throw new AppException("Product not found", ApiResultStatusCode.NotFound);
|
||||||
|
|
||||||
var specialOffer = await _repositoryWrapper.SetRepository<ProductDiscount>()
|
await _mediator.Send(new CalculateProductDiscountCommand(ent), cancellationToken);
|
||||||
.TableNoTracking
|
|
||||||
.Where(pd => pd.IsSpecialOffer && pd.ProductId == ent.Id)
|
|
||||||
.Select(DiscountMapper.ProjectToSDto)
|
|
||||||
.FirstOrDefaultAsync(cancellationToken);
|
|
||||||
|
|
||||||
ent.SpecialOffer = specialOffer;
|
|
||||||
|
|
||||||
if (ent.SpecialOffer == null || ent.SpecialOffer.ExpireDate.Date < DateTime.Today.Date)
|
|
||||||
ent.IsSpecialOffer = false;
|
|
||||||
else
|
|
||||||
ent.IsSpecialOffer = true;
|
|
||||||
|
|
||||||
var response = new GetProductResponseDto
|
var response = new GetProductResponseDto
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue