|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Threading.Tasks;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Haoliang.Core.Services.IServices;
|
|
|
|
|
|
namespace Haoliang.Api.Controllers
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 系统配置API控制器
|
|
|
/// </summary>
|
|
|
[ApiController]
|
|
|
[Route("api/v1/[controller]")]
|
|
|
public class SystemConfigController : ControllerBase
|
|
|
{
|
|
|
private readonly Services.ISystemConfigService _systemConfigService;
|
|
|
private readonly ILogger<SystemConfigController> _logger;
|
|
|
|
|
|
public SystemConfigController(Services.ISystemConfigService systemConfigService, ILogger<SystemConfigController> logger)
|
|
|
{
|
|
|
_systemConfigService = systemConfigService;
|
|
|
_logger = logger;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取所有配置
|
|
|
/// </summary>
|
|
|
[HttpGet]
|
|
|
public async Task<IActionResult> GetAllConfigs()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var configs = await _systemConfigService.GetAllConfigsAsync();
|
|
|
return Ok(configs);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "获取所有配置失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// <20>类别获取配置
|
|
|
/// </summary>
|
|
|
[HttpGet("category/{category}")]
|
|
|
public async Task<IActionResult> GetConfigsByCategory(string category)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var configs = await _systemConfigService.GetConfigsByCategoryAsync(category);
|
|
|
return Ok(configs);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "获取配置失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取单个配置
|
|
|
/// </summary>
|
|
|
[HttpGet("key/{key}")]
|
|
|
public async Task<IActionResult> GetConfig(string key)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var config = await _systemConfigService.GetConfigAsync(key);
|
|
|
|
|
|
if (config == null)
|
|
|
{
|
|
|
return NotFound(new { message = $"配置键 {key} 不存在" });
|
|
|
}
|
|
|
|
|
|
return Ok(config);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "获取配置失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 更新或创建配置
|
|
|
/// </summary>
|
|
|
[HttpPost]
|
|
|
public async Task<IActionResult> UpdateConfig([FromBody] ConfigUpdateRequest request)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(request.Key))
|
|
|
{
|
|
|
return BadRequest(new { message = "配置键不能为空" });
|
|
|
}
|
|
|
|
|
|
var config = await _systemConfigService.UpdateConfigAsync(request.Key, request.Value, request.UpdatedBy, request.Reason);
|
|
|
|
|
|
if (config == null)
|
|
|
{
|
|
|
return BadRequest(new { message = $"配置创建失败,可能是键 {request.Key} 已存在或类别无效" });
|
|
|
}
|
|
|
|
|
|
return Ok(new { success = true, data = config });
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "更新配置失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 批量更新配置
|
|
|
/// </summary>
|
|
|
[HttpPost("batch")]
|
|
|
public async Task<IActionResult> BatchUpdateConfigs([FromBody] Dictionary<string, string> configs)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (configs == null || !configs.Any())
|
|
|
{
|
|
|
return BadRequest(new { message = "配置数据不能为空" });
|
|
|
}
|
|
|
|
|
|
var result = await _systemConfigService.UpdateConfigsAsync(configs, "System");
|
|
|
return Ok(result);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "批量更新配置失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 重置配置为默认值
|
|
|
/// </summary>
|
|
|
[HttpDelete("key/{key}")]
|
|
|
public async Task<IActionResult> ResetConfig(string key)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var result = await _systemConfigService.ResetConfigAsync(key);
|
|
|
|
|
|
if (!result)
|
|
|
{
|
|
|
return NotFound(new { message = $"配置键 {key} 不存在或无法重置" });
|
|
|
}
|
|
|
|
|
|
return Ok(new { success = true });
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "重置配置失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 启用/禁用配置
|
|
|
/// </summary>
|
|
|
[HttpPut("enabled/{key}")]
|
|
|
public async Task<IActionResult> SetConfigEnabled(string key, bool enabled)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var result = await _systemConfigService.SetConfigEnabledAsync(key, enabled);
|
|
|
|
|
|
if (!result)
|
|
|
{
|
|
|
return NotFound(new { message = $"配置键 {key} 不存在" });
|
|
|
}
|
|
|
|
|
|
return Ok(new { success = true });
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "设置配置启用状态失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 获取配置变更日志
|
|
|
/// </summary>
|
|
|
[HttpGet("logs")]
|
|
|
public async Task<IActionResult> GetLogs([FromQuery] string key, [FromQuery] DateTime? fromTime, [FromQuery] DateTime? toTime)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
var logs = await _systemConfigService.GetConfigLogsAsync(key, fromTime, toTime);
|
|
|
return Ok(logs);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogError(ex, "获取配置日志失败: {Message}", ex.Message);
|
|
|
return StatusCode(500).ObjectResult(new { success = false, message = ex.Message });
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|