|  | @@ -22,6 +22,9 @@ using ZhonTai.DynamicApi;
 | 
	
		
			
				|  |  |  using ZhonTai.DynamicApi.Attributes;
 | 
	
		
			
				|  |  |  using ZhonTai.Admin.Core.Helpers;
 | 
	
		
			
				|  |  |  using ZhonTai.Admin.Core.Consts;
 | 
	
		
			
				|  |  | +using ZhonTai.Admin.Domain.Employee;
 | 
	
		
			
				|  |  | +using ZhonTai.Admin.Domain;
 | 
	
		
			
				|  |  | +using ZhonTai.Admin.Domain.Organization;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  namespace ZhonTai.Admin.Services.User;
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -31,27 +34,17 @@ namespace ZhonTai.Admin.Services.User;
 | 
	
		
			
				|  |  |  [DynamicApi(Area = AdminConsts.AreaName)]
 | 
	
		
			
				|  |  |  public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |  {
 | 
	
		
			
				|  |  | -    private readonly AppConfig _appConfig;
 | 
	
		
			
				|  |  | -    private readonly IUserRepository _userRepository;
 | 
	
		
			
				|  |  | -    private readonly IRepositoryBase<UserRoleEntity> _userRoleRepository;
 | 
	
		
			
				|  |  | -    private readonly ITenantRepository _tenantRepository;
 | 
	
		
			
				|  |  | -    private readonly IApiRepository _apiRepository;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    private AppConfig _appConfig => LazyGetRequiredService<AppConfig>();
 | 
	
		
			
				|  |  | +    private IUserRepository _userRepository => LazyGetRequiredService<IUserRepository>();
 | 
	
		
			
				|  |  | +    private IRepositoryBase<UserRoleEntity> _userRoleRepository => LazyGetRequiredService<IRepositoryBase<UserRoleEntity>>();
 | 
	
		
			
				|  |  | +    private ITenantRepository _tenantRepository => LazyGetRequiredService<ITenantRepository>();
 | 
	
		
			
				|  |  | +    private IApiRepository _apiRepository => LazyGetRequiredService<IApiRepository>();
 | 
	
		
			
				|  |  |      private IRoleRepository _roleRepository => LazyGetRequiredService<IRoleRepository>();
 | 
	
		
			
				|  |  | +    private IEmployeeRepository _employeeRepository => LazyGetRequiredService<IEmployeeRepository>();
 | 
	
		
			
				|  |  | +    private IRepositoryBase<EmployeeOrganizationEntity> _employeeOrganizationRepository => LazyGetRequiredService<IRepositoryBase<EmployeeOrganizationEntity>>();
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    public UserService(
 | 
	
		
			
				|  |  | -        AppConfig appConfig,
 | 
	
		
			
				|  |  | -        IUserRepository userRepository,
 | 
	
		
			
				|  |  | -        IRepositoryBase<UserRoleEntity> userRoleRepository,
 | 
	
		
			
				|  |  | -        ITenantRepository tenantRepository,
 | 
	
		
			
				|  |  | -        IApiRepository apiRepository
 | 
	
		
			
				|  |  | -    )
 | 
	
		
			
				|  |  | +    public UserService()
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        _appConfig = appConfig;
 | 
	
		
			
				|  |  | -        _userRepository = userRepository;
 | 
	
		
			
				|  |  | -        _userRoleRepository = userRoleRepository;
 | 
	
		
			
				|  |  | -        _tenantRepository = tenantRepository;
 | 
	
		
			
				|  |  | -        _apiRepository = apiRepository;
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /// <summary>
 | 
	
	
		
			
				|  | @@ -61,14 +54,24 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      /// <returns></returns>
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> GetAsync(long id)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        var entity = await _userRepository.Select
 | 
	
		
			
				|  |  | +        var output = await _userRepository.Select
 | 
	
		
			
				|  |  |          .WhereDynamic(id)
 | 
	
		
			
				|  |  | -        .IncludeMany(a => a.Roles.Select(b => new RoleEntity { Id = b.Id }))
 | 
	
		
			
				|  |  | -        .ToOneAsync();
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        var roles = await _roleRepository.Select.ToListAsync(a => new { a.Id, a.Name });
 | 
	
		
			
				|  |  | +        .IncludeMany(a => a.Roles.Select(b => new RoleEntity { Id = b.Id, Name = b.Name }))
 | 
	
		
			
				|  |  | +        .IncludeMany(a => a.Emp.Orgs.Select(b => new OrganizationEntity { Id = b.Id, Name = b.Name }))
 | 
	
		
			
				|  |  | +        .ToOneAsync(a=>new
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            a.Id,
 | 
	
		
			
				|  |  | +            a.UserName,
 | 
	
		
			
				|  |  | +            a.Name,
 | 
	
		
			
				|  |  | +            a.Roles,
 | 
	
		
			
				|  |  | +            Emp = new
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                a.Emp.MainOrgId,
 | 
	
		
			
				|  |  | +                a.Emp.Orgs
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        });
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        return ResultOutput.Ok(new { Form = Mapper.Map<UserGetOutput>(entity), Select = new { roles } });
 | 
	
		
			
				|  |  | +        return ResultOutput.Ok(output);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      /// <summary>
 | 
	
	
		
			
				|  | @@ -117,17 +120,6 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |          return output.Ok(entityDto);
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -    /// <summary>
 | 
	
		
			
				|  |  | -    /// 查询下拉数据
 | 
	
		
			
				|  |  | -    /// </summary>
 | 
	
		
			
				|  |  | -    /// <returns></returns>
 | 
	
		
			
				|  |  | -    public async Task<IResultOutput> GetSelectAsync()
 | 
	
		
			
				|  |  | -    {
 | 
	
		
			
				|  |  | -        var roles = await _roleRepository.Select.ToListAsync(a => new { a.Id, a.Name });
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -        return ResultOutput.Ok(new { Select = new { roles } });
 | 
	
		
			
				|  |  | -    }
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |      /// <summary>
 | 
	
		
			
				|  |  |      /// 查询用户基本信息
 | 
	
		
			
				|  |  |      /// </summary>
 | 
	
	
		
			
				|  | @@ -170,6 +162,22 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> AddAsync(UserAddInput input)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        if (await _userRepository.Select.AnyAsync(a => a.UserName == input.UserName))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"账号已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Mobile.NotNull() && await _userRepository.Select.AnyAsync(a => a.Mobile == input.Mobile))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"手机号已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Email.NotNull() && await _userRepository.Select.AnyAsync(a => a.Email == input.Email))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"邮箱已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        // 用户信息
 | 
	
		
			
				|  |  |          if (input.Password.IsNull())
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              input.Password = _appConfig.DefaultPassword;
 | 
	
	
		
			
				|  | @@ -182,15 +190,32 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          if (!(user?.Id > 0))
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            return ResultOutput.NotOk();
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk("新增用户失败");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        //用户角色
 | 
	
		
			
				|  |  |          if (input.RoleIds != null && input.RoleIds.Any())
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              var roles = input.RoleIds.Select(a => new UserRoleEntity { UserId = user.Id, RoleId = a });
 | 
	
		
			
				|  |  |              await _userRoleRepository.InsertAsync(roles);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        // 员工信息
 | 
	
		
			
				|  |  | +        var emp = Mapper.Map<EmployeeEntity>(input.Emp);
 | 
	
		
			
				|  |  | +        emp.Id = user.Id;
 | 
	
		
			
				|  |  | +        await _employeeRepository.InsertAsync(emp);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //所属部门
 | 
	
		
			
				|  |  | +        if (input.Emp.OrgIds != null && input.Emp.OrgIds.Any())
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            var organizations = input.Emp.OrgIds.Select(organizationId => new EmployeeOrganizationEntity
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                EmployeeId = emp.Id,
 | 
	
		
			
				|  |  | +                OrganizationId = organizationId
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            await _employeeOrganizationRepository.InsertAsync(organizations);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          return ResultOutput.Ok();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -202,28 +227,61 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> UpdateAsync(UserUpdateInput input)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | -        if (!(input?.Id > 0))
 | 
	
		
			
				|  |  | +        var user = await _userRepository.GetAsync(input.Id);
 | 
	
		
			
				|  |  | +        if (!(user?.Id > 0))
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            return ResultOutput.NotOk();
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk("用户不存在");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        var user = await _userRepository.GetAsync(input.Id);
 | 
	
		
			
				|  |  | -        if (!(user?.Id > 0))
 | 
	
		
			
				|  |  | +        if (await _userRepository.Select.AnyAsync(a => a.Id != input.Id && a.UserName == input.UserName))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"账号已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Mobile.NotNull() && await _userRepository.Select.AnyAsync(a => a.Id != input.Id && a.Mobile == input.Mobile))
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  | -            return ResultOutput.NotOk("用户不存在!");
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"手机号已存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (input.Email.NotNull() && await _userRepository.Select.AnyAsync(a => a.Id != input.Id && a.Email == input.Email))
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            return ResultOutput.NotOk($"邮箱已存在");
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          Mapper.Map(input, user);
 | 
	
		
			
				|  |  |          await _userRepository.UpdateAsync(user);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        // 用户角色
 | 
	
		
			
				|  |  |          await _userRoleRepository.DeleteAsync(a => a.UserId == user.Id);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          if (input.RoleIds != null && input.RoleIds.Any())
 | 
	
		
			
				|  |  |          {
 | 
	
		
			
				|  |  |              var roles = input.RoleIds.Select(a => new UserRoleEntity { UserId = user.Id, RoleId = a });
 | 
	
		
			
				|  |  |              await _userRoleRepository.InsertAsync(roles);
 | 
	
		
			
				|  |  |          }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +        // 员工信息
 | 
	
		
			
				|  |  | +        var emp = await _employeeRepository.GetAsync(input.Emp.Id);
 | 
	
		
			
				|  |  | +        if(emp == null)
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            emp = new EmployeeEntity();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        Mapper.Map(input.Emp, emp);
 | 
	
		
			
				|  |  | +        emp.Id = user.Id;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        await _employeeRepository.InsertOrUpdateAsync(emp);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        //所属部门
 | 
	
		
			
				|  |  | +        await _employeeOrganizationRepository.DeleteAsync(a => a.EmployeeId == emp.Id);
 | 
	
		
			
				|  |  | +        if (input.Emp.OrgIds != null && input.Emp.OrgIds.Any())
 | 
	
		
			
				|  |  | +        {
 | 
	
		
			
				|  |  | +            var organizations = input.Emp.OrgIds.Select(organizationId => new EmployeeOrganizationEntity
 | 
	
		
			
				|  |  | +            {
 | 
	
		
			
				|  |  | +                EmployeeId = emp.Id,
 | 
	
		
			
				|  |  | +                OrganizationId = organizationId
 | 
	
		
			
				|  |  | +            });
 | 
	
		
			
				|  |  | +            await _employeeOrganizationRepository.InsertAsync(organizations);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |          return ResultOutput.Ok();
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -279,8 +337,14 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> DeleteAsync(long id)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        //删除员工所属部门
 | 
	
		
			
				|  |  | +        await _employeeOrganizationRepository.DeleteAsync(a => a.EmployeeId == id);
 | 
	
		
			
				|  |  | +        //删除员工
 | 
	
		
			
				|  |  | +        await _employeeRepository.DeleteAsync(a => a.Id == id);
 | 
	
		
			
				|  |  | +        //删除用户角色
 | 
	
		
			
				|  |  |          await _userRoleRepository.DeleteAsync(a => a.UserId == id);
 | 
	
		
			
				|  |  | -        await _userRepository.DeleteAsync(m => m.Id == id);
 | 
	
		
			
				|  |  | +        //删除用户
 | 
	
		
			
				|  |  | +        await _userRepository.DeleteAsync(a => a.Id == id);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          return ResultOutput.Ok();
 | 
	
		
			
				|  |  |      }
 | 
	
	
		
			
				|  | @@ -293,7 +357,13 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> BatchDeleteAsync(long[] ids)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        //删除员工所属部门
 | 
	
		
			
				|  |  | +        await _employeeOrganizationRepository.DeleteAsync(a => ids.Contains(a.EmployeeId));
 | 
	
		
			
				|  |  | +        //删除员工
 | 
	
		
			
				|  |  | +        await _employeeRepository.DeleteAsync(a => ids.Contains(a.Id));
 | 
	
		
			
				|  |  | +        //删除用户角色
 | 
	
		
			
				|  |  |          await _userRoleRepository.DeleteAsync(a => ids.Contains(a.UserId));
 | 
	
		
			
				|  |  | +        //删除用户
 | 
	
		
			
				|  |  |          await _userRepository.DeleteAsync(a => ids.Contains(a.Id));
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |          return ResultOutput.Ok();
 | 
	
	
		
			
				|  | @@ -307,6 +377,8 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> SoftDeleteAsync(long id)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        await _employeeOrganizationRepository.DeleteAsync(a => a.EmployeeId == id);
 | 
	
		
			
				|  |  | +        await _employeeRepository.SoftDeleteAsync(a => a.Id == id);
 | 
	
		
			
				|  |  |          await _userRoleRepository.DeleteAsync(a => a.UserId == id);
 | 
	
		
			
				|  |  |          await _userRepository.SoftDeleteAsync(id);
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -321,6 +393,8 @@ public class UserService : BaseService, IUserService, IDynamicApi
 | 
	
		
			
				|  |  |      [Transaction]
 | 
	
		
			
				|  |  |      public async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
 | 
	
		
			
				|  |  |      {
 | 
	
		
			
				|  |  | +        await _employeeOrganizationRepository.DeleteAsync(a => ids.Contains(a.EmployeeId));
 | 
	
		
			
				|  |  | +        await _employeeRepository.SoftDeleteAsync(a => ids.Contains(a.Id));
 | 
	
		
			
				|  |  |          await _userRoleRepository.DeleteAsync(a => ids.Contains(a.UserId));
 | 
	
		
			
				|  |  |          await _userRepository.SoftDeleteAsync(ids);
 | 
	
		
			
				|  |  |  
 |