fix 增加权限数据展开

This commit is contained in:
2025-12-24 17:24:43 +08:00
parent 74c5eafa8c
commit e14577b76c
3 changed files with 202 additions and 8 deletions

View File

@@ -3,14 +3,18 @@ using Dlhis.Basic.Device.Response;
using Dlhis.Basic.Device.Services;
using Dlhis.Basic.Organization.Request;
using Dlhis.Basic.Organization.Response;
using Dlhis.Basic.Utils;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Sys.Enums;
using Kurisu.AspNetCore.DataAccess.Dto;
using Kurisu.AspNetCore.DataAccess.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Microsoft.AspNetCore.Mvc.RazorPages;
using SqlSugar;
namespace Dlhis.Basic.Organization.Services.Implements;
@@ -41,6 +45,160 @@ public class GainfoPermissionService : IGainfoPermissionService
});
}
public async Task<Dictionary<string, List<TenantAssignTypeDeviceCodeExpanded>>> ExpandUserAssignDeviceCodesWithTypeInTenantAsync(Dictionary<string, List<TenantAssignTypeDeviceCode>> values)
{
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync();
var departments = await _dbContext.Queryable<DepartmentEntity>().Select(x => new { x.No, x.Name }).ToListAsync();
var userGroups = await _dbContext.Queryable<GainfoGroupEntity>().Select(x => new { x.Code, x.Name }).ToListAsync();
var deviceGroupRelationships = await _dbContext.Queryable<DeviceGroupRelationshipEntity>().ToListAsync();
return values.ToDictionary(x => x.Key, x => x.Value.SelectMany(item =>
{
return item.AssignTypes.SelectMany(p =>
{
if (p.UniqueType == DeviceAssignmentTypeEnumType.Group)
{
var currentDeviceGroupCode = p.OriginalCodes.First();
var link = new List<Guid> { currentDeviceGroupCode };
var r = deviceGroupRelationships.Where(x => x.GroupCode == currentDeviceGroupCode).Select(x => new TenantAssignTypeDeviceCodeExpanded
{
Tenant = item.Tenant,
AssignType = p.AssignType,
AssignValue = p.AssignValue,
UniqueType = p.UniqueType,
DeviceCode = x.DeviceCode,
DeviceGroupCodes = link
}).ToList();
HandleNext(currentDeviceGroupCode, link, deviceGroups, deviceGroupRelationships, r);
r.ForEach(x =>
{
x.Tenant = item.Tenant;
x.AssignType = p.AssignType;
x.AssignValue = p.AssignValue;
x.UniqueType = p.UniqueType;
});
return r;
}
if (p.UniqueType == DeviceAssignmentTypeEnumType.Device)
{
return p.Codes.Select(x => new TenantAssignTypeDeviceCodeExpanded()
{
Tenant = item.Tenant,
AssignType = p.AssignType,
AssignValue = p.AssignValue,
UniqueType = p.UniqueType,
DeviceCode = x
}).ToList();
}
return new List<TenantAssignTypeDeviceCodeExpanded>();
}).ToList();
}).ToList());
}
public async Task<Pagination<GainfoDevicePermissionResponse>> GetAssignDeviceCodes2Async(DeviceAssRequest request)
{
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(new List<string> { request.UserNo });
if (!result.TryGetValue(request.UserNo, out var permissions))
{
return new Pagination<GainfoDevicePermissionResponse>();
}
var expandResult = await ExpandUserAssignDeviceCodesWithTypeInTenantAsync(result);
var deviceCodes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
var list = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceRegistryEntity>((d, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search)
.Where(d => deviceCodes.Contains(d.Code))
.Select((d, dr) => new GainfoDevicePermissionResponse { Factory = d.TenantId }, true)
.ToListAsync();
var deviceGroups = await _dbContext.Queryable<DeviceGroupEntity>().Select(x => new { x.Code, x.Name }).ToListAsync();
var departments = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Department))
? await _dbContext.Queryable<DepartmentEntity>().Select(x => new { x.No, x.Name }).ToListAsync()
: new[] { new { No = string.Empty, Name = string.Empty } }.ToList();
var userGroups = permissions.Any(x => x.AssignTypes.Any(p => p.AssignType == AssignTypeEnumType.Group))
? await _dbContext.Queryable<GainfoGroupEntity>().Select(x => new { x.Code, x.Name }).ToListAsync()
: new[] { new { Code = Guid.Empty, Name = string.Empty } }.ToList();
list.ForEach(item =>
{
var permission = permissions.FirstOrDefault(x => x.Tenant == item.Factory);
if (permission != null)
{
item.Source = new DevicePermissionSourceDto
{
Factory = item.Factory,
Types = permission.AssignTypes.Where(x => x.Codes.Any(p => p == item.Code)).Select(x =>
{
var codeDesc = x.UniqueType == DeviceAssignmentTypeEnumType.Device
? item.DeployPlace
: deviceGroups.FirstOrDefault(p => p.Code == x.OriginalCodes.First())?.Name;
var currentAssignValue = x.AssignValue;
currentAssignValue = x.AssignType switch
{
AssignTypeEnumType.Department => departments.FirstOrDefault(p => p.No == x.AssignValue.Split(":")[1])?.Name,
AssignTypeEnumType.Group => userGroups.FirstOrDefault(p => p.Code == Guid.Parse(currentAssignValue))?.Name,
AssignTypeEnumType.User => "自己",
_ => currentAssignValue
};
var r = new DevicePermissionSourceTypeDto
{
AssignType = x.AssignType,
AssignValue = currentAssignValue,
UniqueType = x.UniqueType,
Codes = x.UniqueType == DeviceAssignmentTypeEnumType.Device
? x.Codes.Where(p => p == item.Code)
: x.Codes.Take(1),
CodeDesc = codeDesc
};
return r;
})
};
}
});
var page = list.Where(x => x.Type == request.DeviceType).ToPage(request);
page.Total = list.Where(x => x.Type == request.DeviceType).Count();
return page;
}
private void HandleNext(Guid current, List<Guid> links, List<DeviceGroupEntity> deviceGroups, List<DeviceGroupRelationshipEntity> deviceGroupRelationships, List<TenantAssignTypeDeviceCodeExpanded> result)
{
var items = deviceGroups.Where(x => x.PCode == current).ToList();
foreach (var item in items)
{
var newLinks = links.ToList();
newLinks.Add(item.Code);
var deviceCodes = deviceGroupRelationships.Where(x => x.GroupCode == item.Code).ToList();
if (deviceCodes.Any())
{
result.AddRange(deviceCodes.Select(x => new TenantAssignTypeDeviceCodeExpanded
{
DeviceCode = x.DeviceCode,
DeviceGroupCodes = newLinks
}).ToList());
}
HandleNext(item.Code, newLinks, deviceGroups, deviceGroupRelationships, result);
}
}
public async Task<Pagination<GainfoDevicePermissionResponse>> GetAssignDeviceCodesAsync(DeviceAssRequest request)
{
@@ -50,13 +208,13 @@ public class GainfoPermissionService : IGainfoPermissionService
return new Pagination<GainfoDevicePermissionResponse>();
}
var codes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
var deviceCodes = permissions.SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
var page = await _dbContext.Queryable<DeviceEntity>()
.LeftJoin<DeviceRegistryEntity>((d, dr) => d.UniqueNo == dr.DeviceUniqueNo)
.WhereIF(request.DeviceType.HasValue, d => d.Type == request.DeviceType)
.WhereIF(request.Search.IsPresent(), d => d.UniqueNo == request.Search)
.Where(d => codes.Contains(d.Code))
.Where(d => deviceCodes.Contains(d.Code))
.Select((d, dr) => new GainfoDevicePermissionResponse { Factory = d.TenantId }, true)
.ToPageListAsync(request);