using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using WIDESEA_Core;
using WIDESEA_Core.Core;
namespace WIDESEA_WMSServer.Controllers
{
///
/// 出库时效配置控制器,读写独立的 outbound_time_config.json 配置文件
///
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class OutboundTimeConfigController : ControllerBase
{
private readonly IOptionsMonitor _optionsMonitor;
private readonly IWebHostEnvironment _env;
///
/// 配置文件名
///
private const string ConfigFileName = "outbound_time_config.json";
///
/// 构造函数
///
/// 出库时效配置 Options 监控器
/// Web 宿主环境,用于定位配置文件路径
public OutboundTimeConfigController(IOptionsMonitor optionsMonitor, IWebHostEnvironment env)
{
_optionsMonitor = optionsMonitor;
_env = env;
}
///
/// 获取当前出库时效配置
///
/// 当前配置值
[HttpPost("get")]
public IActionResult Get()
{
var config = _optionsMonitor.CurrentValue;
return Ok(WebResponseContent.Instance.OK("获取成功", new
{
config.Gw1FirstHours,
config.Gw1SecondHours,
config.Cw1Hours
}));
}
///
/// 更新出库时效配置,写入独立的 outbound_time_config.json 文件
///
/// 新的配置值
/// 操作结果
[HttpPost("update")]
public IActionResult Update([FromBody] OutboundTimeConfigOptions config)
{
if (config.Gw1FirstHours <= 0 || config.Gw1SecondHours <= 0 || config.Cw1Hours <= 0)
{
return Ok(WebResponseContent.Instance.Error("配置值必须大于0"));
}
try
{
var filePath = Path.Combine(_env.ContentRootPath, ConfigFileName);
// 构建配置 JSON,包裹在 OutboundTimeConfig 节下以匹配 Options 绑定
var configSection = new JObject
{
["Gw1FirstHours"] = config.Gw1FirstHours,
["Gw1SecondHours"] = config.Gw1SecondHours,
["Cw1Hours"] = config.Cw1Hours
};
var jsonObj = new JObject
{
[OutboundTimeConfigOptions.SectionName] = configSection
};
// 写入文件,格式化输出
System.IO.File.WriteAllText(filePath, jsonObj.ToString(Formatting.Indented));
return Ok(WebResponseContent.Instance.OK("配置更新成功"));
}
catch (Exception ex)
{
return Ok(WebResponseContent.Instance.Error($"配置更新失败: {ex.Message}"));
}
}
}
}