using YLErp.Modules.AppModule; using YLErp.Models; namespace YLErp.Modules.SwapModule { public static class SwapMarginTemplateConfigService { public const string ConfigName = "Trade.SwapMarginTemplateConfig"; public const string DefaultConfigJson = @"{ ""options"": [ { ""text"": ""授信保证金"", ""value"": ""授信保证金"" }, { ""text"": ""现金保证金"", ""value"": ""现金保证金"" } ], ""defaultValue"": ""现金保证金"" }"; public static SwapMarginTemplateConfig GetConfig() { var json = AppManager.GetAppConfigValue("ProjectConfig", ConfigName); return TryParse(json, out var config, out _) ? config : GetDefaultConfig(); } public static bool TryValidate(string json, out string error) { return TryParse(json, out _, out error); } private static SwapMarginTemplateConfig GetDefaultConfig() { return JsonHelper.Deserialize(DefaultConfigJson); } private static bool TryParse(string json, out SwapMarginTemplateConfig config, out string error) { config = null; error = null; try { config = JsonHelper.Deserialize(json); } catch (Exception) { error = "保证金模板配置不是有效的 JSON"; return false; } var items = config?.options? .Where(item => item != null && !string.IsNullOrWhiteSpace(item.Value)) .ToArray(); if (items == null || items.Length == 0) { error = "保证金模板配置至少需要一个有效选项"; return false; } if (items.Select(item => item.Value).Distinct(StringComparer.OrdinalIgnoreCase).Count() != items.Length) { error = "保证金模板配置的选项值不能重复"; return false; } var defaultValue = config.defaultValue; if (!items.Any(item => item.Value == defaultValue)) { error = "保证金模板默认值必须在选项中"; return false; } config.options = items; return true; } } public class SwapMarginTemplateConfig { public IEnumerable options { get; set; } public string defaultValue { get; set; } } }