/// <summary>
 /// 新增超限报警记录数据
 /// </summary>
 /// <param name="item">超限报警记录实体</param>        
 /// <returns>新增的数据行数</returns>
 public int AddOverLimitRecord(KPI_OverLimitRecordEntity item)
 {
     int Result = 0;
     string SqlText = @"Insert KPI_OverLimitRecord (AlarmID,TagID,AlarmStartTime,AlarmEndTime,
                         Duration,AlarmValue,StandardValue,Offset,AlarmType,Period,Shift,UnitID)
                         Values (@AlarmID,@TagID,@AlarmStartTime,@AlarmEndTime,@Duration,@AlarmValue,
                         @StandardValue,@Offset,@AlarmType,@Period,@Shift,@UnitID) ";
     IDbDataParameter AlarmIDParam = new SqlParameter("@AlarmID", item.AlarmID);
     IDbDataParameter TagIDParam = new SqlParameter("@TagID", item.TagID);
     IDbDataParameter AlarmStartTimeParam = new SqlParameter("@AlarmStartTime", item.AlarmStartTime);
     IDbDataParameter AlarmEndTimeParam = new SqlParameter("@AlarmEndTime", DBNull.Value);
     IDbDataParameter DurationParam = new SqlParameter("@Duration", DBNull.Value);
     IDbDataParameter AlarmValueParam = new SqlParameter("@AlarmValue", item.AlarmValue);
     IDbDataParameter StandardValueParam = new SqlParameter("@StandardValue", item.StandardValue);
     IDbDataParameter OffsetParam = new SqlParameter("@Offset", item.Offset);
     IDbDataParameter AlarmTypeParam = new SqlParameter("@AlarmType", item.AlarmType);
     IDbDataParameter PeriodParam = new SqlParameter("@Period", item.Period);
     IDbDataParameter ShiftParam = new SqlParameter("@Shift", item.Shift);
     IDbDataParameter UnitIDParam = new SqlParameter("@UnitID", item.UnitID);
     //RelaInterface DB = DBAccess.GetRelation();
     try {
         Result = m_DB.ExecuteNonQuery(SqlText, AlarmIDParam, TagIDParam, AlarmStartTimeParam,
             AlarmEndTimeParam, DurationParam, AlarmValueParam, StandardValueParam, OffsetParam,
             AlarmTypeParam, PeriodParam, ShiftParam, UnitIDParam);
     }
     catch (Exception ex) {
         throw ex;
     }
     return Result;
 }
 public bool ExistsOverLimitRecord(KPI_OverLimitRecordEntity item)
 {
     bool Result = false;
     string SqlText = @"SELECT TagID FROM KPI_OverLimitRecord
                        WHERE (TagID=@TagID AND AlarmType=@AlarmType)  AND (AlarmEndTime IS NULL)";
     IDbDataParameter TagIDParam = new SqlParameter("@TagID", item.TagID);
     IDbDataParameter AlarmTypeParam = new SqlParameter("@AlarmType", item.AlarmType);
     //RelaInterface DB = DBAccess.GetRelation();
     try {
         Object obj = m_DB.ExecuteScalar(SqlText, TagIDParam, AlarmTypeParam);
         Result = obj != null;
     }
     catch (Exception ex) {
         throw ex;
     }
     return Result;
 }
        /// <summary>
        /// 启动报警,向KPI_OverLimitRecord表中添加数据
        /// </summary>
        /// <param name="faultConfiguration">报警配置信息</param>
        /// <param name="alarmType">报警类型 1=超高限,2=超高高限,3=超更高</param>
        /// <param name="alarmTime">开始报警时间</param>
        /// <param name="maxValue">超标极值</param>
        /// <param name="standardValue">标准值</param>
        protected void AddOrUpdateOverLimitRecordToDB(OverLimitConfigEntity faultConfiguration, int alarmType,
            DateTime alarmTime, double standardValue, double maxValue)
        {
            try
            {
                KPI_OverLimitRecordEntity entity = new KPI_OverLimitRecordEntity();
                entity.AlarmID = Guid.NewGuid() + "";
                entity.UnitID = faultConfiguration.UnitID;
                entity.TagID = faultConfiguration.TagName;
                entity.AlarmType = alarmType;
                entity.AlarmStartTime = alarmTime;
                entity.StandardValue = Convert.ToDecimal(standardValue);
                entity.Offset = Convert.ToDecimal(maxValue - standardValue);
                entity.Duration = 0;
                entity.AlarmValue = Convert.ToDecimal(maxValue);
                String Period = "";
                String Shift = "";
                GetShiftAndPeriod(faultConfiguration.UnitID, out Shift, out Period, alarmTime);
                entity.Shift = Shift;
                entity.Period = Period;

                using (KPI_OverLimitRecordDal DataAccess = new KPI_OverLimitRecordDal())
                {
                    if (DataAccess.ExistsOverLimitRecord(entity))
                    {
                        DataAccess.UpdateAlarmMaxValue(entity);
                        return;
                    }
                    DataAccess.AddOverLimitRecord(entity);
                }
            }
            catch (Exception ex)
            {
                m_Logger.InfoFormat("AddOrUpdateOverLimitRecordToDB安全指标超限报警计算错误,错误信息是:{0},调用栈信息是:{1}",
                    ex.Message, ex.StackTrace);
                m_Logger.Error(ex.Message, ex);
            }
        }
 /// <summary>
 /// 解除报警,更新KPI_OverLimitRecord表中数据
 /// </summary>
 /// <param name="faultConfiguration">报警配置信息</param>
 /// <param name="alarmType">报警类型 1=超高限,2=超高高限,3=超更高;</param>
 /// <param name="alarmTime">解除报警时间</param>
 protected void UpdateExistOverLimitRecordToDB(OverLimitConfigEntity faultConfiguration, int alarmType, DateTime alarmTime)
 {
     try
     {
         KPI_OverLimitRecordEntity entity = new KPI_OverLimitRecordEntity();
         entity.TagID = faultConfiguration.TagName;
         entity.AlarmEndTime = alarmTime;
         entity.AlarmType = alarmType;
         using (KPI_OverLimitRecordDal DataAccess = new KPI_OverLimitRecordDal())
         {
             DataAccess.UpdateOverLimitRecord(entity);
         }
     }
     catch (Exception ex)
     {
         m_Logger.InfoFormat("UpdateExistOverLimitRecordToDB安全指标超限报警计算错误,错误信息是:{0},调用栈信息是:{1}",
             ex.Message, ex.StackTrace);
         m_Logger.Error(ex.Message, ex);
     }
 }
 /// <summary>
 /// 更新超限报警记录数据
 /// </summary>
 /// <param name="item">超限报警记录实体</param>        
 /// <returns>更新的数据行数</returns>
 public int UpdateOverLimitRecord(KPI_OverLimitRecordEntity item)
 {
     int Result = 0;
     String SqlText = @"IF EXISTS(SELECT TAGID FROM KPI_OverLimitRecord 	Where (TagID=@TagID AND AlarmType=@AlarmType)  AND (AlarmEndTime IS NULL))
                       BEGIN
                         Update KPI_OverLimitRecord Set AlarmEndTime=@AlarmEndTime,
                                                         Duration=DATEDIFF(s,AlarmStartTime,@AlarmEndTime)
                                                         Where (TagID=@TagID AND AlarmType=@AlarmType)  AND (AlarmEndTime IS NULL)
                       END";
     IDbDataParameter[] parames = new SqlParameter[] {
         new SqlParameter("@AlarmEndTime",DbType.DateTime),
         new SqlParameter("@TagID",DbType.String),
         new SqlParameter("@AlarmType",DbType.String)};
     parames[0].Value = item.AlarmEndTime;
     parames[1].Value = item.TagID;
     parames[2].Value = item.AlarmType;
     //RelaInterface DB = DBAccess.GetRelation();
     try {
         Result = m_DB.ExecuteNonQuery(SqlText, parames);
     }
     catch (Exception ex) {
         throw ex;
     }
     return Result;
 }
 /// <summary>
 /// 更新报警时间段内最大值
 /// </summary>
 /// <param name="item"></param>
 public void UpdateAlarmMaxValue(KPI_OverLimitRecordEntity item)
 {
     String SqlText = @"Update KPI_OverLimitRecord Set AlarmValue=@AlarmValue,Offset= @AlarmValue - StandardValue
                         WHERE (TagID=@TagID AND AlarmType=@AlarmType) AND (AlarmEndTime IS NULL) AND (AlarmValue<@AlarmValue) ";
     IDbDataParameter[] parames = new SqlParameter[] {
         new SqlParameter("@AlarmValue",DbType.Decimal),
         new SqlParameter("@TagID",DbType.String),
         new SqlParameter("@AlarmType",DbType.String)};
     parames[0].Value = item.AlarmValue;
     parames[1].Value = item.TagID;
     parames[2].Value = item.AlarmType;
     try {
         m_DB.ExecuteNonQuery(SqlText, parames);
     }
     catch (Exception ex) {
         throw ex;
     }
 }
 /// <summary>
 /// 交接时关闭上一班报警
 /// </summary>
 /// <param name="faultConfiguration"></param>
 private void StopLastShiftAlarm(OverLimitConfigEntity faultConfiguration)
 {
     KPI_UnitEntity Entity = KPI_UnitDal.GetEntity(faultConfiguration.UnitID);
     if (Entity == null) return;
     String strWorkID = Entity.WorkID;
     String strStartTime = "";
     String strEndTime = "";
     String Shift = "";
     String Period = "";
     String strCurrentMinute = DateTime.Now.ToString("yyyy-MM-dd HH:mm:00");
     KPI_WorkDal.GetShiftAndPeriod(strWorkID, strCurrentMinute,
         ref Shift, ref Period, ref strStartTime, ref strEndTime);
     DateTime ShiftStartTime = Convert.ToDateTime(strStartTime);
     DateTime CurrentTime = DateTime.Now;
     TimeSpan Span = CurrentTime - ShiftStartTime;
     double TotalMinutes = Span.TotalMinutes;
     if (TotalMinutes <= 10) {
         try {
             using (KPI_OverLimitRecordDal DataAccess = new KPI_OverLimitRecordDal()) {
                 KPI_OverLimitRecordEntity entity = new KPI_OverLimitRecordEntity();
                 for (int i = 1; i < 4; i++) {
                     entity.TagID = faultConfiguration.TagName;
                     entity.AlarmStartTime = ShiftStartTime;
                     entity.AlarmType = i;
                     DataAccess.UpdateOverLimitRecord(entity);
                 }
             }
         }
         catch (Exception ex) {
             LogUtil.LogMessage(string.Format("安全指标超限报警计算错误,错误信息是:{0},调用栈信息是:{1}",
                 ex.Message, ex.StackTrace));
         }
     }
 }
 /// <summary>
 /// 解除报警,更新KPI_OverLimitRecord表中数据
 /// </summary>
 /// <param name="faultConfiguration">报警配置信息</param>
 /// <param name="alarmType">报警类型 1=超高限,2=超高高限,3=超更高;</param>
 /// <param name="alarmTime">解除报警时间</param>
 private void StopAlarm(OverLimitConfigEntity faultConfiguration, int alarmType, DateTime alarmTime)
 {
     try {
         KPI_OverLimitRecordEntity entity = new KPI_OverLimitRecordEntity();
         entity.TagID = faultConfiguration.TagName;
         entity.AlarmStartTime = alarmTime;
         entity.AlarmType = alarmType;
         using (KPI_OverLimitRecordDal DataAccess = new KPI_OverLimitRecordDal()) {
             DataAccess.UpdateOverLimitRecord(entity);
         }
     }
     catch (Exception ex) {
         LogUtil.LogMessage(string.Format("安全指标超限报警计算错误,错误信息是:{0},调用栈信息是:{1}",
             ex.Message, ex.StackTrace));
     }
 }
        /// <summary>
        /// 启动报警,向KPI_OverLimitRecord表中添加数据
        /// </summary>
        /// <param name="faultConfiguration">报警配置信息</param>
        /// <param name="alarmType">报警类型 1=超高限,2=超高高限,3=超更高</param>
        /// <param name="alarmTime">开始报警时间</param>
        /// <param name="maxValue">超标极值</param>
        /// <param name="standardValue">标准值</param>
        private void StartAlarm(OverLimitConfigEntity faultConfiguration, int alarmType,
			DateTime alarmTime, double standardValue, double maxValue)
        {
            //Console.WriteLine(String.Format("测点{0}的报警类型是:{1}报警值是:{2}", faultConfiguration.TagDesc, alarmType, maxValue));
            try {
                KPI_OverLimitRecordEntity entity = new KPI_OverLimitRecordEntity();
                entity.AlarmID = Guid.NewGuid() + "";
                entity.UnitID = faultConfiguration.UnitID;
                entity.TagID = faultConfiguration.TagName;
                entity.AlarmType = alarmType;
                entity.AlarmStartTime = alarmTime;
                entity.StandardValue = Convert.ToDecimal(standardValue);
                entity.Offset = Convert.ToDecimal(maxValue - standardValue);
                entity.Duration = 0;
                entity.AlarmValue = Convert.ToDecimal(maxValue);
                String Period = "";
                String Shift = "";
                GetShiftAndPeriod(faultConfiguration.UnitID, out Shift, out Period);
                entity.Shift = Shift;
                entity.Period = Period;
                using (KPI_OverLimitRecordDal DataAccess = new KPI_OverLimitRecordDal()) {
                    if (DataAccess.ExistsOverLimitRecord(entity)) return;
                    DataAccess.AddOverLimitRecord(entity);
                }
            }
            catch (Exception ex) {
                LogUtil.LogMessage(string.Format("安全指标超限报警计算错误,错误信息是:{0},调用栈信息是:{1}",
                    ex.Message, ex.StackTrace));
            }
        }