feat 添加获取设备下所有人员工号接口

This commit is contained in:
2025-06-23 16:37:03 +08:00
parent e2f43cb01b
commit 7c57948316
3 changed files with 107 additions and 27 deletions

View File

@@ -103,6 +103,17 @@ public class DevicePermissionsController : ControllerBase
await _devicePermissionService.ResetGainfoPageByDeviceAsync(request.Code, request.Search, request.UserType);
}
/// <summary>
/// 获取设备下所有人员
/// </summary>
/// <param name="deviceUniqueNo"></param>
[HttpGet("gainfos/all")]
[IgnoreTenant]
public async Task ResetGainfoPageByDeviceAsync(string deviceUniqueNo)
{
await _devicePermissionService.GetDeviceUserCodesAsync(deviceUniqueNo);
}
[HttpGet("userPermissionCodes")]
[IgnoreTenant]
public async Task<List<Guid>> GetAssignDeviceCodesAsync(string userCode)

View File

@@ -50,4 +50,7 @@ public interface IDevicePermissionService : IScopeDependency
/// <param name="userType"></param>
/// <returns></returns>
Task ResetGainfoPageByDeviceAsync(Guid code, string search, GainfoUserTypeEnumType? userType);
Task<List<string>> GetDeviceUserCodesAsync(string deviceUniqueNo);
}

View File

