fix 逻辑修正
This commit is contained in:
@@ -11,6 +11,7 @@ using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Mapster;
|
||||
using MySqlX.XDevAPI.Relational;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SqlSugar;
|
||||
@@ -269,10 +270,8 @@ public class DeviceService : IDeviceService
|
||||
|
||||
public async Task<DeviceNumberResponse> GetDeviceNumberAsync()
|
||||
{
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>()
|
||||
.Where(x => x.Type == DeviceTypeEnumType.Attendance && x.Enable)
|
||||
.ToListAsync();
|
||||
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)).ToListAsync();
|
||||
|
||||
var offlineNumber = await GetOfflineDevicesCount(devices);
|
||||
|
||||
var res = new DeviceNumberResponse
|
||||
@@ -286,32 +285,48 @@ public class DeviceService : IDeviceService
|
||||
/// <summary>
|
||||
/// 获取离线设备
|
||||
/// </summary>
|
||||
/// <param name="devices"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<List<DeviceEntity>> GetOfflineDevicesCount(List<DeviceEntity> devices)
|
||||
{
|
||||
var nos = devices.Select(x => x.UniqueNo).ToList();
|
||||
var deviceRegistrys = await _dbContext.Queryable<DeviceRegistryEntity>()
|
||||
.Where(x => nos.Contains(x.DeviceUniqueNo))
|
||||
var now = DateTime.Now;
|
||||
|
||||
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
|
||||
|
||||
var deviceRegistries = await _dbContext.Queryable<DeviceRegistryEntity>()
|
||||
.LeftJoin<DeviceEntity>((x, d) => x.DeviceUniqueNo == d.UniqueNo)
|
||||
.Where(x => deviceUniqueNos.Contains(x.DeviceUniqueNo))
|
||||
.Select((x, d) => new
|
||||
{
|
||||
UniqueNo = d.UniqueNo,
|
||||
d.HeartbeatInterval,
|
||||
x.LastHeartbeatTime
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
var list = new List<DeviceEntity>();
|
||||
foreach (var device in devices)
|
||||
{
|
||||
var regis = deviceRegistrys.FirstOrDefault(x => x.DeviceUniqueNo == device.UniqueNo);
|
||||
if (regis == null || device.HeartbeatInterval == 0)
|
||||
var OnlineDevices = deviceRegistries
|
||||
.Where(x =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
TimeSpan timeDifference = DateTime.Now - regis.LastHeartbeatTime;
|
||||
if (timeDifference.TotalSeconds > (device.HeartbeatInterval * 3))
|
||||
if (x.HeartbeatInterval <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now;
|
||||
|
||||
})
|
||||
.Select(x => new
|
||||
{
|
||||
list.Add(device);
|
||||
}
|
||||
|
||||
}
|
||||
UniqueNo = x.UniqueNo,
|
||||
})
|
||||
.ToList();
|
||||
var onlineUniqueNos = new List<string>();
|
||||
OnlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo));
|
||||
|
||||
var offUniqueNos = deviceUniqueNos.Except(onlineUniqueNos).ToList();
|
||||
var list = devices.Where(x => offUniqueNos.Contains(x.UniqueNo)).ToList();
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
public async Task<Pagination<DevicePageResponse>> GetAttendanceTotalAsync(AttendanceTotalRequest request)
|
||||
{
|
||||
@@ -332,9 +347,7 @@ 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.Enable)
|
||||
.ToListAsync();
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)).ToListAsync();
|
||||
|
||||
//离线
|
||||
var offDevices = await GetOfflineDevicesCount(devices);
|
||||
|
||||
Reference in New Issue
Block a user