using System.Collections.Specialized; using YLErp.Web.App_Start.AppConfig; namespace YLErp.Web { /// /// 程序帮助类 /// public static partial class AppHelper { /// /// 全局锁定对象 /// public static readonly object LockObj; static readonly NameValueCollection _tradeRiskConfig; static readonly NameValueCollection _futureVersionConfig; private static List _menus; static AppHelper() { LockObj = new object(); _tradeRiskConfig = new NameValueCollection(); _futureVersionConfig = new NameValueCollection(); } /// /// 初始化配置 /// public static void InitConfig() { if (OtcConfig != null) { throw new InvalidOperationException(); } string configJson = null; var configFilePath = Server.MapPath("~/App_Data/Config/Config.json"); if (File.Exists(configFilePath)) { configJson = File.ReadAllText(configFilePath); } ReadConfig(configJson); } /// /// 设置配置 /// public static void ResetConfig(string configJson) { if (string.IsNullOrWhiteSpace(configJson)) { configJson = "{}"; } var configFilePath = Server.MapPath("~/App_Data/Config/Config.json"); Directory.CreateDirectory(Path.GetDirectoryName(configFilePath)); File.WriteAllText(configFilePath, configJson); ReadConfig(configJson); } //读写配置 private static void ReadConfig(string configJson) { OtcConfig = JsonHelper.Deserialize(configJson) ?? new OtcAppConfig(); var emptyNv = new NameValueCollection(); var nv = YieldChain.Helpers.UrlHelper.ParseQueryString(OtcConfig.TradeRiskCalcV2) ?? emptyNv; var keys = nv.AllKeys.Concat(_tradeRiskConfig.AllKeys).ToHashSet(StringComparer.OrdinalIgnoreCase); foreach (var k in keys) { _tradeRiskConfig[k] = nv[k].TrimToEmpty(); } nv = YieldChain.Helpers.UrlHelper.ParseQueryString(OtcConfig.FutureVersion) ?? emptyNv; foreach (var k in nv.AllKeys) { _futureVersionConfig[k] = nv[k].TrimToEmpty().ToUpperInvariant(); } OtcConfig.SetReadOnly(); OtcAppConfigHelper.HedgingSource= OtcConfig.HedgingSource; OtcAppConfigHelper.HTUpdate= OtcConfig.HTUpdate; } /// /// 获取配置字段属性 /// public static IEnumerable GetConfigFieldAttributes() { var props = typeof(OtcAppConfig).GetProperties(); var list = new List(props.Length); foreach (var p in props) { var attrs = p.GetCustomAttributes(typeof(ConfigFieldAttribute), false); if (attrs != null && attrs.Length > 0) { var attr = (ConfigFieldAttribute)attrs[0]; var dto = new ConfigFieldAttributeDto(attr) { FieldName = p.Name, FieldType = p.PropertyType, FieldValue = p.GetValue(OtcConfig)?.ToString() }; list.Add(dto); } } return list; } public static List Menus => _menus ??= MenuHelper.GetMenus("Menus.txt"); public static void ResetMenus() { _menus = null; } //-------------------------------------- // 当前项目配置 //-------------------------------------- /// /// /// public static OtcAppConfig OtcConfig { get; private set; } /// /// 是否在当前系统中运行实时风险计算V2 /// public static bool RunTradeRiskCalcV2 => _tradeRiskConfig["run"] == "true"; /// /// 实时风险计算V2-视图显示模式 /// public static string TradeRiskCalcV2ViewMode => _tradeRiskConfig["view"]; /// /// 实时风险计算V2-支持波动率调整 /// public static bool TradeRiskCalcV2VolAdjust => _tradeRiskConfig["voladjust"] == "1"; /// /// 实时风险计算V2-支持Delta风险敞口 /// public static bool TradeRiskCalcDeltaRisk => _tradeRiskConfig["deltarisk"] == "1"; /// /// 实时风险计算V2-是否Index2显示股票 /// public static bool ShowStockIndex2 => _tradeRiskConfig["stock"] == "1"; /// /// 是否支持给定波动率类型的实时风险计算 /// public static bool IsSupportVolTypeOnRiskCalc(string volType) { return !string.IsNullOrWhiteSpace(volType) && _tradeRiskConfig[volType] == "true"; } //-------------------------------------- // 功能版本配置 //-------------------------------------- /// /// 功能版本配置 /// public static string GetFutureVersion(string futureKey) { if (string.IsNullOrEmpty(futureKey)) { throw new ArgumentException("参数不能为空值", nameof(futureKey)); } return _futureVersionConfig[futureKey].TrimToEmpty(); } /// /// 功能版本配置 /// public static bool UseFutureVersion(string futureKey, string version) { if (string.IsNullOrEmpty(futureKey)) { throw new ArgumentException("参数不能为空值", nameof(futureKey)); } return _futureVersionConfig[futureKey].TrimToEmpty().Equals(version, StringComparison.OrdinalIgnoreCase); } } }