2024-07-08 17:45:17 +03:30
|
|
|
|
using Brizco.Domain.Entities.Tasks;
|
|
|
|
|
|
using Task = Brizco.Domain.Entities.Tasks.Task;
|
2023-12-09 09:54:34 +03:30
|
|
|
|
|
|
|
|
|
|
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
|
2023-12-09 09:54:34 +03:30
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
private readonly RoleManager<ApplicationRole> _roleManager = roleManager;
|
|
|
|
|
|
|
2024-06-30 22:29:57 +03:30
|
|
|
|
public async Task<AppDashboardPageDto> GetAppDashboardAsync(CancellationToken cancellationToken = default)
|
2023-12-09 09:54:34 +03:30
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (currentUserService.UserId == null)
|
2023-12-09 09:54:34 +03:30
|
|
|
|
throw new AppException("User id is null ");
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (!Guid.TryParse(currentUserService.UserId, out Guid userId))
|
2023-12-09 09:54:34 +03:30
|
|
|
|
throw new AppException("User id is wrong");
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
throw new AppException("Complex id is wrong");
|
2023-12-09 09:54:34 +03:30
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var todayActivities = await repositoryWrapper.SetRepository<Activity>()
|
2023-12-09 09:54:34 +03:30
|
|
|
|
.TableNoTracking
|
2024-04-25 16:56:15 +03:30
|
|
|
|
.Where(a => a.ComplexId == complexId && a.SetFor.Date == DateTime.Today.Date)
|
2023-12-09 09:54:34 +03:30
|
|
|
|
.ToListAsync(cancellationToken);
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var todayShiftPlans = await repositoryWrapper.SetRepository<ShiftPlan>()
|
2024-04-25 02:33:17 +03:30
|
|
|
|
.TableNoTracking
|
2024-04-25 16:56:15 +03:30
|
|
|
|
.Where(a => a.PlanFor.Date == DateTime.Today.Date && a.ComplexId == complexId)
|
2024-04-25 02:33:17 +03:30
|
|
|
|
.Select(ShiftPlanMapper.ProjectToSDto)
|
|
|
|
|
|
.ToListAsync(cancellationToken);
|
2024-06-02 11:26:18 +03:30
|
|
|
|
|
2024-06-30 22:29:57 +03:30
|
|
|
|
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 );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-04-25 02:33:17 +03:30
|
|
|
|
var names = new List<string>();
|
|
|
|
|
|
names.AddRange(todayShiftPlans.SelectMany(sp => sp.Users).Select(s => s.UserFullName).ToList());
|
2023-12-11 14:32:29 +03:30
|
|
|
|
var page = new AppDashboardPageDto
|
2023-12-09 09:54:34 +03:30
|
|
|
|
{
|
2024-06-30 22:29:57 +03:30
|
|
|
|
DoneActivitiesToday = todayActivities.Count(t => t.IsDone),
|
|
|
|
|
|
TotalActivitiesToday = todayActivities.Count,
|
|
|
|
|
|
UnDoneActivitiesToday = todayActivities.Count(t => t.IsDone!),
|
2024-04-25 02:33:17 +03:30
|
|
|
|
TotalShiftToday = todayShiftPlans.Count,
|
|
|
|
|
|
TodayStaffNames = names,
|
2024-06-30 22:29:57 +03:30
|
|
|
|
TotalStaffToday = todayShiftPlans.SelectMany(sp => sp.Users).GroupBy(u=>u.UserId).Count(),
|
|
|
|
|
|
ShiftPlans = todayShiftPlans
|
2023-12-09 09:54:34 +03:30
|
|
|
|
};
|
2024-05-06 14:08:15 +03:30
|
|
|
|
|
|
|
|
|
|
var currentShift = todayShiftPlans.FirstOrDefault(s => s.EndAt >= DateTime.Now.TimeOfDay && s.StartAt <= DateTime.Now.TimeOfDay);
|
|
|
|
|
|
if (currentShift != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
page.CurrentShift = currentShift.ShiftTitle;
|
2024-06-30 22:29:57 +03:30
|
|
|
|
page.CurrentShiftId = currentShift.Id;
|
2024-05-06 14:08:15 +03:30
|
|
|
|
page.CurrentPosition = currentShift.Users.FirstOrDefault(f => f.UserId == userId)?.PositionName ?? string.Empty;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (currentUserService.Permissions != null)
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
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))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasRoutine = await repositoryWrapper.SetRepository<Routine>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.TableNoTracking
|
2024-05-06 14:08:15 +03:30
|
|
|
|
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
|
|
|
|
|
|
if (hasRoutine)
|
2023-12-11 14:32:29 +03:30
|
|
|
|
completeStepCount++;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentStep.IsNullOrEmpty())
|
|
|
|
|
|
currentStep = "تکمیل بخش روتین ها";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (currentUserService.Permissions.Exists(s => s == ApplicationPermission.ManageSections))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasSection = await repositoryWrapper.SetRepository<Section>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.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))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasPosition = await repositoryWrapper.SetRepository<Position>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.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))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasStaff = await repositoryWrapper.SetRepository<ComplexUser>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.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))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasShift = await repositoryWrapper.SetRepository<Shift>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.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))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasTask = await repositoryWrapper.SetRepository<Task>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.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))
|
2023-12-11 14:32:29 +03:30
|
|
|
|
{
|
|
|
|
|
|
totalStepCount++;
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var hasStaff = await repositoryWrapper.SetRepository<ShiftPlan>()
|
2023-12-11 14:32:29 +03:30
|
|
|
|
.TableNoTracking
|
|
|
|
|
|
.AnyAsync(r => r.ComplexId == complexId, cancellationToken);
|
|
|
|
|
|
if (hasStaff)
|
|
|
|
|
|
completeStepCount++;
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (currentStep.IsNullOrEmpty())
|
|
|
|
|
|
currentStep = "تکمیل بخش شیفت بندی ها";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-05-27 00:35:53 +03:30
|
|
|
|
page.SignUpCompletePercent = completeStepCount != 0 ? ((totalStepCount * 100) / completeStepCount) : 0;
|
2023-12-11 14:32:29 +03:30
|
|
|
|
page.CurrentSignUpStep = currentStep;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return page;
|
2023-12-09 09:54:34 +03:30
|
|
|
|
}
|
2024-05-20 22:51:01 +03:30
|
|
|
|
|
2024-06-30 22:29:57 +03:30
|
|
|
|
public async Task<List<AppShiftingPageDayDto>> GetShiftingPageAsync(Guid routineId,CancellationToken cancellationToken = default)
|
2024-05-20 22:51:01 +03:30
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (currentUserService.ComplexId == null)
|
2024-05-20 22:51:01 +03:30
|
|
|
|
throw new AppException("ComplexId is null", ApiResultStatusCode.NotFound);
|
2024-08-09 21:42:04 +03:30
|
|
|
|
if (!Guid.TryParse(currentUserService.ComplexId, out Guid complexId))
|
2024-05-20 22:51:01 +03:30
|
|
|
|
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),
|
2024-05-20 22:51:01 +03:30
|
|
|
|
cancellationToken);
|
|
|
|
|
|
|
2024-05-27 00:35:53 +03:30
|
|
|
|
for (int i = 0 ; i < 15; i++ , day = day.AddDays(1))
|
2024-05-20 22:51:01 +03:30
|
|
|
|
{
|
|
|
|
|
|
var item = new AppShiftingPageDayDto
|
|
|
|
|
|
{
|
|
|
|
|
|
DateTime = DateTimeExtensions.DateTimeToUnixTimeStamp(day)
|
|
|
|
|
|
};
|
2024-05-27 00:35:53 +03:30
|
|
|
|
shifts.Where(s=>s.Day == day.DayOfWeek)
|
|
|
|
|
|
.SelectMany(s=>s.Shifts).OrderBy(s=>s.StartAt).ToList().ForEach(s=>item.Shifts.Add(s.Clone()));
|
2024-05-20 22:51:01 +03:30
|
|
|
|
foreach (var shift in item.Shifts)
|
|
|
|
|
|
{
|
2024-08-09 21:42:04 +03:30
|
|
|
|
var existedShiftPlan = await repositoryWrapper.SetRepository<ShiftPlan>()
|
2024-05-20 22:51:01 +03:30
|
|
|
|
.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;
|
|
|
|
|
|
}
|
2023-12-09 09:54:34 +03:30
|
|
|
|
}
|