patrick.xu
2021-05-24 c585dd8ddbbdc5dece1033bd6a1201493ed610a0
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
 
namespace M423project
{
    public class WarningData : IDisposable
    {
        private SqlConnection connection;
        public WarningData()
        {
            string connStr = ConfigurationManager.AppSettings["connectionString"];
            connection = new SqlConnection(connStr);
            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                CommonUtil.WriteLog(LogType.Err, "连接数据库失败!");
            }
        }
 
        public void Dispose()
        {
            connection.Close();
        }
 
        public void SaveWarning(int type)
        {
            lock (this)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"INSERT INTO Warning (Type,Time) VALUES('{0}','{1}')",
                    type, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
 
        public void UpdateWarning()
        {
            lock (this)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendFormat(@"UPDATE Warning SET ClearTime='{0}' WHERE ID=(SELECT TOP 1 ID FROM Warning ORDER BY TIME DESC) AND ClearTime IS NULL", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
                using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
                {
                    cmd.ExecuteNonQuery();
                }
            }
        }
 
        public List<WarningConfigure> GetWarningType(int? val)
        {
            List<WarningConfigure> list = new List<WarningConfigure>();
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"SELECT * FROM WarningConfigure WHERE 1=1");
            if (val != null)
            {
                sql.AppendFormat(@" AND Val={0}", val.Value);
            }
            DataTable dt = new DataTable();
            using (SqlCommand cmd = new SqlCommand(sql.ToString(), connection))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                dt.Load(dr);
            }
            if (dt.Rows.Count > 0)
            {
                list = DataConvertFill<WarningConfigure>.SetValueListObject(dt);
            }
            return list;
        }
 
        public static List<EnumEntity> GetEnumList(Type enumType)
        {
            var pairs = new List<EnumEntity>();
            var values = Enum.GetValues(enumType);
            foreach (var value in values)
            {
                var name = Enum.GetName(enumType, value);
                var field = enumType.GetField(name);
                var attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
                pairs.Add(new EnumEntity()
                {
                    Name = name,
                    Value = Convert.ToInt32(value),
                    Description = attribute == null ? null : attribute.Description
                });
            }
 
            return pairs;
        }
 
        public void CleanData()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(@"delete from Warning
                            where DATEDIFF(DAY,Time,GETDATE())>40");
            using (SqlCommand cmd = new SqlCommand(sb.ToString(), connection))
            {
                cmd.ExecuteNonQuery();
            }
 
        }
    }
 
    public class WarningConfigure
    {
        /// <summary>
        /// 主键
        /// </summary>
        public int Val { get; set; }
 
        /// <summary>
        /// 类型(1-报警 2-警告)
        /// </summary>
        public int Type { get; set; }
 
        /// <summary>
        /// 警告内容
        /// </summary>
        public string Content { get; set; }
 
        /// <summary>
        /// 解决方法
        /// </summary>
        public string Solution { get; set; }
    }
 
    public class Warning
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        public int ID { get; set; }
 
        /// <summary>
        /// 类型
        /// </summary>
        public int Type { get; set; }
 
        /// <summary>
        /// 报警时间
        /// </summary>
        public DateTime Time { get; set; }
 
        /// <summary>
        /// 警报清除时间
        /// </summary>
        public DateTime? ClearTime { get; set; }
    }
 
    public class ComboboxItem
    {
        public int Key { get; set; }
 
        public string Value { get; set; }
    }
 
    public class EnumEntity
    {
        public string Name { get; set; }
 
        public int Value { get; set; }
 
        public string Description { get; set; }
 
        public override string ToString()
        {
            return Description;
        }
    }
 
    public class DataConvertFill<T> where T : new()
    {
        public static List<T> SetValueListObject(DataTable dt)
        {
            if (dt == null || dt.Rows.Count <= 0)
            {
                return null;
            }
            List<T> lst = new List<T>();
            foreach (DataRow dr in dt.Rows)
            {
                T t = new T();
                System.Reflection.PropertyInfo property1 = t.GetType().GetProperty("SerialNumber",
                 BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                if (null != property1)
                {
                    property1.SetValue(t, lst.Count + 1, null);
                }
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    System.Reflection.PropertyInfo property = t.GetType().GetProperty(dt.Columns[i].ColumnName,
                        BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
                    if (null == property)
                        continue;
                    switch (property.PropertyType.ToString())
                    {
                        case "System.Int16":
                        case "System.Nullable`1[System.Int16]":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? Int16.MinValue : Convert.ToInt16(dr[i].ToString()), null);
                            break;
                        case "System.Int32":
                        case "System.Nullable`1[System.Int32]":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? Int32.MinValue : Convert.ToInt32(dr[i].ToString()), null);
 
                            break;
                        case "System.Int64":
                        case "System.Nullable`1[System.Int64]":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? Int64.MinValue : (string.IsNullOrEmpty(Convert.ToString(dr[i])) ? 0 : Convert.ToInt64(dr[i])), null);
                            break;
                        case "System.Decimal":
                        case "System.Nullable`1[System.Decimal]":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? 0 : Convert.ToDecimal(dr[i]), null);
                            break;
                        case "System.DateTime":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? default(DateTime) : Convert.ToDateTime(dr[i]), null);
                            break;
                        case "System.Boolean":
                        case "bool":
                        case "System.Nullable`1[System.Boolean]":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? false : Convert.ToBoolean(dr[i]), null);
                            break;
                        case "System.Nullable`1[System.DateTime]":
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? DateTime.MinValue : Convert.ToDateTime(dr[i]), null);
                            break;
                        default:
                            property.SetValue(t, (dr[i] is System.DBNull || string.IsNullOrEmpty(dr[i].ToString())) ? string.Empty : dr[i], null);
                            break;
                    }
                }
                lst.Add(t);
            }
            return lst;
        }
    }
}