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

View File

@@ -163,51 +163,48 @@ public class GainfoOutsourceService : IGainfoOutsourceService
foreach (var outsource in grp) foreach (var outsource in grp)
{ {
DateTime? raiseDateTime = await _dbContext.Queryable<DeviceUploadLogEntity>() DateTime raiseDateTime = await _dbContext.Queryable<DeviceUploadLogEntity>()
.Where(x => x.UserUniqueNo == outsource.Code) .Where(x => x.UserUniqueNo == outsource.Code)
.Where(x => x.TenantId == grp.Key) //本厂区的 .Where(x => x.TenantId == grp.Key) //本厂区的
.GroupBy(x => x.UserUniqueNo) .GroupBy(x => x.UserUniqueNo)
.Select(x => SqlFunc.IsNull(SqlFunc.AggregateMax(x.RaiseDateTime), default(DateTime?))) .Select(x => SqlFunc.IsNull(SqlFunc.AggregateMax(x.RaiseDateTime), outsource.CreateTime))
.SingleAsync(); .SingleAsync();
if (raiseDateTime.HasValue) var checkDates = GetDaysUntilToday(raiseDateTime);
var days = checkDates.Count;
foreach (var date in checkDates)
{ {
var checkDates = GetDaysUntilToday(raiseDateTime.Value); //date.DayOfWeek
var days = checkDates.Count; foreach (var holiday in holidaies)
foreach (var date in checkDates)
{ {
//date.DayOfWeek var isPresent = date >= holiday.Start && date <= holiday.Start;
foreach (var holiday in holidaies) if (isPresent)
{ {
var isPresent = date >= holiday.Start && date <= holiday.Start; if (holiday.Type == HolidayEnumType.Rest)
if (isPresent)
{ {
if (holiday.Type == HolidayEnumType.Rest) days--;
{
days--;
}
} }
} }
if (weekends.Contains(date.DayOfWeek))
{
days--;
}
} }
if (days >= setting.NoRecordAutoResignOutsourceDays) if (weekends.Contains(date.DayOfWeek))
{ {
try days--;
}
}
if (days >= setting.NoRecordAutoResignOutsourceDays)
{
try
{
await _gainfoService.ResignAsync(new GainfoResignRequest
{ {
await _gainfoService.ResignAsync(new GainfoResignRequest Id = outsource.Id,
{ Reason = $"{setting.NoRecordAutoResignOutsourceDays}天无通行记录",
Id = outsource.Id, Date = DateTime.Now
Reason = $"{setting.NoRecordAutoResignOutsourceDays}天无通行记录", });
Date = DateTime.Now }
}); catch (Exception e)
} {
catch (Exception e) _logger.LogError(e, "自动离职外包异常:{msg}", e.Message);
{
_logger.LogError(e, "自动离职外包异常:{msg}", e.Message);
}
} }
} }
} }