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 GetWarningType(int? val) { List list = new List(); 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.SetValueListObject(dt); } return list; } public static List GetEnumList(Type enumType) { var pairs = new List(); 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 { /// /// 主键 /// public int Val { get; set; } /// /// 类型(1-报警 2-警告) /// public int Type { get; set; } /// /// 警告内容 /// public string Content { get; set; } /// /// 解决方法 /// public string Solution { get; set; } } public class Warning { /// /// 主键ID /// public int ID { get; set; } /// /// 类型 /// public int Type { get; set; } /// /// 报警时间 /// public DateTime Time { get; set; } /// /// 警报清除时间 /// 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 where T : new() { public static List SetValueListObject(DataTable dt) { if (dt == null || dt.Rows.Count <= 0) { return null; } List lst = new List(); 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; } } }