311 lines
12 KiB
C#
311 lines
12 KiB
C#
using Dlhis.Basic.Device.Dtos;
|
|
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.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
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 IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
|
|
|
public GainfoGroupService(IDbContext dbContext,
|
|
ICurrentUser currentUser,
|
|
IDevicePermissionAnalyzeService devicePermissionAnalyzeService)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
|
}
|
|
|
|
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();
|
|
|
|
|
|
var presentUserNos = 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 r = await PermissionResetAsync(request.GroupCode, relations.Select(x => x.UserNo).Distinct().ToList(), presentUserNos);
|
|
|
|
await _dbContext.DeleteAsync(relations);
|
|
var entities = presentUserNos.Select(x => new GainfoGroupRelationshipEntity
|
|
{
|
|
GroupCode = request.GroupCode,
|
|
UserNo = x
|
|
}).ToList();
|
|
|
|
await _dbContext.InsertAsync(entities);
|
|
|
|
|
|
return r;
|
|
}
|
|
|
|
|
|
private async Task<DevicePermissionAssignResponse> PermissionResetAsync(Guid groupCode, List<string> beforeUserNos, List<string> presentUserNos)
|
|
{
|
|
var usersToAdd = presentUserNos.Except(beforeUserNos).ToList();
|
|
var usersToDelete = beforeUserNos.Except(presentUserNos).ToList();
|
|
if (usersToAdd.IsEmpty() && usersToDelete.IsEmpty())
|
|
{
|
|
return new DevicePermissionAssignResponse(0, 0);
|
|
}
|
|
|
|
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>().ToListAsync();
|
|
var gainfoGroupCodes = RecursionHelper.HandlerGainfoGroupPreviousCodes(groupCode, gainfoGroups).Select(x => x.ToString());
|
|
|
|
var dps = await _dbContext.Queryable<DevicePermissionEntity>()
|
|
.Where(x => x.AssignType == AssignTypeEnumType.Group)
|
|
.Where(x => gainfoGroupCodes.Contains(x.AssignValue))
|
|
.ToListAsync();
|
|
|
|
//var result = new List<DevicePermissionAssignResponse>();
|
|
var deviceGroups = dps.Any(x => x.UniqueType == DeviceAssignmentTypeEnumType.Group)
|
|
? await _dbContext.Queryable<DeviceGroupEntity>().ToListAsync()
|
|
: new List<DeviceGroupEntity>(0);
|
|
|
|
//只会循环2次
|
|
var deviceCodes = new List<Guid>();
|
|
foreach (var devicePermissionGroup in dps.GroupBy(x => x.UniqueType))
|
|
{
|
|
var codes = devicePermissionGroup.Select(x => x.UniqueValue).Distinct().ToList();
|
|
|
|
if (devicePermissionGroup.Key == DeviceAssignmentTypeEnumType.Group)
|
|
{
|
|
deviceCodes.AddRange(await GetDeviceCodeByGroupAsync(codes, deviceGroups));
|
|
}
|
|
|
|
deviceCodes.AddRange(codes);
|
|
}
|
|
|
|
deviceCodes = deviceCodes.Distinct().ToList();
|
|
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceCodes.Contains(x.Code)).ToListAsync();
|
|
|
|
var userTenantDeviceCodes = usersToDelete.IsPresent() && dps.Exists(x => x.AssignType != AssignTypeEnumType.Tenant)
|
|
? await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(beforeUserNos.Union(presentUserNos).ToList())
|
|
: new Dictionary<string, IEnumerable<TenantAssignTypeDeviceCode>>();
|
|
|
|
var addTasks = new List<DeviceTaskEntity>();
|
|
var deleteTasks = new List<DeviceTaskEntity>();
|
|
foreach (var device in devices)
|
|
{
|
|
foreach (var item in usersToDelete)
|
|
{
|
|
var permissions = userTenantDeviceCodes[item].ToList();
|
|
foreach (var permission in permissions)
|
|
{
|
|
permission.AssignTypes = permission.AssignTypes.Where(p => !gainfoGroupCodes.Contains(p.AssignValue)).ToList();
|
|
}
|
|
|
|
var myDeviceCodes = permissions.SelectMany(x => x.AssignTypes).SelectMany(x => x.Codes);
|
|
if (myDeviceCodes.Any(x => x == device.Code))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
deleteTasks.Add(new DeviceTaskEntity
|
|
{
|
|
Type = DeviceTaskEnumType.Delete,
|
|
DeviceType = device.Type,
|
|
UserId = null,
|
|
UserNo = item,
|
|
DeviceUniqueNo = device.UniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
Version = 0,
|
|
ResultMessage = null,
|
|
CancellationId = 0,
|
|
Source = DeviceTaskSourceEnumType.Manually,
|
|
Priority = 0,
|
|
TenantId = device.TenantId
|
|
});
|
|
}
|
|
|
|
foreach (var item in usersToAdd)
|
|
{
|
|
addTasks.Add(new DeviceTaskEntity
|
|
{
|
|
Type = DeviceTaskEnumType.Add,
|
|
DeviceType = device.Type,
|
|
UserId = null,
|
|
UserNo = item,
|
|
DeviceUniqueNo = device.UniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
Version = 0,
|
|
ResultMessage = null,
|
|
CancellationId = 0,
|
|
Source = DeviceTaskSourceEnumType.Manually,
|
|
Priority = 0,
|
|
TenantId = device.TenantId
|
|
});
|
|
}
|
|
}
|
|
|
|
await _dbContext.InsertAsync(addTasks);
|
|
await _dbContext.InsertAsync(deleteTasks);
|
|
|
|
return new DevicePermissionAssignResponse(addTasks.Count, deleteTasks.Count);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取分组下的deviceCode
|
|
/// </summary>
|
|
/// <param name="codes"></param>
|
|
/// <param name="deviceGroups"></param>
|
|
/// <returns></returns>
|
|
private async Task<List<Guid>> GetDeviceCodeByGroupAsync(List<Guid> codes, List<DeviceGroupEntity> deviceGroups)
|
|
{
|
|
var result = new List<Guid>();
|
|
codes.ForEach(groupCode => result.AddRange(RecursionHelper.HandlerDeviceGroupNextCodes(groupCode, deviceGroups)));
|
|
|
|
result = result.Distinct().ToList();
|
|
|
|
return await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
.Where(x => result.Contains(x.GroupCode))
|
|
.Select(x => x.DeviceCode)
|
|
.Distinct()
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |