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

View File

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