Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Jobs/AutoRemoveAppointmentJob.cs
2024-12-18 11:53:32 +08:00

96 lines
3.8 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Visitor.Entity;
using Dlhis.Entity.Visitor.Enums;
using Dlhis.ScheduledTask.Attributes;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 访客预约移除
/// </summary>
[RecurringJob(nameof(AutoRemoveAppointmentJob), "0 0/5 * * * ?", Name = "访客预约移除")]
public class AutoRemoveAppointmentJob : BaseJob
{
/// <inheritdoc />
public AutoRemoveAppointmentJob(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <inheritdoc />
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var now = DateTime.Now;
var details = await DbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(x => (x.IsEnterDoorArea && x.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived) ||
(x.IsEnterWorkplace && x.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Arrived))
.ToListAsync();
foreach (var detail in details)
{
var areaCodes = GetValidAreaCodes(detail).ToList();
if (areaCodes.IsEmpty())
{
continue;
}
var existsAreaCodes = await DbContext.Queryable<AppointmentAreaEntity>().Where(x => areaCodes.Contains(x.CustomCode)).Select(x => x.CustomCode).ToListAsync();
//当前分配的设备组code
var deviceGroupCodes = await DbContext.Queryable<AppointmentAreaAssignmentEntity>()
.Where(x => existsAreaCodes.Contains(x.CustomCode))
.Select(x => x.DeviceGroupCode)
.ToListAsync();
var deviceCodes = await DbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => deviceGroupCodes.Contains(x.GroupCode)).Select(x => x.DeviceCode).ToListAsync();
var deviceUniqueNos = await DbContext.Queryable<DeviceEntity>().Where(x => deviceCodes.Contains(x.Code)).Select(x => new { x.UniqueNo, x.TenantId }).ToListAsync();
await DbContext.InsertAsync(deviceUniqueNos.Select(x => new DeviceTaskEntity
{
DeviceType = DeviceTypeEnumType.Door,
DeviceUniqueNo = x.UniqueNo,
Status = DeviceTaskStatusEnumType.Wait,
UserNo = detail.UserNo,
Type = DeviceTaskEnumType.Delete,
Priority = 100,
TenantId = x.TenantId,
Source = "定时任务判断触发移除",
Version = 0
}).ToList());
if (detail.IsEnterDoorArea && detail.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived && detail.DoorEnd <= now)
{
detail.DoorStatus = VisitorAppointmentDetailStatusEnumType.Leave;
}
if (detail.IsEnterWorkplace && detail.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Arrived && detail.WorkplaceEnd <= now)
{
detail.WorkplaceStatus = VisitorAppointmentDetailStatusEnumType.Leave;
}
await DbContext.UpdateAsync(detail);
}
}
private static IEnumerable<string> GetValidAreaCodes(VisitorAppointmentDetailEntity detail)
{
var now = DateTime.Now;
if (detail.IsEnterDoorArea && detail.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived && detail.DoorEnd <= now)
{
yield return detail.ApplyDoorArea;
}
if (detail.IsEnterWorkplace && detail.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Arrived && detail.WorkplaceEnd <= now)
{
foreach (var item in detail.ApplyWorkplaceAreas)
{
yield return item;
}
}
}
}