Files
mapo-core/MP.Data/Controllers/MpInveController.cs
T
2022-11-23 12:23:14 +01:00

410 lines
13 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using MP.Data.DatabaseModels;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MP.Data.Controllers
{
public class MpInveController : IDisposable
{
#region Public Constructors
public MpInveController(IConfiguration configuration)
{
_configuration = configuration;
Log.Info("Avviata classe MpInveController");
}
#endregion Public Constructors
#region Public Methods
public void Dispose()
{
_configuration = null;
}
/// <summary>
/// Elenco Scansioni dato Id sessione inventario
/// </summary>
/// <param name="InveSessId"></param>
/// <returns></returns>
public List<ScanDataModel> ScanBySession(int InveSessId)
{
List<ScanDataModel> dbResult = new List<ScanDataModel>();
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbScanData
.Where(x => x.InveSessID == InveSessId)
.AsNoTracking()
.OrderByDescending(x => x.DtScan)
.ToList();
}
return dbResult;
}
#region gestione operatori
/// <summary>
/// Elenco operatori
/// </summary>
/// /// <param name="MatrOpr"></param>
/// /// <param name="authKey"></param>
/// <returns></returns>
public List<AnagOperatoriModel> ElencoOperatori()
{
List<AnagOperatoriModel> dbResult = new List<AnagOperatoriModel>();
using (var dbCtx = new MoonProContext(_configuration))
{
dbResult = dbCtx
.DbOperatori
.Where(s => s.MatrOpr > 0)
.AsNoTracking()
.OrderBy(x => x.MatrOpr)
.ToList();
}
return dbResult;
}
/// <summary>
/// login operatori
/// </summary>
/// /// <param name="MatrOpr"></param>
/// /// <param name="authKey"></param>
/// <returns></returns>
public bool LoginOperatore(int matrOpr, string authKey)
{
List<AnagOperatoriModel> dbResult = new List<AnagOperatoriModel>();
bool answ = false;
using (var dbCtx = new MoonProContext(_configuration))
{
dbResult = dbCtx
.DbOperatori
.Where(s => (s.MatrOpr > 0) && (s.MatrOpr == matrOpr) && (s.authKey == authKey))
.AsNoTracking()
.ToList();
if(dbResult.Count == 1)
{
answ = true;
}
}
return answ;
}
#endregion gestione operatori
#region gestione magazzini
/// <summary>
/// Elenco Magazzini
/// </summary>
/// <returns></returns>
public List<AnagMagModel> ElencoMagazzini()
{
List<AnagMagModel> dbResult = new List<AnagMagModel>();
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbAnagMag
.AsNoTracking()
.OrderBy(x => x.MagID)
.ToList();
}
return dbResult;
}
/// <summary>
/// insert di un record magazzino
/// </summary>
/// <returns></returns>
public async Task<bool> InsertNewMag(AnagMagModel newRec)
{
bool fatto = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
try
{
dbCtx
.DbAnagMag
.Add(newRec);
await dbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante InsertNewMag{Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// modifica di un record magazzino
/// </summary>
/// <returns></returns>
public async Task<bool> UpdateMag(AnagMagModel magRec)
{
bool fatto = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
try
{
var dbResult =
dbCtx
.DbAnagMag
.AsNoTracking()
.Where(x => x.MagID == magRec.MagID)
.FirstOrDefault();
if (dbResult != null)
{
if (dbResult.DescMag != magRec.DescMag)
{
dbResult.DescMag = magRec.DescMag.ToUpper();
dbCtx.Entry(dbResult).State = EntityState.Modified;
}
}
await dbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante UpdateMag{Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// delete magazzino
/// </summary>
/// <returns></returns>
public async Task<bool> DeleteMag(AnagMagModel record)
{
bool fatto = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
try
{
dbCtx
.DbAnagMag
.Remove(record);
await dbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante deleteMag{Environment.NewLine}{exc}");
}
}
return fatto;
}
#endregion gestione magazzini
#region gestione UDC
/// <summary>
/// elenco udc
/// </summary>
/// <returns></returns>
public List<AnagUdcModel> ElencoUdc()
{
List<AnagUdcModel> dbResult = new List<AnagUdcModel>();
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbUdcData
.Include(m=>m.lottoNav)
.AsNoTracking()
.OrderByDescending(x => x.UDC)
.ToList();
}
return dbResult;
}
/// <summary>
/// check udc
/// </summary>
/// <param name="Udc"></param>
/// <returns></returns>
public bool IsUDC(string Udc)
{
List<AnagUdcModel> dbResult = new List<AnagUdcModel>();
bool answ = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbUdcData
.Where(x => x.UDC == Udc)
.AsNoTracking()
.OrderByDescending(x => x.UDC)
.ToList();
if(dbResult.Count == 1)
{
answ = true;
}
}
return answ;
}
#endregion gestione UDC
#region gestione lotti
/// <summary>
/// elenco lotti
/// </summary>
/// <returns></returns>
public List<AnagLottoModel> ElencoLotti()
{
List<AnagLottoModel> dbResult = new List<AnagLottoModel>();
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbLottoData
.AsNoTracking()
.OrderByDescending(x => x.Lotto)
.ToList();
}
return dbResult;
}
/// <summary>
/// check lotto
/// </summary>
/// <param name="lotto"></param>
/// <returns></returns>
public bool IsLotto(string lotto)
{
List<AnagLottoModel> dbResult = new List<AnagLottoModel>();
bool answ = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbLottoData
.Where(x => x.Lotto == lotto)
.AsNoTracking()
.OrderByDescending(x => x.Lotto)
.ToList();
if (dbResult.Count == 1)
{
answ = true;
}
}
return answ;
}
#endregion gestione lotti
#region gestione sessione
/// <summary>
/// delete sessione
/// </summary>
/// <returns></returns>
public async Task<bool> deleteSessione(InventorySessionModel record)
{
bool fatto = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
try
{
dbCtx
.DbInveSess
.Remove(record);
await dbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante deleteSessione{Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// insert di un record sessione
/// </summary>
/// <returns></returns>
public async Task<bool> InsertNewSessione(InventorySessionModel newRec)
{
bool fatto = false;
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
try
{
dbCtx
.DbInveSess
.Add(newRec);
await dbCtx.SaveChangesAsync();
fatto = true;
}
catch (Exception exc)
{
Log.Error($"Eccezione durante InsertNewSessione{Environment.NewLine}{exc}");
}
}
return fatto;
}
/// <summary>
/// Elenco Inventari tipo Azienda (TUTTI, chiusi e paerti) filtrati x data
/// </summary>
/// <param name="FromDate"></param>
/// <param name="ToDate"></param>
/// <returns></returns>
public List<InventorySessionModel> InventSessHistList(DateTime FromDate, DateTime ToDate)
{
List<InventorySessionModel> dbResult = new List<InventorySessionModel>();
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbInveSess
.Where(x => x.DtStart >= FromDate && x.DtStart <= ToDate && x.DtEnd != null)
.Include(m => m.AnagMagNav)
.AsNoTracking()
.OrderByDescending(x => x.DtStart)
.ToList();
}
return dbResult;
}
/// <summary>
/// Elenco Inventari CORRENTI (=aperti, senza data fine)
/// </summary>
/// <returns></returns>
public List<InventorySessionModel> InventSessCurrList()
{
List<InventorySessionModel> dbResult = new List<InventorySessionModel>();
using (var dbCtx = new MoonPro_InveContext(_configuration))
{
dbResult = dbCtx
.DbInveSess
.Where(x => x.DtEnd == null)
.Include(m => m.AnagMagNav)
.AsNoTracking()
.OrderByDescending(x => x.DtStart)
.ToList();
}
return dbResult;
}
#endregion gestione sessione
#endregion Public Methods
#region Private Fields
private static IConfiguration _configuration;
private static NLog.Logger Log = LogManager.GetCurrentClassLogger();
#endregion Private Fields
}
}