init
This commit is contained in:
57
DouyinApi.Services/TenantService.cs
Normal file
57
DouyinApi.Services/TenantService.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using DouyinApi.Common.DB;
|
||||
using DouyinApi.Common.Seed;
|
||||
using DouyinApi.IServices;
|
||||
using DouyinApi.Model.Models;
|
||||
using DouyinApi.Repository.UnitOfWorks;
|
||||
using DouyinApi.Services.BASE;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DouyinApi.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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user