|  | @@ -6,6 +6,9 @@ using ZhonTai.Admin.Domain.Org;
 | 
	
		
			
				|  |  |  using ZhonTai.Admin.Core.Consts;
 | 
	
		
			
				|  |  |  using ZhonTai.DynamicApi.Attributes;
 | 
	
		
			
				|  |  |  using ZhonTai.DynamicApi;
 | 
	
		
			
				|  |  | +using ZhonTai.Admin.Core.Repositories;
 | 
	
		
			
				|  |  | +using ZhonTai.Admin.Domain;
 | 
	
		
			
				|  |  | +using ZhonTai.Admin.Core.Attributes;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  namespace ZhonTai.Admin.Services.Org;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -17,6 +20,8 @@ public class OrgService : BaseService, IOrgService, IDynamicApi
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  |      private readonly IOrgRepository _orgRepository;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    private IRepositoryBase<RoleOrgEntity> _roleOrgRepository => LazyGetRequiredService<IRepositoryBase<RoleOrgEntity>>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |      public OrgService(IOrgRepository orgRepository)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  |          _orgRepository = orgRepository;
 | 
	
	
		
			
				|  | @@ -56,6 +61,16 @@ public class OrgService : BaseService, IOrgService, IDynamicApi
 | 
	
		
			
				|  |  |      /// <returns></returns>
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> AddAsync(OrgAddInput input)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        if (await _orgRepository.Select.AnyAsync(a => a.ParentId == input.ParentId && a.Name == input.Name))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"此部门已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Code.NotNull() && await _orgRepository.Select.AnyAsync(a => a.ParentId == input.ParentId && a.Code == input.Code))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"此部门编码已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          var dictionary = Mapper.Map<OrgEntity>(input);
 | 
	
		
			
				|  |  |          var id = (await _orgRepository.InsertAsync(dictionary)).Id;
 | 
	
		
			
				|  |  |          return ResultOutput.Result(id > 0);
 | 
	
	
		
			
				|  | @@ -76,7 +91,28 @@ public class OrgService : BaseService, IOrgService, IDynamicApi
 | 
	
		
			
				|  |  |          var entity = await _orgRepository.GetAsync(input.Id);
 | 
	
		
			
				|  |  |          if (!(entity?.Id > 0))
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            return ResultOutput.NotOk("数据字典不存在!");
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk("部门不存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Id == input.ParentId)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk("上级部门不能是本部门");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (await _orgRepository.Select.AnyAsync(a => a.ParentId == input.ParentId && a.Id != input.Id && a.Name == input.Name))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"此部门已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Code.NotNull() && await _orgRepository.Select.AnyAsync(a => a.ParentId == input.ParentId && a.Id != input.Id && a.Code == input.Code))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"此部门编码已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        var childIdList = await _orgRepository.GetChildIdListAsync(input.Id);
 | 
	
		
			
				|  |  | +        if (childIdList.Contains(input.ParentId))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"上级部门不能是下级部门");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          Mapper.Map(input, entity);
 | 
	
	
		
			
				|  | @@ -89,22 +125,58 @@ public class OrgService : BaseService, IOrgService, IDynamicApi
 | 
	
		
			
				|  |  |      /// </summary>
 | 
	
		
			
				|  |  |      /// <param name="id"></param>
 | 
	
		
			
				|  |  |      /// <returns></returns>
 | 
	
		
			
				|  |  | +    [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> DeleteAsync(long id)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        var result = await _orgRepository.DeleteRecursiveAsync(a => a.Id == id);
 | 
	
		
			
				|  |  | +        //本部门下是否有员工
 | 
	
		
			
				|  |  | +        if(await _orgRepository.HasUser(id))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"当前部门有员工无法删除");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        return ResultOutput.Result(result);
 | 
	
		
			
				|  |  | +        var orgIdList = await _orgRepository.GetChildIdListAsync(id);
 | 
	
		
			
				|  |  | +        //本部门的下级部门下是否有员工
 | 
	
		
			
				|  |  | +        if (await _orgRepository.HasUser(orgIdList))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"本部门的下级部门有员工无法删除");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //删除部门角色
 | 
	
		
			
				|  |  | +        await _roleOrgRepository.DeleteAsync(a => orgIdList.Contains(a.OrgId));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //删除本部门和下级部门
 | 
	
		
			
				|  |  | +        await _orgRepository.DeleteAsync(a => orgIdList.Contains(a.Id));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return ResultOutput.Ok();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /// <summary>
 | 
	
		
			
				|  |  | -    /// 批量删除
 | 
	
		
			
				|  |  | +    /// 删除
 | 
	
		
			
				|  |  |      /// </summary>
 | 
	
		
			
				|  |  |      /// <param name="id"></param>
 | 
	
		
			
				|  |  |      /// <returns></returns>
 | 
	
		
			
				|  |  | +    [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> SoftDeleteAsync(long id)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        var result = await _orgRepository.SoftDeleteRecursiveAsync(a => a.Id == id);
 | 
	
		
			
				|  |  | +        //本部门下是否有员工
 | 
	
		
			
				|  |  | +        if (await _orgRepository.HasUser(id))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"当前部门有员工无法删除");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        var orgIdList = await _orgRepository.GetChildIdListAsync(id);
 | 
	
		
			
				|  |  | +        //本部门的下级部门下是否有员工
 | 
	
		
			
				|  |  | +        if (await _orgRepository.HasUser(orgIdList))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"本部门的下级部门有员工无法删除");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //删除部门角色
 | 
	
		
			
				|  |  | +        await _roleOrgRepository.SoftDeleteAsync(a => orgIdList.Contains(a.OrgId));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        return ResultOutput.Result(result);
 | 
	
		
			
				|  |  | +        //删除本部门和下级部门
 | 
	
		
			
				|  |  | +        await _orgRepository.SoftDeleteAsync(a => orgIdList.Contains(a.Id));
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return ResultOutput.Ok();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  }
 |