diff --git a/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs b/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs index 29642d0f..b83cca85 100644 --- a/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs +++ b/Src/Dlhis.Basic/Device/Controllers/DevicePermissionsController.cs @@ -103,6 +103,17 @@ public class DevicePermissionsController : ControllerBase await _devicePermissionService.ResetGainfoPageByDeviceAsync(request.Code, request.Search, request.UserType); } + /// + /// 获取设备下所有人员 + /// + /// + [HttpGet("gainfos/all")] + [IgnoreTenant] + public async Task ResetGainfoPageByDeviceAsync(string deviceUniqueNo) + { + await _devicePermissionService.GetDeviceUserCodesAsync(deviceUniqueNo); + } + [HttpGet("userPermissionCodes")] [IgnoreTenant] public async Task> GetAssignDeviceCodesAsync(string userCode) diff --git a/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs index 103b15f9..03dc4616 100644 --- a/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs @@ -50,4 +50,7 @@ public interface IDevicePermissionService : IScopeDependency /// /// Task ResetGainfoPageByDeviceAsync(Guid code, string search, GainfoUserTypeEnumType? userType); + + + Task> GetDeviceUserCodesAsync(string deviceUniqueNo); } \ No newline at end of file diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs index a4f3ba72..0d632232 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DevicePermissionService.cs @@ -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 /// private async Task> GetDeviceUsersQueryAsync(string deviceUniqueNo, string search, GainfoUserTypeEnumType? userType) { - var device = await _dbContext.Queryable() - .Where(x => x.UniqueNo == deviceUniqueNo) - .SingleAsync(); + var device = await _dbContext.Queryable().SingleAsync(x => x.UniqueNo == deviceUniqueNo); + device.ThrowIfNull("设备不存在"); + + var ids = await GetDeviceUserIdsAsync(deviceUniqueNo); + var factorySetting = await _factorySettingService.GetFactorySettingAsync(device.TenantId, false); + var userTypes = new List(); + if (userType.HasValue) + { + if (userType == GainfoUserTypeEnumType.Normal) + { + userTypes.Add(GainfoUserTypeEnumType.Normal); + userTypes.Add(GainfoUserTypeEnumType.SecurityWhite); + } + else + { + userTypes.Add(userType.Value); + } + } + + var query = _dbContext.Queryable() + .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(); + + return query; + } + + private async Task> GetDeviceUserIdsAsync(string deviceUniqueNo) + { + var device = await _dbContext.Queryable().SingleAsync(x => x.UniqueNo == deviceUniqueNo); device.ThrowIfNull("设备不存在"); var departments = await _dbContext.Queryable().Select().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(); - 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() - .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(); + public async Task> GetDeviceUserCodesAsync(string deviceUniqueNo) + { + var device = await _dbContext.Queryable().SingleAsync(x => x.UniqueNo == deviceUniqueNo); + device.ThrowIfNull("设备不存在"); - return query; + var userIds = await GetDeviceUserIdsAsync(deviceUniqueNo); + + var devicePermissionManuallyUserNos = await _dbContext.Queryable() + .Where(x => SqlFunc.Subqueryable() + .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() + .Where(x => userIds.Contains(x.Id) || userNos.Contains(x.No)) + .Select(x => x.Code) + .ToListAsync(); + + return gainfoUserCodes; + } + + private async Task> GetDeviceUserNosFromVisitorAsync(Guid deviceCode) + { + var now = DateTime.Now; + + var groupCodes = await _dbContext.Queryable() + .Where(x => SqlFunc.Subqueryable() + .Where(p => p.GroupCode == x.Code) + .Where(p => p.DeviceCode == deviceCode) + .Any()) + .Select(x => x.Code) + .ToListAsync(); + + var areaCodes = await _dbContext.Queryable() + .LeftJoin((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() + .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(); } } \ No newline at end of file