TenantService.cs
5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using System.Reflection;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.Modularity;
using Volo.Abp.Uow;
using Yi.Framework.Ddd.Application;
using Yi.Framework.SqlSugarCore.Abstractions;
using Yi.Framework.TenantManagement.Application.Contracts;
using Yi.Framework.TenantManagement.Application.Contracts.Dtos;
using Yi.Framework.TenantManagement.Domain;
namespace Yi.Framework.TenantManagement.Application
{
/// <summary>
/// 租户管理
/// </summary>
public class TenantService :
YiCrudAppService<TenantAggregateRoot, TenantGetOutputDto, TenantGetListOutputDto, Guid, TenantGetListInput,
TenantCreateInput, TenantUpdateInput>, ITenantService
{
private ISqlSugarRepository<TenantAggregateRoot, Guid> _repository;
private IDataSeeder _dataSeeder;
public TenantService(ISqlSugarRepository<TenantAggregateRoot, Guid> repository, IDataSeeder dataSeeder) :
base(repository)
{
_repository = repository;
_dataSeeder = dataSeeder;
}
/// <summary>
/// 租户单查
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override Task<TenantGetOutputDto> GetAsync(Guid id)
{
return base.GetAsync(id);
}
/// <summary>
/// 租户多查
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<PagedResultDto<TenantGetListOutputDto>> GetListAsync(TenantGetListInput 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>
/// 租户选项
/// </summary>
/// <returns></returns>
public async Task<List<TenantSelectOutputDto>> GetSelectAsync()
{
var entites = await _repository._DbQueryable.ToListAsync();
return entites.Select(x => new TenantSelectOutputDto { Id = x.Id, Name = x.Name }).ToList();
}
/// <summary>
/// 创建租户
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<TenantGetOutputDto> CreateAsync(TenantCreateInput input)
{
if (await _repository.IsAnyAsync(x => x.Name == input.Name))
{
throw new UserFriendlyException("创建失败,当前租户已存在");
}
return await base.CreateAsync(input);
}
/// <summary>
/// 更新租户
/// </summary>
/// <param name="id"></param>
/// <param name="input"></param>
/// <returns></returns>
public override async Task<TenantGetOutputDto> UpdateAsync(Guid id, TenantUpdateInput input)
{
if (await _repository.IsAnyAsync(x => x.Name == input.Name && x.Id != id))
{
throw new UserFriendlyException("更新后租户名已经存在");
}
return await base.UpdateAsync(id, input);
}
/// <summary>
/// 租户删除
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public override Task DeleteAsync(IEnumerable<Guid> id)
{
return base.DeleteAsync(id);
}
/// <summary>
/// 初始化租户
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPut("tenant/init/{id}")]
public async Task InitAsync([FromRoute] Guid id)
{
await CurrentUnitOfWork.SaveChangesAsync();
using (CurrentTenant.Change(id))
{
await CodeFirst(this.LazyServiceProvider);
await _dataSeeder.SeedAsync(id);
}
}
private async Task CodeFirst(IServiceProvider service)
{
var moduleContainer = service.GetRequiredService<IModuleContainer>();
//没有数据库,不能创工作单元,创建库,先关闭
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
ISqlSugarClient db = await _repository.GetDbContextAsync();
//尝试创建数据库
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<DefaultTenantTableAttribute>() is null)
.Where(x => x.GetCustomAttribute<SplitTableAttribute>() is null));
}
if (types.Count > 0)
{
db.CodeFirst.InitTables(types.ToArray());
}
await uow.CompleteAsync();
}
}
}
}