ningshuxia
2024-04-30 b50a06a3185619c4cbbc521ab026f1bfa4d13aae
Schedule/IStation.Algorithm/DAL/ScheduleConclusion.cs
@@ -17,6 +17,8 @@
            return $"{_tableNamePrefix}{runFlag}";
        }
        #region Table
        /// <summary>
        /// 获取全部表名
        /// </summary>
@@ -50,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>
        /// 数据库创建表
@@ -65,13 +66,41 @@
            {
                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;
                return bol;
            }
        }
        /// <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>
        /// 大批量插入
@@ -131,7 +160,6 @@
            }
        }
        /// <summary>
        /// 大批量插入
        /// </summary>
@@ -152,8 +180,6 @@
                return db.Fastest<Entity.ScheduleConclusion>().AS(tableName).BulkCopy(list) > 0;
            }
        }
        /// <summary>
        /// 大批量插入
@@ -193,28 +219,32 @@
            return true;
        }
        #endregion
        /// <summary>
        /// 获取全部表名
        /// 大批量插入
        /// </summary>
        public bool DeleteAllTable()
        public List<Entity.ScheduleConclusion> GetList(string runFlag, double targetFlow, double targetHead, int takeCount)
        {
            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 && x.Head <= targetHead * 1.01 && x.Flow > targetFlow)
                      .OrderBy(x => x.Power)
                      .Take(takeCount)
                      .ToList();
                return list;
            }
            return true;
        }
    }