Files
pos-system/services/merchant-service-net/src/MerchantService.API/Application/Queries/Admin/GetMerchantStatisticsQuery.cs

73 lines
2.6 KiB
C#

// EN: Query to get merchant statistics for admin dashboard.
// VI: Query để lấy thống kê merchant cho dashboard admin.
using MediatR;
using Microsoft.EntityFrameworkCore;
using MerchantService.Domain.AggregatesModel.MerchantAggregate;
using MerchantService.Domain.AggregatesModel.ShopAggregate;
using MerchantService.Infrastructure;
namespace MerchantService.API.Application.Queries.Admin;
/// <summary>
/// EN: Query to get merchant statistics (Admin only).
/// VI: Query để lấy thống kê merchant (chỉ Admin).
/// </summary>
public record GetMerchantStatisticsQuery : IRequest<AdminMerchantStatisticsDto>;
/// <summary>
/// EN: Handler for GetMerchantStatisticsQuery.
/// VI: Handler cho GetMerchantStatisticsQuery.
/// </summary>
public class GetMerchantStatisticsQueryHandler : IRequestHandler<GetMerchantStatisticsQuery, AdminMerchantStatisticsDto>
{
private readonly MerchantServiceContext _context;
public GetMerchantStatisticsQueryHandler(MerchantServiceContext context)
{
_context = context;
}
public async Task<AdminMerchantStatisticsDto> Handle(
GetMerchantStatisticsQuery request,
CancellationToken cancellationToken)
{
var merchantsQuery = _context.Merchants.Where(m => !m.IsDeleted);
var totalMerchants = await merchantsQuery.CountAsync(cancellationToken);
var pendingApproval = await merchantsQuery
.CountAsync(m => m.StatusId == MerchantStatus.PendingApproval.Id, cancellationToken);
var active = await merchantsQuery
.CountAsync(m => m.StatusId == MerchantStatus.Active.Id, cancellationToken);
var suspended = await merchantsQuery
.CountAsync(m => m.StatusId == MerchantStatus.Suspended.Id, cancellationToken);
var banned = await merchantsQuery
.CountAsync(m => m.StatusId == MerchantStatus.Banned.Id, cancellationToken);
var shopsQuery = _context.Shops.Where(s => !s.IsDeleted);
var totalShops = await shopsQuery.CountAsync(cancellationToken);
// EN: Active shops (published/active)
// VI: Shops đang hoạt động
var activeShops = await shopsQuery
.CountAsync(s => s.StatusId == ShopStatus.Active.Id, cancellationToken);
var totalStaff = await _context.MerchantStaff.CountAsync(cancellationToken);
return new AdminMerchantStatisticsDto(
totalMerchants,
pendingApproval,
active,
suspended,
banned,
totalShops,
activeShops,
totalStaff);
}
}