using EntitlementBLL.Interfaces; using EntitlementDAL.DTOs; using EntitlementDAL.Interfaces; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using static GB5Shared.GB5Constant.Constant; namespace EntitlementBLL.Implementations; public class PlanBLL : IPlanBLL { private readonly IPlanDAL _PlanDAL; private readonly AutoNumber _AutoNumber; private readonly IQueryExecutor _QueryExecutor; public PlanBLL(IPlanDAL planDAL, AutoNumber autoNumber, IQueryExecutor queryExecutor) { _PlanDAL = planDAL; _AutoNumber = autoNumber; _QueryExecutor = queryExecutor; } public async Task GetAsync(int planId, LoginDTO login, CancellationToken ct) => await _PlanDAL.GetByIdAsync(planId, login, ct).ConfigureAwait(false); public async Task> GetListForPartnerProductAsync(int? partnerProductId, LoginDTO login, CancellationToken ct) => await _PlanDAL.GetListByPartnerProductAsync(partnerProductId, login, ct).ConfigureAwait(false); public async Task> GetPlanFeaturesAsync(int planId, LoginDTO login, CancellationToken ct) => await _PlanDAL.GetPlanFeatureDetailsAsync(planId, login, ct).ConfigureAwait(false); public async Task SaveAsync(PlanDTO dto, LoginDTO login, CancellationToken ct) { if (dto is null) throw new ArgumentNullException(nameof(dto)); if (string.IsNullOrWhiteSpace(dto.PlanCode)) throw new ArgumentException("PlanCode is required.", nameof(dto)); if (string.IsNullOrWhiteSpace(dto.PlanName)) throw new ArgumentException("PlanName is required.", nameof(dto)); bool isNew = dto.PlanId == 0; var tx = await _QueryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { GB5Trace.Step("validate-entitlement-plan", new { dto.PlanId, isNew }); var now = DateTime.UtcNow; if (isNew) { var auto = await _AutoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.ENTITLEMENTPLAN, login).ConfigureAwait(false); dto.PlanId = auto.StartNumber; dto.CreatedById = login.UserId; dto.CreatedOn = now; } dto.ModifiedById = login.UserId; dto.ModifiedOn = now; GB5Trace.Step("save-entitlement-plan", new { dto.PlanId, isNew }); if (isNew) await _PlanDAL.SaveAsync(dto, login, tx, ct).ConfigureAwait(false); else await _PlanDAL.UpdateAsync(dto, login, tx, ct).ConfigureAwait(false); await _QueryExecutor.CommitAsync(tx).ConfigureAwait(false); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.PlanId}" : SuccessResponse.UpdateSuccess; } catch (Exception ex) { await _QueryExecutor.RollbackAsync(tx).ConfigureAwait(false); GB5Trace.MarkFailed("save-entitlement-plan-failed", ex); throw; } } public async Task DeleteAsync(int planId, LoginDTO login, CancellationToken ct) { var tx = await _QueryExecutor.BeginTransactionAsync(login).ConfigureAwait(false); try { GB5Trace.Step("delete-entitlement-plan", new { planId }); await _PlanDAL.DeleteAsync(planId, login, tx, ct).ConfigureAwait(false); await _QueryExecutor.CommitAsync(tx).ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } catch (Exception ex) { await _QueryExecutor.RollbackAsync(tx).ConfigureAwait(false); GB5Trace.MarkFailed("delete-entitlement-plan-failed", ex); throw; } } }