using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using System.ComponentModel.DataAnnotations; using System.Text.Json; using TMSDAL.CustomCode.EvaluationInsight; using TMSDAL.DTO.EvaluationInsight; namespace TMSBLL.EvaluationInsight { public class EvaluationInsightBLL : IEvaluationInsightBLL { private readonly IEvaluationInsightDAL _EvaluationInsightDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public EvaluationInsightBLL( IEvaluationInsightDAL evaluationInsightDAL, AutoNumber autoNumber, IValidation validation) { _EvaluationInsightDAL = evaluationInsightDAL; _AutoNumber = autoNumber; _Validation = validation; } public async Task GetEvaluationInsight(int evaluationInsightId, LoginDTO loginDTO) { try { return await _EvaluationInsightDAL.GetEvaluationInsight(evaluationInsightId, loginDTO); } catch (Exception) { throw; } } public async Task GetEvaluationInsightList(int evaluationPlanId, LoginDTO loginDTO) { try { return await _EvaluationInsightDAL.GetEvaluationInsightList(evaluationPlanId, loginDTO); } catch (Exception) { throw; } } /// /// Save or Update insight. /// InsightId = 0 → INSERT with AutoNumber. /// InsightId > 0 → UPDATE. /// public async Task SaveEvaluationInsight(EvaluationInsightDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { dto.TenantId = loginDTO.ClientId; dto.ModifiedById = loginDTO.UserId; dto.ModifiedOn = DateTime.UtcNow; string message; if (dto.EvaluationInsightId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "EVALUATIONINSIGHT", loginDTO); dto.EvaluationInsightId = autoNumberDTO.StartNumber; dto.GeneratedById = loginDTO.UserId; dto.GeneratedOn = DateTime.UtcNow; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; await _EvaluationInsightDAL.SaveEvaluationInsight(dto, loginDTO); var savedJson = await _EvaluationInsightDAL.GetEvaluationInsight(dto.EvaluationInsightId, loginDTO); var saved = JsonSerializer.Deserialize( savedJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); message = string.Format(SuccessResponse.SaveSuccessWithName, saved?.PlanTitle); } else { await _EvaluationInsightDAL.UpdateEvaluationInsight(dto, loginDTO); var savedJson = await _EvaluationInsightDAL.GetEvaluationInsight(dto.EvaluationInsightId, loginDTO); var saved = JsonSerializer.Deserialize( savedJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); message = string.Format(SuccessResponse.UpdateSuccessWithName, saved?.PlanTitle); } return message; } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && dto.EvaluationInsightId > 0) await _AutoNumber.RollbackAutoNumber("EVALUATIONINSIGHT", dto.EvaluationInsightId, loginDTO); throw; } } public async Task DeleteEvaluationInsight(int evaluationInsightId, LoginDTO loginDTO) { try { return await _EvaluationInsightDAL.DeleteEvaluationInsight(evaluationInsightId, loginDTO); } catch (Exception) { throw; } } } }