Files
cms_thermo_active/Step.Database/Controllers/AlarmsController.cs
T
Lucio Maranta 2294b52a65 Fix Siemens active file
Fix machine counters
WIP Siemens axes name
2018-11-07 17:27:44 +01:00

236 lines
7.0 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.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<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 && x.Type == type
// && userIds.Any(y => x.Users.Select(z => z.UserId).Any(z => z == y))
)
.Skip(page * pageSize)
.Take(pageSize)
.Include("AlarmDescription")
.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.SaveChangesAsync(); // 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<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
}
}