Shuxia Ning
2024-11-06 adf8dc1c7cae1b12f486dcdb3d7daf4a5a59ec52
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
 
namespace IStation.DAL
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AnalysisParameter  
    {
        /// <summary>
        /// 
        /// </summary>
        public ConnectionConfig ConnectionConfig
        {
            get
            { 
                var dbName = "analysis.db";
                var filePath = $"{FileHelper.GetDataFolder()}\\{dbName}";
                var config = new ConnectionConfig
                {
 
                    DbType = SqlSugar.DbType.Sqlite,//数据库类型
                    ConnectionString = $"DataSource={filePath}",
                    IsAutoCloseConnection = true,//是否自动关闭
                    MoreSettings = new ConnMoreSettings()
                    {
                        //PgSqlIsAutoToLower = false //数据库存在大写字段的 ,需要把这个设为false ,并且实体和字段名称要一样
                    },
                    AopEvents = new AopEvents
                    {
                        OnLogExecuting = (sql, p) =>
                        {
                            var sqlString = UtilMethods.GetNativeSql(sql, p); 
                        }
                    },
                    ConfigureExternalServices = new ConfigureExternalServices()
                    {
                        EntityService = (property, column) =>
                        {
                            //除主键外都可空
                            if (!column.IsPrimarykey)
                            {
                                column.IsNullable = true;
                            }
                        }
                    }
                };
                return config;
            }
 
        }
 
        /// <summary>
        /// 
        /// </summary>
        private string TableNamePrefix
        {
            get { return "AnalysisParameter_"; }
        }
 
        /// <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    PumpID  BIGINT,\r\n    Hz   REAL,\r\n    Head   REAL,\r\n    PressureDiff REAL,\r\n    Flow  REAL,\r\n    Power   REAL,\r\n    WP    REAL,\r\n    UWP REAL\r\n);";
            return sql;
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        private string GetExistTableSql(string tableName)
        {
            var sql = $"select count(*)  from sqlite_master where type = 'table' and name = '{tableName}';";
            return sql;
        }
 
 
        /// <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<Dto.AnalysisParameter>> GetAllTable()
        {
             
 
            var tableNames = GetAllTableName();
            if (tableNames == null || !tableNames.Any())
                return default;
 
            var dict = new Dictionary<string, List<Dto.AnalysisParameter>>();
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                foreach (var tableName in tableNames)
                {
                    var sql = $"select * from {tableName}";
                    var list = db.Queryable<Dto.AnalysisParameter>().AS(tableName).ToList();
                    if (list == null || !list.Any())
                        continue;
                    dict.Add(tableName, list);
                }
                return dict;
            }
        }
 
 
        /// <summary>
        /// 大批量插入(表不存在就新建)
        /// </summary>
        public bool BulkInsertsEx(string runFlag, List<Dto.AnalysisParameter> 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<Dto.AnalysisParameter>().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;
        }
 
 
        /// <summary>
        /// 查询
        /// </summary>
        public List<Dto.AnalysisParameter> GetList(string runFlag, double targetHead)
        {
            var tableName = GetTableName(runFlag);
            using (SqlSugarClient db = new SqlSugarClient(ConnectionConfig))
            {
                var sql = GetExistTableSql(tableName);
                if (db.Ado.GetInt(sql) < 1)
                {
                    return default;
                }
                var list = db.Queryable<Dto.AnalysisParameter>().AS(tableName)
                      .Where(x => x.Head == targetHead)
                      .OrderBy(x => x.Power)
                      .ToList();
                return list;
            }
        }
    }
}