Files
jinko.yikatong/Src/Dlhis.ScheduledTask/Jobs/AutoConfirmAppointmentJob.cs
2025-05-30 14:26:23 +08:00

82 lines
3.3 KiB
C#

using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Visitor.Entity;
using Dlhis.Entity.Visitor.Enums;
using Dlhis.ScheduledTask.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
/// <summary>
/// 访客预约移除
/// </summary>
[RecurringJob(nameof(AutoConfirmAppointmentJob), "0 0/5 * * * ?", Name = "访客预约自动处理车间")]
public class AutoConfirmAppointmentJob : BaseJob
{
/// <inheritdoc />
public AutoConfirmAppointmentJob(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <inheritdoc />
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var details = await DbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(x => x.IsEnterDoorArea == false || (x.IsEnterDoorArea && x.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived))
.Where(x => x.IsEnterWorkplace && x.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Wait && DateTime.Now >= x.WorkplaceStart)
.ToListAsync();
foreach (var detail in details)
{
var gainfo = await DbContext.Queryable<GainfoEntity>().Where(x => x.IdNo == detail.IdNo || x.Code == detail.IdNo).SingleAsync();
if (gainfo == null)
{
continue;
}
var areaCodes = GetValidAreaCodes(detail).ToList();
if (!areaCodes.Any())
{
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 = gainfo.No,
Type = DeviceTaskEnumType.Add,
Priority = 100,
TenantId = x.TenantId,
Source = "定时任务判断触发车间下发任务",
Version = 0
}).ToList());
if (detail.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Wait)
{
detail.WorkplaceStatus = VisitorAppointmentDetailStatusEnumType.Arrived;
}
await DbContext.UpdateAsync(detail);
}
}
private static IEnumerable<string> GetValidAreaCodes(VisitorAppointmentDetailEntity detail)
{
return detail.ApplyWorkplaceAreas;
}
}