/// <summary> /// Check to see if there is already an EventCode for this Customer with the following attributes: /// SourceId, TypeId, CategoryId and AssetTypeId /// </summary> /// <param name="model"></param> /// <returns></returns> public bool CanCreateEventCode(EventCodeEditModel model) { // Check to see if there is already an EventCode for this Customer with the following attributes: // SourceId, TypeId, CategoryId and AssetTypeId EventCodeAssetType ecat = null; EventCode ecm = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventType == model.TypeId && m.EventCategory == model.CategoryId); if (ecm != null) { // Does this EventCode have the same asset type already assigned? ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == ecm.CustomerID && m.EventSource == ecm.EventSource && m.EventCode == ecm.EventCode1); } return(ecm != null && ecat != null); }
/// <summary> /// sets the event code edit model with the event code data in the system /// </summary> /// <param name="model"></param> public void SetEventCodeEditModel(EventCodeEditModel model) { // Get the original EventCode var eventCode = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode1 == model.Id); if (eventCode != null) { eventCode.AlarmTier = model.AlarmTierId; eventCode.EventDescAbbrev = model.DescAbbrev; eventCode.EventDescVerbose = model.DescVerbose; eventCode.SLAMinutes = model.ApplySLA ? model.SLAMinutes : null; eventCode.ApplySLA = model.ApplySLA; eventCode.IsAlarm = model.IsAlarm; eventCode.EventType = model.TypeId; eventCode.EventCategory = model.CategoryId; // Insert/update [EventCodeAssetType] var ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode == model.Id); if (ecat == null) { ecat = new EventCodeAssetType() { CustomerID = model.CustomerId, EventSource = model.SourceId, EventCode = model.Id, }; PemsEntities.EventCodeAssetTypes.Add(ecat); } ecat.MeterGroupId = model.AssetTypeId; // Save changes PemsEntities.SaveChanges(); // Add audit record. Audit(eventCode); } }
/// <summary> /// Created an event code int he system /// </summary> /// <param name="model"></param> /// <returns></returns> public int CreateEventCode(EventCodeEditModel model) { EventCode eventCode = null; // Get next EventCode id for a given customer & EventSource. // If model.Id is non-zero then a particular event code id has been requested. int nextId = model.Id; eventCode = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode1 == nextId); if (eventCode == null) { if (nextId < 0) { nextId = 1; eventCode = PemsEntities.EventCodes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId); if (eventCode != null) { nextId = PemsEntities.EventCodes.Where(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId).Max(m => m.EventCode1) + 1; } } // Create the new EventCode eventCode = new EventCode() { CustomerID = model.CustomerId, EventSource = model.SourceId, EventCode1 = nextId, AlarmTier = model.AlarmTierId, EventDescAbbrev = model.DescAbbrev, EventDescVerbose = model.DescVerbose, SLAMinutes = model.ApplySLA ? model.SLAMinutes : null, ApplySLA = model.ApplySLA, IsAlarm = model.IsAlarm, EventType = model.TypeId, EventCategory = model.CategoryId }; PemsEntities.EventCodes.Add(eventCode); PemsEntities.SaveChanges(); } else { eventCode.AlarmTier = model.AlarmTierId; eventCode.EventDescAbbrev = model.DescAbbrev; eventCode.EventDescVerbose = model.DescVerbose; eventCode.SLAMinutes = model.ApplySLA ? model.SLAMinutes : null; eventCode.ApplySLA = model.ApplySLA; eventCode.IsAlarm = model.IsAlarm; eventCode.EventType = model.TypeId; eventCode.EventCategory = model.CategoryId; PemsEntities.SaveChanges(); } // Insert/update [EventCodeAssetType] var ecat = PemsEntities.EventCodeAssetTypes.FirstOrDefault(m => m.CustomerID == model.CustomerId && m.EventSource == model.SourceId && m.EventCode == nextId); if (ecat == null) { ecat = new EventCodeAssetType() { CustomerID = model.CustomerId, EventSource = model.SourceId, EventCode = nextId, }; PemsEntities.EventCodeAssetTypes.Add(ecat); } ecat.MeterGroupId = model.AssetTypeId; // Save changes PemsEntities.SaveChanges(); // Add audit record. Audit(eventCode); // Return new EventCode id return(nextId); }