Files
zszq-trs/YLErpWeb/App/AppHelper.cs
T
2024-05-09 14:06:26 +08:00

193 lines
6.3 KiB
C#

using System.Collections.Specialized;
using YLErp.Web.App_Start.AppConfig;
namespace YLErp.Web
{
/// <summary>
/// 程序帮助类
/// </summary>
public static partial class AppHelper
{
/// <summary>
/// 全局锁定对象
/// </summary>
public static readonly object LockObj;
static readonly NameValueCollection _tradeRiskConfig;
static readonly NameValueCollection _futureVersionConfig;
private static List<MenuItem> _menus;
static AppHelper()
{
LockObj = new object();
_tradeRiskConfig = new NameValueCollection();
_futureVersionConfig = new NameValueCollection();
}
/// <summary>
/// 初始化配置
/// </summary>
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);
}
/// <summary>
/// 设置配置
/// </summary>
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<OtcAppConfig>(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;
}
/// <summary>
/// 获取配置字段属性
/// </summary>
public static IEnumerable<ConfigFieldAttributeDto> GetConfigFieldAttributes()
{
var props = typeof(OtcAppConfig).GetProperties();
var list = new List<ConfigFieldAttributeDto>(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<MenuItem> Menus => _menus ??= MenuHelper.GetMenus("Menus.txt");
public static void ResetMenus()
{
_menus = null;
}
//--------------------------------------
// 当前项目配置
//--------------------------------------
/// <summary>
///
/// </summary>
public static OtcAppConfig OtcConfig { get; private set; }
/// <summary>
/// 是否在当前系统中运行实时风险计算V2
/// </summary>
public static bool RunTradeRiskCalcV2 => _tradeRiskConfig["run"] == "true";
/// <summary>
/// 实时风险计算V2-视图显示模式
/// </summary>
public static string TradeRiskCalcV2ViewMode => _tradeRiskConfig["view"];
/// <summary>
/// 实时风险计算V2-支持波动率调整
/// </summary>
public static bool TradeRiskCalcV2VolAdjust => _tradeRiskConfig["voladjust"] == "1";
/// <summary>
/// 实时风险计算V2-支持Delta风险敞口
/// </summary>
public static bool TradeRiskCalcDeltaRisk => _tradeRiskConfig["deltarisk"] == "1";
/// <summary>
/// 实时风险计算V2-是否Index2显示股票
/// </summary>
public static bool ShowStockIndex2 => _tradeRiskConfig["stock"] == "1";
/// <summary>
/// 是否支持给定波动率类型的实时风险计算
/// </summary>
public static bool IsSupportVolTypeOnRiskCalc(string volType)
{
return !string.IsNullOrWhiteSpace(volType) && _tradeRiskConfig[volType] == "true";
}
//--------------------------------------
// 功能版本配置
//--------------------------------------
/// <summary>
/// 功能版本配置
/// </summary>
public static string GetFutureVersion(string futureKey)
{
if (string.IsNullOrEmpty(futureKey))
{
throw new ArgumentException("参数不能为空值", nameof(futureKey));
}
return _futureVersionConfig[futureKey].TrimToEmpty();
}
/// <summary>
/// 功能版本配置
/// </summary>
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);
}
}
}