74 lines
3.1 KiB
C#
74 lines
3.1 KiB
C#
using Brizco.Domain.CommandQueries.Queries;
|
|
using Brizco.Domain.Entities.Shift;
|
|
using Brizco.Domain.Entities.Task;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Brizco.Core.EntityServices;
|
|
|
|
public class ActivityService : IActivityService
|
|
{
|
|
private readonly IMediator _mediator;
|
|
private readonly IRepositoryWrapper _repositoryWrapper;
|
|
|
|
public ActivityService(IMediator mediator,IRepositoryWrapper repositoryWrapper)
|
|
{
|
|
_mediator = mediator;
|
|
_repositoryWrapper = repositoryWrapper;
|
|
}
|
|
public async Task<bool> CreateActivitiesByShiftPlan(Guid shiftPlanId,CancellationToken cancellationToken)
|
|
{
|
|
var shiftPlan = await _mediator.Send(new GetShiftPlanQuery(shiftPlanId), cancellationToken);
|
|
if (shiftPlan.Id == Guid.Empty)
|
|
return false;
|
|
var tasks = await _repositoryWrapper.SetRepository<Brizco.Domain.Entities.Task.Task>()
|
|
.ExecuteCommand(
|
|
$@"SELECT t.""Id"", t.""Amount"", t.""AmountType"", t.""ComplexId"",
|
|
t.""CreatedAt"", t.""CreatedBy"", t.""Description"", t.""Discriminator"", t.""HasDisposed"", t.""IsDisposable"", t.""IsRemoved"", t.""ModifiedAt"",
|
|
t.""ModifiedBy"", t.""RemovedAt"", t.""RemovedBy"", t.""ScheduleType"", t.""SetFor"", t.""Title"", t.""Type"", t.""DoneAt"", t.""IsDone"",
|
|
t.""PerformanceDescription"", t.""ShiftId"", t.""Status"", t.""UserId""
|
|
FROM public.""Tasks"" t
|
|
INNER JOIN public.""TaskShifts"" t1 ON t.""Id"" = t1.""TaskId""
|
|
INNER JOIN public.""TaskDays"" t2 ON t.""Id"" = t1.""TaskId""
|
|
INNER JOIN public.""TaskRoutines"" t3 ON t.""Id"" = t1.""TaskId""
|
|
AND {shiftPlan.ShiftId} = t1.""ShiftId""
|
|
AND {shiftPlan.RoutineId} = t3.""RoutineId""
|
|
AND {shiftPlan.PlanFor.DayOfWeek} = t2.""DayOfWeek""
|
|
GROUP BY t.""Id""").ToListAsync(cancellationToken);
|
|
|
|
var shiftPlanPositions = await _repositoryWrapper.SetRepository<ShiftPlanUser>()
|
|
.TableNoTracking
|
|
.Where(spu => spu.ShiftPlanId == shiftPlan.Id)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
foreach (var task in tasks)
|
|
{
|
|
var taskPositions = await _repositoryWrapper.SetRepository<TaskPosition>()
|
|
.TableNoTracking
|
|
.Where(tp => tp.TaskId == task.Id)
|
|
.ToListAsync(cancellationToken);
|
|
foreach (var taskPosition in taskPositions)
|
|
{
|
|
var fundedUser = shiftPlanPositions.FirstOrDefault(spu => spu.PositionId == taskPosition.PositionId);
|
|
if (fundedUser != null)
|
|
{
|
|
var activity = Activity.Create(ActivityStatus.Created,
|
|
DateTime.MinValue,
|
|
string.Empty,
|
|
task.Title,
|
|
task.Description,
|
|
task.Type,
|
|
task.IsDisposable,
|
|
task.SetFor,
|
|
task.HasDisposed,
|
|
task.Amount,
|
|
task.AmountType,
|
|
task.ComplexId,
|
|
task.ScheduleType);
|
|
activity.SetUser(fundedUser.UserId);
|
|
}
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |