You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.1 KiB
93 lines
3.1 KiB
using Easy;
|
|
using Easy.DDD.Domain;
|
|
using Easy.DDD.Domain.Repositories;
|
|
using Identity.Api.Clean.Domain.Entites;
|
|
using Identity.Api.Clean.Shared.IServices;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Identity.Api.Clean.Domain.Services;
|
|
|
|
public class OrganizationUnitManager : DomainService, IOrganizationUnitManager
|
|
{
|
|
protected IRepository<OrganizationUnit> OrganizationUnitRepository { get; }
|
|
public OrganizationUnitManager(IRepository<OrganizationUnit> organizationUnitRepository)
|
|
{
|
|
OrganizationUnitRepository = organizationUnitRepository;
|
|
}
|
|
|
|
public virtual async Task CreateAsync(OrganizationUnit organizationUnit)
|
|
{
|
|
organizationUnit.Code = await GetNextChildCodeAsync(organizationUnit.ParentId);
|
|
await ValidateOrganizationUnitAsync(organizationUnit);
|
|
}
|
|
|
|
|
|
protected virtual async Task ValidateOrganizationUnitAsync(OrganizationUnit organizationUnit)
|
|
{
|
|
var existDisplayName = await OrganizationUnitRepository
|
|
.Where(ou => ou.Id != organizationUnit.Id)
|
|
.Select(o => o.DisplayName)
|
|
.ContainsAsync(organizationUnit.DisplayName);
|
|
|
|
var message = "重复的组织单元显示名称:" + organizationUnit.DisplayName;
|
|
|
|
When.Is(existDisplayName, message);
|
|
}
|
|
|
|
public virtual async Task DeleteAsync(Guid id)
|
|
{
|
|
var code = await GetCodeOrDefaultAsync(id);
|
|
await OrganizationUnitRepository.Set
|
|
.Include(o => o.Roles)
|
|
.Where(ou => ou.Code.StartsWith(code))
|
|
.DeleteFromQueryAsync();
|
|
}
|
|
|
|
public virtual async Task<string> GetNextChildCodeAsync(Guid? parentId)
|
|
{
|
|
string parentCode = null;
|
|
if (parentId != null)
|
|
{
|
|
var lastChild = await OrganizationUnitRepository
|
|
.Where(o => o.ParentId == parentId)
|
|
.OrderByDescending(c => c.Code)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (lastChild != null)
|
|
{
|
|
return OrganizationUnit.CalculateNextCode(lastChild.Code);
|
|
}
|
|
parentCode = await GetCodeOrDefaultAsync(parentId.Value);
|
|
}
|
|
return OrganizationUnit.AppendCode(parentCode, OrganizationUnit.CreateCode(1));
|
|
}
|
|
|
|
public virtual async Task<string> GetCodeOrDefaultAsync(Guid id)
|
|
{
|
|
return await OrganizationUnitRepository.Set
|
|
.Where(o => o.Id == id)
|
|
.Select(o => o.Code)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
public virtual async Task AddRoleToOrganizationUnitAsync(Guid roleId, Guid ouId)
|
|
{
|
|
OrganizationUnit ou = await OrganizationUnitRepository.Set
|
|
.Include(o => o.Roles)
|
|
.FirstOrDefaultAsync(o => o.Id == ouId);
|
|
|
|
ou.AddRole(roleId);
|
|
|
|
await OrganizationUnitRepository.UpdateAsync(ou, true);
|
|
}
|
|
public virtual async Task RemoveRoleFromOrganizationUnitAsync(Guid roleId, Guid ouId)
|
|
{
|
|
OrganizationUnit ou = await OrganizationUnitRepository.Set
|
|
.Include(o => o.Roles)
|
|
.FirstOrDefaultAsync(o => o.Id == ouId);
|
|
|
|
ou.RemoveRole(roleId);
|
|
|
|
await OrganizationUnitRepository.UpdateAsync(ou, true);
|
|
}
|
|
}
|
|
|