288 lines
9.1 KiB
C#
288 lines
9.1 KiB
C#
using Autofac;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.Collections.Concurrent;
|
|
using System.Diagnostics;
|
|
using YLErp.BLL;
|
|
using YLErp.Commons;
|
|
using YLErp.Models;
|
|
using YLErp.Modules.AppModule;
|
|
|
|
namespace YLErp
|
|
{
|
|
public static class AppManager
|
|
{
|
|
static IContainer _iocContainer;
|
|
static readonly ConcurrentDictionary<string, SysInfo> _sysInfoDic;
|
|
static IConfiguration _configuration;
|
|
|
|
static AppManager()
|
|
{
|
|
_sysInfoDic = new ConcurrentDictionary<string, SysInfo>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
YLAutoMapper.Initialize();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 程序版本
|
|
/// </summary>
|
|
public static Version Version => new(5, 5, 0, 20230329);
|
|
|
|
/// <summary>
|
|
/// 插件信息
|
|
/// </summary>
|
|
public static PluginInfo PluginInfo { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 系统开始时间
|
|
/// </summary>
|
|
public static DateTime StartTime { get; } = DateTime.Now;
|
|
|
|
/// <summary>
|
|
/// 邮件发送配置
|
|
/// </summary>
|
|
public static SmtpConfigExt SmtpConfig { get; private set; } = new SmtpConfigExt();
|
|
|
|
/// <summary>
|
|
/// 子系统类型
|
|
/// </summary>
|
|
public static Enums.SubSystemName SubSystem { get; private set; }
|
|
|
|
/// <summary>
|
|
/// 系统初始化(日志|日历|数据映射|数据保护|系统升级|插件加载)
|
|
/// </summary>
|
|
public static void Initialize(Enums.SubSystemName subSystem, IConfiguration configuration)
|
|
{
|
|
SubSystem = subSystem;
|
|
|
|
_configuration = configuration;
|
|
|
|
//注册gb2312编码
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
void setEnvironment(string[] rootKeys, string pathSperator = ":")
|
|
{
|
|
foreach (var rootKey in rootKeys)
|
|
{
|
|
var root = configuration.GetSection(rootKey);
|
|
|
|
foreach (var c in root.GetChildren())
|
|
{
|
|
var key = string.Concat(rootKey, pathSperator, c.Key);
|
|
Environment.SetEnvironmentVariable(key, c.Value?.Trim());
|
|
}
|
|
}
|
|
}
|
|
|
|
setEnvironment(new[] { "ConnectionStrings", "AppSettings" });
|
|
setEnvironment(new[] { "LibreOffice" }, "_");
|
|
setEnvironment(new[] { "KafkaConfig" }, "_");
|
|
setEnvironment(new[] { "BondOmsInterface" }, "_");
|
|
setEnvironment(new[] { "OrcaleDatabaseConfig" }, "_");
|
|
Environment.SetEnvironmentVariable("Reg_Report_Url", configuration["Reg_Report_Url"]);
|
|
//用于必须引入程序集System.Numerics.Vectors
|
|
_ = System.Numerics.Vector<double>.Zero;
|
|
|
|
//初始化
|
|
|
|
InitializeSetting setting = null;
|
|
|
|
switch (subSystem)
|
|
{
|
|
case Enums.SubSystemName.OtcWeb:
|
|
setting = new InitializeSetting { UpgradeSystem = true, LoadPlugin = true };
|
|
break;
|
|
case Enums.SubSystemName.PluginTest:
|
|
setting = new InitializeSetting { UpgradeSystem = false, LoadPlugin = true };
|
|
break;
|
|
default:
|
|
setting = new InitializeSetting { UpgradeSystem = false, LoadPlugin = false };
|
|
break;
|
|
}
|
|
|
|
//初始化配置
|
|
if (setting == null)
|
|
{
|
|
setting = new InitializeSetting();
|
|
}
|
|
|
|
//初始化QDP日历
|
|
LogFactory.GetLogger(nameof(AppManager)).Info("初始化日历数据");
|
|
CalendarBLL.ResetCalendarForQdp();
|
|
|
|
//AppInitializer
|
|
|
|
var initializer = new AppInitializer();
|
|
|
|
//升级系统
|
|
if (setting.UpgradeSystem)
|
|
{
|
|
initializer.UpgradSystem(Version);
|
|
}
|
|
|
|
//初始化配置
|
|
initializer.InitializePsConfig(new SmtpConfigHandler(SmtpConfig));
|
|
|
|
// 初始化Redis配置
|
|
initializer.InitialRedisConfig();
|
|
|
|
//otcformat
|
|
if (subSystem == Enums.SubSystemName.OtcWeb)
|
|
{
|
|
var otcformat = initializer.GetOtcFormatConfig();
|
|
var destFolder = OtcAppContext.MapPath("/App_Data/config");
|
|
Directory.CreateDirectory(destFolder);
|
|
var fmtFilePath = Path.Combine(destFolder, "otcformat.js");
|
|
if (string.IsNullOrEmpty(otcformat))
|
|
{
|
|
otcformat = File.ReadAllText(OtcAppContext.MapPath("/Scripts/init/otcformat.js"));
|
|
File.WriteAllText(fmtFilePath, otcformat);
|
|
}
|
|
else
|
|
{
|
|
OtcFormatHelper.Initialize(otcformat);
|
|
|
|
File.WriteAllText(fmtFilePath, $"var main = main || {{}};\r\nmain.formatOptions={otcformat};");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
var otcformat = initializer.GetOtcFormatConfig();
|
|
OtcFormatHelper.Initialize(otcformat);
|
|
}
|
|
|
|
//系统交易日期
|
|
valuedateBLL.ResetValueDate();
|
|
|
|
//载入插件
|
|
if (setting.LoadPlugin)
|
|
{
|
|
PluginInfo = AppInitializer.InitializePlugin(out _iocContainer);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取App配置
|
|
/// </summary>
|
|
public static string GetConnectionString(string dbname)
|
|
{
|
|
return dbname switch
|
|
{
|
|
"ylcms" => _configuration.GetConnectionString("ylcms"),
|
|
"yladmin" => _configuration.GetConnectionString("yladmin"),
|
|
"ylclient" => _configuration.GetConnectionString("ylclient"),
|
|
"bondoms" => _configuration.GetConnectionString("bondoms"),
|
|
"apex_oracle"=> _configuration.GetConnectionString("apex_oracle"),
|
|
_ => string.Empty,
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取配置对象
|
|
/// </summary>
|
|
public static IConfiguration GetConfiguration()
|
|
{
|
|
return _configuration;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解析IOC容器中的实现(当前主要用于插件)
|
|
/// </summary>
|
|
public static bool TryResolve<TService>(out TService service) where TService : class
|
|
{
|
|
service = default;
|
|
|
|
if (_iocContainer == null || !typeof(TService).IsInterface)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
using (var scope = _iocContainer.BeginLifetimeScope())
|
|
{
|
|
return scope.TryResolve(out service);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置系统配置
|
|
/// </summary>
|
|
public static void ResetAppConfig()
|
|
{
|
|
new AppInitializer().InitializePsConfig(new SmtpConfigHandler(SmtpConfig));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 重置邮件发送配置
|
|
/// </summary>
|
|
public static void ResetSmtpConfig(SmtpConfigExt newSmtpConfig)
|
|
{
|
|
new SmtpConfigHandler(SmtpConfig).ResetConfig(newSmtpConfig, true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 设置系统信息
|
|
/// </summary>
|
|
public static void SetSysInfo(string key, string message)
|
|
{
|
|
Trace.WriteLine(message, key ?? "SetSysInfo");
|
|
|
|
if (key is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_sysInfoDic.AddOrUpdate(key
|
|
, k => new SysInfo
|
|
{
|
|
Key = key,
|
|
Message = message,
|
|
TimeStamp = DateTime.Now
|
|
}
|
|
, (k, v) =>
|
|
{
|
|
v.Message = message;
|
|
v.TimeStamp = DateTime.Now;
|
|
return v;
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取系统信息
|
|
/// </summary>
|
|
public static IEnumerable<SysInfo> GetSysInfo()
|
|
{
|
|
return _sysInfoDic.Values.OrderBy(n => n.Key).ToArray();
|
|
}
|
|
|
|
public static string GetSwapMonitorValue()
|
|
{
|
|
return new AppInitializer().GetConfigValue("MonitorConfig", "SwapConfig");
|
|
}
|
|
|
|
public static void SetSwapMonitorValue(string value)
|
|
{
|
|
new AppInitializer().SetConfigValue("MonitorConfig", "SwapConfig", value,"互换预付金监控预警设置");
|
|
}
|
|
|
|
public static string GetAppConfigValue(string group ,string key)
|
|
{
|
|
return new AppInitializer().GetConfigValue(group, key);
|
|
}
|
|
public static void SetAppConfigValue(string group, string key,string value,string remark)
|
|
{
|
|
new AppInitializer().SetConfigValue(group, key, value, remark);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 系统信息
|
|
/// </summary>
|
|
public class SysInfo
|
|
{
|
|
public string Key { get; set; }
|
|
|
|
public string Message { get; set; }
|
|
|
|
public DateTime TimeStamp { get; set; } = DateTime.Now;
|
|
}
|
|
}
|