From f1bf86ec0c68bd44f2d9f3453416c2e255d1e391 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Sun, 9 Jun 2024 02:40:29 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E9=97=A8=E7=A6=81=E9=A6=96=E9=A1=B5?= =?UTF-8?q?=E7=BB=9F=E8=AE=A1=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Src/Dlhis.Basic/Summary/Dto/TrendDto.cs | 6 +- .../Summary/Response/DoorDataResponse.cs | 51 +++++------- .../Services/Implements/SummaryService.cs | 79 +++++++------------ .../Controllers/AssignmentsController.cs | 4 +- 4 files changed, 57 insertions(+), 83 deletions(-) diff --git a/Src/Dlhis.Basic/Summary/Dto/TrendDto.cs b/Src/Dlhis.Basic/Summary/Dto/TrendDto.cs index 2d2d046d..b5d876e7 100644 --- a/Src/Dlhis.Basic/Summary/Dto/TrendDto.cs +++ b/Src/Dlhis.Basic/Summary/Dto/TrendDto.cs @@ -1,7 +1,11 @@ -namespace Dlhis.Basic.Summary.Dto; +using Kurisu.AspNetCore.Utils.Converters; +using Newtonsoft.Json; + +namespace Dlhis.Basic.Summary.Dto; public class TrendDto { + [JsonConverter(typeof(ToDateJsonConverter))] public DateTime Date { get; set; } public int Count { get; set; } diff --git a/Src/Dlhis.Basic/Summary/Response/DoorDataResponse.cs b/Src/Dlhis.Basic/Summary/Response/DoorDataResponse.cs index c7d7a178..184971f1 100644 --- a/Src/Dlhis.Basic/Summary/Response/DoorDataResponse.cs +++ b/Src/Dlhis.Basic/Summary/Response/DoorDataResponse.cs @@ -1,35 +1,24 @@ -namespace Dlhis.Basic.Summary.Response +namespace Dlhis.Basic.Summary.Response; + +public class DoorDataResponse { - public class DoorDataResponse - { - /// - /// 门禁总人数 - /// - public int Total => Employed + Unemployed; + /// + /// 门禁总人数 + /// + public int Total => Employed + Unemployed; - /// - /// 在职人数 - /// - public int Employed { get; set; } + /// + /// 在职人数 + /// + public int Employed { get; set; } - /// - /// 周在职人数 - /// - public int WeekEmployed { get; set; } + /// + /// 非在职人数 + /// + public int Unemployed { get; set; } - /// - /// 非在职人数 - /// - public int Unemployed { get; set; } - - /// - /// 周非在职人数 - /// - public int WeekUnemployed { get; set; } - - /// - /// 人员趋势 7天 - /// - public GainfoTrendResponse GainfoTrend { get; set; } - } -} + /// + /// 人员趋势 7天 + /// + public GainfoTrendResponse GainfoTrend { get; set; } +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs b/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs index 37f540c8..3affb075 100644 --- a/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs +++ b/Src/Dlhis.Basic/Summary/Services/Implements/SummaryService.cs @@ -35,8 +35,10 @@ public class SummaryService : ISummaryService var before30Day = DateTime.Today.AddDays(-29); var before7Day = DateTime.Today.AddDays(-6); - var devices = await _dbContext.Queryable().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)) + var devices = await _dbContext.Queryable() + .Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)) .ToListAsync(); + var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList(); var deviceRegistries = await _dbContext.Queryable() @@ -49,15 +51,18 @@ public class SummaryService : ISummaryService }) .ToListAsync(); - var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).Where(x => x.Factory == tenantId).ToListAsync(); - var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).Where(x => x.Factory == tenantId).ToListAsync(); + var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working) + .Where(x => x.Factory == tenantId).ToListAsync(); + var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave) + .Where(x => x.Factory == tenantId) + .ToListAsync(); return new AttendanceSummaryResponse { DeviceSummary = new AttendanceDeviceSummary { Total = devices.Count, - EnableCount = devices.Where(x => x.Enable).Count(), + EnableCount = devices.Count(x => x.Enable), OnlineCount = deviceRegistries.Where(x => { if (x.HeartbeatInterval <= 0) @@ -65,7 +70,7 @@ public class SummaryService : ISummaryService return true; } - return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now; + return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now; }).Count(), Trend = FixTrend(devices.Where(x => x.CreateTime >= before30Day && x.CreateTime <= currentDay) .GroupBy(x => x.CreateTime.Date) @@ -79,10 +84,10 @@ public class SummaryService : ISummaryService { EmployedTrend = FixTrend(workingGainfos.Where(x => x.EntryDate >= before7Day && x.EntryDate <= currentDay) .GroupBy(x => x.EntryDate) - .Select(x => new TrendDto { Date = x.Key.Value, Count = x.Count() }), before7Day, currentDay), + .Select(x => new TrendDto { Date = x.Key!.Value, Count = x.Count() }), before7Day, currentDay), UnemployedTrend = FixTrend(leaveGainfos.Where(x => x.ResignationDate >= before7Day && x.ResignationDate <= currentDay) .GroupBy(x => x.ResignationDate) - .Select(x => new TrendDto { Date = x.Key.Value, Count = x.Count() }), before7Day, currentDay), + .Select(x => new TrendDto { Date = x.Key!.Value, Count = x.Count() }), before7Day, currentDay), } } }; @@ -94,7 +99,6 @@ public class SummaryService : ISummaryService var offDevices = await GetOfflineDevicesCount(devices); - //获取近30天并转化为"yyyy-MM-dd"集合 DateTime currentDate = DateTime.Now; var times = new List(); for (int i = 0; i < 30; i++) @@ -107,7 +111,7 @@ public class SummaryService : ISummaryService var list = new List(); times.ForEach(time => { - var count = devices.Where(x => x.CreateTime.ToString("yyyy-MM-dd") == time).Count(); + var count = devices.Count(x => x.CreateTime.ToString("yyyy-MM-dd") == time); var trend = new TrendDto { Date = DateTime.ParseExact(time, "yyyy-MM-dd", null), @@ -126,49 +130,26 @@ public class SummaryService : ISummaryService public async Task GetDoorDataAsync() { - var res = new DoorDataResponse(); var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).ToListAsync(); var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).ToListAsync(); - res.Employed = workingGainfos.Count; - res.Unemployed = leaveGainfos.Count; - var employedTrend = new List(); - var unemployedTrend = new List(); + var currentDay = DateTime.Today.AddDays(1).AddSeconds(-1); + var before7Day = DateTime.Today.AddDays(-6); - //获取近7天并转化为"yyyy-MM-dd"集合 - var currentDate = DateTime.Now; - var times = new List(); - for (int i = 0; i < 7; i++) + return new DoorDataResponse { - DateTime date = currentDate.AddDays(-i); - string dateString = date.ToString("yyyy-MM-dd"); - times.Add(dateString); - } - - - foreach (var time in times) - { - var emDto = new TrendDto + Employed = workingGainfos.Count, + Unemployed = leaveGainfos.Count, + GainfoTrend = new GainfoTrendResponse() { - Date = DateTime.ParseExact(time, "yyyy-MM-dd", null), - Count = workingGainfos.Where(x => x.CreateTime.ToString("yyy-MM-dd") == time).ToList().Count, - }; - employedTrend.Add(emDto); - var unDto = new TrendDto - { - Date = DateTime.ParseExact(time, "yyyy-MM-dd", null), - Count = leaveGainfos.Where(x => x.CreateTime.ToString("yyy-MM-dd") == time).ToList().Count, - }; - unemployedTrend.Add(unDto); - } - - var ginfoTrendResponse = new GainfoTrendResponse(); - ginfoTrendResponse.EmployedTrend = employedTrend; - ginfoTrendResponse.UnemployedTrend = unemployedTrend; - res.GainfoTrend = ginfoTrendResponse; - employedTrend.ForEach(x => res.WeekEmployed += x.Count); - unemployedTrend.ForEach(x => res.WeekUnemployed += x.Count); - return res; + EmployedTrend = FixTrend(workingGainfos.Where(x => x.EntryDate >= before7Day && x.EntryDate <= currentDay) + .GroupBy(x => x.EntryDate) + .Select(x => new TrendDto { Date = x.Key!.Value, Count = x.Count() }), before7Day, currentDay), + UnemployedTrend = FixTrend(leaveGainfos.Where(x => x.ResignationDate >= before7Day && x.ResignationDate <= currentDay) + .GroupBy(x => x.ResignationDate) + .Select(x => new TrendDto { Date = x.Key!.Value, Count = x.Count() }), before7Day, currentDay) + } + }; } /// @@ -230,7 +211,7 @@ public class SummaryService : ISummaryService }) .ToListAsync(); - var OnlineDevices = deviceRegistries + var onlineDevices = deviceRegistries .Where(x => { if (x.HeartbeatInterval <= 0) @@ -238,7 +219,7 @@ public class SummaryService : ISummaryService return true; } - return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now; + return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now; }) .Select(x => new { @@ -246,7 +227,7 @@ public class SummaryService : ISummaryService }) .ToList(); var onlineUniqueNos = new List(); - OnlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo)); + onlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo)); var offUniqueNos = deviceUniqueNos.Except(onlineUniqueNos).ToList(); var list = devices.Where(x => offUniqueNos.Contains(x.UniqueNo)).ToList(); diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs index f49e4ee8..fdcbf37f 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/AssignmentsController.cs @@ -27,7 +27,7 @@ public class AssignmentsController : ControllerBase /// /// [IgnoreTenant] - [HttpGet("devicetasks")] + [HttpGet("deviceTasks")] public async Task> DeviceGetTasksGetAsync([FromQuery] DeviceTaskGetRequest request) { return await _assignmentService.DeviceGetTasksAsync(request); @@ -37,7 +37,7 @@ public class AssignmentsController : ControllerBase /// 获取设备任务(按设备) /// /// - [HttpPost("devicetasks")] + [HttpPost("deviceTasks")] [IgnoreTenant] public async Task> DeviceGetTasksPostAsync(DeviceTaskGetRequest request) {