123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- using System.Linq;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Mvc;
- using ZhonTai.Admin.Core.Attributes;
- using ZhonTai.Admin.Core.Repositories;
- using ZhonTai.Common.Helpers;
- using ZhonTai.Admin.Core.Dto;
- using ZhonTai.Admin.Domain.Role;
- using ZhonTai.Admin.Domain.RolePermission;
- using ZhonTai.Admin.Domain.Tenant;
- using ZhonTai.Admin.Domain.User;
- using ZhonTai.Admin.Domain.UserRole;
- using ZhonTai.Admin.Services.Tenant.Dto;
- using ZhonTai.Admin.Domain.Tenant.Dto;
- using ZhonTai.DynamicApi;
- using ZhonTai.DynamicApi.Attributes;
- using ZhonTai.Admin.Core.Consts;
- using ZhonTai.Admin.Core.Configs;
- using ZhonTai.Admin.Domain.Org;
- using ZhonTai.Admin.Domain.Staff;
- using ZhonTai.Admin.Domain;
- using FreeSql;
- using Microsoft.Extensions.DependencyInjection;
- using ZhonTai.Admin.Core.Db;
- using ZhonTai.Admin.Core.Db.Transaction;
- namespace ZhonTai.Admin.Services.Tenant;
- /// <summary>
- /// 租户服务
- /// </summary>
- [DynamicApi(Area = AdminConsts.AreaName)]
- public class TenantService : BaseService, ITenantService, IDynamicApi
- {
- private readonly ITenantRepository _tenantRepository;
- private readonly IRoleRepository _roleRepository;
- private readonly IUserRepository _userRepository;
- private readonly IRepositoryBase<UserRoleEntity> _userRoleRepository;
- private readonly IRepositoryBase<RolePermissionEntity> _rolePermissionRepository;
- private IStaffRepository _staffRepository => LazyGetRequiredService<IStaffRepository>();
- private IRepositoryBase<UserOrgEntity> _userOrgRepository => LazyGetRequiredService<IRepositoryBase<UserOrgEntity>>();
- private AppConfig _appConfig => LazyGetRequiredService<AppConfig>();
- public TenantService(
- ITenantRepository tenantRepository,
- IRoleRepository roleRepository,
- IUserRepository userRepository,
- IRepositoryBase<UserRoleEntity> userRoleRepository,
- IRepositoryBase<RolePermissionEntity> rolePermissionRepository
- )
- {
- _tenantRepository = tenantRepository;
- _roleRepository = roleRepository;
- _userRepository = userRepository;
- _userRoleRepository = userRoleRepository;
- _rolePermissionRepository = rolePermissionRepository;
- }
- /// <summary>
- /// 查询租户
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public async Task<IResultOutput> GetAsync(long id)
- {
- var result = await _tenantRepository.GetAsync<TenantGetOutput>(id);
- return ResultOutput.Ok(result);
- }
- /// <summary>
- /// 查询租户列表
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [HttpPost]
- public async Task<IResultOutput> GetPageAsync(PageInput<TenantGetPageDto> input)
- {
- var key = input.Filter?.Name;
- var list = await _tenantRepository.Select
- .WhereDynamicFilter(input.DynamicFilter)
- .WhereIf(key.NotNull(), a => a.Name.Contains(key))
- .Count(out var total)
- .OrderByDescending(true, c => c.Id)
- .Page(input.CurrentPage, input.PageSize)
- .ToListAsync<TenantListOutput>();
- var data = new PageOutput<TenantListOutput>()
- {
- List = list,
- Total = total
- };
- return ResultOutput.Ok(data);
- }
- /// <summary>
- /// 新增
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- [Transaction]
- public virtual async Task<IResultOutput> AddAsync(TenantAddInput input)
- {
- if (await _tenantRepository.Select.AnyAsync(a => a.Name == input.Name))
- {
- return ResultOutput.NotOk($"企业名称已存在");
- }
- if (await _tenantRepository.Select.AnyAsync(a => a.Code == input.Code))
- {
- return ResultOutput.NotOk($"企业编码已存在");
- }
- //添加租户
- TenantEntity entity = Mapper.Map<TenantEntity>(input);
- TenantEntity tenant = await _tenantRepository.InsertAsync(entity);
- long tenantId = tenant.Id;
-
- var cloud = LazyGetRequiredService<FreeSqlCloud>();
- var tenantDb = cloud.GetTenantDb(ServiceProvider, tenantId);
- //添加部门
- var org = new OrgEntity
- {
- TenantId = tenantId,
- Name = input.Name,
- Code = input.Code,
- ParentId = 0,
- MemberCount = 1
- };
- await tenantDb.GetRepositoryBase<OrgEntity>().InsertAsync(org);
- //添加主管理员
- string pwd = MD5Encrypt.Encrypt32(_appConfig.DefaultPassword);
- var user = new UserEntity
- {
- TenantId = tenantId,
- UserName = input.Phone,
- Password = pwd,
- Name = input.RealName,
- Mobile = input.Phone,
- Email = input.Email,
- Status = UserStatus.Enabled,
- Type = UserType.TenantAdmin,
- MainOrgId = org.Id
- };
- await tenantDb.GetRepositoryBase<UserEntity>().InsertAsync(user);
- long userId = user.Id;
- //添加员工
- var emp = new StaffEntity
- {
- Id = userId,
- TenantId = tenantId
- };
- await tenantDb.GetRepositoryBase<StaffEntity>().InsertAsync(emp);
- //添加用户部门
- var userOrg = new UserOrgEntity
- {
- UserId = userId,
- OrgId = org.Id
- };
- await tenantDb.GetRepositoryBase<UserOrgEntity>().InsertAsync(userOrg);
- var roleRepository = tenantDb.GetRepositoryBase<RoleEntity>();
- //添加角色分组
- var roleGroup = new RoleEntity
- {
- TenantId = tenantId,
- Name = "系统默认",
- ParentId = 0
- };
- await roleRepository.InsertAsync(roleGroup);
- //添加角色
- var role = new RoleEntity
- {
- TenantId = tenantId,
- Name = "主管理员",
- Code = "main-admin",
- ParentId = roleGroup.Id
- };
- await roleRepository.InsertAsync(role);
- //添加用户角色
- var userRole = new UserRoleEntity()
- {
- UserId = userId,
- RoleId = role.Id
- };
- await tenantDb.GetRepositoryBase<UserRoleEntity>().InsertAsync(userRole);
- //更新租户的用户
- tenant.UserId = userId;
- await _tenantRepository.UpdateAsync(tenant);
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public async Task<IResultOutput> UpdateAsync(TenantUpdateInput input)
- {
- if (!(input?.Id > 0))
- {
- return ResultOutput.NotOk();
- }
- var entity = await _tenantRepository.GetAsync(input.Id);
- if (!(entity?.Id > 0))
- {
- return ResultOutput.NotOk("租户不存在!");
- }
- Mapper.Map(input, entity);
- await _tenantRepository.UpdateAsync(entity);
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 彻底删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [Transaction]
- public virtual async Task<IResultOutput> DeleteAsync(long id)
- {
- //删除角色权限
- await _rolePermissionRepository.Where(a => a.Role.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
- //删除用户角色
- await _userRoleRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
- //删除员工
- await _staffRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
- //删除用户部门
- await _userOrgRepository.Where(a => a.User.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
- //删除用户
- await _userRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
- //删除角色
- await _roleRepository.Where(a => a.TenantId == id).DisableGlobalFilter("Tenant").ToDelete().ExecuteAffrowsAsync();
- //删除租户
- await _tenantRepository.DeleteAsync(id);
- return ResultOutput.Ok();
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [Transaction]
- public virtual async Task<IResultOutput> SoftDeleteAsync(long id)
- {
- //删除用户
- await _userRepository.SoftDeleteAsync(a => a.TenantId == id, "Tenant");
- //删除角色
- await _roleRepository.SoftDeleteAsync(a => a.TenantId == id, "Tenant");
- //删除租户
- var result = await _tenantRepository.SoftDeleteAsync(id);
- return ResultOutput.Result(result);
- }
- /// <summary>
- /// 批量删除
- /// </summary>
- /// <param name="ids"></param>
- /// <returns></returns>
- [Transaction]
- public virtual async Task<IResultOutput> BatchSoftDeleteAsync(long[] ids)
- {
- //删除用户
- await _userRepository.SoftDeleteAsync(a => ids.Contains(a.TenantId.Value), "Tenant");
- //删除角色
- await _roleRepository.SoftDeleteAsync(a => ids.Contains(a.TenantId.Value), "Tenant");
- //删除租户
- var result = await _tenantRepository.SoftDeleteAsync(ids);
- return ResultOutput.Result(result);
- }
- }
|