using System.ComponentModel.DataAnnotations; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.EntityHandler; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Telemetry; using GB5Shared.Validation; using MarketingDAL.CustomCode.NDPHypothesis; using MarketingDAL.DTO.NDPHypothesis; using Microsoft.Extensions.Logging; using static GB5Shared.GB5Constant.Constant; namespace MarketingBLL.NDPHypothesis; public class NDPHypothesisBLL : INDPHypothesisBLL { private readonly INDPHypothesisDAL _NDPHypothesisDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; private readonly BaseEntityAppService _BaseEntityAppService; private readonly ILogger _Logger; public NDPHypothesisBLL( INDPHypothesisDAL ndpHypothesisDAL, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate, BaseEntityAppService baseEntityAppService, ILogger logger) { _NDPHypothesisDAL = ndpHypothesisDAL; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; _BaseEntityAppService = baseEntityAppService; _Logger = logger; } public async Task GetNDPHypothesis(int ndpHypothesisId, LoginDTO loginDTO, CancellationToken ct) { try { return await _NDPHypothesisDAL.GetNDPHypothesis(ndpHypothesisId, loginDTO, ct).ConfigureAwait(false); } catch (Exception ex) { GB5Trace.MarkFailed("get-ndphypothesis-failed", ex); _Logger.LogError(ex, "GetNDPHypothesis failed for NDPHypothesisId {NDPHypothesisId}", ndpHypothesisId); throw; } } public async Task SaveNDPHypothesis(NDPHypothesisDTO ndpHypothesisDTO, LoginDTO loginDTO, CancellationToken ct) { if (ndpHypothesisDTO == null) throw new ArgumentNullException(nameof(ndpHypothesisDTO)); AutoNumberDTO? autoNumberDTO = null; try { GB5Trace.Step("validate-ndphypothesis", new { ndpHypothesisDTO.NDPHypothesisId }); await _Validation.NotEmpty(ndpHypothesisDTO.NDPText, nameof(ndpHypothesisDTO.NDPText)); await _Validation.Range((byte)1, (byte)3, ndpHypothesisDTO.NDPType, nameof(ndpHypothesisDTO.NDPType)); await _Validation.Range((byte)1, (byte)2, ndpHypothesisDTO.Dimension, nameof(ndpHypothesisDTO.Dimension)); await _Validation.Range((byte)1, (byte)10, ndpHypothesisDTO.IntensityRating, nameof(ndpHypothesisDTO.IntensityRating)); ndpHypothesisDTO.ModifiedById = loginDTO.UserId; ndpHypothesisDTO.ModifiedOn = DateTime.UtcNow; bool isNew = ndpHypothesisDTO.NDPHypothesisId == 0; if (isNew) { autoNumberDTO = await _AutoNumber.GetNumberAsync(1, "NDPHYPOTHESIS", loginDTO).ConfigureAwait(false); ndpHypothesisDTO.NDPHypothesisId = autoNumberDTO.StartNumber; ndpHypothesisDTO.CreatedById = loginDTO.UserId; ndpHypothesisDTO.CreatedOn = DateTime.UtcNow; } GB5Trace.Step("save-ndphypothesis", new { ndpHypothesisDTO.NDPHypothesisId, isNew }); await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTMARKETINGNDPHYPOTHESIS, isNew ? EventTypeConstant.SAVEMARKETINGNDPHYPOTHESISEVENTTYPEID : EventTypeConstant.UPDATEMARKETINGNDPHYPOTHESISEVENTTYPEID, ndpHypothesisDTO, loginDTO, async tx => { _ = isNew ? await _NDPHypothesisDAL.SaveNDPHypothesis(ndpHypothesisDTO, loginDTO, tx, ct).ConfigureAwait(false) : await _NDPHypothesisDAL.UpdateNDPHypothesis(ndpHypothesisDTO, loginDTO, tx, ct).ConfigureAwait(false); return ndpHypothesisDTO.NDPHypothesisId; }, null, -1, -1, null, isNew).ConfigureAwait(false); GB5Trace.Step("event-publish", new { EventTypeId = isNew ? EventTypeConstant.SAVEMARKETINGNDPHYPOTHESISEVENTTYPEID : EventTypeConstant.UPDATEMARKETINGNDPHYPOTHESISEVENTTYPEID }); var cacheKey = new CacheKeyGeneration().KeyGeneration( ndpHypothesisDTO.NDPHypothesisId, EntityConstant.OBJECTMARKETINGNDPHYPOTHESIS, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); _Logger.LogInformation("NDPHypothesis {NDPHypothesisId} {Action} by user {UserId}", ndpHypothesisDTO.NDPHypothesisId, isNew ? "saved" : "updated", loginDTO.UserId); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {ndpHypothesisDTO.NDPHypothesisId}" : $"{SuccessResponse.UpdateSuccessMessage} {ndpHypothesisDTO.NDPHypothesisId}"; } catch (ValidationException vex) { GB5Trace.MarkFailed("save-ndphypothesis-failed", vex); if (autoNumberDTO != null && ndpHypothesisDTO.NDPHypothesisId > 0) await _AutoNumber.RollbackAutoNumber("NDPHYPOTHESIS", ndpHypothesisDTO.NDPHypothesisId, loginDTO).ConfigureAwait(false); throw new Exception(vex.Message); } catch (Exception ex) { GB5Trace.MarkFailed("save-ndphypothesis-failed", ex); _Logger.LogError(ex, "SaveNDPHypothesis failed for NDPHypothesisId {NDPHypothesisId}", ndpHypothesisDTO.NDPHypothesisId); if (autoNumberDTO != null && ndpHypothesisDTO.NDPHypothesisId > 0) await _AutoNumber.RollbackAutoNumber("NDPHYPOTHESIS", ndpHypothesisDTO.NDPHypothesisId, loginDTO).ConfigureAwait(false); throw; } } public async Task GetSelectListNDPHypothesis(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { return await _NDPHypothesisDAL.GetSelectListNDPHypothesis(criteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception ex) { GB5Trace.MarkFailed("get-selectlist-ndphypothesis-failed", ex); _Logger.LogError(ex, "GetSelectListNDPHypothesis failed"); throw; } } public async Task DeleteNDPHypothesis(int ndpHypothesisId, LoginDTO loginDTO, CancellationToken ct) { try { GB5Trace.Step("delete-ndphypothesis", new { ndpHypothesisId }); var result = await _NDPHypothesisDAL.DeleteNDPHypothesis(ndpHypothesisId, loginDTO, ct).ConfigureAwait(false); var cacheKey = new CacheKeyGeneration().KeyGeneration( ndpHypothesisId, EntityConstant.OBJECTMARKETINGNDPHYPOTHESIS, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); _Logger.LogInformation("NDPHypothesis {NDPHypothesisId} deleted by user {UserId}", ndpHypothesisId, loginDTO.UserId); return result; } catch (Exception ex) { GB5Trace.MarkFailed("delete-ndphypothesis-failed", ex); _Logger.LogError(ex, "DeleteNDPHypothesis failed for NDPHypothesisId {NDPHypothesisId}", ndpHypothesisId); throw; } } }