using System.Threading.Tasks; using System.Xml.Linq; namespace IStation.DAL { /// /// /// public class ScheduleCombine { public SqlSugarClient Connection { get { return ConfigHelper.GetSqlSugarClient(); } } /// /// 获取全部表名 /// public List GetAllTableName() { var tables = new List(); using (var connection = new SQLiteConnection(ConfigHelper.ConnectionString)) { connection.Open(); string query = "SELECT name FROM sqlite_master WHERE type='table';"; using (var command = new SQLiteCommand(query, connection)) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { var tableName = reader["name"].ToString(); if (!tableName.Contains("_Cb_")) continue; tables.Add(tableName); } } } } return tables; } /// /// 通过表名获取所有未分析组合 /// public List GetAllUnanalyzedAnaByTableName(string tableName) { using (SqlSugarClient db = Connection) { var sql = $"select * from {tableName} where AnaStatus=False"; var list = db.Ado.SqlQuery(sql); return list; } } /// /// 大批量插入 /// public bool BulkInserts_SplitTable(List list) { if (list == null || list.Count < 1) return default; using (SqlSugarClient db = Connection) { ///自己来制定定义的规则 db.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService = new Entity.ScheduleCombineSubTableService(); db.CodeFirst .SplitTables()//标识分表 .InitTables(); //程序启动时加这一行,如果一张表没有会初始化一张 //大数据写入+表不存在会建表 //自动找表大数据写入 return db.Fastest().SplitTable().BulkCopy(list) > 0; } } /// /// 设置已分析状态 /// public bool SetAnaStatus(string tableName, long id) { using (SqlSugarClient db = Connection) { var sql = $"update {tableName} set AnaStatus=True where ID={id}"; var bol = db.Ado.ExecuteCommand(sql) > 0; return bol; } } /// /// 设置已分析状态 /// public bool SetAnaStatus(string tableName, List ids) { using (SqlSugarClient db = Connection) { db.BeginTran(); var sql = $"update {tableName} set AnaStatus=True where ID in(@ids)"; var obj = new { ids = ids.ToArray() }; var bol = db.Ado.ExecuteCommand(sql, obj) > 0; return bol; } } /// /// 判断数据库是否存在表 /// public bool ExistTable(string runFlag) { using (SqlSugarClient db = Connection) { var sql = $"select count(*) from sqlite_master where type = 'table' and name = 'ScheduleCombine_Cb_{runFlag}';"; var bol = db.Ado.ExecuteCommand(sql) > 0; return bol; } } } }