fix 门禁首页统计优化

This commit is contained in:
2024-06-09 02:40:29 +08:00
parent 82a3994d4d
commit f1bf86ec0c
4 changed files with 57 additions and 83 deletions

View File

@@ -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 public class TrendDto
{ {
[JsonConverter(typeof(ToDateJsonConverter))]
public DateTime Date { get; set; } public DateTime Date { get; set; }
public int Count { get; set; } public int Count { get; set; }

View File

@@ -1,35 +1,24 @@
namespace Dlhis.Basic.Summary.Response namespace Dlhis.Basic.Summary.Response;
public class DoorDataResponse
{ {
public class DoorDataResponse /// <summary>
{ /// 门禁总人数
/// <summary> /// </summary>
/// 门禁总人数 public int Total => Employed + Unemployed;
/// </summary>
public int Total => Employed + Unemployed;
/// <summary> /// <summary>
/// 在职人数 /// 在职人数
/// </summary> /// </summary>
public int Employed { get; set; } public int Employed { get; set; }
/// <summary> /// <summary>
/// 周在职人数 /// 非在职人数
/// </summary> /// </summary>
public int WeekEmployed { get; set; } public int Unemployed { get; set; }
/// <summary> /// <summary>
/// 非在职人数 /// 人员趋势 7天
/// </summary> /// </summary>
public int Unemployed { get; set; } public GainfoTrendResponse GainfoTrend { get; set; }
}
/// <summary>
/// 周非在职人数
/// </summary>
public int WeekUnemployed { get; set; }
/// <summary>
/// 人员趋势 7天
/// </summary>
public GainfoTrendResponse GainfoTrend { get; set; }
}
}

View File

@@ -35,8 +35,10 @@ public class SummaryService : ISummaryService
var before30Day = DateTime.Today.AddDays(-29); var before30Day = DateTime.Today.AddDays(-29);
var before7Day = DateTime.Today.AddDays(-6); var before7Day = DateTime.Today.AddDays(-6);
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)) var devices = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance))
.ToListAsync(); .ToListAsync();
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList(); var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
var deviceRegistries = await _dbContext.Queryable<DeviceRegistryEntity>() var deviceRegistries = await _dbContext.Queryable<DeviceRegistryEntity>()
@@ -49,15 +51,18 @@ public class SummaryService : ISummaryService
}) })
.ToListAsync(); .ToListAsync();
var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).Where(x => x.Factory == tenantId).ToListAsync(); var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working)
var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).Where(x => x.Factory == tenantId).ToListAsync(); .Where(x => x.Factory == tenantId).ToListAsync();
var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave)
.Where(x => x.Factory == tenantId)
.ToListAsync();
return new AttendanceSummaryResponse return new AttendanceSummaryResponse
{ {
DeviceSummary = new AttendanceDeviceSummary DeviceSummary = new AttendanceDeviceSummary
{ {
Total = devices.Count, Total = devices.Count,
EnableCount = devices.Where(x => x.Enable).Count(), EnableCount = devices.Count(x => x.Enable),
OnlineCount = deviceRegistries.Where(x => OnlineCount = deviceRegistries.Where(x =>
{ {
if (x.HeartbeatInterval <= 0) if (x.HeartbeatInterval <= 0)
@@ -65,7 +70,7 @@ public class SummaryService : ISummaryService
return true; return true;
} }
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now; return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now;
}).Count(), }).Count(),
Trend = FixTrend(devices.Where(x => x.CreateTime >= before30Day && x.CreateTime <= currentDay) Trend = FixTrend(devices.Where(x => x.CreateTime >= before30Day && x.CreateTime <= currentDay)
.GroupBy(x => x.CreateTime.Date) .GroupBy(x => x.CreateTime.Date)
@@ -79,10 +84,10 @@ public class SummaryService : ISummaryService
{ {
EmployedTrend = FixTrend(workingGainfos.Where(x => x.EntryDate >= before7Day && x.EntryDate <= currentDay) EmployedTrend = FixTrend(workingGainfos.Where(x => x.EntryDate >= before7Day && x.EntryDate <= currentDay)
.GroupBy(x => x.EntryDate) .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) UnemployedTrend = FixTrend(leaveGainfos.Where(x => x.ResignationDate >= before7Day && x.ResignationDate <= currentDay)
.GroupBy(x => x.ResignationDate) .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); var offDevices = await GetOfflineDevicesCount(devices);
//获取近30天并转化为"yyyy-MM-dd"集合
DateTime currentDate = DateTime.Now; DateTime currentDate = DateTime.Now;
var times = new List<string>(); var times = new List<string>();
for (int i = 0; i < 30; i++) for (int i = 0; i < 30; i++)
@@ -107,7 +111,7 @@ public class SummaryService : ISummaryService
var list = new List<TrendDto>(); var list = new List<TrendDto>();
times.ForEach(time => 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 var trend = new TrendDto
{ {
Date = DateTime.ParseExact(time, "yyyy-MM-dd", null), Date = DateTime.ParseExact(time, "yyyy-MM-dd", null),
@@ -126,49 +130,26 @@ public class SummaryService : ISummaryService
public async Task<DoorDataResponse> GetDoorDataAsync() public async Task<DoorDataResponse> GetDoorDataAsync()
{ {
var res = new DoorDataResponse();
var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).ToListAsync(); var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).ToListAsync();
var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).ToListAsync(); var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).ToListAsync();
res.Employed = workingGainfos.Count;
res.Unemployed = leaveGainfos.Count;
var employedTrend = new List<TrendDto>(); var currentDay = DateTime.Today.AddDays(1).AddSeconds(-1);
var unemployedTrend = new List<TrendDto>(); var before7Day = DateTime.Today.AddDays(-6);
//获取近7天并转化为"yyyy-MM-dd"集合 return new DoorDataResponse
var currentDate = DateTime.Now;
var times = new List<string>();
for (int i = 0; i < 7; i++)
{ {
DateTime date = currentDate.AddDays(-i); Employed = workingGainfos.Count,
string dateString = date.ToString("yyyy-MM-dd"); Unemployed = leaveGainfos.Count,
times.Add(dateString); GainfoTrend = new GainfoTrendResponse()
}
foreach (var time in times)
{
var emDto = new TrendDto
{ {
Date = DateTime.ParseExact(time, "yyyy-MM-dd", null), EmployedTrend = FixTrend(workingGainfos.Where(x => x.EntryDate >= before7Day && x.EntryDate <= currentDay)
Count = workingGainfos.Where(x => x.CreateTime.ToString("yyy-MM-dd") == time).ToList().Count, .GroupBy(x => x.EntryDate)
}; .Select(x => new TrendDto { Date = x.Key!.Value, Count = x.Count() }), before7Day, currentDay),
employedTrend.Add(emDto); UnemployedTrend = FixTrend(leaveGainfos.Where(x => x.ResignationDate >= before7Day && x.ResignationDate <= currentDay)
var unDto = new TrendDto .GroupBy(x => x.ResignationDate)
{ .Select(x => new TrendDto { Date = x.Key!.Value, Count = x.Count() }), before7Day, currentDay)
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;
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -230,7 +211,7 @@ public class SummaryService : ISummaryService
}) })
.ToListAsync(); .ToListAsync();
var OnlineDevices = deviceRegistries var onlineDevices = deviceRegistries
.Where(x => .Where(x =>
{ {
if (x.HeartbeatInterval <= 0) if (x.HeartbeatInterval <= 0)
@@ -238,7 +219,7 @@ public class SummaryService : ISummaryService
return true; return true;
} }
return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval * 3) >= now; return x.LastHeartbeatTime.AddSeconds(x.HeartbeatInterval) >= now;
}) })
.Select(x => new .Select(x => new
{ {
@@ -246,7 +227,7 @@ public class SummaryService : ISummaryService
}) })
.ToList(); .ToList();
var onlineUniqueNos = new List<string>(); var onlineUniqueNos = new List<string>();
OnlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo)); onlineDevices.ForEach(x => onlineUniqueNos.Add(x.UniqueNo));
var offUniqueNos = deviceUniqueNos.Except(onlineUniqueNos).ToList(); var offUniqueNos = deviceUniqueNos.Except(onlineUniqueNos).ToList();
var list = devices.Where(x => offUniqueNos.Contains(x.UniqueNo)).ToList(); var list = devices.Where(x => offUniqueNos.Contains(x.UniqueNo)).ToList();

View File

@@ -27,7 +27,7 @@ public class AssignmentsController : ControllerBase
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[IgnoreTenant] [IgnoreTenant]
[HttpGet("devicetasks")] [HttpGet("deviceTasks")]
public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksGetAsync([FromQuery] DeviceTaskGetRequest request) public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksGetAsync([FromQuery] DeviceTaskGetRequest request)
{ {
return await _assignmentService.DeviceGetTasksAsync(request); return await _assignmentService.DeviceGetTasksAsync(request);
@@ -37,7 +37,7 @@ public class AssignmentsController : ControllerBase
/// 获取设备任务(按设备) /// 获取设备任务(按设备)
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpPost("devicetasks")] [HttpPost("deviceTasks")]
[IgnoreTenant] [IgnoreTenant]
public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksPostAsync(DeviceTaskGetRequest request) public async Task<List<DeviceTaskToDeviceByDeviceResponse>> DeviceGetTasksPostAsync(DeviceTaskGetRequest request)
{ {