Api/Brizco.Core/CoreServices/PageService.cs

210 lines
9.2 KiB
C#
Raw Permalink Normal View History

using Brizco.Domain.Entities.Tasks;
using Task = Brizco.Domain.Entities.Tasks.Task;
namespace Brizco.Core.CoreServices;
2024-08-09 21:42:04 +03:30
public class PageService(
ICurrentUserService currentUserService,
IRepositoryWrapper repositoryWrapper,
RoleManager<ApplicationRole> roleManager,
IMediator mediator)
: IPageService
{
2024-08-09 21:42:04 +03:30
private readonly RoleManager<ApplicationRole> _roleManager = roleManager;
public async Task<AppDashboardPageDto> GetAppDashboardAsync(CancellationToken cancellationToken = default)
{
2024-08-09 21:42:04 +03:30
if (currentUserService.UserId == null)
throw new AppException("User id is null ");
2024-08-09 21:42:04 +03:30
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
throw new AppException("User id is wrong");
2024-08-09 21:42:04 +03:30
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
throw new AppException("Complex id is wrong");
2024-08-09 21:42:04 +03:30
var todayActivities = await repositoryWrapper.SetRepository<Activity>()
.TableNoTracking
.Where(a => a.ComplexId == complexId && a.SetFor.Date == DateTime.Today.Date)
.ToListAsync(cancellationToken);
2024-08-09 21:42:04 +03:30
var todayShiftPlans = await repositoryWrapper.SetRepository<ShiftPlan>()
.TableNoTracking
.Where(a => a.PlanFor.Date == DateTime.Today.Date && a.ComplexId == complexId)
.Select(ShiftPlanMapper.ProjectToSDto)
.ToListAsync(cancellationToken);
foreach (var shiftPlan in todayShiftPlans)
{
shiftPlan.DoneActivitiesCount = todayActivities.Count(a=>a.ShiftPlanId == shiftPlan.Id && a.IsDone);
shiftPlan.UndoneActivitiesCount = todayActivities.Count(a=>a.ShiftPlanId == shiftPlan.Id && !a.IsDone);
shiftPlan.TotalActivitiesCount = todayActivities.Count(a=>a.ShiftPlanId == shiftPlan.Id );
}
var names = new List<string>();
names.AddRange(todayShiftPlans.SelectMany(sp => sp.Users).Select(s => s.UserFullName).ToList());
var page = new AppDashboardPageDto
{
DoneActivitiesToday = todayActivities.Count(t => t.IsDone),
TotalActivitiesToday = todayActivities.Count,
UnDoneActivitiesToday = todayActivities.Count(t => t.IsDone!),
TotalShiftToday = todayShiftPlans.Count,
TodayStaffNames = names,
TotalStaffToday = todayShiftPlans.SelectMany(sp => sp.Users).GroupBy(u=>u.UserId).Count(),
ShiftPlans = todayShiftPlans
};
var currentShift = todayShiftPlans.FirstOrDefault(s => s.EndAt >= DateTime.Now.TimeOfDay && s.StartAt <= DateTime.Now.TimeOfDay);
if (currentShift != null)
{
page.CurrentShift = currentShift.ShiftTitle;
page.CurrentShiftId = currentShift.Id;
page.CurrentPosition = currentShift.Users.FirstOrDefault(f => f.UserId == userId)?.PositionName ?? string.Empty;
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions != null)
{
int totalStepCount = 0;
int completeStepCount = 0;
string currentStep = string.Empty;
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageRoutines))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasRoutine = await repositoryWrapper.SetRepository<Routine>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasRoutine)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش روتین ها";
}
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageSections))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasSection = await repositoryWrapper.SetRepository<Section>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasSection)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش سکشن ها";
}
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManagePositions))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasPosition = await repositoryWrapper.SetRepository<Position>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasPosition)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش پوزیشن ها";
}
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageStaffs))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasStaff = await repositoryWrapper.SetRepository<ComplexUser>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasStaff)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش کاربر ها";
}
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageShifts))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasShift = await repositoryWrapper.SetRepository<Shift>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasShift)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش شیفت ها";
}
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageTasks))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasTask = await repositoryWrapper.SetRepository<Task>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasTask)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش تسک ها";
}
}
2024-08-09 21:42:04 +03:30
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageShiftPlans))
{
totalStepCount++;
2024-08-09 21:42:04 +03:30
var hasStaff = await repositoryWrapper.SetRepository<ShiftPlan>()
.TableNoTracking
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
if (hasStaff)
completeStepCount++;
else
{
if (currentStep.IsNullOrEmpty())
currentStep = "تکمیل بخش شیفت بندی ها";
}
}
page.SignUpCompletePercent = completeStepCount != 0 ? ((totalStepCount * 100) / completeStepCount) : 0;
page.CurrentSignUpStep = currentStep;
}
return page;
}
public async Task<List<AppShiftingPageDayDto>> GetShiftingPageAsync(Guid routineId,CancellationToken cancellationToken = default)
{
2024-08-09 21:42:04 +03:30
if (currentUserService.ComplexId == null)
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
2024-08-09 21:42:04 +03:30
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
throw new AppException("ComplexId is wrong", ApiResultStatusCode.NotFound);
var days = new List<AppShiftingPageDayDto>();
DateTime day = DateTime.Today;
2024-08-09 21:42:04 +03:30
var shifts = await mediator.Send(new GetRoutineShiftsQuery(routineId, 0),
cancellationToken);
for (int i = 0 ; i < 15; i++ , day = day.AddDays(1))
{
var item = new AppShiftingPageDayDto
{
DateTime = DateTimeExtensions.DateTimeToUnixTimeStamp(day)
};
shifts.Where(s=>s.Day == day.DayOfWeek)
.SelectMany(s=>s.Shifts).OrderBy(s=>s.StartAt).ToList().ForEach(s=>item.Shifts.Add(s.Clone()));
foreach (var shift in item.Shifts)
{
2024-08-09 21:42:04 +03:30
var existedShiftPlan = await repositoryWrapper.SetRepository<ShiftPlan>()
.TableNoTracking
.FirstOrDefaultAsync(s => s.ShiftId == shift.Id && s.PlanFor.Date == day.Date, cancellationToken);
shift.CurrentShiftPlanId = existedShiftPlan?.Id ?? default;
shift.HasCurrentShiftPlan = shift.CurrentShiftPlanId != default;
}
item.TotalShifts = item.Shifts.Count;
item.TotalShiftPlans = item.Shifts.Count(s => s.CurrentShiftPlanId != default);
days.Add(item);
}
return days;
}
}