private bool DeleteRecord() { try { string strsql = string.Format(@"select Id from personnelfirstinareas where Type=1"); List <int> list = db.PersonnelFirstInAreas.GetListIntBySql(strsql); if (list != null && list.Count > 0) { PersonnelFirstInArea person = db.PersonnelFirstInAreas.DeleteByKeys(list); } return(true); } catch (Exception ex) { Log.Error("SignInAlarmThread.DeleteRecord:" + ex.ToString()); return(false); } }
/// <summary> /// 产生告警 /// </summary> /// <param name="list1"></param> /// <returns></returns> private List <LocationAlarm> GenerateAreaAlarm(List <Position> list1) { List <LocationAlarm> newAlarmList = new List <LocationAlarm>(); if (list1 == null) { Log.Error(tag, "GenerateAreaAlarm", "list1 == null"); return(newAlarmList); } foreach (Position p in list1) { if (p == null || p.IsAreaNull() || p.PersonnelID == null || p.AreaId == null || p.IsDynamicAreaPos) { continue; } CardRole role = roles.Find(i => i.Id == p.RoleId); if (role == null) { int pAreaId = (int)p.AreaId; //RemoveDuplicateAlarms(p, pAreaId, null, "标签未配置区域权限。", LocationAlarmLevel.四级告警, ref newAlarmList); continue; } //清除人员进入区域记录 Bll db = Bll.NewBllNoRelation(); List <PersonnelFirstInArea> paList = db.PersonnelFirstInAreas.Where(i => i.personId == p.PersonnelID && i.type == 0); List <Area> areaList = p.Areas.ToList(); foreach (PersonnelFirstInArea personnelInArea in paList) { Area area = areaList.Find(i => i.Id == personnelInArea.areaId); if (area == null) { db.PersonnelFirstInAreas.DeleteById(personnelInArea.Id); } } string personDepartment = GetPersonDepartInfo(p.PersonnelID, p.Code); foreach (var area in p.Areas) { AreaAuthorizationRecord aar = aarList.Find(i => i.AreaId == area.Id && i.CardRoleId == role.Id); if (aar != null) { LocationAlarm la = realAlarms.Find(j => j.LocationCardId == p.CardId && j.AreaId == area.Id); DateTime dtBegin = p.DateTime; DateTime dtEnd = p.DateTime; if (la != null) { dtBegin = la.AlarmTime; } TimeSpan ts = dtEnd.Subtract(dtBegin).Duration(); int nTimeStamp = Convert.ToInt32(ts.TotalMinutes); if (aar.AccessType == AreaAccessType.能进入) { RemoveDuplicateAlarms(p, area.Id, null, string.Format("人员:{0},在区域'{1}'未配置权限。", personDepartment, area), LocationAlarmLevel.四级告警, ref newAlarmList); } else { DateTime startTime = new DateTime(p.DateTime.Year, p.DateTime.Month, p.DateTime.Day, aar.StartTime.Hour, aar.StartTime.Minute, aar.StartTime.Second); DateTime endTime = new DateTime(p.DateTime.Year, p.DateTime.Month, p.DateTime.Day, aar.EndTime.Hour, aar.EndTime.Minute, aar.EndTime.Second); if (aar.TimeType == TimeSettingType.无限制) { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList); } else if (aar.TimeType == TimeSettingType.时间长度) { //获取 PersonnelFirstInArea getPA = db.PersonnelFirstInAreas.Find(i => i.personId == p.PersonnelID && i.areaId == area.Id && i.type == 0); if (getPA == null) //不存在表示已经出了这个区域 { PersonnelFirstInArea person = new PersonnelFirstInArea(); DateTime nowTime = DateTime.Now; person.personId = p.PersonnelID; person.areaId = area.Id; person.dateTime = nowTime; person.type = 0; bool result = db.PersonnelFirstInAreas.Add(person); Log.Info("记录人员:" + p.PersonnelName + "进入区域:" + area.Name + "权限:时间长度,结果:" + result); RemoveDuplicateAlarms(p, area.Id, aar, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList, LocationAlarmType.超时告警); } else//告警 { if ((p.DateTime - getPA.dateTime).TotalMinutes > aar.TimeSpan)//超过时长 { string timeOut = ((p.DateTime - getPA.dateTime).TotalMinutes - aar.TimeSpan).ToString(); RemoveDuplicateAlarms(p, area.Id, aar, string.Format("人员:{0},在区域:{1}超时停留{2}分", personDepartment, area, timeOut), LocationAlarmLevel.四级告警, ref newAlarmList, LocationAlarmType.超时告警); } else { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList, LocationAlarmType.超时告警); } } } else if (aar.TimeType == TimeSettingType.时间点范围) { if (p.DateTime > startTime && p.DateTime < endTime) { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList, LocationAlarmType.超时告警); } else { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("告警,人员:{0} 在区域:{1}未设置权限", area), LocationAlarmLevel.四级告警, ref newAlarmList, LocationAlarmType.超时告警); } } else if (aar.TimeType == TimeSettingType.时间长度加范围) { if (p.DateTime > startTime && p.DateTime < endTime) //判断在时间范围内后,再判断时间长度 { PersonnelFirstInArea getPA = db.PersonnelFirstInAreas.Find(i => i.personId == p.PersonnelID && i.areaId == area.Id && i.type == 0); if (getPA == null) //不存在表示已经出了这个区域 { PersonnelFirstInArea person = new PersonnelFirstInArea(); DateTime nowTime = DateTime.Now; person.personId = p.PersonnelID; person.areaId = area.Id; person.dateTime = nowTime; person.type = 0; bool result = db.PersonnelFirstInAreas.Add(person); Log.Info("记录人员:" + p.PersonnelName + "进入区域:" + area.Name + "权限:时间长度加范围,结果:" + result); RemoveDuplicateAlarms(p, area.Id, aar, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList, LocationAlarmType.超时告警); } else//告警 { if ((p.DateTime - getPA.dateTime).TotalMinutes > aar.TimeSpan)//超过时长 { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("人员:{0},在区域:{1}未设置权限", personDepartment, area), LocationAlarmLevel.四级告警, ref newAlarmList, LocationAlarmType.超时告警); } else { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList, LocationAlarmType.超时告警); } } } else { RemoveDuplicateAlarms(p, area.Id, aar, string.Format("告警,人员:{0} 在区域:{1}未设置权限", area), LocationAlarmLevel.四级告警, ref newAlarmList); } } //if (aar.IsTimeValid(dtBegin, dtEnd, nTimeStamp) == false) //{ // RemoveDuplicateAlarms(p, area.Id, aar, string.Format("可以进入区域'{0}',但是未在有效时间范围内。", area), LocationAlarmLevel.四级告警, ref newAlarmList); //} //else } if (aar.SignIn)//有签到限制(添加一条人员进入区域的记录) { PersonnelFirstInArea getPA = db.PersonnelFirstInAreas.Find(i => i.personId == p.PersonnelID && i.areaId == area.Id && i.type == 1); if (getPA == null) { PersonnelFirstInArea person = new PersonnelFirstInArea(); DateTime nowTime = DateTime.Now; person.personId = p.PersonnelID; person.areaId = area.Id; person.dateTime = nowTime; person.type = 1; bool result = db.PersonnelFirstInAreas.Add(person); Log.Info("记录人员:" + p.PersonnelName + "进入区域:" + area.Name + "权限:签到,结果:" + result); } } } else { if (role.Id == 1 || role.Name == SuperUserName)//超级管理员 { RemoveDuplicateAlarms(p, area.Id, null, string.Format("正常,所在区域:{0}", area), LocationAlarmLevel.正常, ref newAlarmList); } else { RemoveDuplicateAlarms(p, area.Id, null, string.Format("人员:{0},在区域'{1}'未配置权限。", personDepartment, area), LocationAlarmLevel.四级告警, ref newAlarmList); } } } } return(newAlarmList); }
private List <LocationAlarm> GetAlarmList() { List <LocationAlarm> list = new List <LocationAlarm>(); try { List <Personnel> personnelList = db.Personnels.ToList(); List <Area> areaList = db.Areas.ToList(); List <AreaAuthorizationRecord> aarList = db.AreaAuthorizationRecords.FindAll(i => i.SignIn == true); foreach (Personnel person in personnelList) { string strsql = string.Format(@"select a.* from cardroles a inner join locationcards b on a.id=b.CardRoleId inner join locationcardtopersonnels c on b.id=c.LocationCardId where c.PersonnelId=" + person.Id); CardRole role = db.CardRoles.GetDataBySql <CardRole>(strsql); LocationCardToPersonnel card = db.LocationCardToPersonnels.Find(i => i.PersonnelId == person.Id); if (role == null || card == null) { continue; } foreach (Area area in areaList) { AreaAuthorizationRecord aar = aarList.Find(i => i.AreaId == area.Id && i.CardRoleId == role.Id); if (aar != null) { DateTime nowTime = DateTime.Now; DateTime nowEndTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, aar.EndTime.Hour, aar.EndTime.Minute, aar.EndTime.Second); DateTime nowStartTime = new DateTime(nowTime.Year, nowTime.Month, nowTime.Day, aar.StartTime.Hour, aar.StartTime.Minute, aar.StartTime.Second); if (nowTime > nowEndTime)//可以判断是否产生告警 { Position position = new Position(); position.CardId = card.LocationCardId; position.RoleId = role.Id; position.PersonnelID = person.Id; PersonnelFirstInArea personArea = db.PersonnelFirstInAreas.Find(i => i.areaId == area.Id && i.personId == person.Id && i.type == 1); if (personArea == null)//表示人员person当天未到过该区域 { //未签到告警 LocationAlarm alarm1 = new LocationAlarm(position, area.Id, aar, person.Name, LocationAlarmLevel.四级告警, LocationAlarmType.签到告警); list.Add(alarm1); } else { DateTime dateTime = personArea.dateTime; if (dateTime > nowStartTime && dateTime < nowEndTime)//时间范围内进入过 { //正常告警 LocationAlarm alarm1 = new LocationAlarm(position, area.Id, aar, person.Name, LocationAlarmLevel.正常, LocationAlarmType.签到告警); list.Add(alarm1); } else { //未签到告警 LocationAlarm alarm1 = new LocationAlarm(position, area.Id, aar, person.Name, LocationAlarmLevel.四级告警, LocationAlarmType.签到告警); list.Add(alarm1); } } } } } } } catch (Exception ex) { Log.Error("SignInAlarmThread.GetCacheList:" + ex.ToString()); } return(list); }