using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ICSharpCode.Core;
using Mesnac.Codd.Session;
using Mesnac.Action.ChemicalWeighing.Entity;
using DevExpress.Office.Utils;
using Mesnac.Action.ChemicalWeighing.Entity.PptPlan;
namespace Mesnac.Action.ChemicalWeighing.Alarm
{
///
/// 报警管理辅助类
///
public class AlarmHelper
{
#region 报警参数
#region 判断报警代码是否存在
///
/// 判断报警代码是否存在
///
/// 要判断的报警PLC
/// 要判断的报警代码
/// 如果存在返回true,否则返回false
public static bool IsExistsAlarmCode(string alarmcode)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_ID = @AlarmCode";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@AlarmCode", alarmcode);
object result = dbHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
int intResult = 0;
if (int.TryParse(result.ToString(), out intResult))
{
if (intResult > 0)
{
return true;
}
}
}
return false;
}
#endregion
#region 判断报警名称是否存在
///
/// 判断报警名称是否存在
///
/// 要判断的报警PLC
/// 要判断的报警名称
/// 如果存在返回true,否则返回false
public static bool IsExistsAlarmName(string alarmPLC, string alarmName)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_PLC = @AlarmPLC and Alarm_Cn_Info = @AlarmName";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@AlarmPLC", alarmPLC);
dbHelper.AddParameter("@AlarmName", alarmName);
object result = dbHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
int intResult = 0;
if (int.TryParse(result.ToString(), out intResult))
{
if (intResult > 0)
{
return true;
}
}
}
return false;
}
#endregion
#region 判断报警数据块是否存在
///
/// 判断报警数据块是否存在
///
/// 要判断的报警数据块
/// 如果存在返回true,否则返回false
public static bool IsExistsAlarmBlock(string alarmblock)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_Block = @AlarmBlock";
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@AlarmBlock", alarmblock);
object result = dbHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
int intResult = 0;
if (int.TryParse(result.ToString(), out intResult))
{
if (intResult > 0)
{
return true;
}
}
}
return false;
}
#endregion
#region 判断对应的报警地址和位是否存在
///
/// 判断对应的报警地址和位是否存在
///
/// 报警地址
/// 报警位
/// 存在返回true,否则返回false
public static bool IsExistsAlarmAddressAndBit(string alarmAddress, string alarmBit)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select count(Alarm_ID) from Pmt_Alarm where Alarm_Alias = @Alarm_Alias";
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@AlarmAddress", alarmAddress);
dbHelper.AddParameter("@AlarmBit", alarmBit);
object result = dbHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
int intResult = 0;
if (int.TryParse(result.ToString(), out intResult))
{
if (intResult > 0)
{
return true;
}
}
}
return false;
}
#endregion
#region 判断对应的报警参数是否存在
///
/// 判断对应的报警参数是否存在
///
/// 报警PLC
/// 报警地址块
/// 报警地址字
/// 报警地址位
/// 存在返回true,不存在返回false
public static bool IsExists(int ID,string alarmPLC, string alarmBlock, string alarm_Alias)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select count(ID) from Pmt_Alarm where ID<>@ID and Alarm_PLC = @AlarmPLC and Alarm_Block = @AlarmBlock and Alarm_Alias = @Alarm_Alias";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@ID", ID);
dbHelper.AddParameter("@AlarmPLC", alarmPLC);
dbHelper.AddParameter("@AlarmBlock", alarmBlock);
dbHelper.AddParameter("@Alarm_Alias", alarm_Alias);
object result = dbHelper.ToScalar();
if (result != null && result != System.DBNull.Value)
{
int intResult = 0;
if (int.TryParse(result.ToString(), out intResult))
{
if (intResult > 0)
{
return true;
}
}
}
return false;
}
#endregion
#region 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数
///
/// 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数
///
/// 报警参数PLC
/// 报警参数数据块
/// 报警参数地址字
/// 报警参数位
/// 返回对应的报警参数数据行
public static DataRow GetAlarmInfoDataRow(string alarmPLC, string alarmBlock, int Alarm_Alias)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select * from Pmt_Alarm where Alarm_PLC = @AlarmPLC and Alarm_Alias = @Alarm_Alias";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@AlarmPLC", alarmPLC);
dbHelper.AddParameter("@AlarmBlock", alarmBlock);
dbHelper.AddParameter("@Alarm_Alias", Alarm_Alias);
DataTable dt = dbHelper.ToDataTable();
if (dt != null && dt.Rows.Count > 0)
{
return dt.Rows[0];
}
return null;
}
///
/// 根据报警参数数据块、报警参数地址字和报警参数数据位查询报警参数
///
/// 报警参数PLC
/// 报警参数数据块
/// 报警参数地址字
/// 报警参数位
/// 返回对应的报警参数实体
public static Entity.PmtAlarmInfo GetAlarmInfoEntity(string alarmPLC, string alarmBlock, int Alarm_Alias)
{
DataRow dr = GetAlarmInfoDataRow(alarmPLC, alarmBlock, Alarm_Alias);
Entity.PmtAlarmInfo entity = ConvertDataRowToPmtAlarmInfo(dr);
return entity;
}
public static Entity.PmtAlarmInfo GetAlarmInfoEntity(string ID)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select * from Pmt_Alarm where ID = @ID";
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@ID", ID);
DataTable dt = dbHelper.ToDataTable();
if (dt != null && dt.Rows.Count > 0)
{
return ConvertDataRowToPmtAlarmInfo(dt.Rows[0]);
}
return null;
}
#endregion
#region 查询所有报警参数
///
/// 查询所有报警参数
///
/// 返回报警参数数据表
public static DataTable GetPmtAlarmInfoTable()
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select * from Pmt_Alarm order by Alarm_PLC, Alarm_Block, Alarm_Alias";
DataTable dt = dbHelper.GetDataTableBySql(sqlstr);
return dt;
}
///
/// 查询所有报警参数
///
/// 返回报警参数实体类列表
public static List GetPmtAlarmInfoList()
{
List lst = new List();
DataTable dt = GetPmtAlarmInfoTable();
if (dt != null)
{
foreach(DataRow dr in dt.Rows)
{
Entity.PmtAlarmInfo entity = ConvertDataRowToPmtAlarmInfo(dr);
lst.Add(entity);
}
}
return lst;
}
#endregion
#region 分类查询
///
/// 分类查询
///
/// 返回分类查询数据表
public static DataTable GetBlockStartLengthTable()
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = "select Alarm_PLC, Alarm_Block, MIN(Alarm_Word) as Start, (MAX(Alarm_Word) - MIN(Alarm_Word) + 1) as Length from Pmt_Alarm group by Alarm_PLC, Alarm_Block";
DataTable dt = dbHelper.GetDataTableBySql(sqlstr);
return dt;
}
///
/// 分类查询
///
/// 返回分类查询实体列表
public static List GetBlockStartLengthList()
{
List list = new List();
DataTable dt = GetBlockStartLengthTable();
if (dt != null)
{
foreach(DataRow dr in dt.Rows)
{
Entity.AlarmInfo entity = ConvertDataRowToAlarmInfo(dr);
list.Add(entity);
}
}
return list;
}
#endregion
#region 把数据行转换为报警参数实体对象
///
/// 把数据行转换为报警参数实体对象
///
/// 报警参数数据行
/// 返回对应的报警参数实体对象
public static Entity.PmtAlarmInfo ConvertDataRowToPmtAlarmInfo(DataRow dr)
{
if (dr != null)
{
Entity.PmtAlarmInfo entity = new Entity.PmtAlarmInfo();
entity.ID = Mesnac.Basic.DataProcessor.RowValue(dr, "ID", 0);
entity.Equip_Code = Mesnac.Basic.DataProcessor.RowValue(dr, "Equip_Code", String.Empty);
entity.Alarm_ID = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_ID", 0);
entity.AlarmPLC = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_PLC", String.Empty);
entity.AlarmBlock = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Block", String.Empty);
entity.AlarmAlias = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Alias", String.Empty);
entity.Alarm_Cn_Info = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Cn_Info", String.Empty);
return entity;
}
return null;
}
///
/// 把数据行转换为报警参数扩展实体对象
///
/// 数据行
/// 返回对应的报警参数实体对象
public static Entity.AlarmInfo ConvertDataRowToAlarmInfo(DataRow dr)
{
if (dr != null)
{
Entity.AlarmInfo entity = new AlarmInfo();
entity.AlarmPLC = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_PLC", String.Empty);
entity.AlarmBlock = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Block", String.Empty);
entity.Start = Mesnac.Basic.DataProcessor.RowValue(dr, "Start", 0);
entity.Length = Mesnac.Basic.DataProcessor.RowValue(dr, "Length", 0);
return entity;
}
return null;
}
#endregion
#endregion
#region 报警记录
#region 查询报警记录
///
/// 查询报警记录
///
/// 报警记录实体数据
public static DataTable QueryAlarmLogData()
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = @"select * from LR_Alarmlog where Alarm_Status=1";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
DataTable table = dbHelper.ToDataTable();
if (table != null && table.Rows.Count > 0)
{
return table;
}
return null;
}
///
/// 查询报警记录
///
/// 报警记录实体数据
public static List QueryAlarmLog()
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
List list = new List();
string sqlstr = @"select a.Alarm_PLC,a.Alarm_Block,a.Alarm_Alias,a.Alarm_Cn_Info,l.Alarm_OccurTime from Pmt_Alarm a inner join LR_Alarmlog l on a.Alarm_ID=l.Alarm_ID where Alarm_Status=1";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
DataTable table = dbHelper.ToDataTable();
if (table != null && table.Rows.Count > 0)
{
SimpleAlarmInfo entity = new SimpleAlarmInfo();
foreach (DataRow row in table.Rows)
{
entity = ConvertDataRowToAlarm(row);
list.Add(entity);
}
}
return null;
}
public static DataTable QueryAlarmLogDataTable()
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = @"select ROW_NUMBER() over(order by a.Alarm_ID) Alarm_ID,a.Alarm_PLC,a.Alarm_Block,a.Alarm_Alias,a.Alarm_Cn_Info,l.Alarm_OccurTime from Pmt_Alarm a inner join LR_Alarmlog l on a.Alarm_ID=l.Alarm_ID where Alarm_Status=1";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
DataTable table = dbHelper.ToDataTable();
if (table != null && table.Rows.Count > 0)
{
return table;
}
return null;
}
public static bool IsExist(int ID)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
SimpleAlarmInfo entity = new SimpleAlarmInfo();
string sqlstr = @"select a.Alarm_PLC,a.Alarm_Block,a.Alarm_Alias,a.Alarm_Cn_Info,l.Alarm_OccurTime from Pmt_Alarm a inner join LR_Alarmlog l on a.Alarm_ID=l.Alarm_ID where a.Alarm_ID=@ID";
dbHelper.CommandText = sqlstr;
dbHelper.ClearParameter();
dbHelper.AddParameter("@ID", ID);
DataTable table = dbHelper.ToDataTable();
if (table != null && table.Rows.Count > 0)
{
foreach (DataRow row in table.Rows)
{
entity = ConvertDataRowToAlarm(row);
break;
}
return true;
}
return false;
}
private static SimpleAlarmInfo ConvertDataRowToAlarm(DataRow dr)
{
if (dr != null)
{
SimpleAlarmInfo alarm = new SimpleAlarmInfo();
alarm.Alarm_ID = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_ID",0);
alarm.AlramPLC = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_PLC", String.Empty);
alarm.AlarmBlock = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Block", String.Empty);
alarm.Alarm_Alias = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Alias", String.Empty);
alarm.Alarm_Cn = Mesnac.Basic.DataProcessor.RowValue(dr, "Alarm_Cn_Info", String.Empty);
return alarm;
}
else
{
return null;
}
}
#endregion
#region 添加报警记录
///
/// 添加报警记录
///
/// 报警记录实体数据
public static void InsertAlarmLogData(Entity.LR_Alarmlog alarmData)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = @"insert into LR_Alarmlog(Equip_Code,Alarm_ID,Alarm_OccurTime,Alarm_ClearTime,Alarm_Status,Alarm_GUID)
values(@Equip_Code,@Alarm_ID,@Alarm_OccurTime,@Alarm_ClearTime,@Alarm_Status,@Alarm_GUID)";
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@Equip_Code", alarmData.Equip_Code);
dbHelper.AddParameter("@Alarm_ID", alarmData.Alarm_ID);
dbHelper.AddParameter("@Alarm_OccurTime", alarmData.Alarm_OccurTime);
dbHelper.AddParameter("@Alarm_ClearTime", alarmData.Alarm_ClearTime);
dbHelper.AddParameter("@Alarm_Status", alarmData.Alarm_Status);
dbHelper.AddParameter("@Alarm_GUID", alarmData.Alarm_GUID);
dbHelper.ExecuteNonQuery();
}
#endregion
#region 更新报警记录
///
/// 更新报警记录
///
/// 报警记录ID
/// 报警清除时间
public static void UpdateAlarmLogData(int alarmID,string clearTime)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = @"update LR_Alarmlog set Alarm_Status = 0,Alarm_ClearTime = @Alarm_ClearTime where ID = @Alarm_ID";
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@Alarm_ID", alarmID);
dbHelper.AddParameter("@Alarm_ClearTime", clearTime);
dbHelper.ExecuteNonQuery();
}
#endregion
#endregion
#region 更新报警设置
///
/// 更新报警记录
///
/// 报警记录ID
/// 报警清除时间
public static void UpdateAlarmData(PmtAlarmInfo alarm)
{
DbHelper dbHelper = Mesnac.Basic.DataSourceFactory.Instance.GetDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType.Local);
if (dbHelper == null)
{
throw new Exception(Mesnac.Basic.LanguageHelper.DataBaseConnectError);
}
string sqlstr = @"update Pmt_Alarm set Alarm_PLC = @Alarm_PLC,Alarm_Block = @Alarm_Block,Alarm_Alias=@Alarm_Alias,Alarm_Cn_Info=@Alarm_Cn_Info where ID = @ID";
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.Text;
dbHelper.CommandText = sqlstr;
dbHelper.AddParameter("@Alarm_PLC", alarm.AlarmPLC);
dbHelper.AddParameter("@Alarm_Block", alarm.AlarmBlock);
dbHelper.AddParameter("@Alarm_Alias", alarm.AlarmAlias);
dbHelper.AddParameter("@Alarm_Cn_Info", alarm.Alarm_Cn_Info);
dbHelper.AddParameter("@ID", alarm.ID);
dbHelper.ExecuteNonQuery();
}
#endregion
}
}