wanshenmean
2 天以前 0b2869539598059704e1d208e2bcb18603b0fe0f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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}"));
            }
        }
    }
}