feat 升级版本, 设备权限
This commit is contained in:
@@ -33,6 +33,17 @@ public class DeviceGroupsController : ControllerBase
|
||||
return await _deviceGroupService.GetPageAsync(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取设备分组树
|
||||
/// </summary>
|
||||
/// <param name="showDevices">是否显示设备</param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("tree")]
|
||||
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices)
|
||||
{
|
||||
return await _deviceGroupService.GetTreeAsync(showDevices);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设备分组保存
|
||||
/// </summary>
|
||||
@@ -61,7 +72,7 @@ public class DeviceGroupsController : ControllerBase
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("assign")]
|
||||
public async Task AssignDevicesAsync(AssignDevicesRequest request)
|
||||
public async Task AssignDevicesAsync(DeviceAssignRequest request)
|
||||
{
|
||||
await _deviceGroupService.AssignDevicesAsync(request);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Basic.Device.Services;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Dlhis.Basic.Device.Controllers;
|
||||
|
||||
[Authorize]
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class DevicePermissionsController : ControllerBase
|
||||
{
|
||||
private readonly IDevicePermissionService _devicePermissionService;
|
||||
|
||||
public DevicePermissionsController(IDevicePermissionService devicePermissionService)
|
||||
{
|
||||
_devicePermissionService = devicePermissionService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分配权限
|
||||
/// </summary>
|
||||
/// <param name="requests"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("assign")]
|
||||
public async Task AssignmentAsync(List<DevicePermissionSaveReuqest> requests)
|
||||
{
|
||||
await _devicePermissionService.AssignmentAsync(requests);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 当前类型的权限分配
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("assignments")]
|
||||
public async Task<List<Guid>> GetAssignmentsAsync(DeviceGetAssignmentsRequest request)
|
||||
{
|
||||
return await _devicePermissionService.GetAssignmentsAsync(request.UserId, request.UniqueType, request.UniqueValue);
|
||||
}
|
||||
}
|
||||
@@ -2,11 +2,16 @@
|
||||
|
||||
namespace Dlhis.Basic.Device.Request;
|
||||
|
||||
public class AssignDevicesRequest
|
||||
public class DeviceAssignRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备组
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "设备组不能为空")]
|
||||
public Guid GroupCode { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 设备code
|
||||
/// </summary>
|
||||
public List<Guid> DeviceCodes { get; set; } = new List<Guid>();
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Dlhis.Basic.Device.Request;
|
||||
|
||||
public class DeviceGetAssignmentsRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户档案号
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 权限分配类型 MenuAssignmentTypeEnumType
|
||||
/// </summary>
|
||||
[EnumDataType(typeof(DeviceAssignmentTypeEnumType))]
|
||||
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 类型对应的唯一值(可为空)
|
||||
/// </summary>
|
||||
public Guid? UniqueValue { get; set; }
|
||||
}
|
||||
@@ -6,6 +6,11 @@ public class DeviceGroupSaveRequest
|
||||
{
|
||||
public long? Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 上级分组
|
||||
/// </summary>
|
||||
public Guid? PCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 设备组名称
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
|
||||
namespace Dlhis.Basic.Device.Request;
|
||||
|
||||
public class DevicePermissionSaveReuqest
|
||||
{
|
||||
/// <summary>
|
||||
/// 人员档案号
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "人员档案号不能为空")]
|
||||
public string UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分配权限类型 DevicePermissionTypeEnumType
|
||||
/// </summary>
|
||||
[EnumDataType(typeof(DeviceAssignmentTypeEnumType), ErrorMessage = "枚举类型错误")]
|
||||
public DeviceAssignmentTypeEnumType UniqueType { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分配权限值不能为空
|
||||
/// </summary>
|
||||
[Required(ErrorMessage = "分配权限值不能为空")]
|
||||
public Guid UniqueValue { get; set; }
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace Dlhis.Basic.Device.Response;
|
||||
namespace Dlhis.Basic.Device.Response;
|
||||
|
||||
public class DeviceGroupPageResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 设备分组code
|
||||
/// </summary>
|
||||
public Guid Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
|
||||
24
Src/Dlhis.Basic/Device/Response/DeviceGroupTreeResponse.cs
Normal file
24
Src/Dlhis.Basic/Device/Response/DeviceGroupTreeResponse.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
namespace Dlhis.Basic.Device.Response;
|
||||
|
||||
public class DeviceGroupTreeResponse
|
||||
{
|
||||
/// <summary>
|
||||
/// 上级分组code
|
||||
/// </summary>
|
||||
public Guid? PCode { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分组code/设备code
|
||||
/// </summary>
|
||||
public Guid Code { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 分组名称/设备名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 下级
|
||||
/// </summary>
|
||||
public List<DeviceGroupTreeResponse> Next { get; set; } = new();
|
||||
}
|
||||
@@ -8,9 +8,11 @@ public interface IDeviceGroupService : IScopeDependency
|
||||
{
|
||||
Task<Pagination<DeviceGroupPageResponse>> GetPageAsync(DeviceGroupPageRequest request);
|
||||
|
||||
Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices);
|
||||
|
||||
Task SaveAsync(DeviceGroupSaveRequest request);
|
||||
|
||||
Task<List<DeviceUnderGroupResponse>> GetDevicesAsync(Guid code);
|
||||
|
||||
Task AssignDevicesAsync(AssignDevicesRequest requests);
|
||||
Task AssignDevicesAsync(DeviceAssignRequest requests);
|
||||
}
|
||||
|
||||
16
Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs
Normal file
16
Src/Dlhis.Basic/Device/Services/IDevicePermissionService.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
|
||||
namespace Dlhis.Basic.Device.Services;
|
||||
|
||||
public interface IDevicePermissionService : IScopeDependency
|
||||
{
|
||||
/// <summary>
|
||||
/// 保存设备权限
|
||||
/// </summary>
|
||||
/// <param name="reuqests"></param>
|
||||
/// <returns></returns>
|
||||
Task AssignmentAsync(List<DevicePermissionSaveReuqest> reuqests);
|
||||
|
||||
Task<List<Guid>> GetAssignmentsAsync(string userId, DeviceAssignmentTypeEnumType uniqueType, Guid? uniqueValue);
|
||||
}
|
||||
@@ -27,8 +27,26 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
.ToPageListAsync(request);
|
||||
}
|
||||
|
||||
public async Task<List<DeviceGroupTreeResponse>> GetTreeAsync(bool showDevices)
|
||||
{
|
||||
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>()
|
||||
.Select<DeviceGroupTreeResponse>()
|
||||
.ToListAsync();
|
||||
|
||||
var tops = deviceGroups.Where(x => !x.PCode.HasValue).ToList();
|
||||
tops.ForEach(x =>
|
||||
{
|
||||
RecursionGetNext(x, deviceGroups, showDevices);
|
||||
});
|
||||
|
||||
return tops;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(DeviceGroupSaveRequest request)
|
||||
{
|
||||
//检查父级节点
|
||||
await CheckPCodeExistsDeviceAsync(request.PCode);
|
||||
|
||||
if (!request.Id.HasValue)
|
||||
{
|
||||
var entity = request.Adapt<DeviceGroupEntity>();
|
||||
@@ -52,11 +70,13 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
await _dbContext.DeleteAsync(deviceGroup);
|
||||
}
|
||||
|
||||
public async Task AssignDevicesAsync(AssignDevicesRequest request)
|
||||
public async Task AssignDevicesAsync(DeviceAssignRequest request)
|
||||
{
|
||||
var group = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
||||
group.ThrowIfNull("设备组不存在");
|
||||
|
||||
await CheckNoNextGroupAsync(group.Code);
|
||||
|
||||
if (!request.DeviceCodes.Any())
|
||||
{
|
||||
//todo 删除时候判断权限是否分配
|
||||
@@ -75,7 +95,6 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
|
||||
await _dbContext.InsertAsync(entities);
|
||||
}
|
||||
|
||||
//todo 生成下发任务
|
||||
}
|
||||
|
||||
@@ -90,4 +109,63 @@ public class DeviceGroupService : IDeviceGroupService
|
||||
.ToListAsync();
|
||||
|
||||
}
|
||||
|
||||
/****************************************************** help methods *********************************************************/
|
||||
|
||||
/// <summary>
|
||||
/// 检查
|
||||
/// </summary>
|
||||
/// <param name="pCode"></param>
|
||||
/// <returns></returns>
|
||||
private async Task CheckPCodeExistsDeviceAsync(Guid? pCode)
|
||||
{
|
||||
if (pCode.HasValue)
|
||||
{
|
||||
var exists = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().Where(x => x.GroupCode == pCode).AnyAsync();
|
||||
exists.ThrowIfTrue("父级分组存在设备");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="currentCode"></param>
|
||||
/// <returns></returns>
|
||||
private async Task CheckNoNextGroupAsync(Guid currentCode)
|
||||
{
|
||||
var exists = await _dbContext.Queryable<DeviceGroupEntity>().Where(x => x.PCode == currentCode).AnyAsync();
|
||||
exists.ThrowIfTrue("存在下级设备分组");
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 递归获取树
|
||||
/// </summary>
|
||||
/// <param name="current"></param>
|
||||
/// <param name="departments"></param>
|
||||
/// <param name="showDevices"></param>
|
||||
private void RecursionGetNext(DeviceGroupTreeResponse current, List<DeviceGroupTreeResponse> departments, bool showDevices)
|
||||
{
|
||||
var next = departments.Where(x => x.PCode == current.Code).ToList();
|
||||
current.Next = next;
|
||||
foreach (var item in next)
|
||||
{
|
||||
RecursionGetNext(item, departments, showDevices);
|
||||
}
|
||||
|
||||
//最后层级附加设备信息
|
||||
if (!current.Next.Any() && showDevices)
|
||||
{
|
||||
current.Next = _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
||||
.LeftJoin<DeviceEntity>((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
|
||||
})
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
using Dlhis.Basic.Device.Request;
|
||||
using Dlhis.Entity.Sys.Entities;
|
||||
using Dlhis.Entity.Sys.Enums;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
|
||||
namespace Dlhis.Basic.Device.Services.Implements;
|
||||
|
||||
public class DevicePermissionService : IDevicePermissionService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public DevicePermissionService(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task AssignmentAsync(List<DevicePermissionSaveReuqest> reuqests)
|
||||
{
|
||||
foreach (var item in reuqests)
|
||||
{
|
||||
var permissions = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
.Where(x => x.UniqueType == item.UniqueType)
|
||||
.Where(x => x.UniqueValue == item.UniqueValue)
|
||||
.Where(x => x.UserId == item.UserId)
|
||||
.ToListAsync();
|
||||
|
||||
//todo 差异数据
|
||||
await _dbContext.Client.Deleteable(permissions).ExecuteCommandAsync();
|
||||
|
||||
await _dbContext.InsertAsync(new DevicePermissionEntity
|
||||
{
|
||||
UserId = item.UserId,
|
||||
UniqueType = item.UniqueType,
|
||||
UniqueValue = item.UniqueValue,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<List<Guid>> GetAssignmentsAsync(string userId, DeviceAssignmentTypeEnumType uniqueType, Guid? uniqueValue)
|
||||
{
|
||||
var assignments = await _dbContext.Queryable<DevicePermissionEntity>()
|
||||
.Where(x => x.UniqueType == uniqueType)
|
||||
.WhereIF(uniqueValue.HasValue, x => x.UniqueValue == uniqueValue)
|
||||
.Where(x => x.UserId == userId)
|
||||
.Select(x => x.UniqueValue)
|
||||
.ToListAsync();
|
||||
|
||||
return assignments;
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.4.7" />
|
||||
<PackageReference Include="Kurisu.AspNetCore" Version="0.4.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Reference in New Issue
Block a user