using FAMDAL.CustomCode.Depreciation; using FAMDAL.DTO.Depreciation; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using Microsoft.Extensions.Logging; using System; using System.Threading; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace FAMBLL.Depreciation { public class DepreciationBLL : IDepreciationBLL { private readonly IDepreciationDAL _DepreciationDAL; private readonly IQueryExecutor _queryExecutor; private readonly AutoNumber _autoNumber; private readonly KeyInvalidate _keyInvalidate; private readonly ILogger _logger; public DepreciationBLL( IDepreciationDAL IDepreciationDAL, IQueryExecutor queryExecutor, AutoNumber autoNumber, KeyInvalidate keyInvalidate, ILogger logger) { _DepreciationDAL = IDepreciationDAL; _queryExecutor = queryExecutor; _autoNumber = autoNumber; _keyInvalidate = keyInvalidate; _logger = logger; } public async Task GetDepreciation(int DepreciationId, LoginDTO LoginDTO) { try { return await _DepreciationDAL.GetDepreciation(DepreciationId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListDepreciation(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _DepreciationDAL.GetSelectListDepreciation(FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListDepreciation(LoginDTO login, CancellationToken ct) => await _DepreciationDAL.GetSelectListDepreciation(login, ct).ConfigureAwait(false); public async Task SaveDepreciation(DepreciationDTO dto, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("validate-depreciation", new { dto.DepreciationId }); if (string.IsNullOrWhiteSpace(dto.DepreciationCode)) throw new ArgumentException("DepreciationCode is required"); if (string.IsNullOrWhiteSpace(dto.DepreciationName)) throw new ArgumentException("DepreciationName is required"); bool isNew = dto.DepreciationId == 0; dto.DepreciationTenantId = login.ClientId; GB5Trace.Step("save-depreciation", new { dto.DepreciationId, isNew }); await using var tx = await _queryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { if (isNew) { var auto = await _autoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.DEPRECIATION, login).ConfigureAwait(false); dto.DepreciationId = auto.StartNumber; await _DepreciationDAL.SaveDepreciation(dto, login, tx, ct).ConfigureAwait(false); } else { await _DepreciationDAL.UpdateDepreciation(dto, 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.DepreciationId, EntityConstant.OBJECTDEPRECIATION, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.DepreciationId}" : SuccessResponse.UpdateSuccess; } catch (Exception ex) { GB5Trace.MarkFailed("save-depreciation-failed", ex); _logger.LogError(ex, "SaveDepreciation failed for DepreciationId {Id}", dto.DepreciationId); throw; } } public async Task DeleteDepreciation(int depreciationId, LoginDTO login, CancellationToken ct) { try { GB5Trace.Step("delete-depreciation", new { depreciationId }); var result = await _DepreciationDAL.DeleteDepreciation(depreciationId, login, ct).ConfigureAwait(false); var cacheKey = new CacheKeyGeneration().KeyGeneration(depreciationId, EntityConstant.OBJECTDEPRECIATION, CacheKeyLevel.CLIENT_LEVEL, login); await _keyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return result.IsSuccess ? SuccessResponse.DeleteSuccessMessage : result.Error; } catch (Exception ex) { GB5Trace.MarkFailed("delete-depreciation-failed", ex); _logger.LogError(ex, "DeleteDepreciation failed for DepreciationId {Id}", depreciationId); throw; } } } }