Files
lux/EgwCoreLib.Lux.Data/Services/Items/ItemService.cs
T
Samuele E. Locatelli (W11-AI) 3c923ecf97 Continuo fix commenti
2026-03-25 11:05:52 +01:00

178 lines
5.8 KiB
C#

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
/// <inheritdoc />
public async Task<bool> 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;
});
}
/// <inheritdoc />
public async Task<List<ItemModel>> 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
);
});
}
/// <inheritdoc />
public async Task<List<BomItemDTO>> GetBomFixItemIdAsync(List<BomItemDTO> listOrg)
{
return await TraceAsync($"{_className}.GetBomFixItemIdAsync", async (activity) =>
{
List<BomItemDTO> 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;
});
}
/// <inheritdoc />
public async Task<List<ItemModel>> 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
);
});
}
/// <inheritdoc />
public async Task<List<ItemModel>> GetSearchAsync(string term)
{
return await TraceAsync($"{_className}.GetSearch", async (activity) =>
{
return await GetOrSetCacheAsync(
$"{_redisBaseKey}:{_className}:{term}",
async () => await _repo.GetSearchAsync(term),
UltraLongCache
);
});
}
/// <inheritdoc />
public async Task<bool> MassUpdateAsync(List<BomItemDTO> 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;
});
}
/// <inheritdoc />
public async Task<bool> 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;
});
}
/// <inheritdoc />
public async Task<bool> UpsertFromBomAsync(List<BomItemDTO> 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
}
}