Api/Brizco.Core/QuartzServices/ShiftPlanNotificationSchedu...

80 lines
4.0 KiB
C#
Raw Normal View History

using Brizco.Domain.Entities.ShiftPlans;
using Brizco.Domain.Entities.Tasks;
using Microsoft.Extensions.Logging;
using Quartz;
using Task = System.Threading.Tasks.Task;
namespace Brizco.Core.QuartzServices;
public class ShiftPlanNotificationScheduledJob : IJob
{
private readonly ILogger<ShiftPlanNotificationScheduledJob> _logger;
private readonly IRepositoryWrapper _repositoryWrapper;
private readonly IMediator _mediator;
public ShiftPlanNotificationScheduledJob(ILogger<ShiftPlanNotificationScheduledJob> logger,IRepositoryWrapper repositoryWrapper,IMediator mediator)
{
_logger = logger;
_repositoryWrapper = repositoryWrapper;
_mediator = mediator;
}
public async Task Execute(IJobExecutionContext context)
{
try
{
var notifyType = (ShiftPlanNotifyType)int.Parse(context.JobDetail.Key.Name);
var shiftPlanId = Guid.Parse(context.JobDetail.Key.Group);
var shiftPlan = await _repositoryWrapper.SetRepository<ShiftPlan>().TableNoTracking
.FirstOrDefaultAsync(f => f.Id == shiftPlanId);
if (shiftPlan == null)
throw new Exception("Shift plan not found");
var shiftPlanUsers = await _repositoryWrapper.SetRepository<ShiftPlanUser>()
.TableNoTracking
.Where(f => f.ShiftPlanId == shiftPlanId)
.ToListAsync();
var superVisorId = shiftPlan.SupervisorId;
switch (notifyType)
{
case ShiftPlanNotifyType.None:
return;
break;
case ShiftPlanNotifyType.BeforeEndShift30Min:
var activities = await _repositoryWrapper.SetRepository<Activity>().TableNoTracking
.Where(a => a.ShiftPlanId == shiftPlanId).ToListAsync();
foreach (var shiftPlanUser in shiftPlanUsers)
{
var unDoneCount = activities.Count(a => a.UserId == shiftPlanUser.UserId && a.IsDone == false);
if(unDoneCount == 0)
continue;
string message = $"شما تعداد {unDoneCount} وظیفه انجام نشده در شیفت مورد نظر دارید ، نیم ساعت به پایان شیفت مانده است";
await _mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
}
break;
case ShiftPlanNotifyType.EndOfShift:
var activitiesEndShift = await _repositoryWrapper.SetRepository<Activity>().TableNoTracking
.Where(a => a.ShiftPlanId == shiftPlanId).ToListAsync();
foreach (var shiftPlanUser in shiftPlanUsers)
{
var unDoneCount = activitiesEndShift.Count(a => a.UserId == shiftPlanUser.UserId && a.IsDone == false);
if (unDoneCount == 0)
continue;
string message = $"شما تعداد {unDoneCount} وظیفه انجام نشده در شیفت مورد نظر دارید";
await _mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
}
string superVisorAfterShiftMessage = $"سوپر وایزر محترم ، شیفت مورد نظر به پایان رسیده است ، لطفا فعالیت ها را رسیدگی کنید";
await _mediator.Send(new CreateNotificationCommand(superVisorAfterShiftMessage, shiftPlan.SupervisorId, shiftPlan.ComplexId));
break;
case ShiftPlanNotifyType.AfterStartShift2Hour:
break;
default:
return;
}
}
catch (Exception e)
{
_logger.LogError(e.Message);
}
}
}