diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs index 8c4ae728..8e4baa4d 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.SqlSugarCore/DataSeeds/MenuDataSeed.cs @@ -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()) { diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantCreateInput.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantCreateInput.cs index 4d1533c4..93a11715 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantCreateInput.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantCreateInput.cs @@ -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; } } } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListInput.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListInput.cs index 1d863220..ce2fc42d 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListInput.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListInput.cs @@ -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; } } } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListOutputDto.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListOutputDto.cs index f1127f18..a9ba762c 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListOutputDto.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetListOutputDto.cs @@ -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 { + 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; } } } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetOutputDto.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetOutputDto.cs index 6c6f192f..28830803 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetOutputDto.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantGetOutputDto.cs @@ -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 { + 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; } } } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantUpdateInput.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantUpdateInput.cs index 56013886..863ab72d 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantUpdateInput.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Dtos/TenantUpdateInput.cs @@ -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; } } } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Yi.Framework.TenantManagement.Application.Contracts.csproj b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Yi.Framework.TenantManagement.Application.Contracts.csproj index c12af7cb..b683f5d6 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Yi.Framework.TenantManagement.Application.Contracts.csproj +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application.Contracts/Yi.Framework.TenantManagement.Application.Contracts.csproj @@ -11,5 +11,6 @@ + diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application/TenantService.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application/TenantService.cs index 43de14cf..f215cdad 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application/TenantService.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Application/TenantService.cs @@ -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, ITenantService { private ISqlSugarRepository _repository; - public TenantService(ISqlSugarRepository repository) : base(repository) + private IDataSeeder _dataSeeder; + public TenantService(ISqlSugarRepository repository, IDataSeeder dataSeeder) : base(repository) { _repository = repository; + _dataSeeder = dataSeeder; } /// @@ -39,9 +50,14 @@ namespace Yi.Framework.TenantManagement.Application /// /// /// - public override Task> GetListAsync(TenantGetListInput input) + public override async Task> GetListAsync(TenantGetListInput input) { - return base.GetListAsync(input); + RefAsync 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(total, await MapToGetListOutputDtosAsync(entities)); } /// @@ -49,9 +65,13 @@ namespace Yi.Framework.TenantManagement.Application /// /// /// - public override Task CreateAsync(TenantCreateInput input) + public override async Task CreateAsync(TenantCreateInput input) { - return base.CreateAsync(input); + if (await _repository.IsAnyAsync(x => x.Name == input.Name)) + { + throw new UserFriendlyException("创建失败,当前租户已存在"); + } + return await base.CreateAsync(input); } /// @@ -60,11 +80,17 @@ namespace Yi.Framework.TenantManagement.Application /// /// /// - public override Task UpdateAsync(Guid id, TenantUpdateInput input) + public override async Task 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); } + /// /// 租户删除 /// @@ -74,5 +100,45 @@ namespace Yi.Framework.TenantManagement.Application { return base.DeleteAsync(id); } + + + /// + /// 初始化租户 + /// + /// + /// + [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(); + + //尝试创建数据库 + db.DbMaintenance.CreateDatabase(); + + List types = new List(); + foreach (var module in moduleContainer.Modules) + { + types.AddRange(module.Assembly.GetTypes() + .Where(x => x.GetCustomAttribute() == null) + .Where(x => x.GetCustomAttribute() != null) + .Where(x=>x.GetCustomAttribute()==null) + .Where(x => x.GetCustomAttribute() is null)); + } + if (types.Count > 0) + { + db.CodeFirst.InitTables(types.ToArray()); + } + + } } } diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db b/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db new file mode 100644 index 00000000..f46d3d4e Binary files /dev/null and b/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db differ diff --git a/Yi.RuoYi.Vue3/src/api/system/tenant.js b/Yi.RuoYi.Vue3/src/api/system/tenant.js new file mode 100644 index 00000000..fbcce7d8 --- /dev/null +++ b/Yi.RuoYi.Vue3/src/api/system/tenant.js @@ -0,0 +1,55 @@ +import request from '@/utils/request' +/* 以下为api的模板,通用的crud,将以下变量替换即可: +tenant : 实体模型 +*/ +// 分页查询 +export function listData(query) { + return request({ + url: '/tenant', + method: 'get', + params: query + }) +} + +// id查询 +export function getData(id) { + return request({ + url: `/tenant/${id}`, + method: 'get' + }) +} + +// 新增 +export function addData(data) { + return request({ + url: '/tenant', + method: 'post', + data: data + }) +} + +// 修改 +export function updateData(id,data) { + return request({ + url: `/tenant/${id}`, + method: 'put', + data: data + }) +} + +// 初始化 +export function InitData(id) { + return request({ + url: `/tenant/init/${id}`, + method: 'put' + }) +} + +// 删除 +export function delData(ids) { + return request({ + url: `/tenant`, + method: 'delete', + params:{id:ids} + }) +} diff --git a/Yi.RuoYi.Vue3/src/views/system/menu/index.vue b/Yi.RuoYi.Vue3/src/views/system/menu/index.vue index d9c84779..e04a697e 100644 --- a/Yi.RuoYi.Vue3/src/views/system/menu/index.vue +++ b/Yi.RuoYi.Vue3/src/views/system/menu/index.vue @@ -117,13 +117,13 @@ - 目录 - 菜单 - 按钮 + 目录 + 菜单 + 按钮 - + - +