Files
lux/EgwCoreLib.Lux.Data/Services/Catalog/TemplateRowService.cs
T
2026-05-29 19:21:45 +02:00

273 lines
9.1 KiB
C#

namespace EgwCoreLib.Lux.Data.Services.Catalog
{
public class TemplateRowService : BaseServ, ITemplateRowService
{
#region Public Constructors
public TemplateRowService(
IConfiguration config,
IConnectionMultiplexer redis,
ITemplateRowRepository repo) : base(config, redis)
{
_className = "TemplateRow";
_repo = repo;
}
#endregion Public Constructors
#region Public Methods
/// <inheritdoc />
public async Task<bool> CloneAsync(TemplateRowModel rec2clone)
{
return await TraceAsync($"{_className}.Clone", async (activity) =>
{
// eseguo clone
var result = await _repo.CloneAsync(rec2clone);
// se eseguito, pulisco la cache correlata
if (result)
{
// Invalido sia la lista classi che eventuali dettagli correlati
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return result;
});
}
/// <inheritdoc />
public async Task<bool> DeleteAsync(TemplateRowModel rec2del)
{
return await TraceAsync($"{_className}.Delete", async (activity) =>
{
var dbResult = await _repo.GetRowAsync(rec2del.TemplateRowID);
if (dbResult == null) return false;
bool success = await _repo.DeleteAsync(dbResult);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
/// <inheritdoc />
public async Task<List<string>> FixRowUidAsync(int templateId)
{
return await TraceAsync($"{_className}.FixRowUidAsync", async (activity) =>
{
// 1. Recupero righe
var rows = await _repo.GetRowsAsync(templateId);
// 2. Trovo quelle da sistemare
var list2fix = rows
.Where(x => string.IsNullOrEmpty(x.TemplateRowUID) ||
x.TemplateRowUID != x.TemplateRowDtx)
.ToList();
// 3. Se non c’è nulla da fare → ritorno
if (list2fix.Count == 0)
return new List<string>();
// 4. Preparo la lista da restituire
var result = list2fix
.Select(x => x.TemplateRowDtx)
.ToList();
// 5. Aggiorno i record
foreach (var row in list2fix)
row.TemplateRowUID = row.TemplateRowDtx;
// 6. Salvo
bool success = await _repo.SaveRowsAsync(list2fix);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return result;
});
}
/// <inheritdoc />
public async Task<List<TemplateRowModel>> GetAllAsync()
{
return await TraceAsync($"{_className}.GetAllAsync", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:ALL",
async () => await _repo.GetAllWithNavAsync(),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<List<TemplateRowModel>> GetByParentAsync(int templateId)
{
return await TraceAsync($"{_className}.GetByParent", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:{templateId}",
async () => await _repo.GetRowsAsync(templateId),
LongCache
);
});
}
/// <summary>
/// Aggiorna stato await per riga template
/// </summary>
/// <param name="templateRowId">ID Riga Template da aggiornare</param>
/// <param name="awaitBom">Nuovo valore flag AwaitBom</param>
/// <param name="awaitPrice">Nuovo valore flag AwaitPrice</param>
/// <param name="flushCache">Se true, invalida cache correlata</param>
/// <returns></returns>
public async Task<bool> UpdateAwaitStateAsync(int templateRowId, bool? awaitBom, bool? awaitPrice, bool flushCache = false)
{
return await TraceAsync($"{_className}.UpdateAwaitState", async (activity) =>
{
var currRec = await _repo.GetRowAsync(templateRowId);
if (currRec == null)
return false;
if (awaitBom.HasValue)
currRec.AwaitBom = awaitBom.Value;
if (awaitPrice.HasValue)
currRec.AwaitPrice = awaitPrice.Value;
//_repo.Update(currRec);
activity?.SetTag("db.operation", "UPDATE");
bool success = await _repo.UpdateAsync(currRec);
if (success && flushCache)
{
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
/// <inheritdoc />
public async Task<bool> UpdateFileDataAsync(TemplateRowModel updRec)
{
return await TraceAsync($"{_className}.UpdateFileData", async (activity) =>
{
var currRec = await _repo.GetRowAsync(updRec.TemplateRowID);
if (currRec == null)
return false;
currRec.FileName = updRec.FileName;
currRec.FileResource = updRec.FileResource;
currRec.FileSize = updRec.FileSize;
currRec.SerStruct = updRec.SerStruct;
currRec.ImgType = Core.Enums.ImageType.Calculated;
//_repo.Update(currRec);
activity?.SetTag("db.operation", "UPDATE");
bool success = await _repo.UpdateAsync(currRec);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
/// <inheritdoc />
public async Task<bool> UpdateSerStructAsync(int TemplateRowID, string serStruct)
{
return await TraceAsync($"{_className}.UpdateSerStruct", async (activity) =>
{
var currRec = await _repo.GetRowAsync(TemplateRowID);
if (currRec == null)
return false;
currRec.SerStruct = serStruct;
currRec.ImgType = Core.Enums.ImageType.Calculated;
//_repo.Update(currRec);
activity?.SetTag("db.operation", "UPDATE");
bool success = await _repo.UpdateAsync(currRec);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
/// <inheritdoc />
public async Task<bool> UpsertAsync(TemplateRowModel upsRec)
{
return await TraceAsync($"{_className}.Upsert", async (activity) =>
{
// verifico imgType...
if (upsRec.SellingItemNav != null && (upsRec.SellingItemNav.SourceType == ItemSourceType.Jwd || upsRec.SellingItemNav.SourceType == ItemSourceType.FileBTL))
{
upsRec.ImgType = ImageType.Calculated;
}
// cerso sul DB
var currRec = await _repo.GetRowAsync(upsRec.TemplateRowID);
string operation = "UPDATE";
bool success = false;
if (currRec != null)
{
success = await _repo.UpdateAsync(upsRec);
}
else
{
operation = "INSERT";
success = await _repo.AddAsync(upsRec);
}
activity?.SetTag("db.operation", operation);
if (success)
{
await ClearCacheAsync($"{_redisBaseKey}:Template:*");
await ClearCacheAsync($"{_redisBaseKey}:{_className}:*");
}
return success;
});
}
#endregion Public Methods
#region Private Fields
private readonly string _className;
private readonly ITemplateRowRepository _repo;
#endregion Private Fields
}
}