private static void InitClearExpiredAlarmItem() { clearExpiredAlarmItemTimer = new Timer(state => { try { var dbContextFactory = LocalServiceLocator.GetService<IDbContextFactory>(); using (var context = dbContextFactory.CreateContext<AlarmDbContext>()) { var openItems = context.AlarmItems.Where(a => a.AlarmStatusId == (int)AlarmStatus.Handling).ToList(); foreach (var openItem in openItems) { var configItem = AlarmConfiguration.GetAlarmConfigurationItem(openItem.AlarmConfigName); if (configItem != null && openItem.HandleTime < DateTime.Now.Subtract(configItem.ProcessTimeout)) { openItem.AlarmStatus = AlarmStatus.Closed; openItem.CloseTime = DateTime.Now; var process = new AlarmProcessItem { AlarmItemId = openItem.Id, EventTime = DateTime.Now, MobileComment = string.Format("事件在 {0} 内没有处理完成,系统自动关闭", configItem.ProcessTimeout), MailComment = string.Format("事件在 {0} 内没有处理完成,系统自动关闭", configItem.ProcessTimeout), ProcessUserName = "", ProcessUserRealName = "", AlarmStatus = AlarmStatus.Closed, }; context.AlarmProcessItems.Add(process); } context.SaveChanges(); } } } catch (Exception ex) { ex.Handle(AlarmConfigurationBase.ModuleName, "AlarmService", "InitClearExpiredAlarmItem"); } }, null, 0, 1000 * 60); }
static void Main(string[] args) { AdhesiveFramework.Start(); Console.ReadLine(); IDbContextFactory dbContextFactory = LocalServiceLocator.GetService<IDbContextFactory>(); using (var context = dbContextFactory.CreateContext<AlarmDbContext>()) { var alarm1 = new AlarmItem { AlarmConfigName = "Asdadsad", AlarmStatus = AlarmStatus.Open, OpenTime = DateTime.Now, AlarmTimes = 10, }; var alarm2 = new AlarmItem { AlarmConfigName = "Asdadsad", AlarmStatus = AlarmStatus.Handling, HandleTime = DateTime.Now, OpenTime = DateTime.Now, AlarmTimes = 10, }; var alarm3 = new AlarmItem { AlarmConfigName = "Asdadsad", AlarmStatus = AlarmStatus.Closed, AlarmTimes = 10, OpenTime = DateTime.Now, CloseTime = DateTime.Now, HandleTime = DateTime.Now, }; var alarmprocess1 = new AlarmProcessItem() { AlarmItem = alarm3, MailComment = "邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息", MobileComment = "短信消息短信消息短信消息短信消息短信消息短信消息", AlarmStatus = AlarmStatus.Handling, ProcessUserName = "******", ProcessUserRealName = "朱晔", EventTime = DateTime.Now, }; var alarmprocess2 = new AlarmProcessItem() { AlarmItem = alarm3, MailComment = "邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息", MobileComment = "短信消息短信消息短信消息短信消息短信消息短信消息", AlarmStatus = AlarmStatus.Handling, ProcessUserName = "******", ProcessUserRealName = "朱晔", EventTime = DateTime.Now, }; var alarmprocess3 = new AlarmProcessItem() { AlarmItem = alarm3, MailComment = "邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息邮件消息", MobileComment = "短信消息短信消息短信消息短信消息短信消息短信消息", AlarmStatus = AlarmStatus.Closed, ProcessUserName = "******", ProcessUserRealName = "朱晔", EventTime = DateTime.Now, }; context.AlarmItems.Add(alarm1); context.AlarmItems.Add(alarm2); context.AlarmItems.Add(alarm3); context.AlarmProcessItems.Add(alarmprocess1); context.AlarmProcessItems.Add(alarmprocess2); context.AlarmProcessItems.Add(alarmprocess3); context.SaveChanges(); } AdhesiveFramework.End(); }
public void CloseAlarmEvent(string id, string mobileMessage, string mailMessage, string userName, string userRealName) { try { var dbContextFactory = LocalServiceLocator.GetService<IDbContextFactory>(); using (var context = dbContextFactory.CreateContext<AlarmDbContext>()) { var item = context.AlarmItems.SingleOrDefault(a => a.Id == id && a.AlarmStatusId != (int)AlarmStatus.Closed); if (item != null) { item.CloseTime = DateTime.Now; item.AlarmStatus = AlarmStatus.Closed; var process = new AlarmProcessItem { AlarmItemId = id, EventTime = DateTime.Now, MobileComment = string.Format("{0} 关闭了事件: {1},结论:{2}", userRealName, item.AlarmConfigName, mobileMessage), MailComment = string.Format("{0} 关闭了事件: {1},结论:{2}", userRealName, item.AlarmConfigName, mailMessage), ProcessUserName = userName, ProcessUserRealName = userRealName, AlarmStatus = AlarmStatus.Closed, }; context.AlarmProcessItems.Add(process); context.SaveChanges(); mobileMessage = string.Format("{0} 关闭了事件: {1},结论:{2}", userRealName, item.AlarmConfigName, mobileMessage); mailMessage = string.Format("{0} 关闭了事件: {1},结论:{2}", userRealName, item.AlarmConfigName, mailMessage); SendMessage(item.AlarmConfigName, mobileMessage, mailMessage); } } } catch (Exception ex) { ex.Handle("CloseAlarmEvent"); throw; } }