-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathTenantService.cs
57 lines (46 loc) · 1.37 KB
/
TenantService.cs
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
using Blog.Core.Common.DB;
using Blog.Core.Common.Seed;
using Blog.Core.IServices;
using Blog.Core.Model.Models;
using Blog.Core.Repository.UnitOfWorks;
using Blog.Core.Services.BASE;
using System.Threading.Tasks;
namespace Blog.Core.Services;
public class TenantService : BaseServices<SysTenant>, ITenantService
{
private readonly IUnitOfWorkManage _uowManager;
public TenantService(IUnitOfWorkManage uowManage)
{
this._uowManager = uowManage;
}
public async Task SaveTenant(SysTenant tenant)
{
bool initDb = tenant.Id == 0;
using (var uow = _uowManager.CreateUnitOfWork())
{
tenant.DefaultTenantConfig();
if (tenant.Id == 0)
{
await Db.Insertable(tenant).ExecuteReturnSnowflakeIdAsync();
}
else
{
var oldTenant = await QueryById(tenant.Id);
if (oldTenant.Connection != tenant.Connection)
{
initDb = true;
}
await Db.Updateable(tenant).ExecuteCommandAsync();
}
uow.Commit();
}
if (initDb)
{
await InitTenantDb(tenant);
}
}
public async Task InitTenantDb(SysTenant tenant)
{
await DBSeed.InitTenantSeedAsync(Db.AsTenant(), tenant.GetConnectionConfig());
}
}