@@ -10,6 +10,7 @@ using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Sys.Entities;
using Dlhis.Entity.Sys.Enums;
using Dlhis.Entity.Visitor.Entity;
using Dlhis.Entity.Visitor.Enums;
using Dlhis.Sys.Api;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Dto;
@@ -346,9 +347,40 @@ public class DevicePermissionService : IDevicePermissionService
/// <exception cref="ArgumentOutOfRangeException"></exception>
private async Task<ISugarQueryable<DeviceGainfoResponse>> GetDeviceUsersQueryAsync(string deviceUniqueNo, string search, GainfoUserTypeEnumType? userType)
{
var device = await _dbContext.Queryable<DeviceEntity>()
.Where(x => x.UniqueNo == deviceUniqueNo)
.SingleAsync();
var device = await _dbContext.Queryable<DeviceEntity>().SingleAsync(x => x.UniqueNo == deviceUniqueNo);
device.ThrowIfNull("设备不存在");
var ids = await GetDeviceUserIdsAsync(deviceUniqueNo);
var factorySetting = await _factorySettingService.GetFactorySettingAsync(device.TenantId, false);
var userTypes = new List<GainfoUserTypeEnumType>();
if (userType.HasValue)
{
if (userType == GainfoUserTypeEnumType.Normal)
{
userTypes.Add(GainfoUserTypeEnumType.Normal);
userTypes.Add(GainfoUserTypeEnumType.SecurityWhite);
}
else
{
userTypes.Add(userType.Value);
}
}
var query = _dbContext.Queryable<GainfoEntity>()
.Where(x => ids.Contains(x.Id))
.WhereIF(userTypes.Any(), x => userTypes.Contains(x.Type))
.Where(x => x.Type != GainfoUserTypeEnumType.External)
.WhereIF(factorySetting.EnableOutsourceDown == false, x => x.Type != GainfoUserTypeEnumType.Outsource)
.WhereIF(device.Type == DeviceTypeEnumType.Meal, x => x.Type != GainfoUserTypeEnumType.External && x.Type != GainfoUserTypeEnumType.Outsource)
.WhereIF(search.IsPresent(), x => x.Code == search || x.Name.Contains(search))
.Select<DeviceGainfoResponse>();
return query;
}
private async Task<List<long>> GetDeviceUserIdsAsync(string deviceUniqueNo)
{
var device = await _dbContext.Queryable<DeviceEntity>().SingleAsync(x => x.UniqueNo == deviceUniqueNo);
device.ThrowIfNull("设备不存在");
var departments = await _dbContext.Queryable<DepartmentEntity>().Select<DepartmentDto>().ToListAsync();
@@ -433,31 +465,65 @@ public class DevicePermissionService : IDevicePermissionService
}
}
ids = ids.Distinct().ToList();
var factorySetting = await _factorySettingService.GetFactorySettingAsync(device.TenantId, false);
var userTypes = new List<GainfoUserTypeEnumType>();
if (userType.HasValue)
{
if (userType == GainfoUserTypeEnumType.Normal)
{
userTypes.Add(GainfoUserTypeEnumType.Normal);
userTypes.Add(GainfoUserTypeEnumType.SecurityWhite);
}
else
{
userTypes.Add(userType.Value);
}
}
return ids.Distinct().ToList();
}
var query = _dbContext.Queryable<GainfoEntity>()
.Where(x => ids.Contains(x.Id))
.WhereIF(userTypes.Any(), x => userTypes.Contains(x.Type))
.Where(x => x.Type != GainfoUserTypeEnumType.External)
.WhereIF(factorySetting.EnableOutsourceDown == false, x => x.Type != GainfoUserTypeEnumType.Outsource)
.WhereIF(device.Type == DeviceTypeEnumType.Meal, x => x.Type != GainfoUserTypeEnumType.External && x.Type != GainfoUserTypeEnumType.Outsource)
.WhereIF(search.IsPresent(), x => x.Code == search || x.Name.Contains(search))
.Select<DeviceGainfoResponse>();
public async Task<List<string>> GetDeviceUserCodesAsync(string deviceUniqueNo)
{
var device = await _dbContext.Queryable<DeviceEntity>().SingleAsync(x => x.UniqueNo == deviceUniqueNo);
device.ThrowIfNull("设备不存在");
return query;
var userIds = await GetDeviceUserIdsAsync(deviceUniqueNo);
var devicePermissionManuallyUserNos = await _dbContext.Queryable<GainfoEntity>()
.Where(x => SqlFunc.Subqueryable<DevicePermissionManuallyEntity>()
.Where(p => p.DeviceCode == device.Code)
.Where(p => p.UserNo == x.No).Any())
.Select(x => x.No)
.ToListAsync();
var userNos = await GetDeviceUserNosFromVisitorAsync(device.Code);
userNos = userNos.Concat(devicePermissionManuallyUserNos).Distinct().ToList();
var gainfoUserCodes = await _dbContext.Queryable<GainfoEntity>()
.Where(x => userIds.Contains(x.Id) || userNos.Contains(x.No))
.Select(x => x.Code)
.ToListAsync();
return gainfoUserCodes;
}
private async Task<List<string>> GetDeviceUserNosFromVisitorAsync(Guid deviceCode)
{
var now = DateTime.Now;
var groupCodes = await _dbContext.Queryable<DeviceGroupEntity>()
.Where(x => SqlFunc.Subqueryable<DeviceGroupRelationshipEntity>()
.Where(p => p.GroupCode == x.Code)
.Where(p => p.DeviceCode == deviceCode)
.Any())
.Select(x => x.Code)
.ToListAsync();
var areaCodes = await _dbContext.Queryable<AppointmentAreaAssignmentEntity>()
.LeftJoin<AppointmentAreaEntity>((x, y) => x.CustomCode == y.CustomCode)
.Where(x => groupCodes.Contains(x.DeviceGroupCode))
.Select((x, y) => new { x.CustomCode, y.Type })
.ToListAsync();
var doorAreaCodes = areaCodes.Where(p => p.Type == AppointmentAreaEnumType.Door).Select(p => p.CustomCode).ToList();
var workplaceAreaCodes = areaCodes.Where(p => p.Type == AppointmentAreaEnumType.Workplace).Select(p => p.CustomCode).ToList();
var details = await _dbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(x => (x.IsEnterDoorArea && now >= x.DoorStart && now <= x.DoorEnd && x.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived)
|| x.IsEnterWorkplace && now >= x.WorkplaceStart && now <= x.WorkplaceEnd && x.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Arrived)
.ToListAsync();
return details.Where(x =>
(x.IsEnterDoorArea && doorAreaCodes.Contains(x.ApplyDoorArea))
|| (x.IsEnterWorkplace && workplaceAreaCodes.Any(p => x.ApplyWorkplaceAreas.Contains(p)))
)
.Select(x => x.UserNo)
.ToList();
}
}