using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using MaintenanceDAL.CustomCode.TypeComponent; using MaintenanceDAL.DTO.TypeComponent; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace MaintenanceBLL.TypeComponent { public class TypeComponentBLL : ITypeComponentBLL { private readonly ITypeComponentDAL _dal; private readonly IQueryExecutor _queryExecutor; private readonly AutoNumber _autoNumber; private readonly KeyInvalidate _keyInvalidate; private readonly ILogger _logger; public TypeComponentBLL( ITypeComponentDAL dal, IQueryExecutor queryExecutor, AutoNumber autoNumber, KeyInvalidate keyInvalidate, ILogger logger) { _dal = dal; _queryExecutor = queryExecutor; _autoNumber = autoNumber; _keyInvalidate = keyInvalidate; _logger = logger; } public async Task GetTypeComponent(int typeComponentId, LoginDTO login, CancellationToken ct) => await _dal.GetTypeComponent(typeComponentId, login, ct).ConfigureAwait(false); public async Task> GetTypeComponentListByAssetType(int assetTypeId, LoginDTO login, CancellationToken ct) => await _dal.GetTypeComponentListByAssetType(assetTypeId, login, ct).ConfigureAwait(false); public async Task SaveTypeComponent(TypeComponentDTO dto, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("validate-typecomponent", new { dto.TypeComponentId }); bool isNew = dto.TypeComponentId == 0; var newDetailCount = dto.TypeComponentDetailArray?.Count(d => d.TypeComponentDetailId == 0) ?? 0; int nextDetailId = 0; await using var tx = await _queryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { if (isNew) { var auto = await _autoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.TYPECOMPONENT, login).ConfigureAwait(false); dto.TypeComponentId = auto.StartNumber; await _dal.SaveTypeComponent(dto, login, tx, ct).ConfigureAwait(false); } else { await _dal.UpdateTypeComponent(dto, login, tx, ct).ConfigureAwait(false); await _dal.DeleteTypeComponentDetailByComponent(dto.TypeComponentId, login, tx, ct).ConfigureAwait(false); } if (newDetailCount > 0) { var detailAuto = await _autoNumber.GetNumberAsync(newDetailCount, AUTONUMBERCONSTANT.TYPECOMPONENT, login).ConfigureAwait(false); nextDetailId = detailAuto.StartNumber; } GB5Trace.Step("save-typecomponent", new { dto.TypeComponentId, isNew }); if (dto.TypeComponentDetailArray != null) { short slNo = 1; foreach (var detail in dto.TypeComponentDetailArray) { detail.ComponentId = dto.TypeComponentId; detail.TypeComponentDetailSlNo = slNo++; if (detail.TypeComponentDetailId == 0) detail.TypeComponentDetailId = nextDetailId++; await _dal.SaveTypeComponentDetail(detail, login, tx, ct).ConfigureAwait(false); } } await tx.CommitAsync(ct).ConfigureAwait(false); } catch { await tx.RollbackAsync(ct).ConfigureAwait(false); throw; } var cacheKey = new CacheKeyGeneration().KeyGeneration(dto.TypeComponentId, EntityConstant.OBJECTTYPECOMPONENT, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.TypeComponentId}" : SuccessResponse.UpdateSuccess; } catch (Exception ex) { GB5Trace.MarkFailed("save-typecomponent-failed", ex); _logger.LogError(ex, "SaveTypeComponent failed for TypeComponentId {Id}", dto.TypeComponentId); throw; } } public async Task DeleteTypeComponent(int typeComponentId, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("delete-typecomponent", new { typeComponentId }); await using var tx = await _queryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { await _dal.DeleteTypeComponentDetailByComponent(typeComponentId, login, tx, ct).ConfigureAwait(false); await _dal.DeleteTypeComponent(typeComponentId, login, tx, ct).ConfigureAwait(false); await tx.CommitAsync(ct).ConfigureAwait(false); } catch { await tx.RollbackAsync(ct).ConfigureAwait(false); throw; } var cacheKey = new CacheKeyGeneration().KeyGeneration(typeComponentId, EntityConstant.OBJECTTYPECOMPONENT, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } catch (Exception ex) { GB5Trace.MarkFailed("delete-typecomponent-failed", ex); _logger.LogError(ex, "DeleteTypeComponent failed for TypeComponentId {Id}", typeComponentId); throw; } } } }