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 /// public async Task 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; }); } /// public async Task 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; }); } /// public async Task> 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(); // 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; }); } /// public async Task> GetAllAsync() { return await TraceAsync($"{_className}.GetAllAsync", async (activity) => { return await GetOrSetCacheAsync( $"{_redisBaseKey}:{_className}:ALL", async () => await _repo.GetAllWithNavAsync(), UltraLongCache ); }); } /// public async Task> GetByParentAsync(int templateId) { return await TraceAsync($"{_className}.GetByParent", async (activity) => { return await GetOrSetCacheAsync( $"{_redisBaseKey}:{_className}:{templateId}", async () => await _repo.GetRowsAsync(templateId), LongCache ); }); } /// /// Aggiorna stato await per riga template /// /// ID Riga Template da aggiornare /// Nuovo valore flag AwaitBom /// Nuovo valore flag AwaitPrice /// Se true, invalida cache correlata /// public async Task 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; }); } /// public async Task 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; }); } /// public async Task 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; }); } /// public async Task 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 } }