feat: add Dashboard service with manager summary statistics
This commit is contained in:
68
Complex.Application/Services/DashboardService.cs
Normal file
68
Complex.Application/Services/DashboardService.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using Complex.Application;
|
||||||
|
using Complex.Common.Dto;
|
||||||
|
using Complex.Domain.Entities;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Complex.Application.Services.Dashboard
|
||||||
|
{
|
||||||
|
public interface IDashboardService
|
||||||
|
{
|
||||||
|
ResultDto<DashboardSummaryDto> GetManagerSummary(Guid complexId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DashboardService : IDashboardService
|
||||||
|
{
|
||||||
|
private readonly IComplexDBContext _complexDBContext;
|
||||||
|
|
||||||
|
public DashboardService(IComplexDBContext complexDBContext)
|
||||||
|
{
|
||||||
|
_complexDBContext = complexDBContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResultDto<DashboardSummaryDto> GetManagerSummary(Guid complexId)
|
||||||
|
{
|
||||||
|
var units = _complexDBContext.Units.Where(u => u.ComplexId == complexId);
|
||||||
|
|
||||||
|
var totalUnits = units.Count();
|
||||||
|
var ownerUnits = units.Count(u => u.OwnerId != null);
|
||||||
|
var tenantUnits = units.Count(u => u.TenantId != null);
|
||||||
|
|
||||||
|
var activeCostCycles = _complexDBContext.CostCycles
|
||||||
|
.Count(c => c.ComplexId == complexId && c.EndDate >= DateOnly.FromDateTime(DateTime.Now));
|
||||||
|
|
||||||
|
var unitPersonMobileNumbers = units
|
||||||
|
.Where(u => u.OwnerId != null || u.TenantId != null)
|
||||||
|
.Select(u => u.OwnerId)
|
||||||
|
.Union(units.Where(u => u.TenantId != null).Select(u => u.TenantId))
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var totalPersons = _complexDBContext.Persons
|
||||||
|
.Count(p => unitPersonMobileNumbers.Contains(p.Id));
|
||||||
|
|
||||||
|
var result = new DashboardSummaryDto
|
||||||
|
{
|
||||||
|
TotalUnits = totalUnits,
|
||||||
|
OwnerUnits = ownerUnits,
|
||||||
|
TenantUnits = tenantUnits,
|
||||||
|
ActiveCostCycles = activeCostCycles,
|
||||||
|
TotalPersons = totalPersons
|
||||||
|
};
|
||||||
|
|
||||||
|
return new ResultDto<DashboardSummaryDto>
|
||||||
|
{
|
||||||
|
Data = result,
|
||||||
|
IsSuccess = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class DashboardSummaryDto
|
||||||
|
{
|
||||||
|
public int TotalUnits { get; set; }
|
||||||
|
public int OwnerUnits { get; set; }
|
||||||
|
public int TenantUnits { get; set; }
|
||||||
|
public int ActiveCostCycles { get; set; }
|
||||||
|
public int TotalPersons { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Complex.EndPoint/Controllers/DashboardController.cs
Normal file
28
Complex.EndPoint/Controllers/DashboardController.cs
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
using Complex.Application.Services.Dashboard;
|
||||||
|
using Complex.Common.Dto;
|
||||||
|
using Complex.EndPoint.Utility;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Complex.EndPoint.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[ApiVersion("1")]
|
||||||
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
||||||
|
[Authorize]
|
||||||
|
public class DashboardController : Controller
|
||||||
|
{
|
||||||
|
private readonly IDashboardService _dashboardService;
|
||||||
|
|
||||||
|
public DashboardController(IDashboardService dashboardService)
|
||||||
|
{
|
||||||
|
_dashboardService = dashboardService;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public ResultDto<DashboardSummaryDto> ManagerSummary(Guid complexId)
|
||||||
|
{
|
||||||
|
return _dashboardService.GetManagerSummary(complexId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user