feat 设备批量导入
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Dlhis.Basic.Summary.Response;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Basic.Summary.Response;
|
||||
using Dlhis.Basic.Summary.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
@@ -26,4 +27,14 @@ public class SummariesController : ControllerBase
|
||||
{
|
||||
return await _summaryService.GetAttendanceSummaryAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 门禁首页数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet("door-summary")]
|
||||
public async Task<DoorHomeDataResponse> GetDoorHomeDataAsync()
|
||||
{
|
||||
return await _summaryService.GetDoorHomeDataAsync();
|
||||
}
|
||||
}
|
||||
|
||||
11
Src/Dlhis.Basic/Summary/Response/DoorHomeDataResponse.cs
Normal file
11
Src/Dlhis.Basic/Summary/Response/DoorHomeDataResponse.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Dlhis.Basic.Summary.Dto;
|
||||
|
||||
namespace Dlhis.Basic.Summary.Response
|
||||
{
|
||||
public class DoorHomeDataResponse
|
||||
{
|
||||
public int OffNum { get; set; }
|
||||
public int TotalNum { get; set; }
|
||||
public List<TrendDto> Trend { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
using Dlhis.Basic.Summary.Response;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Basic.Summary.Response;
|
||||
|
||||
namespace Dlhis.Basic.Summary.Services;
|
||||
|
||||
public interface ISummaryService : IScopeDependency
|
||||
{
|
||||
Task<AttendanceSummaryResponse> GetAttendanceSummaryAsync();
|
||||
|
||||
Task<DoorHomeDataResponse> GetDoorHomeDataAsync();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Dlhis.Basic.Organization.Services;
|
||||
using Dlhis.Basic.Device.Response;
|
||||
using Dlhis.Basic.Organization.Services;
|
||||
using Dlhis.Basic.Summary.Dto;
|
||||
using Dlhis.Basic.Summary.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
@@ -81,6 +82,42 @@ public class SummaryService : ISummaryService
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<DoorHomeDataResponse> GetDoorHomeDataAsync()
|
||||
{
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Door).ToListAsync();
|
||||
|
||||
var offDevices = await GetOfflineDevicesCount(devices);
|
||||
|
||||
//获取近30天并转化为"yyyy-MM-dd"集合
|
||||
DateTime currentDate = DateTime.Now;
|
||||
List<string> times = new List<string>();
|
||||
for (int i = 0; i < 30; i++)
|
||||
{
|
||||
DateTime date = currentDate.AddDays(-i);
|
||||
string dateString = date.ToString("yyyy-MM-dd");
|
||||
times.Add(dateString);
|
||||
}
|
||||
|
||||
var list = new List<TrendDto>();
|
||||
times.ForEach(time =>
|
||||
{
|
||||
var count = devices.Where(x => x.CreateTime.ToString("yyyy-MM-dd") == time).Count();
|
||||
var trend = new TrendDto
|
||||
{
|
||||
Date = DateTime.ParseExact(time, "yyyy-MM-dd", null),
|
||||
Count = count,
|
||||
};
|
||||
list.Add(trend);
|
||||
});
|
||||
var res = new DoorHomeDataResponse
|
||||
{
|
||||
OffNum = offDevices.Count,
|
||||
TotalNum = devices.Count,
|
||||
Trend = list
|
||||
};
|
||||
return res;
|
||||
}
|
||||
|
||||
private static List<TrendDto> FixTrend(IEnumerable<TrendDto> dtos, DateTime start, DateTime end)
|
||||
{
|
||||
var result = dtos.ToList();
|
||||
@@ -104,4 +141,49 @@ public class SummaryService : ISummaryService
|
||||
|
||||
return result.OrderBy(x => x.Date).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取离线设备
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private async Task<List<DeviceEntity>> GetOfflineDevicesCount(List<DeviceEntity> devices)
|
||||
{
|
||||
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 OnlineDevices = deviceRegistries
|
||||
.Where(x =>
|
||||
{
|
||||
if (x.HeartbeatInterval <= 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now;
|
||||
|
||||
})
|
||||
.Select(x => new
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user