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
|
{
|
/// <summary>
|
/// 出库时效配置控制器,读写独立的 outbound_time_config.json 配置文件
|
/// </summary>
|
[Route("api/[controller]")]
|
[ApiController]
|
[Authorize]
|
public class OutboundTimeConfigController : ControllerBase
|
{
|
private readonly IOptionsMonitor<OutboundTimeConfigOptions> _optionsMonitor;
|
private readonly IWebHostEnvironment _env;
|
|
/// <summary>
|
/// 配置文件名
|
/// </summary>
|
private const string ConfigFileName = "outbound_time_config.json";
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
/// <param name="optionsMonitor">出库时效配置 Options 监控器</param>
|
/// <param name="env">Web 宿主环境,用于定位配置文件路径</param>
|
public OutboundTimeConfigController(IOptionsMonitor<OutboundTimeConfigOptions> optionsMonitor, IWebHostEnvironment env)
|
{
|
_optionsMonitor = optionsMonitor;
|
_env = env;
|
}
|
|
/// <summary>
|
/// 获取当前出库时效配置
|
/// </summary>
|
/// <returns>当前配置值</returns>
|
[HttpPost("get")]
|
public IActionResult Get()
|
{
|
var config = _optionsMonitor.CurrentValue;
|
return Ok(WebResponseContent.Instance.OK("获取成功", new
|
{
|
config.Gw1FirstHours,
|
config.Gw1SecondHours,
|
config.Cw1Hours
|
}));
|
}
|
|
/// <summary>
|
/// 更新出库时效配置,写入独立的 outbound_time_config.json 文件
|
/// </summary>
|
/// <param name="config">新的配置值</param>
|
/// <returns>操作结果</returns>
|
[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}"));
|
}
|
}
|
}
|
}
|