ningshuxia
2024-04-29 6eb4d5574aee7042b1883e043d5798865e84d1d1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System.Threading.Tasks;
using System.Xml.Linq;
 
namespace IStation.DAL
{
    /// <summary>
    ///   
    /// </summary>
    public class ScheduleCombine
    {
        public SqlSugarClient Connection
        {
            get { return ConfigHelper.GetSqlSugarClient(); }
        }
 
        /// <summary>
        /// 获取全部表名
        /// </summary>
        public List<string> GetAllTableName()
        {
            var tables = new List<string>();
            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;
        }
 
        /// <summary>
        /// 通过表名获取所有未分析组合
        /// </summary>
        public List<Entity.ScheduleCombine> GetAllUnanalyzedAnaByTableName(string tableName)
        {
            using (SqlSugarClient db = Connection)
            {
                var sql = $"select * from {tableName} where AnaStatus=False";
                var list = db.Ado.SqlQuery<Entity.ScheduleCombine>(sql);
                return list;
            }
        }
 
 
        /// <summary>
        /// 大批量插入
        /// </summary>
        public bool BulkInserts_SplitTable(List<Entity.ScheduleCombine> list)
        {
            if (list == null || list.Count < 1)
                return default;
            using (SqlSugarClient db = Connection)
            {
                ///自己来制定定义的规则
                db.CurrentConnectionConfig.ConfigureExternalServices.SplitTableService = new Entity.ScheduleCombineSubTableService();
                db.CodeFirst
                    .SplitTables()//标识分表
                    .InitTables<Entity.ScheduleCombine>(); //程序启动时加这一行,如果一张表没有会初始化一张
 
 
                //大数据写入+表不存在会建表
                //自动找表大数据写入 
                return db.Fastest<Entity.ScheduleCombine>().SplitTable().BulkCopy(list) > 0;
            }
        }
 
        /// <summary>
        /// 设置已分析状态
        /// </summary>
        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;
            }
        }
 
 
        /// <summary>
        /// 设置已分析状态
        /// </summary>
        public bool SetAnaStatus(string tableName, List<long> 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;
            }
        }
 
 
        /// <summary>
        /// 判断数据库是否存在表
        /// </summary> 
        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;
            }
        }
 
 
    }
}