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.
205 lines
6.4 KiB
205 lines
6.4 KiB
using Mapster;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Wood.Data.Repository;
|
|
using Wood.Entity;
|
|
using Wood.Entity.SystemManage;
|
|
using Wood.Service.BaseService;
|
|
using Wood.Service.SystemManage.Dto;
|
|
using Wood.Service.SystemManage.Manager;
|
|
using Wood.Service.SystemManage.Param;
|
|
using Wood.Util;
|
|
|
|
namespace Wood.Service.SystemManage
|
|
{
|
|
/// <summary>
|
|
/// 租户管理
|
|
/// </summary>
|
|
public class TenantService : ApiService
|
|
{
|
|
private readonly SqlSugarRepository<TenantEntity> _tenantRepository;
|
|
private readonly OrgManager _orgManager;
|
|
private readonly UserManager _userManager;
|
|
private readonly SqlSugarRepository<RoleEntity> _roleRepository;
|
|
private readonly SqlSugarRepository<MenuAuthorizeEntity> _menuAuthorizeRepository;
|
|
private readonly SqlSugarRepository<MenuEntity> _menuRepository;
|
|
private readonly SqlSugarRepository<UserBelongRoleEntity> _userBelongRoleRepository;
|
|
|
|
public TenantService(SqlSugarRepository<TenantEntity> tenantRepository, OrgManager orgManager, UserManager userManager, SqlSugarRepository<RoleEntity> roleRepository, SqlSugarRepository<MenuAuthorizeEntity> menuAuthorizeRepository, SqlSugarRepository<MenuEntity> menuRepository, SqlSugarRepository<UserBelongRoleEntity> userBelongRoleRepository)
|
|
{
|
|
_tenantRepository = tenantRepository;
|
|
_orgManager = orgManager;
|
|
_userManager = userManager;
|
|
_roleRepository = roleRepository;
|
|
_menuAuthorizeRepository = menuAuthorizeRepository;
|
|
_menuRepository = menuRepository;
|
|
_userBelongRoleRepository = userBelongRoleRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分页查询职位
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public async Task<TDataPaged<TenantPagedDto>> Paged(TenantPagedParam param)
|
|
{
|
|
var dtos = await _tenantRepository.AsQueryable()
|
|
.LeftJoin<OrgEntity>((it, oe) => it.OrgId == oe.Id)
|
|
.WhereIF(!string.IsNullOrEmpty(param.Name), it => it.TenantName.Contains(param.Name!))
|
|
.Select((it, oe) => new TenantPagedDto()
|
|
{
|
|
Id = it.Id.SelectAll(),
|
|
OrgName = oe.OrgName
|
|
})
|
|
.ToPagedListAsync(param);
|
|
|
|
return dtos;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新建租户
|
|
/// 初始化信息
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[UnitOfWork]
|
|
public async Task Add(TenantAddParam param)
|
|
{
|
|
if (await _tenantRepository.IsAnyAsync(it => it.TenantName == param.TenantName))
|
|
throw Oops.Oh("租户名称不能重复!");
|
|
var orgId = IdGeneratorHelper.Instance.GetId();
|
|
//建立租户
|
|
TenantEntity tenantEntity = new TenantEntity()
|
|
{
|
|
TenantName = param.TenantName,
|
|
OrgId = orgId,
|
|
Sort = (await _tenantRepository.AsQueryable().MaxAsync(x => x.Sort)) + 10,
|
|
};
|
|
await _tenantRepository.InsertAsync(tenantEntity);
|
|
//建立机构部门
|
|
OrgEntity entity = new OrgEntity()
|
|
{
|
|
Id = orgId,
|
|
OrgName = param.OrgName,
|
|
ShortName = param.OrgName,
|
|
FormCode = param.OrgFormCode,
|
|
Sort = (await _orgManager.AsQueryable().MaxAsync(x => x.Sort)) + 10,
|
|
TenantId = tenantEntity.Id
|
|
};
|
|
await _orgManager.AsRepository().InsertAsync(entity);
|
|
//建立租户管理员角色
|
|
RoleEntity roleEntity = new RoleEntity()
|
|
{
|
|
FormCode = "zg_admin_" + param.OrgFormCode,
|
|
DataScopeType = DataScopeTypeEnum.All,
|
|
Sort = (await _roleRepository.AsQueryable().MaxAsync(x => x.Sort)) + 10,
|
|
RoleName = "租管-" + param.OrgName,
|
|
TenantId = tenantEntity.Id,
|
|
};
|
|
await _roleRepository.InsertAsync(roleEntity);
|
|
|
|
//给角色赋值 基础 权限
|
|
string[] defaultMenus = ["工作台", "主页", "系统管理", "机构管理", "角色管理", "用户管理", "职位管理", "个人中心"];
|
|
var menus = await _menuRepository.GetListAsync(it => defaultMenus.Contains(it.MenuName));
|
|
|
|
foreach (var item in menus)
|
|
{
|
|
var menuAndBtns = await _menuRepository.AsQueryable().ToChildListAsync(it => it.ParentId, item.Id);
|
|
var menuAuthorizeEntities = menuAndBtns.Select(it => new MenuAuthorizeEntity()
|
|
{
|
|
RoleId = roleEntity.Id,
|
|
MenuId = it.Id,
|
|
TenantId = tenantEntity.Id,
|
|
});
|
|
await _menuAuthorizeRepository.InsertRangeAsync(menuAuthorizeEntities.ToArray());
|
|
}
|
|
|
|
//建立租户管理员账号
|
|
UserEntity userEntity = new UserEntity()
|
|
{
|
|
AccountType = AccountTypeEnum.TenantAdmin,
|
|
NickName = param.RealName,
|
|
UserName = param.UserName,
|
|
Salt = CryptogramHelper.GenerateRandomString(16),
|
|
Password = CryptogramHelper.GMSM4Encrypt("123456"),
|
|
RealName = param.RealName,
|
|
Birthday = DateTime.Now,
|
|
OrgId = orgId,
|
|
TenantId = tenantEntity.Id
|
|
};
|
|
await _userManager.AsRepository().InsertAsync(userEntity);
|
|
|
|
//关联账号和角色信息
|
|
UserBelongRoleEntity belongRoleEntity = new UserBelongRoleEntity()
|
|
{
|
|
RoleId = roleEntity.Id,
|
|
UserId = userEntity.Id,
|
|
};
|
|
await _userBelongRoleRepository.InsertAsync(belongRoleEntity);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取明细
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public async Task<TenantDetailDto> GetDetail(BaseIdParam param)
|
|
{
|
|
return await _tenantRepository.AsQueryable()
|
|
.LeftJoin<OrgEntity>((it, oe) => it.OrgId == oe.Id)
|
|
.Where(it => it.Id == param.Id)
|
|
.Select((it, oe) => new TenantDetailDto()
|
|
{
|
|
Id = it.Id.SelectAll(),
|
|
OrgName = oe.OrgName
|
|
}).FirstAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 假删除
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[UnitOfWork]
|
|
public async Task Delete(BaseIdParam param)
|
|
{
|
|
if (param.Id > 0)
|
|
await _tenantRepository.FakeDeleteAsync(it => param.Id == it.Id);
|
|
}
|
|
/// <summary>
|
|
/// 启用租户
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task Open(BaseIdParam param)
|
|
{
|
|
var tenant = await _tenantRepository.GetByIdAsync(param.Id);
|
|
tenant.Status = 1;
|
|
await _tenantRepository.UpdateAsync(tenant);
|
|
}
|
|
/// <summary>
|
|
/// 禁用租户
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task Close(BaseIdParam param)
|
|
{
|
|
var tenant = await _tenantRepository.GetByIdAsync(param.Id);
|
|
tenant.Status = 0;
|
|
await _tenantRepository.UpdateAsync(tenant);
|
|
}
|
|
|
|
public async Task<int> GetMaxSort()
|
|
{
|
|
var result = await this._tenantRepository.AsQueryable().MaxAsync(it => it.Sort);
|
|
return result + 10;
|
|
}
|
|
}
|
|
}
|
|
|