539 lines
17 KiB
C#
539 lines
17 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using EgtBEAMWALL.DataLayer.DatabaseModels;
|
|
using static EgtBEAMWALL.Core.ConstBeam;
|
|
|
|
namespace EgtBEAMWALL.DataLayer.Controllers
|
|
{
|
|
public class ProjController : IDisposable
|
|
{
|
|
#region Private Fields
|
|
|
|
private DatabaseContext dbCtx;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public ProjController()
|
|
{
|
|
// Initialize database context
|
|
dbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING);
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Helper conversione modelli
|
|
/// </summary>
|
|
/// <param name="currProj"></param>
|
|
/// <returns></returns>
|
|
protected Core.ProjFileM coreConv(ProjModel currProj)
|
|
{
|
|
Core.ProjFileM answ = Core.ProjFileM.CreateProjFileM(currProj.ProjId, ProdIdByProdDbId(currProj.ProdDbId), currProj.DtCreated, currProj.DtExported, currProj.ListName, currProj.BTLFileName, currProj.IsNew, currProj.Locked, currProj.PType);
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get LAST paginated data from DB (DESC ordered)
|
|
/// </summary>
|
|
/// <param name="numRecord"></param>
|
|
/// <returns></returns>
|
|
protected List<ProjModel> GetLastDbModelDesc(int numRecord)
|
|
{
|
|
// se numRecord = 0 --> passo tutti
|
|
if (numRecord == 0)
|
|
{
|
|
numRecord = dbCtx.ProjList.Count();
|
|
}
|
|
// retrieve
|
|
return dbCtx
|
|
.ProjList
|
|
.OrderByDescending(x => x.ProjId)
|
|
.Take(numRecord)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get ProdId by ProdDbId, 0 se non trovato
|
|
/// </summary>
|
|
/// <param name="ProdDbId"></param>
|
|
/// <returns></returns>
|
|
protected int ProdIdByProdDbId(int? ProdDbId)
|
|
{
|
|
int answ = 0;
|
|
|
|
if (ProdDbId != null)
|
|
{
|
|
var prodRecord = dbCtx
|
|
.ProdList
|
|
.Where(x => x.ProdDbId == ProdDbId)
|
|
.SingleOrDefault();
|
|
|
|
if (prodRecord != null)
|
|
{
|
|
answ = prodRecord.ProdId;
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Public Methods
|
|
|
|
public bool DeleteProj(int ProjId)
|
|
{
|
|
bool done = false;
|
|
|
|
var currProj = FindByProjId(ProjId);
|
|
|
|
// sel delle BTLParts da proj
|
|
var parts2del = dbCtx
|
|
.BTLPartList
|
|
.Where(x => x.ProjDbId == currProj.ProjDbId);
|
|
|
|
try
|
|
{
|
|
// remove from database
|
|
dbCtx.BTLPartList.RemoveRange(parts2del);
|
|
dbCtx.ProjList.Remove(currProj);
|
|
// Commit changes
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
done = true;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Console.WriteLine($"EXCEPTION on DeleteProj: {exc}");
|
|
}
|
|
return done;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
// Clear database context
|
|
dbCtx.Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get record by ProjDbId
|
|
/// </summary>
|
|
/// <param name="ProjDbId"></param>
|
|
/// <returns></returns>
|
|
public ProjModel FindByProjDbId(int ProjDbId)
|
|
{
|
|
ProjModel answ = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProjDbId == ProjDbId)
|
|
.SingleOrDefault();
|
|
// forzo rilettura
|
|
if (answ != null)
|
|
{
|
|
dbCtx.Entry(answ).Reload();
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get record by ProjId
|
|
/// </summary>
|
|
/// <param name="ProjId"></param>
|
|
/// <returns></returns>
|
|
public ProjModel FindByProjId(int ProjId)
|
|
{
|
|
var answ = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProjId == ProjId)
|
|
.SingleOrDefault();
|
|
// forzo rilettura
|
|
if (answ != null)
|
|
{
|
|
dbCtx.Entry(answ).Reload();
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get record by ProjId converted
|
|
/// </summary>
|
|
/// <param name="ProjId"></param>
|
|
/// <returns></returns>
|
|
public Core.ProjFileM FindByProjIdConv(int ProjId)
|
|
{
|
|
return coreConv(FindByProjId(ProjId));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get filtered data by ProdId (ASC ordered)
|
|
/// </summary>
|
|
/// <param name="ProdDbId"></param>
|
|
/// <returns></returns>
|
|
public List<Core.ProjFileM> GetByProdAsc(int ProdId)
|
|
{
|
|
List<Core.ProjFileM> answ = new List<Core.ProjFileM>();
|
|
int ProdDbId = 0;
|
|
try
|
|
{
|
|
var currProd = dbCtx
|
|
.ProdList
|
|
.Where(x => x.ProdId == ProdId)
|
|
.FirstOrDefault();
|
|
if (currProd != null)
|
|
{
|
|
ProdDbId = currProd.ProdDbId;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Console.WriteLine($"EXCEPTION on GetByProdAsc: {exc}");
|
|
}
|
|
// retrieve
|
|
var dbRes = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProdDbId == ProdDbId)
|
|
.OrderBy(x => x.ProdDbId)
|
|
.ToList();
|
|
// conversione
|
|
answ = dbRes.Select(x => coreConv(x)).ToList();
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get filtered data by ProdId (DESC ordered)
|
|
/// </summary>
|
|
/// <param name="ProdDbId"></param>
|
|
/// <returns></returns>
|
|
public List<Core.ProjFileM> GetByProdDesc(int ProdId)
|
|
{
|
|
List<Core.ProjFileM> answ = new List<Core.ProjFileM>();
|
|
int ProdDbId = 0;
|
|
try
|
|
{
|
|
var currProd = dbCtx
|
|
.ProdList
|
|
.Where(x => x.ProdId == ProdId)
|
|
.FirstOrDefault();
|
|
if (currProd != null)
|
|
{
|
|
ProdDbId = currProd.ProdDbId;
|
|
}
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Console.WriteLine($"EXCEPTION on GetByProdAsc: {exc}");
|
|
}
|
|
// retrieve
|
|
var dbRes = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProdDbId == ProdDbId)
|
|
.OrderByDescending(x => x.ProdDbId)
|
|
.ToList();
|
|
// conversione
|
|
answ = dbRes.Select(x => coreConv(x)).ToList();
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Elenco progetti
|
|
/// </summary>
|
|
/// <param name="numRecord"></param>
|
|
/// <returns></returns>
|
|
public List<Core.ProjFileM> GetLastDesc(int numRecord)
|
|
{
|
|
List<Core.ProjFileM> answ = new List<Core.ProjFileM>();
|
|
|
|
// se numRecord = 0 --> passo tutti
|
|
if (numRecord == 0)
|
|
{
|
|
numRecord = dbCtx.ProjList.Count();
|
|
}
|
|
// retrieve
|
|
var dbRes = dbCtx
|
|
.ProjList
|
|
.OrderByDescending(x => x.ProjId)
|
|
.Take(numRecord)
|
|
.ToList();
|
|
// conversione
|
|
answ = dbRes.Select(x => coreConv(x)).ToList();
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fornisce nuovo indice VUOTO da usare (allocando sul DB)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public int GetNextIndex()
|
|
{
|
|
int nextId = 0;
|
|
|
|
// cerco se ne ho ALMENO 1....
|
|
var numRec = dbCtx.ProjList.Count();
|
|
if (numRec == 0)
|
|
{
|
|
nextId = 1;
|
|
}
|
|
else
|
|
{
|
|
// retrieve ultimo...
|
|
var maxRecord = dbCtx
|
|
.ProjList
|
|
.OrderByDescending(x => x.ProjId)
|
|
.Take(1)
|
|
.FirstOrDefault();
|
|
// incremento
|
|
nextId = maxRecord.ProjId + 1;
|
|
}
|
|
|
|
// creo nuovo...
|
|
var newRec = dbCtx
|
|
.ProjList
|
|
.Add(new ProjModel() { ProjId = nextId, BTLFileName = "", IsNew = true, Locked = true, DtCreated = DateTime.Now });
|
|
|
|
// Commit changes
|
|
dbCtx.SaveChanges();
|
|
|
|
return nextId;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Manage Lock by ProjId (proj & prod)
|
|
/// </summary>
|
|
/// <param name="ProjId">ProjID</param>
|
|
/// <param name="Locked">Stato Lock da impostare</param>
|
|
/// <returns></returns>
|
|
public Core.ProjFileM LockByProjId(int ProjId, bool Locked)
|
|
{
|
|
var currProj = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProjId == ProjId)
|
|
.SingleOrDefault();
|
|
// aggiorno stato del proj
|
|
currProj.Locked = Locked;
|
|
dbCtx.Entry(currProj).State = System.Data.Entity.EntityState.Modified;
|
|
|
|
// seleziono il prod e lo blocco...
|
|
var currProd = dbCtx
|
|
.ProdList
|
|
.Where(x => x.ProdDbId == currProj.ProdDbId)
|
|
.SingleOrDefault();
|
|
|
|
if (currProd != null)
|
|
{
|
|
// blocco prod corrente
|
|
currProd.Locked = Locked;
|
|
dbCtx.Entry(currProd).State = System.Data.Entity.EntityState.Modified;
|
|
|
|
// ora blocco altri proj del prod...
|
|
var currProjs = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProdDbId == currProd.ProdDbId && x.ProjId != ProjId)
|
|
.ToList();
|
|
|
|
//currProjs.ForEach(x => x.Locked = Locked);
|
|
foreach (var item in currProjs)
|
|
{
|
|
//item.ProdDbId = currProd.ProdDbId;
|
|
item.Locked = Locked;
|
|
dbCtx.Entry(item).State = System.Data.Entity.EntityState.Modified;
|
|
}
|
|
}
|
|
// salvataggio
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
|
|
return coreConv(currProj);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reinizializzaizone del controller
|
|
/// </summary>
|
|
public void ResetController()
|
|
{
|
|
// Re-Initialize database context
|
|
dbCtx = new DatabaseContext(DbConfig.CONNECTION_STRING);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reimposta come NEW
|
|
/// </summary>
|
|
/// <param name="ProjId">ProjID</param>
|
|
/// <param name="Locked">Stato Lock da impostare</param>
|
|
/// <returns></returns>
|
|
public Core.ProjFileM ResetNew(int ProjId)
|
|
{
|
|
var currProj = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProjId == ProjId)
|
|
.SingleOrDefault();
|
|
|
|
// aggiorno stato
|
|
currProj.IsNew = false;
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
|
|
return coreConv(currProj);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update single PROJ
|
|
/// </summary>
|
|
/// <param name="updItem"></param>
|
|
/// <returns></returns>
|
|
public bool Update(ProjModel updItem)
|
|
{
|
|
bool done = false;
|
|
var item2update = dbCtx
|
|
.ProjList
|
|
.Where(x => x.ProjDbId == updItem.ProjDbId)
|
|
.SingleOrDefault();
|
|
try
|
|
{
|
|
// update, vers 1...
|
|
dbCtx.Entry(item2update).CurrentValues.SetValues(updItem);
|
|
|
|
//// update, vers 2
|
|
//dbCtx.PartList.Remove(item2del);
|
|
//dbCtx.PartList.Add(updItem);
|
|
|
|
// Commit changes
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
done = true;
|
|
}
|
|
catch
|
|
{ }
|
|
return done;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record su DB x elenco BTLParts
|
|
/// </summary>
|
|
/// <param name="ProjId"></param>
|
|
/// <param name="newBTLFileName"></param>
|
|
/// <returns></returns>
|
|
public ProjModel UpdateBtlParts(int ProjId, List<Core.BTLPartM> BtlPartList)
|
|
{
|
|
// record del proj corrente
|
|
var currData = FindByProjId(ProjId);
|
|
|
|
// 2021.05.03 modifica update: esistenti le MODIFICO, nuove aggiungo, inesistenti elimino in cascata con Part
|
|
|
|
// sel delle BTLParts del proj
|
|
List<BTLPartModel> oldBtlParts = dbCtx
|
|
.BTLPartList
|
|
.Where(x => x.ProjDbId == currData.ProjDbId)
|
|
.ToList();
|
|
|
|
// converto le BtlParts da core --> DB
|
|
List<BTLPartModel> newBtlParts = BtlPartList.Select(x => BTLPartController.ConvertFromCore(x, currData.ProjDbId)).ToList();
|
|
|
|
try
|
|
{
|
|
// elementi BtlPartId NON + presenti da eliminare
|
|
List<int> bpi2rem = oldBtlParts.Select(x => x.PartId).Except(newBtlParts.Select(y => y.PartId)).ToList();
|
|
List<int> bpi2add = newBtlParts.Select(x => x.PartId).Except(oldBtlParts.Select(y => y.PartId)).ToList();
|
|
List<int> bpiExis = newBtlParts.Select(x => x.PartId).Intersect(oldBtlParts.Select(y => y.PartId)).ToList();
|
|
|
|
// aggiorno existing...
|
|
foreach (var currPartId in bpiExis)
|
|
{
|
|
// recupero item da aggiornare...
|
|
var oldItem = oldBtlParts.Where(x => x.PartId == currPartId).FirstOrDefault();
|
|
// dati nuovo item
|
|
var newItem = newBtlParts.Where(x => x.PartId == currPartId).FirstOrDefault();
|
|
if (newItem != null && oldItem != null)
|
|
{
|
|
oldItem.CALC_State = newItem.CALC_State;
|
|
oldItem.CNT = newItem.CNT;
|
|
oldItem.DO = newItem.DO;
|
|
oldItem.H = newItem.H;
|
|
oldItem.L = newItem.L;
|
|
oldItem.W = newItem.W;
|
|
oldItem.Material = newItem.Material;
|
|
oldItem.NAM = newItem.NAM;
|
|
oldItem.PDN = newItem.PDN;
|
|
}
|
|
}
|
|
|
|
// aggiungo le nuove part
|
|
foreach (var newPartId in bpi2add)
|
|
{
|
|
var newItem = newBtlParts.Where(x => x.PartId == newPartId).FirstOrDefault();
|
|
dbCtx.BTLPartList.Add(newItem);
|
|
}
|
|
|
|
// elimino dal DB i non + esistenti
|
|
foreach (var oldPartId in bpi2rem)
|
|
{
|
|
// elimino parts nei MachGroup
|
|
var oldIstPartList = dbCtx.PartList.Where(x => x.BTLPart.PartId == oldPartId).ToList();
|
|
dbCtx.PartList.RemoveRange(oldIstPartList);
|
|
// elimino BtlParts
|
|
var oldItem = oldBtlParts.Where(x => x.PartId == oldPartId).FirstOrDefault();
|
|
dbCtx.BTLPartList.Remove(oldItem);
|
|
}
|
|
}
|
|
catch
|
|
{ }
|
|
|
|
// aggiorno valore isNew a false
|
|
currData.IsNew = false;
|
|
|
|
// Commit changes
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
|
|
return currData;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record su DB x nomeBTL, ListName, ExportDate
|
|
/// </summary>
|
|
/// <param name="ProjId"></param>
|
|
/// <param name="BTLFileName"></param>
|
|
/// <param name="ListName"></param>
|
|
/// <param name="DtExported"></param>
|
|
/// <returns></returns>
|
|
public Core.ProjFileM UpdateInfo(int ProjId, string BTLFileName, string ListName, DateTime DtExported, BWType PType)
|
|
{
|
|
var currData = FindByProjId(ProjId);
|
|
// aggiorno valore BTL
|
|
currData.BTLFileName = BTLFileName;
|
|
currData.DtExported = DtExported;
|
|
currData.ListName = ListName;
|
|
currData.PType = PType;
|
|
|
|
// Commit changes
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
|
|
return coreConv(currData);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Update record su DB x nomeBTL, ListName, ExportDate
|
|
/// </summary>
|
|
/// <param name="ProjId"></param>
|
|
/// <param name="ListName"></param>
|
|
/// <returns></returns>
|
|
public Core.ProjFileM UpdateListName(int ProjId, string ListName)
|
|
{
|
|
var currData = FindByProjId(ProjId);
|
|
// aggiorno valore BTL
|
|
currData.ListName = ListName;
|
|
|
|
// Commit changes
|
|
dbCtx.SaveChanges();
|
|
ResetController();
|
|
|
|
return coreConv(currData);
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |