FIrst version of prodInfo DB persistence
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
using Thermo.Active.Model.DatabaseModels;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using static Thermo.Active.Config.ServerConfig;
|
||||
using System.Collections.Generic;
|
||||
using CMS_CORE_Library.Models;
|
||||
|
||||
namespace Thermo.Active.Database.Controllers
|
||||
{
|
||||
public class ProdInfoController : IDisposable
|
||||
{
|
||||
private DatabaseContext dbCtx;
|
||||
|
||||
public ProdInfoController()
|
||||
{
|
||||
// Initialize database context
|
||||
dbCtx = new DatabaseContext();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
// Clear database context
|
||||
dbCtx.Dispose();
|
||||
}
|
||||
|
||||
public ProdInfoModel FindByNumDone(int num)
|
||||
{
|
||||
return dbCtx
|
||||
.ProdInfo
|
||||
.Where(x => x.NumDone == num)
|
||||
.SingleOrDefault();
|
||||
}
|
||||
|
||||
public List<ProdInfoModel> GetPaginated(int numStart, int numRecord)
|
||||
{
|
||||
// cehck numEnd
|
||||
int numEnd = numStart - numRecord;
|
||||
if (numEnd < 0)
|
||||
numEnd = 0;
|
||||
// retrieve
|
||||
return dbCtx
|
||||
.ProdInfo
|
||||
.Where(x => x.NumDone <= numStart && x.NumDone >= numEnd)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ProdInfoModel Create(short NumTarget, short NumDone, int TimeWarm, int TimeVent, int TimeVacuum, int TimeCycleGross, int TimeCycleNet, double MaterialTempEndWarm, double MaterialTempEndVent, double MoldTemp, double VacuumReadVal, double MouldEnergyOUT, double MouldEnergyIN)
|
||||
{
|
||||
// Create database machine model
|
||||
ProdInfoModel prodData = new ProdInfoModel()
|
||||
{
|
||||
NumTarget = NumTarget,
|
||||
NumDone = NumDone,
|
||||
TimeWarm = TimeWarm,
|
||||
TimeVent = TimeVent,
|
||||
TimeVacuum = TimeVacuum,
|
||||
TimeCycleGross = TimeCycleGross,
|
||||
TimeCycleNet = TimeCycleNet,
|
||||
MaterialTempEndWarm = MaterialTempEndWarm,
|
||||
MaterialTempEndVent = MaterialTempEndVent,
|
||||
MoldTemp = MoldTemp,
|
||||
VacuumReadVal = VacuumReadVal,
|
||||
MouldEnergyOUT = MouldEnergyOUT,
|
||||
MouldEnergyIN = MouldEnergyIN
|
||||
};
|
||||
// Add to database
|
||||
dbCtx.ProdInfo.Add(prodData);
|
||||
// Commit changes
|
||||
dbCtx.SaveChanges();
|
||||
|
||||
return prodData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Process table and keep only maxKeep most recent ones
|
||||
/// </summary>
|
||||
/// <param name="maxKeep"></param>
|
||||
/// <returns></returns>
|
||||
public bool PurgeOldest(int maxKeep)
|
||||
{
|
||||
bool answ = false;
|
||||
|
||||
// check if purge needed
|
||||
int numRec = dbCtx.ProdInfo.Count();
|
||||
if (numRec > maxKeep)
|
||||
{
|
||||
ProdInfoModel firstToDelete = (ProdInfoModel)(from p in dbCtx.ProdInfo
|
||||
orderby p.DtEvent descending
|
||||
select p).Skip(maxKeep).Take(1);
|
||||
|
||||
// call deletion
|
||||
dbCtx
|
||||
.ProdInfo
|
||||
.RemoveRange(
|
||||
dbCtx
|
||||
.ProdInfo
|
||||
.Where(x => x.DtEvent <= firstToDelete.DtEvent)
|
||||
);
|
||||
|
||||
// save!
|
||||
dbCtx.SaveChanges();
|
||||
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
|
||||
|
||||
//public void UpdateMachineName(int id, string name)
|
||||
//{
|
||||
// // Find machine by id
|
||||
// MachineModel machine = FindById(id);
|
||||
// if (machine != null)
|
||||
// {
|
||||
// // Update machine name
|
||||
// machine.Model = name;
|
||||
// dbCtx.SaveChanges();
|
||||
// }
|
||||
//}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user