239 lines
8.9 KiB
C#
239 lines
8.9 KiB
C#
using Dlhis.Basic.DataPermissions;
|
|
using Dlhis.Basic.Device;
|
|
using Dlhis.Basic.Device.Request;
|
|
using Dlhis.Basic.Device.Response;
|
|
using Dlhis.Basic.Device.Services;
|
|
using Dlhis.Basic.Organization.Request;
|
|
using Dlhis.Basic.Organization.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Sys.Entities;
|
|
using Dlhis.Entity.Sys.Enums;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class GainfoGroupService : IGainfoGroupService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly IMyDataPermissionService _myDataPermissionService;
|
|
private readonly INamedResolver _namedResolver;
|
|
|
|
public GainfoGroupService(IDbContext dbContext,
|
|
ICurrentUser currentUser,
|
|
IMyDataPermissionService myDataPermissionService,
|
|
INamedResolver namedResolver)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_myDataPermissionService = myDataPermissionService;
|
|
_namedResolver = namedResolver;
|
|
}
|
|
|
|
public async Task<List<GainfoGroupTreeResponse>> GetTreeAsync()
|
|
{
|
|
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>()
|
|
.Select<GainfoGroupTreeResponse>()
|
|
.ToListAsync();
|
|
|
|
var tops = gainfoGroups.Where(x => !x.PCode.HasValue).ToList();
|
|
tops.ForEach(x => { RecursionGetNext(x, gainfoGroups); });
|
|
|
|
return tops;
|
|
}
|
|
|
|
public async Task SaveAsync(GainfoGroupSaveRequest request)
|
|
{
|
|
await CheckDuplicateNameAsync(request);
|
|
if (!request.Id.HasValue)
|
|
{
|
|
if (request.PCode.HasValue)
|
|
{
|
|
(await _dbContext.Queryable<GainfoGroupRelationshipEntity>().Where(x => x.GroupCode == request.PCode).AnyAsync()).ThrowIfTrue("上级已分配人员");
|
|
}
|
|
|
|
var entity = request.Adapt<GainfoGroupEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.Id == request.Id).SingleAsync();
|
|
request.Adapt(entity);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
public async Task<List<GainfoPageResponse>> GetGainfosByGroupAsync(Guid code)
|
|
{
|
|
var userNos = await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
|
.Where(x => x.GroupCode == code)
|
|
.Select(x => x.UserNo)
|
|
.ToListAsync();
|
|
|
|
var list = await _dbContext.Queryable<GainfoEntity>()
|
|
.Where(x => userNos.Contains(x.No))
|
|
.Select<GainfoPageResponse>()
|
|
.ToListAsync();
|
|
|
|
return list;
|
|
}
|
|
|
|
public async Task<DevicePermissionAssignResponse> AssignGainfosAsync(GainfoAssignRequest request)
|
|
{
|
|
if (request.UserNos.IsPresent())
|
|
{
|
|
var pGrp = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
|
(pGrp is { Editable: false, IsDefault: true }).ThrowIfTrue("不可分配人员到当前分组");
|
|
}
|
|
|
|
var existsNextGroup = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.PCode == request.GroupCode).AnyAsync();
|
|
existsNextGroup.ThrowIfTrue("存在下级分组,无法分配人员");
|
|
|
|
var relations = await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
|
.Where(x => x.GroupCode == request.GroupCode)
|
|
.ToListAsync();
|
|
await _dbContext.DeleteAsync(relations);
|
|
|
|
var userNos = await _dbContext.Queryable<GainfoEntity>()
|
|
.Where(x => request.UserNos.Contains(x.No) || request.UserNos.Contains(x.Code)) //NO和工号匹配
|
|
.Select(x => x.No)
|
|
.ToListAsync();
|
|
|
|
//(userNos.Count != request.UserNos.Count).ThrowIfTrue("存在权限外人员,无法添加");
|
|
|
|
var entities = userNos.Select(x => new GainfoGroupRelationshipEntity
|
|
{
|
|
GroupCode = request.GroupCode,
|
|
UserNo = x
|
|
}).ToList();
|
|
|
|
await _dbContext.InsertAsync(entities);
|
|
|
|
return await PermissionResetAsync(request.GroupCode);
|
|
}
|
|
|
|
|
|
private async Task<DevicePermissionAssignResponse> PermissionResetAsync(Guid groupCode)
|
|
{
|
|
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync();
|
|
var gainfoGroupCodes = RecursionHelper.HandlerGainfoGroupPreviousCodes(groupCode, gainfoGroups).Select(x => x.ToString());
|
|
|
|
(bool enableDeviceFilter, List<Guid> deviceCodes) = _myDataPermissionService.GetMyDeviceCodes();
|
|
(bool enableDeviceGroupFilter, List<Guid> deviceGroupCodes) = _myDataPermissionService.GetMyDeviceGroupCodes();
|
|
|
|
var dps = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
.Where(x => x.AssignType == AssignTypeEnumType.Group)
|
|
.Where(x => gainfoGroupCodes.Contains(x.AssignValue))
|
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Device)
|
|
.WhereIF(enableDeviceFilter, x => deviceCodes.Contains(x.UniqueValue))
|
|
.ToListAsync();
|
|
|
|
var dps2 = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
.Where(x => x.AssignType == AssignTypeEnumType.Group)
|
|
.Where(x => gainfoGroupCodes.Contains(x.AssignValue))
|
|
.Where(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
|
.WhereIF(enableDeviceGroupFilter, x => deviceGroupCodes.Contains(x.UniqueValue))
|
|
.ToListAsync();
|
|
|
|
dps.AddRange(dps2);
|
|
|
|
var analyzeService = _namedResolver.GetService<IPermissionAnalyzeService>(PermissionAnalyzeConstants.GetTypeName(AssignTypeEnumType.Group));
|
|
|
|
var result = new List<DevicePermissionAssignResponse>();
|
|
|
|
foreach (var devicePermissionGroup in dps.GroupBy(x => new { x.AssignValue, x.TenantId, x.UniqueType, x.Type }))
|
|
{
|
|
var assignValue = devicePermissionGroup.Key.AssignValue;
|
|
var tenantId = devicePermissionGroup.Key.TenantId;
|
|
var uniqueType = devicePermissionGroup.Key.UniqueType;
|
|
var type = devicePermissionGroup.Key.Type;
|
|
|
|
var codes = devicePermissionGroup.Select(x => x.UniqueValue).Distinct().ToList();
|
|
|
|
var r = await analyzeService.AnalyzeAsync(new DevicePermissionSaveRequest
|
|
{
|
|
AssignType = AssignTypeEnumType.Group,
|
|
AssignValue = assignValue,
|
|
UniqueType = uniqueType,
|
|
Codes = codes,
|
|
TenantId = tenantId,
|
|
Type = type
|
|
});
|
|
|
|
result.Add(r);
|
|
}
|
|
|
|
return new DevicePermissionAssignResponse(result.Sum(x => x.AddCount), result.Sum(x => x.DeleteCount));
|
|
}
|
|
|
|
|
|
public async Task<List<GainfoGroupTreeResponse>> GetGainfoGroupsByUserCodeAsync(string userCode)
|
|
{
|
|
var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == userCode).SingleAsync();
|
|
var userNo = gainfo?.No;
|
|
|
|
return await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
|
.LeftJoin<GainfoGroupEntity>((x, xg) => x.GroupCode == xg.Code)
|
|
.Where((x, xg) => x.UserNo == userNo)
|
|
.Select((x, xg) => new GainfoGroupTreeResponse
|
|
{
|
|
Id = x.Id,
|
|
PCode = xg.PCode,
|
|
Code = xg.Code,
|
|
Name = xg.Name,
|
|
Remark = xg.Remark,
|
|
Editable = xg.Editable,
|
|
IsDefault = xg.IsDefault,
|
|
TenantId = xg.TenantId
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
//******************************************************* help methods ***********************************************************
|
|
|
|
/// <summary>
|
|
/// 检查重名
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
private async Task CheckDuplicateNameAsync(GainfoGroupSaveRequest request)
|
|
{
|
|
var exists = await _dbContext.Queryable<GainfoGroupEntity>()
|
|
.Where(x => x.Name == request.Name)
|
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
|
.AnyAsync();
|
|
|
|
exists.ThrowIfTrue($"重复组名{request.Name}");
|
|
}
|
|
|
|
|
|
private void RecursionGetNext(GainfoGroupTreeResponse current, List<GainfoGroupTreeResponse> gainfoGroups)
|
|
{
|
|
var currentTenantId = _currentUser.GetStringTenantId();
|
|
var next = gainfoGroups.Where(x =>
|
|
{
|
|
if (x.PCode == current.Code)
|
|
{
|
|
if (x.TenantId.IsEmpty())
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return x.TenantId == currentTenantId;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}).ToList();
|
|
current.Next = next;
|
|
foreach (var item in next)
|
|
{
|
|
RecursionGetNext(item, gainfoGroups);
|
|
}
|
|
}
|
|
} |