fix 优化判断逻辑和获取时间逻辑

This commit is contained in:
2025-12-19 17:08:18 +08:00
parent 8be9468a8b
commit 429b9c0e1a
2 changed files with 90 additions and 86 deletions

View File

@@ -1,4 +1,5 @@
using Dlhis.Basic.Appointment.Request;
using System.Linq.Expressions;
using Dlhis.Basic.Appointment.Request;
using Dlhis.Basic.Appointment.Response;
using Dlhis.Basic.Common.Message.Requests;
using Dlhis.Basic.Common.Message.Services;
@@ -411,21 +412,37 @@ public class AppointmentService : IAppointmentService
//获取外部人员
var externals = await _dbContext.Queryable<GainfoEntity>()
.Where(x => x.Type == GainfoUserTypeEnumType.External)
.Where(x => x.Code == "EXT059373")
.Select(x => new { x.Id, x.Code, x.Factory, x.CreateTime })
.ToListAsync();
var settings = new List<FactorySettingResponse>() { new FactorySettingResponse
{
Factory= "502",
NoRecordAutoResignExternalDays = 3
} };//await _factorySettingService.GetListAsync(false);
//todo 临时处理循环依赖
var gainfoService = _serviceProvider.GetService<IGainfoService>();
var holidaies = await _dbContext
.Queryable<HolidayEntity>()
await AutoResignBySettingAsync(externals,
(settings, factory) =>
{
var setting = settings.First(x => x.Factory == factory);
return setting.NoRecordAutoResignExternalDays;
},
async (g, l) =>
{
await gainfoService.ResignAsync(new GainfoResignRequest
{
Id = g.Id,
Reason = $"{l}天无通行记录",
Date = DateTime.Now
});
},
null, "自动归档外部人员异常");
}
private async Task AutoResignBySettingAsync(List<GainfoEntity> gainfos
, Func<List<FactorySettingResponse>, string, int> getleaveDay
, Func<GainfoEntity, int, Task> todo
, Expression<Func<DeviceUploadLogEntity, bool>> uploadLogAdditionalExp
, string errorMsgPrefix)
{
var settings = await _factorySettingService.GetListAsync(false);
var holidaies = await _dbContext.Queryable<HolidayEntity>()
.Select(x => new
{
x.Type,
@@ -434,70 +451,60 @@ public class AppointmentService : IAppointmentService
})
.ToListAsync();
var weekends = new List<DayOfWeek>() { DayOfWeek.Saturday, DayOfWeek.Sunday };
//人员分组,key为factory
foreach (var exts in externals.GroupBy(x => x.Factory))
foreach (var todoGainfos in gainfos.GroupBy(x => x.Factory))
{
//factory的设置
var setting = settings.First(x => x.Factory == exts.Key);
if (setting.NoRecordAutoResignExternalDays == 0)
//设置为0则不处理
var leaveDay = getleaveDay(settings, todoGainfos.Key);
if (leaveDay == 0)
{
continue;
}
foreach (var ext in exts)
foreach (var gainfo in todoGainfos)
{
// 查询该外部人员最近一次通行记录的时间
//获取最近一次通行记录的时间,如果没有则取创建时间
DateTime? raiseDateTime = await _dbContext.Queryable<DeviceUploadLogEntity>()
.Where(x => x.UserUniqueNo == ext.Code)
.GroupBy(x => x.UserUniqueNo)
.Select(x => SqlFunc.IsNull(SqlFunc.AggregateMax(x.RaiseDateTime), default(DateTime?)))
.Where(x => x.UserUniqueNo == gainfo.Code)
.WhereIF(uploadLogAdditionalExp != null, uploadLogAdditionalExp)
.Select(x => SqlFunc.IsNull(SqlFunc.AggregateMax(x.RaiseDateTime), gainfo.CreateTime))
.SingleAsync();
// 如果最近通行时间存在且超出未通行天数阈值,则自动归档该外部人员
if (raiseDateTime.HasValue)
var checkDates = GetDaysUntilToday(raiseDateTime.Value);
var days = checkDates.Count;
foreach (var date in checkDates)
{
var checkDates = GetDaysUntilToday(raiseDateTime.Value);
var days = checkDates.Count;
foreach (var date in checkDates)
//date.DayOfWeek
foreach (var holiday in holidaies)
{
//date.DayOfWeek
foreach (var holiday in holidaies)
var isPresent = date >= holiday.Start && date <= holiday.Start;
if (isPresent)
{
var isPresent = date >= holiday.Start && date <= holiday.Start;
if (isPresent)
if (holiday.Type == HolidayEnumType.Rest)
{
if (holiday.Type == HolidayEnumType.Rest)
{
days--;
}
days--;
}
}
if (weekends.Contains(date.DayOfWeek))
{
days--;
}
}
if (days >= setting.NoRecordAutoResignExternalDays)
if (weekends.Contains(date.DayOfWeek))
{
try
{
await gainfoService.ResignAsync(new GainfoResignRequest
{
Id = ext.Id,
Reason = $"{setting.NoRecordAutoResignExternalDays}天无通行记录",
Date = DateTime.Now
});
}
catch (Exception e)
{
// 记录自动归档异常日志
_logger.LogError(e, "自动归档外部人员异常:{msg}", e.Message);
}
days--;
}
}
if (days >= leaveDay)
{
try
{
await todo(gainfo, leaveDay);
}
catch (Exception e)
{
_logger.LogError(e, $"{errorMsgPrefix}:{e.Message}");
}
}
}
}
}