Shuxia Ning
2024-09-10 049f546f25cabfb5b08e29c54f49d61a544b0395
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
namespace IStation.DAL.SQLite
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AnalysisPump : Yw.DAL.SQLite.BaseDAL<Entity.AnalysisPump>, IAnalysisPump
    {
        /// <summary>
        /// 
        /// </summary>
        public override ConnectionConfig ConnectionConfig
        {
            get { return ConfigHelper.AnalysisConnectionConfig; }
        }
 
        /// <summary>
        /// 
        /// </summary>
        private string TableNamePrefix
        {
            get { return "AnalysisPump_"; }
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="runFlag"></param>
        /// <returns></returns>
        public string GetTableName(string runFlag)
        {
            return $"{this.TableNamePrefix}{runFlag}";
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private string GetCreateTableSql(string tableName)
        {
            var sql = $"CREATE TABLE {tableName}  (ID   BIGINT   NOT NULL\r\n   PRIMARY KEY,\r\n    Flag    INTEGER,\r\n    Hz  REAL,\r\n    CurveQH VARCHAR (255),\r\n    CurveQP VARCHAR (255),\r\n    MaxFlow REAL,\r\n    MinFlow REAL,\r\n    MaxHead REAL,\r\n    MinHead REAL\r\n);";
            return sql;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private string GetExistTableSql(string tableName)
        {
            return ConfigHelper.GetExistTableSql(tableName);
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public string GetRunFlag(string tableName)
        {
            return tableName.Replace(this.TableNamePrefix, null);
        }
 
        /// <summary>
        /// 获取全部表名
        /// </summary>
        public List<string> GetAllTableName()
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                var list = new List<string>();
                var sql = $"select name from sqlite_master where type='table' and name like '%{this.TableNamePrefix}%';";
                using (var reader = db.Ado.GetDataReader(sql))
                {
                    while (reader.Read())
                    {
                        var tableName = reader["name"].ToString(); 
                        list.Add(tableName);
                    }
                }
                return list;
            }
        }
 
        /// <summary>
        /// 获取全部表
        /// </summary> 
        public Dictionary<string, List<Entity.AnalysisPump>> GetAllTable()
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                var tableNames = new List<string>();
                var sql_sel_table = $"select name from sqlite_master where type='table' and name like '%{this.TableNamePrefix}%';";
                var reader = db.Ado.GetDataReader(sql_sel_table);
                while (reader.Read())
                {
                    var tableName = reader["name"].ToString();
                    tableNames.Add(tableName);
                }
                if (tableNames == null || !tableNames.Any())
                    return default;
 
                var dict = new Dictionary<string, List<Entity.AnalysisPump>>();
                foreach (var tableName in tableNames)
                {
                    var sql = $"select * from {tableName}";
                    var list = db.Ado.SqlQuery<Entity.AnalysisPump>(sql);
                    if (list == null || !list.Any())
                        continue;
                    dict.Add(tableName, list);
                }
                return dict;
            }
        }
 
        /// <summary>
        /// 通过表名获取全部
        /// </summary>
        public List<Entity.AnalysisPump> GetAllByTableName(string tableName)
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                var sql = $"select * from {tableName}";
                var list = db.Ado.SqlQuery<Entity.AnalysisPump>(sql);
                return list;
            }
        }
 
        /// <summary>
        /// 大批量插入(表不存在就新建)
        /// </summary>
        public bool BulkInsertsEx(string runFlag, List<Entity.AnalysisPump> list)
        {
            if (list == null || list.Count < 1)
                return default;
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                var exist_sql = GetExistTableSql(tableName);
                if (db.Ado.GetInt(exist_sql) < 1)
                {
                    var sql_create_table = GetCreateTableSql(tableName);
                    db.Ado.ExecuteCommand(sql_create_table);
                }
                //大数据写入
                return db.Fastest<Entity.AnalysisPump>().AS(tableName).BulkCopy(list) > 0;
            }
        }
 
         
 
        /// <summary>
        /// 判断数据库是否存在表
        /// </summary> 
        public bool ExistTable(string runFlag)
        {
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                var tableName = GetTableName(runFlag);
                var sql = GetExistTableSql(tableName);
                var bol = db.Ado.ExecuteCommand(sql) > 0;
                return bol;
            }
        }
 
        /// <summary>
        /// 删除所有表
        /// </summary>
        public bool DeleteAllTable()
        {
            var tables = GetAllTableName();
            if (tables == null || !tables.Any())
                return false;
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                db.BeginTran();
                foreach (var table in tables)
                {
                    var sql = $"drop table {table};";
                    db.Ado.ExecuteCommand(sql);
                }
                db.CommitTran();
            }
            return true;
        }
 
    }
}