117 lines
3.2 KiB
C#
117 lines
3.2 KiB
C#
using Newtonsoft.Json.Linq;
|
|
using Newtonsoft.Json;
|
|
using WcsMain.Common;
|
|
using SqlSugar;
|
|
using WcsMain.AppEntity.SystemData;
|
|
|
|
namespace WcsMain.StartAction;
|
|
|
|
/// <summary>
|
|
/// WCS 程序启动前加载运行数据 ---- 链式调用,返回错误信息
|
|
/// </summary>
|
|
public class LoadingRunningData
|
|
{
|
|
/// <summary>
|
|
/// 错误信息
|
|
/// </summary>
|
|
private static string _errMsg = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 是否是开发环境
|
|
/// </summary>
|
|
private static bool _isDevelopment = true;
|
|
|
|
/// <summary>
|
|
/// 加载系统运行数据数据
|
|
/// </summary>
|
|
/// <param name="isDevelopment"></param>
|
|
public static void Loading(bool isDevelopment)
|
|
{
|
|
_isDevelopment = isDevelopment;// 将开发环境暴露到此类全局
|
|
|
|
LoadingAppSetting(); // 加载 Appsettings.json 文件里的设置项
|
|
LoadingWcsDataBase(); // 加载 Wcs 数据库连接字符串
|
|
|
|
// 加载数据库中的
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 返回加载结果
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static string? GetResult() => _errMsg;
|
|
|
|
|
|
/// <summary>
|
|
/// 加载 Appsettings.json 文件里的设置项
|
|
/// </summary>
|
|
public static void LoadingAppSetting()
|
|
{
|
|
if (!string.IsNullOrEmpty(_errMsg)) return;
|
|
ConsoleLog.Info("正在加载配置文件...");
|
|
string setStr;
|
|
try
|
|
{
|
|
using (StreamReader sr = new("appsettings.json"))
|
|
{
|
|
setStr = sr.ReadToEnd();
|
|
}
|
|
JObject jsonObj = JObject.Parse(setStr);
|
|
JObject? settinsOnj = (JObject?)jsonObj["Settings"];
|
|
if (settinsOnj == default)
|
|
{
|
|
_errMsg = "加载系统设置文件失败,请检查是否存在 Settings 键";
|
|
return;
|
|
}
|
|
CommonData.Settings = JsonConvert.DeserializeObject<AppSettingJsonEntity>(settinsOnj.ToString());
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
_ = ex;
|
|
_errMsg = "加载系统设置文件失败,请检查是否存在 appsettings.json 文件";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 加载数据库文件
|
|
/// </summary>
|
|
private static void LoadingWcsDataBase()
|
|
{
|
|
if (!string.IsNullOrEmpty(_errMsg)) return;
|
|
ConsoleLog.Info("正在加载数据库配置...");
|
|
string wcsDataBaseConnectString;
|
|
if (_isDevelopment)
|
|
{
|
|
if(string.IsNullOrEmpty(CommonData.Settings.DBMysqlLocal))
|
|
{
|
|
_errMsg = "调试环境下加载WCS数据库连接字符串失败";
|
|
return;
|
|
}
|
|
wcsDataBaseConnectString = CommonData.Settings.DBMysqlLocal;
|
|
}
|
|
else
|
|
{
|
|
if (string.IsNullOrEmpty(CommonData.Settings.DBMysql))
|
|
{
|
|
_errMsg = "运行环境下加载WCS数据库连接字符串失败";
|
|
return;
|
|
}
|
|
wcsDataBaseConnectString = CommonData.Settings.DBMysql;
|
|
}
|
|
CommonTool.DbServe = new SqlSugarScope(new ConnectionConfig()
|
|
{
|
|
IsAutoCloseConnection = true,
|
|
ConfigId = "0",
|
|
DbType = DbType.MySql,
|
|
ConnectionString = wcsDataBaseConnectString
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|