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;
|
}
|
}
|
}
|