258 lines
7.7 KiB
C#
258 lines
7.7 KiB
C#
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.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using static Step.Model.Constants;
|
|
|
|
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<AlarmOccurrencesModel> GetPaginated(int page, int pageSize)
|
|
{
|
|
Stopwatch st = new Stopwatch();
|
|
st.Start();
|
|
DateTime l = DateTime.Parse("2013/01/01");
|
|
var paginated = dbCtx.AlarmOccurrences.Include("AlarmDescription").OrderBy(x => x.AlarmOccurrenceId)
|
|
.Where(x => x.TimeStamp < DateTime.Today && x.TimeStamp > l)
|
|
.Skip(page * pageSize)
|
|
.Take(pageSize)
|
|
.ToList();
|
|
|
|
Console.WriteLine(st.ElapsedMilliseconds);
|
|
|
|
st.Stop();
|
|
|
|
return paginated;
|
|
}
|
|
|
|
public List<DTOAlarmHistoricModel> GetPaginatedWithFilter(string message, ALARM_TYPE type, int page, int pageSize, DateTime startDate, DateTime endDate, List<int> userIds)
|
|
{
|
|
var occurrences = dbCtx
|
|
.AlarmOccurrences
|
|
.OrderBy(x => x.AlarmOccurrenceId)
|
|
.Where(x =>
|
|
x.TimeStamp >= startDate && x.TimeStamp <= endDate
|
|
// && userIds.Any(y => x.Users.Select(z => z.UserId).Any(z => z == y))
|
|
)
|
|
.Skip(page * pageSize)
|
|
.Take(pageSize)
|
|
.Include("AlarmDescription")
|
|
.ToList();
|
|
|
|
//var ids = occurrences.Select(x => x.Id).ToList();
|
|
//var users = dbCtx.AlarmUsers.Where(x => ids.Contains(x.AlarmOccurrenceId) && userIds.Contains(x.UserId)).ToList();
|
|
|
|
return occurrences
|
|
.Select(x => (DTOAlarmHistoricModel)x)
|
|
.ToList();
|
|
}
|
|
|
|
public void InsertNewOccurrences(List<AlarmOccurrencesModel> alarms)
|
|
{
|
|
dbCtx.AlarmOccurrences.AddRange(alarms);
|
|
|
|
dbCtx.SaveChanges();
|
|
}
|
|
|
|
public void InsertNewNcAlarmDescriptions(List<AlarmDescriptionsModel> 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<AlarmUserModel> loggedUser)
|
|
{
|
|
dbCtx.AlarmUsers.AddRange(loggedUser);
|
|
|
|
dbCtx.SaveChanges();
|
|
}
|
|
|
|
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<DTOAlarmNoteModel> 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<AlarmFileModel> 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
|
|
}
|
|
} |