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
{
///
/// 租户管理
///
public class TenantService :
YiCrudAppService, ITenantService
{
private ISqlSugarRepository _repository;
private IDataSeeder _dataSeeder;
public TenantService(ISqlSugarRepository repository, IDataSeeder dataSeeder) :
base(repository)
{
_repository = repository;
_dataSeeder = dataSeeder;
}
///
/// 租户单查
///
///
///
public override Task GetAsync(Guid id)
{
return base.GetAsync(id);
}
///
/// 租户多查
///
///
///
public override async Task> GetListAsync(TenantGetListInput 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));
}
///
/// 租户选项
///
///
public async Task> GetSelectAsync()
{
var entites = await _repository._DbQueryable.ToListAsync();
return entites.Select(x => new TenantSelectOutputDto { Id = x.Id, Name = x.Name }).ToList();
}
///
/// 创建租户
///
///
///
public override async Task CreateAsync(TenantCreateInput input)
{
if (await _repository.IsAnyAsync(x => x.Name == input.Name))
{
throw new UserFriendlyException("创建失败,当前租户已存在");
}
return await base.CreateAsync(input);
}
///
/// 更新租户
///
///
///
///
public override async Task 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);
}
///
/// 租户删除
///
///
///
public override Task DeleteAsync(IEnumerable id)
{
return base.DeleteAsync(id);
}
///
/// 初始化租户
///
///
///
[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();
//没有数据库,不能创工作单元,创建库,先关闭
using (var uow = UnitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
{
ISqlSugarClient db = await _repository.GetDbContextAsync();
//尝试创建数据库
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() is null)
.Where(x => x.GetCustomAttribute() is null));
}
if (types.Count > 0)
{
db.CodeFirst.InitTables(types.ToArray());
}
await uow.CompleteAsync();
}
}
}
}