namespace Fengling.Console.Controllers; /// /// 租户管理控制器 /// 提供租户的增删改查以及租户用户、角色、配置管理功能 /// [ApiController] [Route("api/console/[controller]")] [Authorize(AuthenticationSchemes = OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme)] public class TenantsController : ControllerBase { private readonly ITenantService _tenantService; private readonly ILogger _logger; public TenantsController(ITenantService tenantService, ILogger logger) { _tenantService = tenantService; _logger = logger; } /// /// 获取租户列表 /// /// 分页查询参数,支持按名称、租户编码和状态筛选 /// 分页的租户列表,包含租户基本信息和状态 /// 成功返回租户分页列表 /// 服务器内部错误 [HttpGet] [Produces("application/json")] [ProducesResponseType(typeof(PagedResultDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task>> GetTenants([FromQuery] TenantQueryDto query) { try { var (items, totalCount) = await _tenantService.GetTenantsAsync(query.Page, query.PageSize, query.Name, query.TenantId, query.Status); var result = new PagedResultDto { Items = items.ToList(), TotalCount = totalCount, Page = query.Page, PageSize = query.PageSize }; return Ok(result); } catch (Exception ex) { _logger.LogError(ex, "Error getting tenants"); return StatusCode(500, new { message = ex.Message }); } } /// /// 获取单个租户详情 /// /// 租户ID /// 租户的详细信息,包括配置、限额等信息 /// 成功返回租户详情 /// 租户不存在 /// 服务器内部错误 [HttpGet("{id}")] [Produces("application/json")] [ProducesResponseType(typeof(TenantDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task> GetTenant(long id) { try { var tenant = await _tenantService.GetTenantAsync(id); if (tenant == null) { return NotFound(); } return Ok(tenant); } catch (Exception ex) { _logger.LogError(ex, "Error getting tenant {TenantId}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 获取指定租户的用户列表 /// /// 租户ID /// 属于该租户的所有用户列表 /// 成功返回用户列表 /// 租户不存在 /// 服务器内部错误 [HttpGet("{tenantId}/users")] [Produces("application/json")] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task>> GetTenantUsers(long tenantId) { try { var users = await _tenantService.GetTenantUsersAsync(tenantId); return Ok(users); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Tenant not found: {TenantId}", tenantId); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error getting users for tenant {TenantId}", tenantId); return StatusCode(500, new { message = ex.Message }); } } /// /// 获取指定租户的角色列表 /// /// 租户ID /// 属于该租户的所有角色列表 /// 成功返回角色列表 /// 租户不存在 /// 服务器内部错误 [HttpGet("{tenantId}/roles")] [Produces("application/json")] [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task>> GetTenantRoles(long tenantId) { try { var roles = await _tenantService.GetTenantRolesAsync(tenantId); return Ok(roles); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Tenant not found: {TenantId}", tenantId); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error getting roles for tenant {TenantId}", tenantId); return StatusCode(500, new { message = ex.Message }); } } /// /// 获取租户配置信息 /// /// 租户ID /// 租户的详细配置信息,包括功能开关、配额限制等 /// 成功返回租户配置 /// 租户不存在 /// 服务器内部错误 [HttpGet("{id}/settings")] [Produces("application/json")] [ProducesResponseType(typeof(TenantSettingsDto), StatusCodes.Status200OK)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task> GetTenantSettings(long id) { try { var settings = await _tenantService.GetTenantSettingsAsync(id); return Ok(settings); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Tenant not found: {TenantId}", id); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error getting settings for tenant {TenantId}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 更新租户配置 /// /// 租户ID /// 需要更新的租户配置信息 /// 无内容响应 /// 成功更新租户配置 /// 租户不存在 /// 服务器内部错误 [HttpPut("{id}/settings")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task UpdateTenantSettings(long id, [FromBody] TenantSettingsDto settings) { try { await _tenantService.UpdateTenantSettingsAsync(id, settings); return NoContent(); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Tenant not found: {TenantId}", id); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error updating settings for tenant {TenantId}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 创建新租户 /// /// 创建租户所需的配置信息 /// 创建的租户详情 /// 成功创建租户 /// 服务器内部错误 [HttpPost] [Produces("application/json")] [ProducesResponseType(typeof(TenantDto), StatusCodes.Status201Created)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task> CreateTenant([FromBody] CreateTenantDto dto) { try { var tenant = await _tenantService.CreateTenantAsync(dto); return CreatedAtAction(nameof(GetTenant), new { id = tenant.Id }, tenant); } catch (Exception ex) { _logger.LogError(ex, "Error creating tenant"); return StatusCode(500, new { message = ex.Message }); } } /// /// 更新租户信息 /// /// 租户ID /// 需要更新的租户配置信息 /// 无内容响应 /// 成功更新租户 /// 租户不存在 /// 服务器内部错误 [HttpPut("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task UpdateTenant(long id, [FromBody] UpdateTenantDto dto) { try { await _tenantService.UpdateTenantAsync(id, dto); return NoContent(); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Tenant not found: {TenantId}", id); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error updating tenant {TenantId}", id); return StatusCode(500, new { message = ex.Message }); } } /// /// 删除租户 /// /// 租户ID /// 无内容响应 /// 成功删除租户 /// 租户不存在 /// 服务器内部错误 [HttpDelete("{id}")] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(typeof(object), StatusCodes.Status404NotFound)] [ProducesResponseType(typeof(object), StatusCodes.Status500InternalServerError)] public async Task DeleteTenant(long id) { try { await _tenantService.DeleteTenantAsync(id); return NoContent(); } catch (KeyNotFoundException ex) { _logger.LogWarning(ex, "Tenant not found: {TenantId}", id); return NotFound(); } catch (Exception ex) { _logger.LogError(ex, "Error deleting tenant {TenantId}", id); return StatusCode(500, new { message = ex.Message }); } } }