Files
jinko.yikatong/Src/Dlhis.Sys/DataPermission/Services/Implements/DataPermissionService.cs
2025-12-18 10:31:47 +08:00

107 lines
3.9 KiB
C#

using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Sys.Entities;
using Dlhis.Entity.Sys.Enums;
using Dlhis.Sys.DataPermission.Request;
using Dlhis.Sys.DataPermission.Response;
using Dlhis.Sys.Identity.Services;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Mapster;
namespace Dlhis.Sys.DataPermission.Services.Implements;
public class DataPermissionService : IDataPermissionService
{
private readonly IDbContext _dbContext;
private readonly IUserService _userService;
public DataPermissionService(IDbContext dbContext, IUserService userService)
{
_dbContext = dbContext;
_userService = userService;
}
/// <inheritdoc />
public async Task SaveAsync(DataPermissionSaveRequest request)
{
var data = await _dbContext.Queryable<DataPermissionEntity>()
.Where(x => x.AssignValue == request.AssignValue)
.Where(x => x.UniqueType == request.UniqueType)
.WhereIF(request.Type.HasValue, x => x.DeviceType == request.Type)
.ToListAsync();
//¸²¸ÇдÈë
await _dbContext.DeleteReallyAsync(data);
if (request.Codes.IsPresent())
{
await _dbContext.InsertAsync(request.Codes.Select(x => new DataPermissionEntity
{
AssignValue = request.AssignValue,
UniqueType = request.UniqueType,
UniqueValue = x,
DeviceType = request.Type
}).ToList());
}
}
public async Task<List<Guid>> GetAssignmentsAsync(string assignValue, DataPermissionAssignmentEnumType uniqueType, DeviceTypeEnumType? type)
{
return await _dbContext.Queryable<DataPermissionEntity>()
.Where(x => x.AssignValue == assignValue)
.Where(x => x.UniqueType == uniqueType)
.WhereIF(type.HasValue, x => x.DeviceType == type)
.Select(x => x.UniqueValue)
.ToListAsync();
}
public async Task<List<GainfoResponse>> GetUsersDeviceDataPermissionsAsync(Guid deviceCode)
{
var deviceGroupCodes = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
.Where(x => x.DeviceCode == deviceCode)
.Select(x => x.GroupCode)
.Distinct()
.ToListAsync();
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
var nextItems = new List<Guid>();
foreach (var code in deviceGroupCodes)
{
nextItems.AddRange(HandlerDeviceGroupNextCodes(code, deviceGroups));
}
var values = await _dbContext.Queryable<DataPermissionEntity>()
.Where(x => (x.UniqueValue == deviceCode && x.UniqueType == DataPermissionAssignmentEnumType.Device)
|| (nextItems.Contains(x.UniqueValue) && x.UniqueType == DataPermissionAssignmentEnumType.Group)
)
.Select(x => x.AssignValue)
.ToListAsync();
var userIds = values.Adapt<List<int>>();
var users = await _userService.GetUserByIdsAsync(userIds);
return users.Select(x => new GainfoResponse
{
UserCode = x.UserName,
UserName = x.OperatorName
}).ToList();
}
private static List<Guid> HandlerDeviceGroupNextCodes(Guid current, List<DeviceGroupEntity> data)
{
var result = new List<Guid> { current };
InternalHandlerCodes(current, data, result);
return result;
static void InternalHandlerCodes(Guid internalCurrent, List<DeviceGroupEntity> internalData, List<Guid> internalResult)
{
var next = internalData.Where(x => x.PCode == internalCurrent);
foreach (var item in next)
{
internalResult.Add(item.Code);
InternalHandlerCodes(item.Code, internalData, internalResult);
}
}
}
}