feat: 添加多租户界面及种子数据
This commit is contained in:
@@ -571,6 +571,83 @@ namespace Yi.Framework.Rbac.SqlSugarCore.DataSeeds
|
||||
};
|
||||
entities.Add(guide);
|
||||
|
||||
//租户管理
|
||||
MenuEntity tenant = new MenuEntity(_guidGenerator.Create())
|
||||
{
|
||||
MenuName = "租户管理",
|
||||
PermissionCode = "system:tenant:list",
|
||||
MenuType = MenuTypeEnum.Menu,
|
||||
Router = "tenant",
|
||||
IsShow = true,
|
||||
IsLink = false,
|
||||
IsCache = true,
|
||||
Component = "system/tenant/index",
|
||||
MenuIcon = "list",
|
||||
OrderNum = 101,
|
||||
ParentId = system.Id,
|
||||
IsDeleted = false
|
||||
};
|
||||
entities.Add(tenant);
|
||||
|
||||
MenuEntity tenantQuery = new MenuEntity(_guidGenerator.Create())
|
||||
{
|
||||
|
||||
MenuName = "租户查询",
|
||||
PermissionCode = "system:tenant:query",
|
||||
MenuType = MenuTypeEnum.Component,
|
||||
OrderNum = 100,
|
||||
ParentId = tenant.Id,
|
||||
IsDeleted = false
|
||||
};
|
||||
entities.Add(tenantQuery);
|
||||
|
||||
MenuEntity tenantAdd = new MenuEntity(_guidGenerator.Create())
|
||||
{
|
||||
|
||||
MenuName = "租户新增",
|
||||
PermissionCode = "system:tenant:add",
|
||||
MenuType = MenuTypeEnum.Component,
|
||||
OrderNum = 100,
|
||||
ParentId = tenant.Id,
|
||||
IsDeleted = false
|
||||
};
|
||||
entities.Add(tenantAdd);
|
||||
|
||||
MenuEntity tenantEdit = new MenuEntity(_guidGenerator.Create())
|
||||
{
|
||||
|
||||
MenuName = "租户修改",
|
||||
PermissionCode = "system:tenant:edit",
|
||||
MenuType = MenuTypeEnum.Component,
|
||||
OrderNum = 100,
|
||||
ParentId = tenant.Id,
|
||||
IsDeleted = false
|
||||
};
|
||||
entities.Add(tenantEdit);
|
||||
|
||||
MenuEntity tenantRemove = new MenuEntity(_guidGenerator.Create())
|
||||
{
|
||||
|
||||
MenuName = "租户删除",
|
||||
PermissionCode = "system:tenant:remove",
|
||||
MenuType = MenuTypeEnum.Component,
|
||||
OrderNum = 100,
|
||||
ParentId = tenant.Id,
|
||||
IsDeleted = false
|
||||
};
|
||||
entities.Add(tenantRemove);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//用户管理
|
||||
MenuEntity user = new MenuEntity(_guidGenerator.Create())
|
||||
{
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Application.Contracts.Dtos
|
||||
{
|
||||
public class TenantCreateInput
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string TenantConnectionString { get; set; }
|
||||
|
||||
public SqlSugar.DbType DbType { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Application.Contracts;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Application.Contracts.Dtos
|
||||
{
|
||||
public class TenantGetListInput
|
||||
public class TenantGetListInput: PagedAllResultRequestDto
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
//public int? EntityVersion { get; set; }
|
||||
|
||||
//public string? TenantConnectionString { get; set; }
|
||||
|
||||
//public DbType? DbType { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -9,5 +10,12 @@ namespace Yi.Framework.TenantManagement.Application.Contracts.Dtos
|
||||
{
|
||||
public class TenantGetListOutputDto:EntityDto<Guid>
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int EntityVersion { get; set; }
|
||||
|
||||
public string TenantConnectionString { get; set; }
|
||||
|
||||
public SqlSugar.DbType DbType { get; set; }
|
||||
public DateTime CreationTime { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -9,5 +10,13 @@ namespace Yi.Framework.TenantManagement.Application.Contracts.Dtos
|
||||
{
|
||||
public class TenantGetOutputDto:EntityDto<Guid>
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public int EntityVersion { get; set; }
|
||||
|
||||
public string TenantConnectionString { get; set; }
|
||||
|
||||
public SqlSugar.DbType DbType { get; set; }
|
||||
|
||||
public DateTime CreationTime { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data;
|
||||
|
||||
namespace Yi.Framework.TenantManagement.Application.Contracts.Dtos
|
||||
{
|
||||
public class TenantUpdateInput
|
||||
{
|
||||
public string? Name { get; set; }
|
||||
public int? EntityVersion { get; set; }
|
||||
|
||||
public string? TenantConnectionString { get; set; }
|
||||
|
||||
public SqlSugar.DbType? DbType { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,5 +11,6 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.Ddd.Application.Contracts\Yi.Framework.Ddd.Application.Contracts.csproj" />
|
||||
<ProjectReference Include="..\..\..\framework\Yi.Framework.SqlSugarCore.Abstractions\Yi.Framework.SqlSugarCore.Abstractions.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SqlSugar;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Data;
|
||||
using Volo.Abp.Domain.Repositories;
|
||||
using Volo.Abp.Modularity;
|
||||
using Volo.Abp.Uow;
|
||||
using Yi.Framework.Ddd.Application;
|
||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||
using Yi.Framework.TenantManagement.Application.Contracts;
|
||||
@@ -19,9 +28,11 @@ namespace Yi.Framework.TenantManagement.Application
|
||||
public class TenantService : YiCrudAppService<TenantAggregateRoot, TenantGetOutputDto, TenantGetListOutputDto, Guid, TenantGetListInput, TenantCreateInput, TenantUpdateInput>, ITenantService
|
||||
{
|
||||
private ISqlSugarRepository<TenantAggregateRoot, Guid> _repository;
|
||||
public TenantService(ISqlSugarRepository<TenantAggregateRoot, Guid> repository) : base(repository)
|
||||
private IDataSeeder _dataSeeder;
|
||||
public TenantService(ISqlSugarRepository<TenantAggregateRoot, Guid> repository, IDataSeeder dataSeeder) : base(repository)
|
||||
{
|
||||
_repository = repository;
|
||||
_dataSeeder = dataSeeder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -39,9 +50,14 @@ namespace Yi.Framework.TenantManagement.Application
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public override Task<PagedResultDto<TenantGetListOutputDto>> GetListAsync(TenantGetListInput input)
|
||||
public override async Task<PagedResultDto<TenantGetListOutputDto>> GetListAsync(TenantGetListInput input)
|
||||
{
|
||||
return base.GetListAsync(input);
|
||||
RefAsync<int> total = 0;
|
||||
|
||||
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Name), x => x.Name.Contains(input.Name!))
|
||||
.WhereIF(input.StartTime is not null && input.EndTime is not null, x => x.CreationTime >= input.StartTime && x.CreationTime <= input.EndTime)
|
||||
.ToPageListAsync(input.SkipCount, input.MaxResultCount, total);
|
||||
return new PagedResultDto<TenantGetListOutputDto>(total, await MapToGetListOutputDtosAsync(entities));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -49,9 +65,13 @@ namespace Yi.Framework.TenantManagement.Application
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public override Task<TenantGetOutputDto> CreateAsync(TenantCreateInput input)
|
||||
public override async Task<TenantGetOutputDto> CreateAsync(TenantCreateInput input)
|
||||
{
|
||||
return base.CreateAsync(input);
|
||||
if (await _repository.IsAnyAsync(x => x.Name == input.Name))
|
||||
{
|
||||
throw new UserFriendlyException("创建失败,当前租户已存在");
|
||||
}
|
||||
return await base.CreateAsync(input);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -60,11 +80,17 @@ namespace Yi.Framework.TenantManagement.Application
|
||||
/// <param name="id"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public override Task<TenantGetOutputDto> UpdateAsync(Guid id, TenantUpdateInput input)
|
||||
public override async Task<TenantGetOutputDto> UpdateAsync(Guid id, TenantUpdateInput input)
|
||||
{
|
||||
return base.UpdateAsync(id, input);
|
||||
if (await _repository.IsAnyAsync(x => x.Name == input.Name && x.Id != id))
|
||||
{
|
||||
throw new UserFriendlyException("更新后租户名已经存在");
|
||||
}
|
||||
|
||||
return await base.UpdateAsync(id, input);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 租户删除
|
||||
/// </summary>
|
||||
@@ -74,5 +100,45 @@ namespace Yi.Framework.TenantManagement.Application
|
||||
{
|
||||
return base.DeleteAsync(id);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 初始化租户
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut("tenant/init/{id}")]
|
||||
public async Task InitAsync([FromRoute]Guid id)
|
||||
{
|
||||
using (CurrentTenant.Change(id))
|
||||
{
|
||||
CodeFirst(await _repository.GetDbContextAsync());
|
||||
await _dataSeeder.SeedAsync(id);
|
||||
}
|
||||
}
|
||||
|
||||
private void CodeFirst(ISqlSugarClient db)
|
||||
{
|
||||
|
||||
var moduleContainer = ServiceProvider.GetRequiredService<IModuleContainer>();
|
||||
|
||||
//尝试创建数据库
|
||||
db.DbMaintenance.CreateDatabase();
|
||||
|
||||
List<Type> types = new List<Type>();
|
||||
foreach (var module in moduleContainer.Modules)
|
||||
{
|
||||
types.AddRange(module.Assembly.GetTypes()
|
||||
.Where(x => x.GetCustomAttribute<IgnoreCodeFirstAttribute>() == null)
|
||||
.Where(x => x.GetCustomAttribute<SugarTable>() != null)
|
||||
.Where(x=>x.GetCustomAttribute<MasterTenantAttribute>()==null)
|
||||
.Where(x => x.GetCustomAttribute<SplitTableAttribute>() is null));
|
||||
}
|
||||
if (types.Count > 0)
|
||||
{
|
||||
db.CodeFirst.InitTables(types.ToArray());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user