diff --git a/Src/Dlhis.Basic/Device/Request/DeviceSaveRequest.cs b/Src/Dlhis.Basic/Device/Request/DeviceSaveRequest.cs index be1d08ae..0c16c523 100644 --- a/Src/Dlhis.Basic/Device/Request/DeviceSaveRequest.cs +++ b/Src/Dlhis.Basic/Device/Request/DeviceSaveRequest.cs @@ -85,7 +85,7 @@ public class DeviceSaveRequest : IValidatableObject if (HeartbeatInterval != 0 && HeartbeatInterval < 30) { - yield return new ValidationResult($"心跳间隔需要设置>=30秒", new[] { nameof(HeartbeatInterval) }); + yield return new ValidationResult("心跳间隔需要设置>=30秒", new[] { nameof(HeartbeatInterval) }); } } } diff --git a/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs b/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs index 1ed12ab1..1e6658fd 100644 --- a/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs +++ b/Src/Dlhis.Basic/Device/Response/DevicePageResponse.cs @@ -1,5 +1,6 @@ using Dlhis.Entity.Device.Enums; using Kurisu.AspNetCore.Utils.Extensions; +using Newtonsoft.Json; namespace Dlhis.Basic.Device.Response; @@ -44,6 +45,7 @@ public class DevicePageResponse } } + [JsonIgnore] public string FuncsStr { get; set; } /// diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs index b8c4cbdc..d473c478 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DeviceService.cs @@ -1,9 +1,6 @@ - -using Dlhis.Basic.Device.Request; +using Dlhis.Basic.Device.Request; using Dlhis.Basic.Device.Response; using Dlhis.Entity.Basic.Entity; -using Dlhis.Entity.Canteen.Entities; -using Dlhis.Entity.Canteen.Enums; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; using Dlhis.Sys.Api; @@ -17,7 +14,6 @@ using Kurisu.AspNetCore.Utils.Extensions; using Mapster; using Newtonsoft.Json; using Newtonsoft.Json.Linq; -using SqlSugar; namespace Dlhis.Basic.Device.Services.Implements; @@ -37,22 +33,22 @@ public class DeviceService : IDeviceService public async Task> GetPageAsync(DevicePageRequest request) { var page = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .LeftJoin((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo) - .LeftJoin((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code) - .WhereIF(request.Type.HasValue, (d, dt) => d.Type == request.Type) - .WhereIF(request.Enable.HasValue, (d, dt) => d.Enable == request.Enable) - .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search || d.DeployPlace.Contains(request.Search)) - .Select((d, dt, dr, bp) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - BusinessPlaceCode = bp.Code, - BusinessPlaceName = bp.Name, - LastHeartbeatTime = dr.LastHeartbeatTime - }, true) - .ToPageListAsync(request); + .LeftJoin((d, dt) => d.TemplateCode == dt.Code) + .LeftJoin((d, dt, dr) => d.UniqueNo == dr.DeviceUniqueNo) + .LeftJoin((d, dt, dr, bp) => dr.BusinessPlaceCode == bp.Code) + .WhereIF(request.Type.HasValue, (d, dt) => d.Type == request.Type) + .WhereIF(request.Enable.HasValue, (d, dt) => d.Enable == request.Enable) + .WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search || d.DeployPlace.Contains(request.Search)) + .Select((d, dt, dr, bp) => new DevicePageResponse + { + FuncsStr = d.Funcs.ToString(), + Brand = dt.Brand, + Type = d.Type, + BusinessPlaceCode = bp.Code, + BusinessPlaceName = bp.Name, + LastHeartbeatTime = dr.LastHeartbeatTime + }, true) + .ToPageListAsync(request); return page; } @@ -95,9 +91,9 @@ public class DeviceService : IDeviceService if (request.Type != entity.Type) { var existsG = await _dbContext.Queryable() - .LeftJoin((dgr, dg) => dgr.GroupCode == dg.Code) - .Where((dgr, dg) => dgr.DeviceCode == entity.Code) - .AnyAsync(); + .LeftJoin((dgr, dg) => dgr.GroupCode == dg.Code) + .Where((dgr, dg) => dgr.DeviceCode == entity.Code) + .AnyAsync(); existsG.ThrowIfTrue("存在设备分组,不可修改设备分类"); } @@ -159,9 +155,9 @@ public class DeviceService : IDeviceService if (entity.Type is DeviceTypeEnumType.Door or DeviceTypeEnumType.Attendance) { var exists = await _dbContext.Queryable() - .Where(x => x.DeviceUniqueNo == entity.UniqueNo) - .Where(x => x.Type == DeviceUploadLogTypeEnumType.Raise) - .AnyAsync(); + .Where(x => x.DeviceUniqueNo == entity.UniqueNo) + .Where(x => x.Type == DeviceUploadLogTypeEnumType.Raise) + .AnyAsync(); if (!exists) { @@ -236,9 +232,9 @@ public class DeviceService : IDeviceService { //查询设备是否存在 var device = await _dbContext.Queryable() - .Where(x => x.UniqueNo == request.UniqueNo) - .WhereIF(request.Id.HasValue, x => x.Id != request.Id) - .FirstAsync(); + .Where(x => x.UniqueNo == request.UniqueNo) + .WhereIF(request.Id.HasValue, x => x.Id != request.Id) + .FirstAsync(); if (device != null) { @@ -252,7 +248,6 @@ public class DeviceService : IDeviceService } catch (Exception) { - } true.ThrowIfTrue($"设备已添加{factory}厂区"); @@ -271,8 +266,8 @@ public class DeviceService : IDeviceService try { return await _dbContext.Queryable() - .Where(x => x.UniqueNo == deviceUniqueNo && x.Enable) - .AnyAsync(); + .Where(x => x.UniqueNo == deviceUniqueNo && x.Enable) + .AnyAsync(); } finally { @@ -293,7 +288,6 @@ public class DeviceService : IDeviceService { FuncsStr = device.Funcs.ToString(), ConnectionInfoStr = device.ConnectionInfo - }, true) .SingleAsync(); @@ -328,6 +322,7 @@ public class DeviceService : IDeviceService }; return res; } + /// /// 获取离线设备 /// @@ -349,22 +344,17 @@ public class DeviceService : IDeviceService }) .ToListAsync(); - var onlineDevices = deviceRegistries - .Where(x => - { - if (x.HeartbeatInterval <= 0) - { - return true; - } + var onlineDevices = deviceRegistries.Where(x => + { + if (x.HeartbeatInterval <= 0) return true; + return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now; + }) + .Select(x => new + { + x.UniqueNo + }) + .ToList(); - return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now; - - }) - .Select(x => new - { - x.UniqueNo, - }) - .ToList(); var onlineUniqueNos = new List(); onlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo)); @@ -377,110 +367,31 @@ public class DeviceService : IDeviceService public async Task> GetAttendanceTotalAsync(AttendanceTotalRequest request) { var page = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .Where(d => d.Type == DeviceTypeEnumType.Attendance || (d.Type == DeviceTypeEnumType.Door && d.AsAttendance)) - .Select((d, dt) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - - }, true) - .ToPageListAsync(request); + .LeftJoin((d, dt) => d.TemplateCode == dt.Code) + .Where(d => d.Type == DeviceTypeEnumType.Attendance || (d.Type == DeviceTypeEnumType.Door && d.AsAttendance)) + .Select((d, dt) => new DevicePageResponse + { + FuncsStr = d.Funcs.ToString(), + Brand = dt.Brand, + Type = d.Type, + }, true) + .ToPageListAsync(request); return page; } public async Task> GetAttendanceOfflineAsync(DeviceOnlineAndOfflineRequest request) { - var devices = await _dbContext.Queryable().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)).ToListAsync(); - - //离线 - var offDevices = await GetOfflineDevicesCount(devices); - var offids = offDevices.Select(x => x.Id).ToList(); - - if (request.IsOnAndOff) - { - //离线 - var offpage = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .Where(d => d.Type == DeviceTypeEnumType.Attendance) - .Where(d => offids.Contains(d.Id)) - .Select((d, dt) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - - }, true) - .ToPageListAsync(request); - return offpage; - } - //在线 - var ids = devices.Select(x => x.Id).ToList(); - List onids = ids.Except(offids).ToList(); - - var page = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .Where(d => d.Type == DeviceTypeEnumType.Attendance) - .Where(d => onids.Contains(d.Id)) - .Select((d, dt) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - - }, true) - .ToPageListAsync(request); - - return page; + return await GetDeviceStatusPageAsync(request.IsOnAndOff, request, DeviceTypeEnumType.Attendance); } + public async Task> GetDoorOfflineAsync(GetDoorOfflineRequest request) { - var devices = await _dbContext.Queryable().Where(x => x.Type == DeviceTypeEnumType.Door).ToListAsync(); - - var offDevices = await GetOfflineDevicesCount(devices); - var offids = offDevices.Select(x => x.Id).ToList(); - - if (request.IsOnAndOff) - { - //离线 - var offpage = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .Where(d => d.Type == DeviceTypeEnumType.Door) - .Where(d => offids.Contains(d.Id)) - .Select((d, dt) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - - }, true) - .ToPageListAsync(request); - return offpage; - } - //在线 - var ids = devices.Select(x => x.Id).ToList(); - List onids = ids.Except(offids).ToList(); - - var page = await _dbContext.Queryable() - .LeftJoin((d, dt) => d.TemplateCode == dt.Code) - .Where(d => d.Type == DeviceTypeEnumType.Door) - .Where(d => onids.Contains(d.Id)) - .Select((d, dt) => new DevicePageResponse - { - FuncsStr = d.Funcs.ToString(), - Brand = dt.Brand, - Type = d.Type, - - }, true) - .ToPageListAsync(request); - - return page; - + return await GetDeviceStatusPageAsync(request.IsOnAndOff, request, DeviceTypeEnumType.Door); } + public async Task> GetRecentNumbersAsync() { //获取近30天并转化为"yyyy-MM-dd"集合 @@ -509,7 +420,35 @@ public class DeviceService : IDeviceService return list; } -} + //************************************************************* help methods ************************************************************************ + private async Task> GetDeviceStatusPageAsync(bool isOnAndOff, PageDto pageDto, DeviceTypeEnumType deviceType) + { + var devices = deviceType == DeviceTypeEnumType.Attendance + ? await _dbContext.Queryable().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)).ToListAsync() + : await _dbContext.Queryable().Where(x => x.Type == DeviceTypeEnumType.Door).ToListAsync(); + var offDevices = await GetOfflineDevicesCount(devices); + //离线 + var ids = offDevices.Select(x => x.Id).ToList(); + if (!isOnAndOff) + { + ids = devices.Select(x => x.Id).Except(ids).ToList(); + } + + return await _dbContext.Queryable() + .LeftJoin((d, dt) => d.TemplateCode == dt.Code) + .Where(d => d.Type == DeviceTypeEnumType.Attendance) + .Where(d => ids.Contains(d.Id)) + .WhereIF(pageDto.Search.IsPresent(), d => d.UniqueNo == pageDto.Search + || d.DeployPlace.Contains(pageDto.Search)) + .Select((d, dt) => new DevicePageResponse + { + FuncsStr = d.Funcs.ToString(), + Brand = dt.Brand, + Type = d.Type, + }, true) + .ToPageListAsync(pageDto); + } +} \ No newline at end of file diff --git a/Src/Dlhis.Entity/Dlhis.Entity.csproj b/Src/Dlhis.Entity/Dlhis.Entity.csproj index 433a1c1b..50a359c2 100644 --- a/Src/Dlhis.Entity/Dlhis.Entity.csproj +++ b/Src/Dlhis.Entity/Dlhis.Entity.csproj @@ -7,7 +7,7 @@ - +