| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- #nullable enable
- using Abp.Domain.Repositories;
- using Abp.Domain.Uow;
- using Abp.Linq;
- using Abp.UI;
- using VberZero.BaseSystem.Organizations;
- using VberZero.DomainService;
- namespace VberZero.Organizations;
- /// <summary>
- /// 为组织单位执行域逻辑。
- /// </summary>
- public class OrganizationUnitManager : VzDomainServiceBase
- {
- protected IRepository<OrganizationUnit, long> OrganizationUnitRepository { get; private set; }
- public IAsyncQueryableExecuter AsyncQueryableExecuter { get; set; }
- public OrganizationUnitManager(IRepository<OrganizationUnit, long> organizationUnitRepository)
- {
- OrganizationUnitRepository = organizationUnitRepository;
- AsyncQueryableExecuter = NullAsyncQueryableExecuter.Instance;
- }
- public virtual async Task CreateAsync(OrganizationUnit organizationUnit)
- {
- using var uow = UnitOfWorkManager.Begin();
- organizationUnit.Code = await GetNextChildCodeAsync(organizationUnit.ParentId);
- await ValidateOrganizationUnitAsync(organizationUnit);
- await OrganizationUnitRepository.InsertAsync(organizationUnit);
- await uow.CompleteAsync();
- }
- public virtual void Create(OrganizationUnit organizationUnit)
- {
- using var uow = UnitOfWorkManager.Begin();
- organizationUnit.Code = GetNextChildCode(organizationUnit.ParentId);
- ValidateOrganizationUnit(organizationUnit);
- OrganizationUnitRepository.Insert(organizationUnit);
- uow.Complete();
- }
- public virtual async Task UpdateAsync(OrganizationUnit organizationUnit)
- {
- await ValidateOrganizationUnitAsync(organizationUnit);
- await OrganizationUnitRepository.UpdateAsync(organizationUnit);
- }
- public virtual void Update(OrganizationUnit organizationUnit)
- {
- ValidateOrganizationUnit(organizationUnit);
- OrganizationUnitRepository.Update(organizationUnit);
- }
- public virtual async Task<string?> GetNextChildCodeAsync(long? parentId)
- {
- var lastChild = await GetLastChildOrNullAsync(parentId);
- if (lastChild == null)
- {
- var parentCode = parentId != null ? await GetPathAsync(parentId.Value) : null;
- return OrganizationUnit.AppendCode(parentCode, OrganizationUnit.CreateCode(1));
- }
- return OrganizationUnit.CalculateNextCode(lastChild.Code);
- }
- public virtual string? GetNextChildCode(long? parentId)
- {
- var lastChild = GetLastChildOrNull(parentId);
- if (lastChild == null)
- {
- var parentCode = parentId != null ? GetPath(parentId.Value) : null;
- return OrganizationUnit.AppendCode(parentCode, OrganizationUnit.CreateCode(1));
- }
- return OrganizationUnit.CalculateNextCode(lastChild.Code);
- }
- public virtual async Task<OrganizationUnit> GetLastChildOrNullAsync(long? parentId)
- {
- var query = OrganizationUnitRepository.GetAll()
- .Where(ou => ou.ParentId == parentId)
- .OrderByDescending(ou => ou.Code);
- return await AsyncQueryableExecuter.FirstOrDefaultAsync(query);
- }
- public virtual OrganizationUnit? GetLastChildOrNull(long? parentId)
- {
- var query = OrganizationUnitRepository.GetAll()
- .Where(ou => ou.ParentId == parentId)
- .OrderByDescending(ou => ou.Code);
- return query.FirstOrDefault();
- }
- public virtual async Task<string?> GetPathAsync(long id)
- {
- return (await OrganizationUnitRepository.GetAsync(id)).Path;
- }
- public virtual string GetPath(long id)
- {
- return OrganizationUnitRepository.Get(id).Path;
- }
- public virtual async Task DeleteAsync(long id)
- {
- using var uow = UnitOfWorkManager.Begin();
- var children = await FindChildrenAsync(id, true);
- foreach (var child in children)
- {
- await OrganizationUnitRepository.DeleteAsync(child);
- }
- await OrganizationUnitRepository.DeleteAsync(id);
- await uow.CompleteAsync();
- }
- public virtual void Delete(long id)
- {
- using var uow = UnitOfWorkManager.Begin();
- var children = FindChildren(id, true);
- foreach (var child in children)
- {
- OrganizationUnitRepository.Delete(child);
- }
- OrganizationUnitRepository.Delete(id);
- uow.Complete();
- }
- public virtual async Task MoveAsync(long id, long? parentId)
- {
- using var uow = UnitOfWorkManager.Begin();
- var organizationUnit = await OrganizationUnitRepository.GetAsync(id);
- if (organizationUnit.ParentId == parentId)
- {
- await uow.CompleteAsync();
- return;
- }
- //Should find children before Code change
- var children = await FindChildrenAsync(id, true);
- //Store old code of OU
- var oldCode = organizationUnit.Code;
- //Move OU
- organizationUnit.Code = await GetNextChildCodeAsync(parentId);
- organizationUnit.ParentId = parentId;
- await ValidateOrganizationUnitAsync(organizationUnit);
- //Update Children Codes
- foreach (var child in children)
- {
- child.Code = OrganizationUnit.AppendCode(organizationUnit.Code, OrganizationUnit.GetRelativeCode(child.Code, oldCode));
- }
- await uow.CompleteAsync();
- }
- public virtual void Move(long id, long? parentId)
- {
- UnitOfWorkManager.WithUnitOfWork(() =>
- {
- var organizationUnit = OrganizationUnitRepository.Get(id);
- if (organizationUnit.ParentId == parentId)
- {
- return;
- }
- //Should find children before Code change
- var children = FindChildren(id, true);
- //Store old code of OU
- var oldCode = organizationUnit.Code;
- //Move OU
- organizationUnit.Code = GetNextChildCode(parentId);
- organizationUnit.ParentId = parentId;
- ValidateOrganizationUnit(organizationUnit);
- //Update Children Codes
- foreach (var child in children)
- {
- child.Code = OrganizationUnit.AppendCode(organizationUnit.Code, OrganizationUnit.GetRelativeCode(child.Code, oldCode));
- }
- });
- }
- public async Task<List<OrganizationUnit>> FindChildrenAsync(long? parentId, bool recursive = false)
- {
- if (!recursive)
- {
- return await OrganizationUnitRepository.GetAllListAsync(ou => ou.ParentId == parentId);
- }
- if (!parentId.HasValue)
- {
- return await OrganizationUnitRepository.GetAllListAsync();
- }
- var parentPath = await GetPathAsync(parentId.Value);
- return await OrganizationUnitRepository.GetAllListAsync(
- ou => parentPath != null && ou.Path.StartsWith(parentPath) && ou.Id != parentId.Value
- );
- }
- public List<OrganizationUnit> FindChildren(long? parentId, bool recursive = false)
- {
- if (!recursive)
- {
- return OrganizationUnitRepository.GetAllList(ou => ou.ParentId == parentId);
- }
- if (!parentId.HasValue)
- {
- return OrganizationUnitRepository.GetAllList();
- }
- var path = GetPath(parentId.Value);
- return OrganizationUnitRepository.GetAllList(
- ou => path != null && ou.Path.StartsWith(path) && ou.Id != parentId.Value
- );
- }
- protected virtual async Task ValidateOrganizationUnitAsync(OrganizationUnit organizationUnit)
- {
- var siblings = (await FindChildrenAsync(organizationUnit.ParentId))
- .Where(ou => ou.Id != organizationUnit.Id)
- .ToList();
- if (siblings.Any(ou => ou.DisplayName == organizationUnit.DisplayName))
- {
- throw new UserFriendlyException(L("OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName));
- }
- }
- protected virtual void ValidateOrganizationUnit(OrganizationUnit organizationUnit)
- {
- var siblings = (FindChildren(organizationUnit.ParentId))
- .Where(ou => ou.Id != organizationUnit.Id)
- .ToList();
- if (siblings.Any(ou => ou.DisplayName == organizationUnit.DisplayName))
- {
- throw new UserFriendlyException(L("OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName));
- }
- }
- }
|