using Step.Model.DatabaseModels; using Step.Model.DTOModels; using Step.Model.DTOModels.AlarmModels; using System; using System.Collections.Generic; using System.Data.Entity; using System.IO; using System.Linq; using static Step.Model.Constants; using static Step.Config.ServerConfig; using System.Diagnostics; namespace Step.Database.Controllers { public class AlarmsController : IDisposable { private DatabaseContext dbCtx; public AlarmsController() { // Initialize database context dbCtx = new DatabaseContext(); } public void Dispose() { // Clear database context dbCtx.Dispose(); } public List GetPaginatedWithFilter(string title, List types, int page, int pageSize, DateTime startDate, DateTime endDate, List userIds, Dictionary plcMessages) { List ncAlarmDescIds = dbCtx .AlarmDescriptions .Where(x => x.Title.Contains(title)) .Select(x => x.AlarmId) .ToList(); // Add Plc messages List plcAlarmDescIds = plcMessages .Where(x => x.Value.Contains(title)) .Select(x => x.Key) .ToList(); var occurrencesQuery = dbCtx .AlarmOccurrences .OrderBy(x => x.AlarmOccurrenceId) .Include("Users") .Where(x => x.TimeStamp >= startDate && x.TimeStamp <= endDate // Filter by date && types.Contains(x.Type) // Type && x.Users.Any(y => userIds.Any(z => z == y.UserId)) // Check user && ((x.Source == ALARM_SOURCE.NC && ncAlarmDescIds.Contains(x.AlarmDescriptionId.Value)) // Check if message is contained in NC messages || (x.Source == ALARM_SOURCE.PLC && plcAlarmDescIds.Contains(x.PlcMessageId.Value))) // Check if message is contained in PLC messages ) .Skip(page * pageSize) // Paginate .Take(pageSize) .Include("AlarmDescription") // Include foreign key .ToList(); return occurrencesQuery .Select(x => (DTOAlarmHistoricModel)x) .ToList(); } public void InsertNewOccurrences(List alarms) { dbCtx.AlarmOccurrences.AddRange(alarms); dbCtx.SaveChanges(); } public void InsertNewNcAlarmDescriptions(List descriptions) { foreach (var desc in descriptions) { // Check if description exists already var dbDesc = dbCtx.AlarmDescriptions.Where(x => x.AlarmId == desc.AlarmId).FirstOrDefault(); // If not add if (dbDesc == null) dbCtx.AlarmDescriptions.Add(desc); } dbCtx.SaveChanges(); } public void InsertNewAlarmUser(List loggedUser) { dbCtx.AlarmUsers.AddRange(loggedUser); dbCtx.SaveChanges(); // TODO check if it is the best solutions } public DTOAlarmsData GetAlarmsData(int pageSize) { // Get page numbers int pagesNumbers = dbCtx.AlarmOccurrences.Count() / pageSize; var firstAlarm = dbCtx.AlarmOccurrences.FirstOrDefault(); // Get first alarm date DateTime date = firstAlarm == null ? DateTime.Now : firstAlarm.TimeStamp; return new DTOAlarmsData { Pages = pagesNumbers, FirstDate = date }; } public AlarmDescriptionsModel FindById(int id) { return dbCtx .AlarmDescriptions .Where(x => x.AlarmId == id) .FirstOrDefault(); } #region NOTES public List GetNotesByAlarmDescId(int alarmDescriptionId) { return dbCtx .AlarmsNotes .Where(x => x.AlarmDescriptionId == alarmDescriptionId) // Filter by id .Select(x => new DTOAlarmNoteModel() // Convert to return model { Id = x.NoteId, DateTime = x.DateTime, Message = x.Message, User = new DTOMessageUserModel() { Id = x.User.UserId, FirstName = x.User.FirstName, LastName = x.User.LastName, Username = x.User.Username } }) .ToList(); } public AlarmNoteModel FindNoteById(int noteId) { return dbCtx .AlarmsNotes .Where(x => x.NoteId == noteId) .Include("User") .FirstOrDefault(); } public DTOAlarmNoteModel CreateNote(int userId, int alarmDescId, DTONewAlarmNoteModel newNote) { // Create model AlarmNoteModel note = new AlarmNoteModel() { Message = newNote.Message, DateTime = DateTime.Now, AlarmDescriptionId = alarmDescId, UserId = userId }; // Add & save into database dbCtx.AlarmsNotes.Add(note); dbCtx.SaveChanges(); // Populate user data dbCtx.Entry(note).Reference(x => x.User).Load(); dbCtx.Users.Attach(note.User); return (DTOAlarmNoteModel)note; } public DTOAlarmNoteModel UpdateNote(AlarmNoteModel note, DTONewAlarmNoteModel newNote) { note.Message = newNote.Message; dbCtx.SaveChanges(); return (DTOAlarmNoteModel)note; } public void DeleteNote(int noteId) { AlarmNoteModel note = FindNoteById(noteId); dbCtx.AlarmsNotes.Remove(note); dbCtx.SaveChanges(); } #endregion NOTES #region ATTACHMENT public List FindAttachmentByAlarmDescId(int alarmDescId) { return dbCtx .AlarmFiles .Where(x => x.AlarmDescriptionId == alarmDescId) .ToList(); } public AlarmFileModel FindAttachmentById(int attachmentId) { return dbCtx .AlarmFiles .Where(x => x.AttachmentId == attachmentId) .FirstOrDefault(); } public AlarmFileModel AddAttachment(string fileName, string localFileName, int alarmDescId, int userId) { // Create obj AlarmFileModel file = new AlarmFileModel() { FileName = fileName, LocalFileName = localFileName, AlarmDescriptionId = alarmDescId, UserId = userId }; // Add to db dbCtx.AlarmFiles.Add(file); dbCtx.SaveChanges(); return file; } public void DeleteAttachment(int attachmentId) { // Get attachmentRow AlarmFileModel file = dbCtx .AlarmFiles .Where(x => x.AttachmentId == attachmentId) .FirstOrDefault(); if (file != null) { dbCtx.AlarmFiles.Remove(file); dbCtx.SaveChanges(); } } public void DeleteAttachment(AlarmFileModel attachment) { dbCtx.AlarmFiles.Remove(attachment); dbCtx.SaveChanges(); if (File.Exists(ALARM_ATTACHMENT_PATH + attachment.LocalFileName)) File.Delete(ALARM_ATTACHMENT_PATH + attachment.LocalFileName); } #endregion ATTACHMENT } }