提交
This commit is contained in:
@ -1,117 +1,169 @@
|
||||
using System;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace DH.Commons.Helper
|
||||
{
|
||||
public static class SchemeHelper
|
||||
{
|
||||
private const string SchemesKey = "Schemes";
|
||||
private const string CurrentSchemeKey = "CurrentScheme";
|
||||
private const char Separator = '|';
|
||||
private const string DefaultSchemeName = "默认方案";
|
||||
private static readonly string ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "schemes.json");
|
||||
|
||||
/// <summary>
|
||||
/// 方案配置数据结构
|
||||
/// </summary>
|
||||
private class SchemeConfig
|
||||
{
|
||||
public List<string> Schemes { get; set; } = new List<string>();
|
||||
public string CurrentScheme { get; set; } = DefaultSchemeName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化配置(首次运行时调用)
|
||||
/// </summary>
|
||||
public static void Initialize()
|
||||
{
|
||||
// 如果Schemes不存在,创建空键
|
||||
if (!SystemConfigHelper.KeyExists(SchemesKey))
|
||||
if (!File.Exists(ConfigFilePath))
|
||||
{
|
||||
SystemConfigHelper.SetValue(SchemesKey, "");
|
||||
}
|
||||
|
||||
// 如果CurrentScheme不存在,创建空键
|
||||
if (!SystemConfigHelper.KeyExists(CurrentSchemeKey))
|
||||
{
|
||||
SystemConfigHelper.SetValue(CurrentSchemeKey, "");
|
||||
var defaultConfig = new SchemeConfig
|
||||
{
|
||||
Schemes = new List<string> { DefaultSchemeName },
|
||||
CurrentScheme = DefaultSchemeName
|
||||
};
|
||||
SaveConfig(defaultConfig);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取所有方案(自动处理空值)
|
||||
/// 获取所有方案
|
||||
/// </summary>
|
||||
public static List<string> GetAllSchemes()
|
||||
{
|
||||
var schemeString = SystemConfigHelper.GetValue(SchemesKey, "");
|
||||
return string.IsNullOrEmpty(schemeString)
|
||||
? new List<string>()
|
||||
: new List<string>(schemeString.Split(Separator));
|
||||
var config = LoadConfig();
|
||||
return config.Schemes ?? new List<string>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加新方案(自动初始化处理)
|
||||
/// 添加新方案
|
||||
/// </summary>
|
||||
public static void AddScheme(string schemeName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(schemeName))
|
||||
throw new ArgumentException("方案名称无效");
|
||||
|
||||
var schemes = GetAllSchemes();
|
||||
var config = LoadConfig();
|
||||
|
||||
if (schemes.Contains(schemeName))
|
||||
if (config.Schemes.Contains(schemeName))
|
||||
throw new InvalidOperationException($"方案 {schemeName} 已存在");
|
||||
|
||||
schemes.Add(schemeName);
|
||||
SaveSchemes(schemes);
|
||||
config.Schemes.Add(schemeName);
|
||||
SaveConfig(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 设置当前方案(空值安全处理)
|
||||
/// 设置当前方案
|
||||
/// </summary>
|
||||
public static void SetCurrentScheme(string schemeName)
|
||||
{
|
||||
var schemes = GetAllSchemes();
|
||||
var config = LoadConfig();
|
||||
|
||||
if (!schemes.Contains(schemeName))
|
||||
if (!config.Schemes.Contains(schemeName))
|
||||
throw new KeyNotFoundException($"方案 {schemeName} 不存在");
|
||||
|
||||
SystemConfigHelper.SetValue(CurrentSchemeKey, schemeName);
|
||||
config.CurrentScheme = schemeName;
|
||||
SaveConfig(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取当前方案(默认值处理)
|
||||
/// 获取当前方案
|
||||
/// </summary>
|
||||
public static string GetCurrentScheme()
|
||||
{
|
||||
var current = SystemConfigHelper.GetValue(CurrentSchemeKey, "");
|
||||
return !string.IsNullOrEmpty(current) ? current : "默认方案";
|
||||
var config = LoadConfig();
|
||||
return !string.IsNullOrEmpty(config.CurrentScheme)
|
||||
? config.CurrentScheme
|
||||
: DefaultSchemeName;
|
||||
}
|
||||
|
||||
private static void SaveSchemes(List<string> schemes)
|
||||
{
|
||||
var schemeString = schemes.Count > 0
|
||||
? string.Join(Separator.ToString(), schemes)
|
||||
: "";
|
||||
SystemConfigHelper.SetValue(SchemesKey, schemeString);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 删除指定方案(自动同步当前方案状态)
|
||||
/// 删除指定方案
|
||||
/// </summary>
|
||||
/// <param name="schemeName">要删除的方案名称</param>
|
||||
/// <exception cref="ArgumentException">当方案名称为空时抛出</exception>
|
||||
/// <exception cref="KeyNotFoundException">当方案不存在时抛出</exception>
|
||||
public static void DeleteScheme(string schemeName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(schemeName))
|
||||
throw new ArgumentException("方案名称无效");
|
||||
|
||||
var schemes = GetAllSchemes();
|
||||
var config = LoadConfig();
|
||||
|
||||
if (!schemes.Contains(schemeName))
|
||||
if (!config.Schemes.Contains(schemeName))
|
||||
throw new KeyNotFoundException($"方案 {schemeName} 不存在");
|
||||
|
||||
// 删除前检查是否是当前方案
|
||||
bool isCurrent = GetCurrentScheme() == schemeName;
|
||||
// 如果是当前方案,需要先切换
|
||||
if (config.CurrentScheme == schemeName)
|
||||
{
|
||||
var otherScheme = config.Schemes.FirstOrDefault(s => s != schemeName);
|
||||
if (otherScheme != null)
|
||||
{
|
||||
config.CurrentScheme = otherScheme;
|
||||
}
|
||||
else
|
||||
{
|
||||
config.CurrentScheme = DefaultSchemeName;
|
||||
if (!config.Schemes.Contains(DefaultSchemeName))
|
||||
{
|
||||
config.Schemes.Add(DefaultSchemeName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 执行删除操作
|
||||
schemes.Remove(schemeName);
|
||||
SaveSchemes(schemes);
|
||||
|
||||
config.Schemes.Remove(schemeName);
|
||||
SaveConfig(config);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加载配置文件
|
||||
/// </summary>
|
||||
private static SchemeConfig LoadConfig()
|
||||
{
|
||||
if (!File.Exists(ConfigFilePath))
|
||||
{
|
||||
Initialize();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(ConfigFilePath);
|
||||
return JsonConvert.DeserializeObject<SchemeConfig>(json) ?? new SchemeConfig();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 如果读取失败,返回默认配置
|
||||
return new SchemeConfig
|
||||
{
|
||||
Schemes = new List<string> { DefaultSchemeName },
|
||||
CurrentScheme = DefaultSchemeName
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存配置文件
|
||||
/// </summary>
|
||||
private static void SaveConfig(SchemeConfig config)
|
||||
{
|
||||
try
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(config, Formatting.Indented);
|
||||
File.WriteAllText(ConfigFilePath, json);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 处理保存失败的情况
|
||||
throw new InvalidOperationException("保存方案配置失败", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user