diff --git a/Src/Dlhis.Basic/Device/Controllers/DeviceGroupsController.cs b/Src/Dlhis.Basic/Device/Controllers/DeviceGroupsController.cs index bfd389b3..4ab2c652 100644 --- a/Src/Dlhis.Basic/Device/Controllers/DeviceGroupsController.cs +++ b/Src/Dlhis.Basic/Device/Controllers/DeviceGroupsController.cs @@ -1,4 +1,5 @@ -using Dlhis.Basic.Device.Request; +using System.ComponentModel.DataAnnotations; +using Dlhis.Basic.Device.Request; using Dlhis.Basic.Device.Response; using Dlhis.Basic.Device.Services; using Dlhis.Entity.Device.Enums; @@ -26,14 +27,16 @@ public class DeviceGroupsController : ControllerBase /// /// 获取设备组树 /// - /// 是否显示设备 /// 分组类型 /// [HttpGet("tree")] [IgnoreTenant] - public async Task> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type) + public async Task> GetTreeAsync( + [EnumDataType(typeof(DeviceTypeEnumType), ErrorMessage = "请选择正确的类型")] + DeviceTypeEnumType type + ) { - return await _deviceGroupService.GetTreeAsync(showDevices, type); + return await _deviceGroupService.GetTreeAsync(type); } /// @@ -54,9 +57,9 @@ public class DeviceGroupsController : ControllerBase /// /// [HttpGet("devices")] - public async Task> GetDevicesAsync(Guid code) + public async Task> GetDevicesByGroupCodeAsync(Guid code) { - return await _deviceGroupService.GetDevicesAsync(code); + return await _deviceGroupService.GetDevicesByGroupCodeAsync(code); } /// @@ -81,4 +84,4 @@ public class DeviceGroupsController : ControllerBase { return await _deviceGroupService.GetAppointmentGroupsAsync(); } -} +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Device/Response/DeviceUnderGroupResponse.cs b/Src/Dlhis.Basic/Device/Response/DeviceUnderGroupResponse.cs index c9d59404..1edfa8a4 100644 --- a/Src/Dlhis.Basic/Device/Response/DeviceUnderGroupResponse.cs +++ b/Src/Dlhis.Basic/Device/Response/DeviceUnderGroupResponse.cs @@ -4,6 +4,9 @@ namespace Dlhis.Basic.Device.Response; public class DeviceUnderGroupResponse { + /// + /// 设备code + /// public Guid Code { get; set; } /// @@ -20,8 +23,7 @@ public class DeviceUnderGroupResponse /// 设备类型 /// public DeviceTypeEnumType Type { get; set; } - - + /// /// 是否启用 /// diff --git a/Src/Dlhis.Basic/Device/Services/IDeviceGroupService.cs b/Src/Dlhis.Basic/Device/Services/IDeviceGroupService.cs index 9772a7ea..5f0a4368 100644 --- a/Src/Dlhis.Basic/Device/Services/IDeviceGroupService.cs +++ b/Src/Dlhis.Basic/Device/Services/IDeviceGroupService.cs @@ -6,13 +6,13 @@ namespace Dlhis.Basic.Device.Services; public interface IDeviceGroupService : IScopeDependency { - Task> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type); + Task> GetTreeAsync(DeviceTypeEnumType type); Task SaveAsync(DeviceGroupSaveRequest request); - Task> GetDevicesAsync(Guid code); + Task> GetDevicesByGroupCodeAsync(Guid code); Task AssignDevicesAsync(DeviceAssignRequest requests); Task> GetAppointmentGroupsAsync(); -} +} \ No newline at end of file diff --git a/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs b/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs index fe3dcbd2..673595f4 100644 --- a/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs +++ b/Src/Dlhis.Basic/Device/Services/Implements/DeviceGroupService.cs @@ -3,6 +3,7 @@ using Dlhis.Basic.Device.Response; using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Device.Entities; using Dlhis.Entity.Device.Enums; +using Dlhis.Sys.Api; using Kurisu.AspNetCore.Authentication.Abstractions; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils.Extensions; @@ -15,27 +16,42 @@ public class DeviceGroupService : IDeviceGroupService private readonly IDbContext _dbContext; private readonly IDevicePermissionService _devicePermissionService; private readonly ICurrentUser _currentUser; + private readonly ITenantApi _tenantApi; - public DeviceGroupService(IDbContext dbContext, IDevicePermissionService devicePermissionService, ICurrentUser currentUser) + public DeviceGroupService(IDbContext dbContext, + IDevicePermissionService devicePermissionService, + ICurrentUser currentUser, + ITenantApi tenantApi + ) { _dbContext = dbContext; _devicePermissionService = devicePermissionService; _currentUser = currentUser; + _tenantApi = tenantApi; } - public async Task> GetTreeAsync(bool showDevices, DeviceTypeEnumType? type) + public async Task> GetTreeAsync(DeviceTypeEnumType type) { + var tenantResult = await _tenantApi.GetMyTenantsAsync(); + var tenantIds = tenantResult.Data.Select(x => x.Code).ToList(); + tenantIds.AddRange(tenantResult.Data.Select(x => x.PCode)); + var deviceGroups = await _dbContext.Queryable() - .WhereIF(type.HasValue, x => x.Type == type) + .Where(x => tenantIds.Contains(x.TenantId)) + .Where(x => x.Type == type) .Select() .ToListAsync(); + var others = await _dbContext.Queryable() + .Where(x => x.Type == type) + .Where(x => x.Name == "*共用区域" || x.Name == "*非共用区域" || x.Name == "*通用考勤设备组" || x.Name == "*专用考勤设备组") + .Select() + .ToListAsync(); + deviceGroups.AddRange(others); + var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList(); - tops.ForEach(x => - { - RecursionGetNext(x, deviceGroups, showDevices); - }); + tops.ForEach(x => { RecursionGetNext(x, deviceGroups); }); return tops; } @@ -69,7 +85,7 @@ public class DeviceGroupService : IDeviceGroupService existsDiffNextGroups.ThrowIfTrue("存在下级分组类型不一致,无法修改"); - var nextDevices = await GetDevicesAsync(entity.Code); + var nextDevices = await GetDevicesByGroupCodeAsync(entity.Code); nextDevices.Any(x => x.Type != request.Type).ThrowIfTrue("分组设备类型不匹配,无法修改类型"); } @@ -99,15 +115,13 @@ public class DeviceGroupService : IDeviceGroupService DeviceCode = x, TenantId = _currentUser.GetStringTenantId(), - }).ToList(); await _dbContext.InsertAsync(entities); - }, null); } - public async Task> GetDevicesAsync(Guid code) + public async Task> GetDevicesByGroupCodeAsync(Guid code) { var relationships = await _dbContext.Queryable() .Where(x => x.GroupCode == code) @@ -117,31 +131,31 @@ public class DeviceGroupService : IDeviceGroupService return await _dbContext.Queryable() .LeftJoin((x, dt) => x.TemplateCode == dt.Code) - .Where(x => deviceCodes.Contains(x.Code)) - .Select((x, dt) => new DeviceUnderGroupResponse - { - UniqueNo = x.UniqueNo, - Type = x.Type, - Code = x.Code, - Enable = x.Enable, - Brand = dt.Brand - }) - .ToListAsync(); + .Where(x => deviceCodes.Contains(x.Code)) + .Select((x, dt) => new DeviceUnderGroupResponse + { + UniqueNo = x.UniqueNo, + Type = x.Type, + Code = x.Code, + Enable = x.Enable, + Brand = dt.Brand + }) + .ToListAsync(); } public async Task> GetAppointmentGroupsAsync() { return await _dbContext.Queryable() - .Where(x => x.AsAppointment) - .Where(x => x.Type == DeviceTypeEnumType.Door) - .Select(x => new DeviceGroupResponse - { - Id = x.Id, - Code = x.Code, - Name = x.Name, - }) - .ToListAsync(); + .Where(x => x.AsAppointment) + .Where(x => x.Type == DeviceTypeEnumType.Door) + .Select(x => new DeviceGroupResponse + { + Id = x.Id, + Code = x.Code, + Name = x.Name, + }) + .ToListAsync(); } /****************************************************** help methods *********************************************************/ @@ -198,47 +212,14 @@ public class DeviceGroupService : IDeviceGroupService /// /// /// - /// - private void RecursionGetNext(DeviceGroupTreeResponse current, List deviceGroups, bool showDevices) + private static void RecursionGetNext(DeviceGroupTreeResponse current, List deviceGroups) { - var currentTenantId = _currentUser.GetStringTenantId(); - var next = deviceGroups.Where(x => - { - if (x.PCode == current.Code) - { - if (x.TenantId.IsEmpty()) - { - return true; - } - else - { - return x.TenantId == currentTenantId; - } - } + var next = deviceGroups.Where(x => x.PCode == current.Code).ToList(); - return false; - - }).ToList(); current.Next = next; foreach (var item in next) { - RecursionGetNext(item, deviceGroups, showDevices); - } - - //最后层级附加设备信息 - if (!current.Next.Any() && showDevices) - { - current.Next = _dbContext.Queryable() - .LeftJoin((relation, device) => relation.DeviceCode == device.Code) - .Where((relation, device) => relation.GroupCode == current.Code) - .Select((relation, device) => new DeviceGroupTreeResponse - { - Code = device.Code, - Name = device.UniqueNo, - PCode = relation.GroupCode - }) - .Distinct() - .ToList(); + RecursionGetNext(item, deviceGroups); } } -} +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/Jobs/CreateDefinitelyGroupJob.cs b/Src/Dlhis.ScheduledTask/Jobs/CreateDefinitelyGroupJob.cs index 03ef1a25..3169a8ec 100644 --- a/Src/Dlhis.ScheduledTask/Jobs/CreateDefinitelyGroupJob.cs +++ b/Src/Dlhis.ScheduledTask/Jobs/CreateDefinitelyGroupJob.cs @@ -24,7 +24,7 @@ public class CreateDefinitelyGroupJob : BaseJob _tenantApi = tenantApi; } - private static readonly string _remark = "系统自动生成"; + private const string _remark = "系统自动生成"; protected override async Task RunAsync(IServiceProvider serviceProvider) { @@ -45,16 +45,15 @@ public class CreateDefinitelyGroupJob : BaseJob //*****************************************************门禁设备组********************************************************/ await CreateDeviceGroupAsync(DeviceTypeEnumType.Door, new List { "*共用区域", "*非共用区域" }, tenants); - } private async Task CreateGainfoGroupsAsync(List tenants) { - var grps = new List { "*自定义人员分组", "*督查组", "*X3及以上领导组" }; - var searchGrps = await DbContext.Queryable().Where(x => grps.Contains(x.Name)).ToListAsync(); - foreach (var grp in grps) + var groups = new List { "*自定义人员分组", "*督查组", "*X3及以上领导组" }; + var searchGroups = await DbContext.Queryable().Where(x => groups.Contains(x.Name)).ToListAsync(); + foreach (var grp in groups) { - if (!searchGrps.Any(x => x.Name == grp)) + if (searchGroups.All(x => x.Name != grp)) { await DbContext.InsertAsync(new GainfoGroupEntity { @@ -122,7 +121,7 @@ public class CreateDefinitelyGroupJob : BaseJob CreateTime = DateTime.Now, ModifiedBy = 1, ModifiedTime = DateTime.Now, - TenantId = null, + TenantId = workplace.Code, }; await DbContext.InsertAsync(workplaceGrp); } @@ -153,4 +152,4 @@ public class CreateDefinitelyGroupJob : BaseJob } } } -} +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs index 1c9cb472..d7c4a362 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Request/DeviceTaskAssignmentRequest.cs @@ -25,6 +25,11 @@ public class DeviceTaskAssignmentRequest : PageDto, IValidatableObject public string UserNo { get; set; } + /// + /// 厂区 + /// + public string Factory { get; set; } + /// /// 搜索范围 /// @@ -32,11 +37,11 @@ public class DeviceTaskAssignmentRequest : PageDto, IValidatableObject public IEnumerable Validate(ValidationContext validationContext) { - if (SearchDateTimePeriod != null && SearchDateTimePeriod.End.HasValue) + if (SearchDateTimePeriod is { End: not null }) { SearchDateTimePeriod.End = SearchDateTimePeriod.End.Value.AddDays(1).AddSeconds(-1); } yield break; } -} +} \ No newline at end of file diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs index 89588486..150bf62a 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/AssignmentService.cs @@ -164,8 +164,6 @@ public class AssignmentService : IAssignmentService }; } break; - default: - break; } return dto; @@ -232,32 +230,32 @@ public class AssignmentService : IAssignmentService var data = await CommonHelper.RetryAsync(async () => { var page = await _dbContext.Queryable() - .LeftJoin((deviceTask, deivce) => deviceTask.DeviceUniqueNo == deivce.UniqueNo) - .LeftJoin((deviceTask, deivce, deivceTamplate) => deivceTamplate.Code == deivce.TemplateCode) - .LeftJoin((deviceTask, deivce, deivceTamplate, ga) => deviceTask.UserNo == ga.No) - .LeftJoin((deviceTask, deivce, deivceTamplate, ga, galeave) => deviceTask.UserNo == galeave.No) - .LeftJoin((deviceTask, deivce, deivceTamplate, ga, galeave, department) => department.No == ga.DepartmentNo) - .LeftJoin((deviceTask, deivce, deivceTamplate, ga, galeave, department, gainfoEx) => deviceTask.UserNo == gainfoEx.No) + .LeftJoin((deviceTask, device) => deviceTask.DeviceUniqueNo == device.UniqueNo) + .LeftJoin((deviceTask, device, deviceTemplate) => deviceTemplate.Code == device.TemplateCode) + .LeftJoin((deviceTask, device, deviceTemplate, ga) => deviceTask.UserNo == ga.No) + .LeftJoin((deviceTask, device, deviceTemplate, ga, gaLeave) => deviceTask.UserNo == gaLeave.No) + .LeftJoin((deviceTask, device, deviceTemplate, ga, gaLeave, department) => department.No == ga.DepartmentNo) + .LeftJoin((deviceTask, device, deviceTemplate, ga, gaLeave, department, gainfoEx) => deviceTask.UserNo == gainfoEx.No) .WhereIF(!request.IsProcess, deviceTask => deviceTask.Status == DeviceTaskStatusEnumType.Wait) .WhereIF(request.IsProcess, deviceTask => deviceTask.Status == DeviceTaskStatusEnumType.Process && SqlFunc.DateDiff(DateType.Minute, deviceTask.ModifiedTime, DateTime.Now) > 30) - .WhereIF(request.DeviceUniqueNo.IsPresent(), (deviceTask, deivce) => deviceTask.DeviceUniqueNo == request.DeviceUniqueNo) - .WhereIF(request.Factory.IsPresent(), (deviceTask, deivce) => deviceTask.TenantId == request.Factory) + .WhereIF(request.DeviceUniqueNo.IsPresent(), (deviceTask, device) => deviceTask.DeviceUniqueNo == request.DeviceUniqueNo) + .WhereIF(request.Factory.IsPresent(), (deviceTask, device) => deviceTask.TenantId == request.Factory) .WhereIF(request.Types.IsPresent(), deviceTask => request.Types.Contains(deviceTask.Type)) - .WhereIF(request.Brand.IsPresent(), (deviceTask, deivce, deivceTamplate) => deivceTamplate.Brand == request.Brand) + .WhereIF(request.Brand.IsPresent(), (deviceTask, device, deviceTemplate) => deviceTemplate.Brand == request.Brand) .OrderBy(deviceTask => new { Priority = SqlFunc.Desc(deviceTask.Priority), deviceTask.CreateTime }) - .Select((deviceTask, deivce, deivceTamplate, ga, galeave, department, gainfoEx) => new TaskDto + .Select((deviceTask, device, deviceTemplate, ga, gaLeave, department, gainfoEx) => new TaskDto { - UserId = SqlFunc.IsNull(SqlFunc.IsNull(deviceTask.UserId, ga.Id), galeave.IdentityUserId), + UserId = SqlFunc.IsNull(SqlFunc.IsNull(deviceTask.UserId, ga.Id), gaLeave.IdentityUserId), TaskId = deviceTask.Id, Type = deviceTask.Type, DeviceUniqueNo = deviceTask.DeviceUniqueNo, - DeviceConnectionInfo = deivce.ConnectionInfo, - Brand = deivceTamplate.Brand, - FuncStr = deivce.Funcs.ToString(), + DeviceConnectionInfo = device.ConnectionInfo, + Brand = deviceTemplate.Brand, + FuncStr = device.Funcs.ToString(), UserNo = deviceTask.UserNo, - UserCode = SqlFunc.IsNull(SqlFunc.IsNull(ga.Code, galeave.Code), gainfoEx.Code), - UserName = SqlFunc.IsNull(SqlFunc.IsNull(ga.Name, galeave.Name), gainfoEx.Name), + UserCode = SqlFunc.IsNull(SqlFunc.IsNull(ga.Code, gaLeave.Code), gainfoEx.Code), + UserName = SqlFunc.IsNull(SqlFunc.IsNull(ga.Name, gaLeave.Name), gainfoEx.Name), DepartmentNo = ga.DepartmentNo, DepartmentName = department.Name, CardNo = ga.CardNo, @@ -294,8 +292,8 @@ public class AssignmentService : IAssignmentService { if (data.Any(x => x.Type == DeviceTaskEnumType.UploadRecord)) { - var deviceNos = data.Select(x => x.DeviceUniqueNo).Distinct(); - var result = new Dictionary(deviceNos.Count()); + var deviceNos = data.Select(x => x.DeviceUniqueNo).Distinct().ToList(); + var result = new Dictionary(deviceNos.Count); //日志数据最后一条 var logInfos = await _dbContext.Queryable().Select(it => new diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs index e752ed7b..afba7364 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Services/Implements/DeviceTaskAssignmentService.cs @@ -28,6 +28,7 @@ public class DeviceTaskAssignmentService : IDeviceTaskAssignmentService var page = await _dbContext.Queryable() .LeftJoin((deviceTask, ga) => deviceTask.UserNo == ga.No) .LeftJoin((deviceTask, ga, device) => deviceTask.DeviceUniqueNo == device.UniqueNo) + .Where(deviceTask => deviceTask.TenantId == request.Factory) .WhereIF(request.Type.HasValue, (deviceTask, ga, device) => deviceTask.DeviceType == request.Type) .WhereIF(request.SearchDateTimePeriod?.Start.HasValue == true && request.SearchDateTimePeriod?.End.HasValue == true, deviceTask => deviceTask.ModifiedTime >= request.SearchDateTimePeriod.Start && deviceTask.ModifiedTime <= request.SearchDateTimePeriod.End