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

84 lines
4.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Brizco.Domain.Entities.Tasks;
using Microsoft.Extensions.Logging;
using Quartz;
using Task = System.Threading.Tasks.Task;
namespace Brizco.Core.QuartzServices;
public class ShiftPlanNotificationScheduledJob(
ILogger<ShiftPlanNotificationScheduledJob> logger,
IRepositoryWrapper repositoryWrapper,
IMediator mediator)
: IJob
{
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
.Where(f => f.Id == shiftPlanId)
.Select(ShiftPlanMapper.ProjectToSDto)
.FirstOrDefaultAsync();
if (shiftPlan == null)
throw new Exception("Shift plan not found");
var shiftPlanUsers = await repositoryWrapper.SetRepository<ShiftPlanUser>()
.TableNoTracking
.Where(f => f.ShiftPlanId == shiftPlanId)
.Select(ShiftPlanUserMapper.ProjectToSDto)
.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 = $"{shiftPlanUser.UserFullName} {shiftPlan.ShiftTitle} {shiftPlan.PlanFor.ToPersianDateTime().ToLongDateTimeString()} تموم شده است و {unDoneCount} - عدد از تسک های شما کاری روشون انجام نشده ، خطر سوپروایزر در کمین است";
await mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
}
string superVisorAfterShiftMessage = $"{shiftPlan.SupervisorFullName} محترم {shiftPlan.ShiftTitle} تموم شد. برو به کار بچه ها نمره بده و مو رو از ماست بکش و کامنت یادت نره";
await mediator.Send(new CreateNotificationCommand(superVisorAfterShiftMessage, shiftPlan.SupervisorId, shiftPlan.ComplexId));
break;
case ShiftPlanNotifyType.AfterStartShift2Hour:
var activities2AfterStartShift = await repositoryWrapper.SetRepository<Activity>().TableNoTracking
.Where(a => a.ShiftPlanId == shiftPlanId).ToListAsync();
foreach (var shiftPlanUser in shiftPlanUsers)
{
var unDoneCount = activities2AfterStartShift.Count(a => a.UserId == shiftPlanUser.UserId && a.IsDone == false);
if (unDoneCount == 0)
continue;
string message = $"{shiftPlanUser.UserFullName} دوساعت از {shiftPlan.ShiftTitle} گذشته ، اون انگشت و بزن روی تیک تسک ها که وقت طلاس مشتیییییی ";
await mediator.Send(new CreateNotificationCommand(message, shiftPlanUser.UserId, shiftPlan.ComplexId));
}
break;
default:
return;
}
}
catch (Exception e)
{
logger.LogError(e.Message);
}
}
}