diff --git a/Src/Dlhis.Basic/Appointment/Services/Implements/AppointmentService.cs b/Src/Dlhis.Basic/Appointment/Services/Implements/AppointmentService.cs index 4b7c45f9..552693eb 100644 --- a/Src/Dlhis.Basic/Appointment/Services/Implements/AppointmentService.cs +++ b/Src/Dlhis.Basic/Appointment/Services/Implements/AppointmentService.cs @@ -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() .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() { new FactorySettingResponse - { - Factory= "502", - NoRecordAutoResignExternalDays = 3 - } };//await _factorySettingService.GetListAsync(false); - - //todo 临时处理循环依赖 var gainfoService = _serviceProvider.GetService(); - var holidaies = await _dbContext - .Queryable() + 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 gainfos + , Func, string, int> getleaveDay + , Func todo + , Expression> uploadLogAdditionalExp + , string errorMsgPrefix) + { + var settings = await _factorySettingService.GetListAsync(false); + var holidaies = await _dbContext.Queryable() .Select(x => new { x.Type, @@ -434,70 +451,60 @@ public class AppointmentService : IAppointmentService }) .ToListAsync(); - var weekends = new List() { 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() - .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}"); + } + } + } } } diff --git a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoOutsourceService.cs b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoOutsourceService.cs index 04443dd1..23f546f9 100644 --- a/Src/Dlhis.Basic/Organization/Services/Implements/GainfoOutsourceService.cs +++ b/Src/Dlhis.Basic/Organization/Services/Implements/GainfoOutsourceService.cs @@ -163,51 +163,48 @@ public class GainfoOutsourceService : IGainfoOutsourceService foreach (var outsource in grp) { - DateTime? raiseDateTime = await _dbContext.Queryable() + DateTime raiseDateTime = await _dbContext.Queryable() .Where(x => x.UserUniqueNo == outsource.Code) .Where(x => x.TenantId == grp.Key) //本厂区的 .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(); - if (raiseDateTime.HasValue) + var checkDates = GetDaysUntilToday(raiseDateTime); + 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.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}天无通行记录", - Date = DateTime.Now - }); - } - catch (Exception e) - { - _logger.LogError(e, "自动离职外包异常:{msg}", e.Message); - } + Id = outsource.Id, + Reason = $"{setting.NoRecordAutoResignOutsourceDays}天无通行记录", + Date = DateTime.Now + }); + } + catch (Exception e) + { + _logger.LogError(e, "自动离职外包异常:{msg}", e.Message); } } }