Files
samuele 0e7e0aaed0 packages update
Update gestione generazione fabbisogni a livello di ordine
update metodo API x recupero filtrato x CodGroup
2026-04-30 11:50:24 +02:00

176 lines
6.3 KiB
C#

namespace EgwCoreLib.Lux.Data.Services.Warehouse
{
public class MatReqService : BaseServ, IMatReqService
{
#region Public Constructors
public MatReqService(
IConfiguration config,
IConnectionMultiplexer redis,
IMatReqRepository repo) : base(config, redis)
{
_className = "MatReq";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task ClearClassCache()
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
/// <inheritdoc />
public async Task<List<MatReqModel>> GetByOrderAsync(int orderId)
{
return await TraceAsync($"{_className}.GetByOrderAsync", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ByOrd:{orderId}",
async () => await _repo.GetFiltAsync(orderId, null, null, null),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<List<MatReqModel>> GetByOrderRowAsync(int orderRowId)
{
return await TraceAsync($"{_className}.GetByOrderRowAsync", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ByOrdRow:{orderRowId}",
async () => await _repo.GetFiltAsync(null, orderRowId, null, null),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<List<MatReqModel>> GetUnprocAsync()
{
return await TraceAsync($"{_className}.GetUnprocAsync", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:Unproc",
async () => await _repo.GetFiltAsync(null, null, null, false),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<bool> ReconcileOrderRowsAsync(List<OrderRowModel> ListOrderRow, bool forceReset = false)
{
return await TraceAsync($"{_className}.ReconcileOrderRowsAsync", async (activity) =>
{
List<MatReqModel> listaSpesa = new();
// ciclo sulle righe x trasformare BOM in fabbisogni...
foreach (var currItem in ListOrderRow)
{
var newReq = await GetFabbisogniAsync(currItem, forceReset);
listaSpesa.AddRange(newReq);
}
bool success = await _repo.UpsertManyAsync(listaSpesa);
activity?.SetTag("db.operation", "ReconcileOrderRowsAsync");
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
await ClearCacheAsync($"{_redisBaseKey}:Order:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly IMatReqRepository _repo;
#endregion Private Fields
#region Private Methods
/// <summary>
/// Genera un set di record fabbisogni da un item (RigaOrdine)
/// </summary>
/// <param name="currItem">Singolo record OrderModel</param>
/// <returns></returns>
private async Task<List<MatReqModel>> GetFabbisogniAsync(OrderRowModel currItem, bool forceReset)
{
List<MatReqModel> newReqList = new();
/*--------------------------------------------------
* Calcolo fabbisogni x rigaOrine (Item)
* - recupera eventuali fabbisogni precedente (da mantenere se confermati/ordinati)
* - genera un nuovo set di fabbisogni x ogni riga della BOM dell'ITEM nuova
* - aggiorna le righe non bloccate
*--------------------------------------------------*/
//// eliminazione preliminare...
//await _repo.DeleteByOrderRowAsync(currItem.OrderRowID, forceReset);
// leggo l'elenco attuale dei fabbisogni presenti
var currMatReqList = await _repo.GetFiltAsync(currItem.OrderID, currItem.OrderRowID, null, null);
// ogni riga a 1..n BOM --> per ogni riga BOM 1 fabbisogno...
foreach (var bomItem in currItem.ListBOM)
{
// valorizzo inizialmente come record intero
MatReqModel newReq = new MatReqModel()
{
OrderID = currItem.OrderID,
OrderRowID = currItem.OrderRowID,
CodGroup = bomItem.ClassCode,
ItemCode = bomItem.ItemCode,
Description = bomItem.DescriptionCode,
Inserted = DateTime.Now,
ItemID = bomItem.ItemID > 0 ? bomItem.ItemID : null,
NumItems = bomItem.ItemQty,
Processed = false,
TotQty = bomItem.Qty
};
// cerco nei fabbisogni...
var cMatReq = currMatReqList
.FirstOrDefault(x => x.ItemID == bomItem.ItemID &&
((x.OrderRowID != null && x.OrderRowID == currItem.OrderRowID) ||
(x.OrderID != null && x.OrderID == currItem.OrderID))
);
// se trovato
if (cMatReq != null)
{
// se già processato --> genera nuovo per differenza
if (cMatReq.Processed)
{
newReq.TotQty -= cMatReq.TotQty;
newReqList.Add(newReq);
}
else
{
// altrimenti aggiorno...
cMatReq.TotQty = newReq.TotQty;
newReqList.Add(cMatReq);
}
}
else
{
newReqList.Add(newReq);
}
}
return newReqList;
}
#endregion Private Methods
}
}