fix 优化首页接口

This commit is contained in:
2024-07-02 16:57:40 +08:00
parent dadd246f05
commit 2c8f7a016f
10 changed files with 35 additions and 189 deletions

View File

@@ -1,5 +1,6 @@
using Dlhis.Basic.Summary.Response; using Dlhis.Basic.Summary.Response;
using Dlhis.Basic.Summary.Services; using Dlhis.Basic.Summary.Services;
using Dlhis.Entity.Device.Enums;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -18,32 +19,12 @@ public class SummariesController : ControllerBase
} }
/// <summary> /// <summary>
/// 考勤首页 /// 首页
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[HttpGet("attendance-summary")] [HttpGet("home-summary")]
public async Task<AttendanceSummaryResponse> GetKaoQingSummary() public async Task<HomeSummaryResponse> GetHomeSummaryAsync(DeviceTypeEnumType type)
{ {
return await _summaryService.GetAttendanceSummaryAsync(); return await _summaryService.GetHomeSummaryAsync(type);
}
/// <summary>
/// 门禁首页设备数据
/// </summary>
/// <returns></returns>
[HttpGet("door-summary")]
public async Task<DoorHomeDataResponse> GetDoorHomeDataAsync()
{
return await _summaryService.GetDoorHomeDataAsync();
}
/// <summary>
/// 门禁首页数据
/// </summary>
/// <returns></returns>
[HttpGet("door-data")]
public async Task<DoorDataResponse> GetDoorDataAsync()
{
return await _summaryService.GetDoorDataAsync();
} }
} }

View File

@@ -1,9 +0,0 @@
namespace Dlhis.Basic.Summary.Response
{
public class AttendanceSummaryResponse
{
public AttendanceDeviceSummary DeviceSummary { get; set; }
public AttendanceDetailSummary AttendanceDetailSummary { get; set; }
}
}

View File

@@ -2,7 +2,7 @@
namespace Dlhis.Basic.Summary.Response; namespace Dlhis.Basic.Summary.Response;
public class AttendanceDeviceSummary public class DeviceSummary
{ {
/// <summary> /// <summary>
/// 总数 /// 总数

View File

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

View File

@@ -1,11 +0,0 @@
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; }
}
}

View File

@@ -1,6 +1,6 @@
namespace Dlhis.Basic.Summary.Response; namespace Dlhis.Basic.Summary.Response;
public class AttendanceDetailSummary public class GainfoDetailSummary
{ {
/// <summary> /// <summary>
/// 考勤总人数 /// 考勤总人数

View File

@@ -1,10 +1,9 @@
using Dlhis.Basic.Summary.Dto; using Dlhis.Basic.Summary.Dto;
namespace Dlhis.Basic.Summary.Response namespace Dlhis.Basic.Summary.Response;
public class GainfoTrendResponse
{ {
public class GainfoTrendResponse public List<TrendDto> EmployedTrend { get; set; }
{ public List<TrendDto> UnemployedTrend { get; set; }
public List<TrendDto> EmployedTrend { get; set; }
public List<TrendDto> UnemployedTrend { get; set; }
}
} }

View File

@@ -0,0 +1,14 @@
namespace Dlhis.Basic.Summary.Response;
public class HomeSummaryResponse
{
/// <summary>
/// 设备
/// </summary>
public DeviceSummary DeviceSummary { get; set; }
/// <summary>
/// 明细
/// </summary>
public GainfoDetailSummary DetailSummary { get; set; }
}

View File

@@ -1,12 +1,9 @@
using Dlhis.Basic.Summary.Response; using Dlhis.Basic.Summary.Response;
using Dlhis.Entity.Device.Enums;
namespace Dlhis.Basic.Summary.Services; namespace Dlhis.Basic.Summary.Services;
public interface ISummaryService : IScopeDependency public interface ISummaryService : IScopeDependency
{ {
Task<AttendanceSummaryResponse> GetAttendanceSummaryAsync(); Task<HomeSummaryResponse> GetHomeSummaryAsync(DeviceTypeEnumType type);
Task<DoorHomeDataResponse> GetDoorHomeDataAsync();
Task<DoorDataResponse> GetDoorDataAsync();
} }

View File

@@ -23,7 +23,7 @@ public class SummaryService : ISummaryService
_currentUser = currentUser; _currentUser = currentUser;
} }
public async Task<AttendanceSummaryResponse> GetAttendanceSummaryAsync() public async Task<HomeSummaryResponse> GetHomeSummaryAsync(DeviceTypeEnumType type)
{ {
var now = DateTime.Now; var now = DateTime.Now;
var tenantId = _currentUser.GetStringTenantId(); var tenantId = _currentUser.GetStringTenantId();
@@ -33,7 +33,7 @@ public class SummaryService : ISummaryService
var before7Day = DateTime.Today.AddDays(-6); var before7Day = DateTime.Today.AddDays(-6);
var devices = await _dbContext.Queryable<DeviceEntity>() var devices = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.Type == DeviceTypeEnumType.Attendance || (x.Type == DeviceTypeEnumType.Door && x.AsAttendance)) .Where(x => x.Type == type)
.ToListAsync(); .ToListAsync();
var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList(); var deviceUniqueNos = devices.Select(x => x.UniqueNo).ToList();
@@ -54,9 +54,9 @@ public class SummaryService : ISummaryService
.Where(x => x.Factory == tenantId) .Where(x => x.Factory == tenantId)
.ToListAsync(); .ToListAsync();
return new AttendanceSummaryResponse return new HomeSummaryResponse
{ {
DeviceSummary = new AttendanceDeviceSummary DeviceSummary = new DeviceSummary
{ {
Total = devices.Count, Total = devices.Count,
EnableCount = devices.Count(x => x.Enable), EnableCount = devices.Count(x => x.Enable),
@@ -73,7 +73,7 @@ public class SummaryService : ISummaryService
.GroupBy(x => x.CreateTime.Date) .GroupBy(x => x.CreateTime.Date)
.Select(x => new TrendDto { Date = x.Key, Count = x.Count() }), before30Day, currentDay) .Select(x => new TrendDto { Date = x.Key, Count = x.Count() }), before30Day, currentDay)
}, },
AttendanceDetailSummary = new AttendanceDetailSummary DetailSummary = new GainfoDetailSummary
{ {
Employed = workingGainfos.Count, Employed = workingGainfos.Count,
Unemployed = leaveGainfos.Count, Unemployed = leaveGainfos.Count,
@@ -90,64 +90,7 @@ 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);
DateTime currentDate = DateTime.Now;
var 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.Count(x => x.CreateTime.ToString("yyyy-MM-dd") == time);
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;
}
public async Task<DoorDataResponse> GetDoorDataAsync()
{
var workingGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Working).ToListAsync();
var leaveGainfos = await _gainfoStore.Queryable(GainfoStatusEnumType.Leave).ToListAsync();
var currentDay = DateTime.Today.AddDays(1).AddSeconds(-1);
var before7Day = DateTime.Today.AddDays(-6);
return new DoorDataResponse
{
Employed = workingGainfos.Count,
Unemployed = leaveGainfos.Count,
GainfoTrend = new GainfoTrendResponse()
{
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)
}
};
}
private static List<TrendDto> FixTrend(IEnumerable<TrendDto> dtos, DateTime start, DateTime end) private static List<TrendDto> FixTrend(IEnumerable<TrendDto> dtos, DateTime start, DateTime end)
@@ -173,48 +116,4 @@ public class SummaryService : ISummaryService
return result.OrderBy(x => x.Date).ToList(); 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) >= 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;
}
} }