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.EmotionalDriver; using MarketingDAL.DTO.EmotionalDriver; using Microsoft.Extensions.Logging; using static GB5Shared.GB5Constant.Constant; namespace MarketingBLL.EmotionalDriver; public class EmotionalDriverBLL : IEmotionalDriverBLL { private readonly IEmotionalDriverDAL _EmotionalDriverDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; private readonly KeyInvalidate _KeyInvalidate; private readonly BaseEntityAppService _BaseEntityAppService; private readonly ILogger _Logger; public EmotionalDriverBLL( IEmotionalDriverDAL emotionalDriverDAL, AutoNumber autoNumber, IValidation validation, KeyInvalidate keyInvalidate, BaseEntityAppService baseEntityAppService, ILogger logger) { _EmotionalDriverDAL = emotionalDriverDAL; _AutoNumber = autoNumber; _Validation = validation; _KeyInvalidate = keyInvalidate; _BaseEntityAppService = baseEntityAppService; _Logger = logger; } public async Task GetEmotionalDriver(int emotionalDriverId, LoginDTO loginDTO, CancellationToken ct) { try { return await _EmotionalDriverDAL.GetEmotionalDriver(emotionalDriverId, loginDTO, ct).ConfigureAwait(false); } catch (Exception ex) { GB5Trace.MarkFailed("get-emotionaldriver-failed", ex); _Logger.LogError(ex, "GetEmotionalDriver failed for EmotionalDriverId {EmotionalDriverId}", emotionalDriverId); throw; } } public async Task SaveEmotionalDriver(EmotionalDriverDTO emotionalDriverDTO, LoginDTO loginDTO, CancellationToken ct) { if (emotionalDriverDTO == null) throw new ArgumentNullException(nameof(emotionalDriverDTO)); AutoNumberDTO? autoNumberDTO = null; try { GB5Trace.Step("validate-emotionaldriver", new { emotionalDriverDTO.EmotionalDriverId }); await _Validation.NotEmpty(emotionalDriverDTO.Trigger, nameof(emotionalDriverDTO.Trigger)); await _Validation.NotEmpty(emotionalDriverDTO.Response, nameof(emotionalDriverDTO.Response)); await _Validation.Range((byte)1, (byte)10, emotionalDriverDTO.IntensityRating, nameof(emotionalDriverDTO.IntensityRating)); emotionalDriverDTO.ModifiedById = loginDTO.UserId; emotionalDriverDTO.ModifiedOn = DateTime.UtcNow; bool isNew = emotionalDriverDTO.EmotionalDriverId == 0; if (isNew) { autoNumberDTO = await _AutoNumber.GetNumberAsync(1, "EMOTIONALDRIVER", loginDTO).ConfigureAwait(false); emotionalDriverDTO.EmotionalDriverId = autoNumberDTO.StartNumber; emotionalDriverDTO.CreatedById = loginDTO.UserId; emotionalDriverDTO.CreatedOn = DateTime.UtcNow; } GB5Trace.Step("save-emotionaldriver", new { emotionalDriverDTO.EmotionalDriverId, isNew }); await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTMARKETINGEMOTIONALDRIVER, isNew ? EventTypeConstant.SAVEMARKETINGEMOTIONALDRIVEREVENTTYPEID : EventTypeConstant.UPDATEMARKETINGEMOTIONALDRIVEREVENTTYPEID, emotionalDriverDTO, loginDTO, async tx => { _ = isNew ? await _EmotionalDriverDAL.SaveEmotionalDriver(emotionalDriverDTO, loginDTO, tx, ct).ConfigureAwait(false) : await _EmotionalDriverDAL.UpdateEmotionalDriver(emotionalDriverDTO, loginDTO, tx, ct).ConfigureAwait(false); return emotionalDriverDTO.EmotionalDriverId; }, null, -1, -1, null, isNew).ConfigureAwait(false); GB5Trace.Step("event-publish", new { EventTypeId = isNew ? EventTypeConstant.SAVEMARKETINGEMOTIONALDRIVEREVENTTYPEID : EventTypeConstant.UPDATEMARKETINGEMOTIONALDRIVEREVENTTYPEID }); var cacheKey = new CacheKeyGeneration().KeyGeneration( emotionalDriverDTO.EmotionalDriverId, EntityConstant.OBJECTMARKETINGEMOTIONALDRIVER, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); _Logger.LogInformation("EmotionalDriver {EmotionalDriverId} {Action} by user {UserId}", emotionalDriverDTO.EmotionalDriverId, isNew ? "saved" : "updated", loginDTO.UserId); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {emotionalDriverDTO.EmotionalDriverId}" : $"{SuccessResponse.UpdateSuccessMessage} {emotionalDriverDTO.EmotionalDriverId}"; } catch (ValidationException vex) { GB5Trace.MarkFailed("save-emotionaldriver-failed", vex); if (autoNumberDTO != null && emotionalDriverDTO.EmotionalDriverId > 0) await _AutoNumber.RollbackAutoNumber("EMOTIONALDRIVER", emotionalDriverDTO.EmotionalDriverId, loginDTO).ConfigureAwait(false); throw new Exception(vex.Message); } catch (Exception ex) { GB5Trace.MarkFailed("save-emotionaldriver-failed", ex); _Logger.LogError(ex, "SaveEmotionalDriver failed for EmotionalDriverId {EmotionalDriverId}", emotionalDriverDTO.EmotionalDriverId); if (autoNumberDTO != null && emotionalDriverDTO.EmotionalDriverId > 0) await _AutoNumber.RollbackAutoNumber("EMOTIONALDRIVER", emotionalDriverDTO.EmotionalDriverId, loginDTO).ConfigureAwait(false); throw; } } public async Task GetSelectListEmotionalDriver(CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { return await _EmotionalDriverDAL.GetSelectListEmotionalDriver(criteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception ex) { GB5Trace.MarkFailed("get-selectlist-emotionaldriver-failed", ex); _Logger.LogError(ex, "GetSelectListEmotionalDriver failed"); throw; } } public async Task DeleteEmotionalDriver(int emotionalDriverId, LoginDTO loginDTO, CancellationToken ct) { try { GB5Trace.Step("delete-emotionaldriver", new { emotionalDriverId }); var result = await _EmotionalDriverDAL.DeleteEmotionalDriver(emotionalDriverId, loginDTO, ct).ConfigureAwait(false); var cacheKey = new CacheKeyGeneration().KeyGeneration( emotionalDriverId, EntityConstant.OBJECTMARKETINGEMOTIONALDRIVER, CacheKeyLevel.CLIENT_LEVEL, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); _Logger.LogInformation("EmotionalDriver {EmotionalDriverId} deleted by user {UserId}", emotionalDriverId, loginDTO.UserId); return result; } catch (Exception ex) { GB5Trace.MarkFailed("delete-emotionaldriver-failed", ex); _Logger.LogError(ex, "DeleteEmotionalDriver failed for EmotionalDriverId {EmotionalDriverId}", emotionalDriverId); throw; } } }