ningshuxia
2024-04-30 d63b83c2c745c1f8a140221c28c0e6dbe61d6110
Schedule/IStation.Algorithm/DAL/ScheduleConclusion.cs
@@ -1,7 +1,4 @@
using MathNet.Numerics.Distributions;
using System.Collections.Generic;
namespace IStation.DAL
namespace IStation.DAL
{
    /// <summary>
    ///   
@@ -19,6 +16,8 @@
        {
            return $"{_tableNamePrefix}{runFlag}";
        }
        #region Table
        /// <summary>
        /// 获取全部表名
@@ -53,11 +52,10 @@
            using (SqlSugarClient db = Connection)
            {
                var sql = $"select count(*)  from sqlite_master where type = 'table' and name = '{GetTableName(runFlag)}';";
                var bol = db.Ado.ExecuteCommand(sql) > 0;
                var bol = db.Ado.GetInt(sql) > 0;
                return bol;
            }
        }
        /// <summary>
        /// 数据库创建表
@@ -68,7 +66,7 @@
            {
                var tableName = GetTableName(runFlag);
                var sql_exist = $"select count(*)  from sqlite_master where type = 'table' and name = '{tableName}';";
                if (db.Ado.ExecuteCommand(sql_exist) > 0)
                if (db.Ado.GetInt(sql_exist) > 0)
                    return true;
                var sql_create_table = $"CREATE TABLE  {tableName}  (\r\n   ID BIGINT  NOT NULL\r\n PRIMARY KEY,\r\n    ScheduleCombineID BIGINT,\r\n   RunFlag    VARCHAR (255),\r\n   Pump1   REAL,\r\n   Pump2   REAL,\r\n   Pump3   REAL,\r\n   Head   REAL,\r\n    Flow    REAL,\r\n   Power  REAL,\r\n    WP  REAL,\r\n   UWP    REAL\r\n);";
                var bol = db.Ado.ExecuteCommand(sql_create_table) > 0;
@@ -76,7 +74,70 @@
            }
        }
        /// <summary>
        //  删除所有表
        /// </summary>
        public bool DeleteAllTable()
        {
            var tables = GetAllTableName();
            if (tables == null || !tables.Any())
                return false;
            using (SqlSugarClient db = Connection)
            {
                db.BeginTran();
                foreach (var table in tables)
                {
                    var sql = $"drop table {table};";
                    db.Ado.ExecuteCommand(sql);
                }
                db.CommitTran();
            }
            return true;
        }
        #endregion
        #region BulkInserts
        /// <summary>
        /// 大批量插入
        /// </summary>
        public bool BulkInserts(string runFlag, List<Entity.ScheduleConclusion> list)
        {
            if (list == null || list.Count < 1)
                return default;
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = Connection)
            {
                return db.Fastest<Entity.ScheduleConclusion>().AS(tableName).BulkCopy(list) > 0;
            }
        }
        /// <summary>
        /// 大批量插入
        /// </summary>
        public async Task<bool> BulkInserts_Create_Async(string runFlag, List<Entity.ScheduleConclusion> list)
        {
            if (list == null || list.Count < 1)
                return default;
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = Connection)
            {
                var exist_sql = $"select count(*)  from sqlite_master where type = 'table' and name = '{tableName}' ;";
                if (db.Ado.GetInt(exist_sql) < 1)
                {
                    var sql_create_table = $"CREATE TABLE  {tableName}  (\r\n   ID BIGINT  NOT NULL\r\n PRIMARY KEY,\r\n    ScheduleCombineID BIGINT,\r\n   RunFlag    VARCHAR (255),\r\n   Pump1   REAL,\r\n   Pump2   REAL,\r\n   Pump3   REAL,\r\n   Head   REAL,\r\n    Flow    REAL,\r\n   Power  REAL,\r\n    WP  REAL,\r\n   UWP    REAL\r\n);";
                    if (db.Ado.ExecuteCommand(sql_create_table) < 1)
                    {
                        return false;
                    }
                }
                return await db.Fastest<Entity.ScheduleConclusion>().AS(tableName).BulkCopyAsync(list) > 0;
            }
        }
        /// <summary>
        /// 大批量插入
@@ -102,7 +163,7 @@
        /// <summary>
        /// 大批量插入
        /// </summary>
        public bool BulkInserts_SplitTable_Create(string runFlag, List<Entity.ScheduleConclusion> list)
        public bool BulkInserts_Create(string runFlag, List<Entity.ScheduleConclusion> list)
        {
            if (list == null || list.Count < 1)
                return default;
@@ -113,30 +174,12 @@
                if (db.Ado.GetInt(exist_sql) < 1)
                {
                    var sql_create_table = $"CREATE TABLE  {tableName}  (\r\n   ID BIGINT  NOT NULL\r\n PRIMARY KEY,\r\n    ScheduleCombineID BIGINT,\r\n   RunFlag    VARCHAR (255),\r\n   Pump1   REAL,\r\n   Pump2   REAL,\r\n   Pump3   REAL,\r\n   Head   REAL,\r\n    Flow    REAL,\r\n   Power  REAL,\r\n    WP  REAL,\r\n   UWP    REAL\r\n);";
                    if (db.Ado.ExecuteCommand(sql_create_table) < 1)
                    {
                        return false;
                    }
                    db.Ado.ExecuteCommand(sql_create_table);
                }
                //大数据写入
                return db.Fastest<Entity.ScheduleConclusion>().AS(tableName).BulkCopy(list) > 0;
            }
        }
        /// <summary>
        /// 大批量插入
        /// </summary>
        public bool BulkInserts(string runFlag, List<Entity.ScheduleConclusion> list)
        {
            if (list == null || list.Count < 1)
                return default;
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = Connection)
            {
                return db.Fastest<Entity.ScheduleConclusion>().AS(tableName).BulkCopy(list) > 0;
            }
        }
        /// <summary>
        /// 大批量插入
@@ -176,28 +219,53 @@
            return true;
        }
        #endregion
        /// <summary>
        /// 获取全部表名
        /// 查询
        /// </summary>
        public bool DeleteAllTable()
        public List<Entity.ScheduleConclusion> GetList(string runFlag, double targetHead)
        {
            var tables = GetAllTableName();
            if (tables == null || !tables.Any())
                return false;
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = Connection)
            {
                db.BeginTran();
                foreach (var table in tables)
                var sql = $"select count(*)  from sqlite_master where type = 'table' and name = '{tableName}';";
                if (db.Ado.GetInt(sql) < 1)
                {
                    var sql = $"drop table {table};";
                    db.Ado.ExecuteCommand(sql);
                    return default;
                }
                db.CommitTran();
                var list = db.Queryable<Entity.ScheduleConclusion>().AS(tableName)
                      .Where(x => x.Head == targetHead)
                      .OrderBy(x => x.Power)
                      .ToList();
                return list;
            }
            return true;
        }
        /// <summary>
        /// 查询
        /// </summary>
        public List<Entity.ScheduleConclusion> GetList(string runFlag, double targetFlow, double targetHead, int takeCount)
        {
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = Connection)
            {
                var sql = $"select count(*)  from sqlite_master where type = 'table' and name = '{tableName}';";
                if (db.Ado.GetInt(sql) < 1)
                {
                    return default;
                }
                var list = db.Queryable<Entity.ScheduleConclusion>().AS(tableName)
                      .Where(x => x.Head >= targetHead && x.Head <= targetHead * 1.01 && x.Flow > targetFlow)
                      .OrderBy(x => x.Power)
                      .Take(takeCount)
                      .ToList();
                return list;
            }
        }
    }