ningshuxia
2024-05-28 3805d90be3760e4881e30f28c22018f0e04b0eb2
IStation.Service/00-core/ConfigHelper.cs
@@ -1,6 +1,9 @@
namespace IStation
{
    internal class ConfigHelper
    /// <summary>
    ///
    /// </summary>
    public class ConfigHelper
    {
        #region PostgreSql-ConnectionConfig
@@ -69,6 +72,33 @@
        #region SQLite-ConnectionConfig
        /// <summary>
        ///
        /// </summary>
        /// <param name="dbFile"></param>
        /// <returns></returns>
        private static string GetConnectionString(string dbFile)
        {
            var filePath = Settings.ParasHelper.LocalFile.DataFolderDirectory + "\\" + dbFile;
            return $"DataSource={filePath}";
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="dbFile"></param>
        /// <returns></returns>
        public static string DeleteDataBase(string dbFile)
        {
            var filePath = Settings.ParasHelper.LocalFile.DataFolderDirectory + "\\" + dbFile;
            if (File.Exists(filePath))
                File.Delete(filePath);
            return filePath;
        }
        /// <summary>
        /// Analysis SQLite连接配置
        /// </summary>
        internal static ConnectionConfig AnalysisConnectionConfig
@@ -79,7 +109,7 @@
                return new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.Sqlite,//数据库类型
                    ConnectionString = Settings.ParasHelper.DataBase.SQLite.AnalysisConnectString,
                    ConnectionString = GetConnectionString(Settings.ParasHelper.DataBase.SQLite.AnalysisConnectString),
                    IsAutoCloseConnection = true,//是否自动关闭
                    MoreSettings = new ConnMoreSettings()
                    {
@@ -109,7 +139,7 @@
                return new ConnectionConfig()
                {
                    DbType = SqlSugar.DbType.Sqlite,//数据库类型
                    ConnectionString = Settings.ParasHelper.DataBase.SQLite.ScheduleConnectString,
                    ConnectionString = GetConnectionString(Settings.ParasHelper.DataBase.SQLite.ScheduleConnectString),
                    IsAutoCloseConnection = true,//是否自动关闭
                    MoreSettings = new ConnMoreSettings()
                    {
@@ -128,6 +158,54 @@
            }
        }
        /// <summary>
        /// 初始化 Analysis 数据库
        /// </summary>
        public static void InitAnalysisDB(bool backup = false)
        {
            DeleteDataBase(Settings.ParasHelper.DataBase.SQLite.AnalysisConnectString);
            var sqlSugarClient = new SqlSugarClient(AnalysisConnectionConfig);
            Type[] types = new Type[] {
                       typeof(Entity.AnalysisLog),
                       //typeof(Entity.AnalysisCombine),
                       //typeof(Entity.AnalysisConclusion),
                   };
            sqlSugarClient.DbMaintenance.CreateDatabase();
            if (backup)
            {
                sqlSugarClient.CodeFirst.BackupTable().InitTables(types);
            }
            else
            {
                sqlSugarClient.CodeFirst.InitTables(types);
            }
        }
        /// <summary>
        /// 初始化 Schedule 数据库
        /// </summary>
        public static void InitScheduleDB(bool backup = false)
        {
            var sqlSugarClient = new SqlSugarClient(ScheduleConnectionConfig);
            Type[] types = new Type[] {
                       typeof(Entity.ScheduleRequest),
                       typeof(Entity.ScheduleScada),
                       typeof(Entity.ScheduleRule),
                       typeof(Entity.SchedulePump),
                       typeof(Entity.ScheduleConclusion),
                   };
            sqlSugarClient.DbMaintenance.CreateDatabase();
            if (backup)
            {
                sqlSugarClient.CodeFirst.BackupTable().InitTables(types);
            }
            else
            {
                sqlSugarClient.CodeFirst.InitTables(types);
            }
        }
        #endregion
        #region SQLite-Sql