From e8b1f8bd3ad181463744cdec911f05c23765cea7 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Mon, 10 Mar 2025 17:18:56 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=88=86=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/DeviceUploadLogService.cs | 85 ++++++++++++++----- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/Src/Dlhis.ScheduledTask/DeviceUploadLog/Services/Implements/DeviceUploadLogService.cs b/Src/Dlhis.ScheduledTask/DeviceUploadLog/Services/Implements/DeviceUploadLogService.cs index 7701d6a1..67d6eae1 100644 --- a/Src/Dlhis.ScheduledTask/DeviceUploadLog/Services/Implements/DeviceUploadLogService.cs +++ b/Src/Dlhis.ScheduledTask/DeviceUploadLog/Services/Implements/DeviceUploadLogService.cs @@ -67,15 +67,16 @@ public class DeviceUploadLogService : IDeviceUploadLogService var max = DateOnly.FromDateTime(requests.Max(x => x.RaiseDateTime)); var workGridSchedules = await _dbContext.Queryable() - .LeftJoin((x, wg) => x.WorkGridCode == wg.Code) .Where(x => x.Date >= min && x.Date <= max && userCodes2.Contains(x.UserCode)) - .Select((x, wg) => new + .Select(x => new { - x.UserCode, x.WorkGridCode, x.Date, x.PeriodType, WorkGridName = wg.Name, - wg.LeaderUserCode + x.UserCode, x.WorkGridCode, x.Date, x.PeriodType }) .ToListAsync(); + var workGrids = await _dbContext.Queryable().ToListAsync(); + var workGridUsers = await _dbContext.Queryable().Where(x => userCodes2.Contains(x.UserCode)).ToListAsync(); + var inserts = new List(); foreach (var request in requests.Where(x => existsDeviceUniqueNos.Contains(x.DeviceUniqueNo))) { @@ -112,7 +113,8 @@ public class DeviceUploadLogService : IDeviceUploadLogService var workGridInfo = new WorkGridInfo(); var currentWorkGridTimePeriods = workGridTimePeriods.Where(x => x.TenantId == device.TenantId).ToList(); var currentWorkGridSchedules = workGridSchedules.Where(x => x.UserCode == request.UserUniqueNo).ToList(); - if (needUpload && currentWorkGridTimePeriods.Any() && currentWorkGridSchedules.Any()) + var currentWorkGridUser = workGridUsers.FirstOrDefault(x => x.UserCode == request.UserUniqueNo); //可能被删除,但还存在当天的排班 + if (needUpload && currentWorkGridTimePeriods.Any() && (currentWorkGridUser != null || currentWorkGridSchedules.Any())) { workGridInfo.TimeSetting = currentWorkGridTimePeriods.Select(x => new { @@ -122,36 +124,48 @@ public class DeviceUploadLogService : IDeviceUploadLogService x.TenantId }).ToList(); - var currentWorkGridSchedule = currentWorkGridSchedules.First(); - workGridInfo.WorkGrid = new + if (currentWorkGridUser != null) { - currentWorkGridSchedule.WorkGridCode, - currentWorkGridSchedule.WorkGridName, - currentWorkGridSchedule.LeaderUserCode - }; + var currentWorkGrid = workGrids.First(x => x.Code == currentWorkGridUser.WorkGridCode); + workGridInfo.WorkGrid = new WorkGridInfoDetail + { + WorkGridCode = currentWorkGrid.Code, + WorkGridName = currentWorkGrid.Name, + LeaderUserCode = currentWorkGrid.LeaderUserCode + }; + } + var currentDate = request.RaiseDateTime.Date; var currentDateOnly = DateOnly.FromDateTime(currentDate); //白班判断 - var currentDayWorkGridTimePeriod = currentWorkGridTimePeriods.FirstOrDefault(x => x.Type == WorkGridTimePeriodEnumType.Day); - if (currentDayWorkGridTimePeriod != null) + var dayTimePeriod = currentWorkGridTimePeriods.FirstOrDefault(x => x.Type == WorkGridTimePeriodEnumType.Day); + if (dayTimePeriod != null) { var currentDayWorkGridSchedule = currentWorkGridSchedules.FirstOrDefault(x => x.PeriodType == WorkGridTimePeriodEnumType.Day && x.Date == currentDateOnly); if (currentDayWorkGridSchedule != null) { - var start = currentDate.Add(currentDayWorkGridTimePeriod.Start.AddMinutes(-currentDayWorkGridTimePeriod.Before).ToTimeSpan()); - var end = currentDate.Add(currentDayWorkGridTimePeriod.End.AddMinutes(currentDayWorkGridTimePeriod.After).ToTimeSpan()); + var start = currentDate.Add(dayTimePeriod.Start.AddMinutes(-dayTimePeriod.Before).ToTimeSpan()); + var end = currentDate.Add(dayTimePeriod.End.AddMinutes(dayTimePeriod.After).ToTimeSpan()); if (request.RaiseDateTime >= start && request.RaiseDateTime <= end) { workGridInfo.PeriodType = WorkGridTimePeriodEnumType.Day; } + + var currentWorkGrid = workGrids.First(x => x.Code == currentDayWorkGridSchedule.WorkGridCode); + workGridInfo.WorkGrid = new WorkGridInfoDetail + { + WorkGridCode = currentWorkGrid.Code, + WorkGridName = currentWorkGrid.Name, + LeaderUserCode = currentWorkGrid.LeaderUserCode + }; } } //晚班判断 - var currentNightWorkGridTimePeriod = currentWorkGridTimePeriods.FirstOrDefault(x => x.Type == WorkGridTimePeriodEnumType.Night); - if (currentNightWorkGridTimePeriod != null) + var nightTimePeriod = currentWorkGridTimePeriods.FirstOrDefault(x => x.Type == WorkGridTimePeriodEnumType.Night); + if (nightTimePeriod != null) { var currentNightWorkGridSchedules = currentWorkGridSchedules .Where(x => x.PeriodType == WorkGridTimePeriodEnumType.Night) @@ -160,11 +174,19 @@ public class DeviceUploadLogService : IDeviceUploadLogService if (currentNightWorkGridSchedules.Any()) { - var startTime = currentNightWorkGridTimePeriod.Start.AddMinutes(-currentNightWorkGridTimePeriod.Before); - var endTime = currentNightWorkGridTimePeriod.End.AddMinutes(currentNightWorkGridTimePeriod.After); - + var startTime = nightTimePeriod.Start.AddMinutes(-nightTimePeriod.Before); + var endTime = nightTimePeriod.End.AddMinutes(nightTimePeriod.After); + if (currentNightWorkGridSchedules.Any(x => x.Date == currentDateOnly)) { + var currentWorkGrid = workGrids.First(x => x.Code == currentNightWorkGridSchedules.First(p => p.Date == currentDateOnly).WorkGridCode); + workGridInfo.WorkGrid = new WorkGridInfoDetail + { + WorkGridCode = currentWorkGrid.Code, + WorkGridName = currentWorkGrid.Name, + LeaderUserCode = currentWorkGrid.LeaderUserCode + }; + var start = currentDateOnly.ToDateTime(startTime); var end = currentDateOnly.ToDateTime(new TimeOnly(23, 59, 59, 999)); @@ -172,10 +194,20 @@ public class DeviceUploadLogService : IDeviceUploadLogService { workGridInfo.PeriodType = WorkGridTimePeriodEnumType.Night; } + + } if (currentNightWorkGridSchedules.Any(x => x.Date == currentDateOnly.AddDays(-1))) { + var currentWorkGrid = workGrids.First(x => x.Code == currentNightWorkGridSchedules.First(p => p.Date == currentDateOnly.AddDays(-1)).WorkGridCode); + workGridInfo.WorkGrid = new WorkGridInfoDetail + { + WorkGridCode = currentWorkGrid.Code, + WorkGridName = currentWorkGrid.Name, + LeaderUserCode = currentWorkGrid.LeaderUserCode + }; + var start = currentDateOnly.AddDays(-1).ToDateTime(startTime); var end = currentDateOnly.ToDateTime(endTime); @@ -196,8 +228,8 @@ public class DeviceUploadLogService : IDeviceUploadLogService { await _dingTalkApi.SendWorkMessageAsync(new JinkoSendWorkMessageRequest { - Content = $"{currentWorkGridSchedule.WorkGridName}的网格长,你好,检测到你网格中 {currentGainfo.Name}({currentGainfo.Code})今日未排班,但是有考勤记录,请前往系统处理!", - WorkCodes = new List { currentWorkGridSchedule.LeaderUserCode } + Content = $"{workGridInfo.WorkGrid.WorkGridName}的网格长,你好,检测到你网格中 {currentGainfo.Name}({currentGainfo.Code})今日未排班,但是有考勤记录,请前往系统处理!", + WorkCodes = new List { workGridInfo.WorkGrid.LeaderUserCode } }); } catch (Exception e) @@ -456,7 +488,14 @@ public class DeviceUploadLogService : IDeviceUploadLogService public class WorkGridInfo { public bool IsError => !PeriodType.HasValue; - public object WorkGrid { get; set; } + public WorkGridInfoDetail WorkGrid { get; set; } public WorkGridTimePeriodEnumType? PeriodType { get; set; } public object TimeSetting { get; set; } +} + +public class WorkGridInfoDetail +{ + public Guid WorkGridCode { get; set; } + public string WorkGridName { get; set; } + public string LeaderUserCode { get; set; } } \ No newline at end of file