using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace HH.WCS.Mobox3.DSZSH.AppStart {
public class Settings
{
///
/// 配置文件
///
public static Config Config { get; set; }
///
/// 库区字典(加载后就不变)
///
public static Dictionary> AreaMap { get; set; } = new Dictionary>();
///
/// 任务字典(加载后就不变)
///
public static Dictionary TaskMap { get; set; } = new Dictionary();
public static void Init() {
// 加载配置文件
LoadJson();
// 针对 Areas 进行转换:将 Config 的 List 加载到 Dict 中
LoadAreas();
// 针对 Tasks 进行转换
LoadTasks();
}
private static void LoadJson() {
LogHelper.Info("加载配置文件信息 开始");
// JSON 文件路径
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "./config/config.json");
try {
// 读取 JSON 文件内容
string jsonContent = File.ReadAllText(filePath);
// 反序列化为 Config 对象
Config = JsonConvert.DeserializeObject(jsonContent);
}
catch (FileNotFoundException) {
LogHelper.Info("JSON 文件未找到");
}
catch (JsonException ex) {
LogHelper.Info($"JSON 解析错误: {ex.Message}");
}
catch (Exception ex) {
LogHelper.Info($"发生错误: {ex.Message}");
}
LogHelper.Info("加载配置文件信息 完成");
}
private static void LoadAreas() {
foreach (var area in Config.Areas) {
AreaMap.Add(area.Name, area.Codes);
}
}
private static void LoadTasks() {
foreach (var task in Config.Tasks) {
TaskMap.Add(task.Name, task);
}
}
}
}