fix 代码优化

This commit is contained in:
2024-05-31 19:34:35 +08:00
parent 8778e5ad84
commit 895524e6c7
4 changed files with 85 additions and 144 deletions

View File

@@ -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) });
}
}
}

View File

@@ -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; }
/// <summary>

View File

@@ -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;
@@ -252,7 +248,6 @@ public class DeviceService : IDeviceService
}
catch (Exception)
{
}
true.ThrowIfTrue($"设备已添加{factory}厂区");
@@ -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;
}
/// <summary>
/// 获取离线设备
/// </summary>
@@ -349,22 +344,17 @@ public class DeviceService : IDeviceService
})
.ToListAsync();
var onlineDevices = deviceRegistries
.Where(x =>
var onlineDevices = deviceRegistries.Where(x =>
{
if (x.HeartbeatInterval <= 0)
{
return true;
}
if (x.HeartbeatInterval <= 0) return true;
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now;
})
.Select(x => new
{
x.UniqueNo,
x.UniqueNo
})
.ToList();
var onlineUniqueNos = new List<string>();
onlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo));
@@ -384,7 +374,6 @@ public class DeviceService : IDeviceService
FuncsStr = d.Funcs.ToString(),
Brand = dt.Brand,
Type = d.Type,
}, true)
.ToPageListAsync(request);
@@ -393,93 +382,15 @@ public class DeviceService : IDeviceService
public async Task<Pagination<DevicePageResponse>> GetAttendanceOfflineAsync(DeviceOnlineAndOfflineRequest request)
{
var devices = await _dbContext.Queryable<DeviceEntity>().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<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((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;
return await GetDeviceStatusPageAsync(request.IsOnAndOff, request, DeviceTypeEnumType.Attendance);
}
//在线
var ids = devices.Select(x => x.Id).ToList();
List<long> onids = ids.Except(offids).ToList();
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((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;
}
public async Task<Pagination<DevicePageResponse>> GetDoorOfflineAsync(GetDoorOfflineRequest request)
{
var devices = await _dbContext.Queryable<DeviceEntity>().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<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((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;
return await GetDeviceStatusPageAsync(request.IsOnAndOff, request, DeviceTypeEnumType.Door);
}
//在线
var ids = devices.Select(x => x.Id).ToList();
List<long> onids = ids.Except(offids).ToList();
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((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;
}
public async Task<List<RecentNumberResponse>> GetRecentNumbersAsync()
{
@@ -509,7 +420,35 @@ public class DeviceService : IDeviceService
return list;
}
//************************************************************* help methods ************************************************************************
private async Task<Pagination<DevicePageResponse>> GetDeviceStatusPageAsync(bool isOnAndOff, PageDto pageDto, DeviceTypeEnumType deviceType)
{
var devices = deviceType == DeviceTypeEnumType.Attendance
? await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)).ToListAsync()
: await _dbContext.Queryable<DeviceEntity>().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<DeviceEntity>()
.LeftJoin<DeviceTemplateEntity>((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);
}
}

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.12" />
<PackageReference Include="Kurisu.AspNetCore" Version="0.9.13" />
</ItemGroup>
</Project>