namespace EgwCoreLib.Lux.Data.Services.Items { public class ItemService : BaseServ, IItemService { #region Public Constructors public ItemService( IConfiguration config, IConnectionMultiplexer redis, IItemRepository repo) : base(config, redis) { _className = "Item"; _repo = repo; } #endregion Public Constructors #region Public Methods /// public async Task DeleteAsync(ItemModel rec2del) { return await TraceAsync($"{_className}.Delete", async (activity) => { var dbResult = await _repo.GetByIdAsync(rec2del.ItemID); if (dbResult == null) return false; bool success = await _repo.DeleteAsync(dbResult); if (success) { await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); } return success; }); } /// public async Task> GetAltAsync(int recId) { // Uso helper TraceAsync che gestisce automaticamente StartActivity, Log e Exception tracking return await TraceAsync($"{_className}.GetAlt", async (activity) => { return await GetOrSetCacheAsync( $"{_redisBaseKey}:{_className}:{recId}", async () => await _repo.GetAltAsync(recId), UltraLongCache ); }); } /// public async Task> GetBomFixItemIdAsync(List listOrg) { return await TraceAsync($"{_className}.GetBomFixItemIdAsync", async (activity) => { List listFix = listOrg; // sistemo ItemID per gli item della BOM... var listItemDB = await _repo.GetFiltAsync("", ItemClassType.Bom); // cerco i record da sistemare ed 1:1 li fixo... foreach (var item in listFix.Where(x => x.ItemID == 0)) { var dbRec = listItemDB.FirstOrDefault(x => x.ExtItemCode == item.ItemCode); if ((dbRec != null)) { item.ItemID = dbRec.ItemID; } } return listFix; }); } /// public async Task> GetFiltAsync(string CodGroup, ItemClassType ItemType) { return await TraceAsync($"{_className}.GetFilt", async (activity) => { return await GetOrSetCacheAsync( $"{_redisBaseKey}:{_className}:{CodGroup}:{ItemType}", async () => await _repo.GetFiltAsync(CodGroup, ItemType), UltraLongCache ); }); } /// public async Task> GetSearchAsync(string term) { return await TraceAsync($"{_className}.GetSearch", async (activity) => { return await GetOrSetCacheAsync( $"{_redisBaseKey}:{_className}:{term}", async () => await _repo.GetSearchAsync(term), UltraLongCache ); }); } /// public async Task MassUpdateAsync(List list2upd, double setCost, double defMargin, double defQtyMax, string defUM, int roundVal = 0, double scaleFactor = 1_000_000.0) { return await TraceAsync($"{_className}.MassUpdate", async (activity) => { string operation = "MassUpdate"; bool success = await _repo.MassUpdateAsync(list2upd, setCost, defMargin, defQtyMax, defUM, roundVal, scaleFactor); activity?.SetTag("db.operation", operation); if (success) { await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); } return success; }); } /// public async Task UpsertAsync(ItemModel upsRec) { return await TraceAsync($"{_className}.Upsert", async (activity) => { // 1. Cerco se esiste già var currRec = await _repo.GetByIdAsync(upsRec.ItemID); 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}:{_className}:*"); } return success; }); } /// public async Task UpsertFromBomAsync(List bomList) { return await TraceAsync($"{_className}.UpsertFromBomAsync", async (activity) => { bool success = await _repo.UpsertFromBomAsync(bomList); activity?.SetTag("db.operation", "UpsertFromBomAsync"); if (success) { await ClearCacheAsync($"{_redisBaseKey}:{_className}:*"); } return success; }); } #endregion Public Methods #region Private Fields private readonly string _className; private readonly IItemRepository _repo; #endregion Private Fields } }