using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
using FrameworkDAL.CustomCode.ECMRights;
using FrameworkDAL.DTO.ECMRights;
using GB5Shared.DTO.Framework.AutoNumber;
using GB5Shared.DTO.Framework.Criteria;
using GB5Shared.DTO.Framework.Login;
using GB5Shared.GenerateAutoNumber;
using GB5Shared.Resource.GB5Solution;
using GB5Shared.Resource.Response;
using GB5Shared.Validation;
namespace FrameworkBLL.ECMRights
{
public class ECMRightsBLL : IECMRightsBLL
{
private readonly IECMRightsDAL _EcmRightsDAL;
private readonly AutoNumber _AutoNumber;
private readonly IValidation _Validation;
public ECMRightsBLL(IECMRightsDAL EcmRightsDAL, AutoNumber AutoNumber, IValidation Validation)
{
_EcmRightsDAL = EcmRightsDAL;
_AutoNumber = AutoNumber;
_Validation = Validation;
}
///
/// Retrieves a single ECMRights record by its unique identifier.
///
public async Task GetECMRights(int ECMRightsId, LoginDTO LoginDTO)
{
try
{
return await _EcmRightsDAL.GetECMRights(ECMRightsId, LoginDTO);
}
catch (Exception)
{
throw;
}
}
///
/// Inserts a new ECMRights record or updates an existing one.
/// Applies business rules such as auto-number assignment for new records.
///
public async Task SaveECMRights(ECMRightsDTO ECMRightsDTO, LoginDTO LoginDTO)
{
if (ECMRightsDTO == null) throw new ArgumentNullException(nameof(ECMRightsDTO));
AutoNumberDTO? autoNumberDTO = null;
try
{
ECMRightsDTO.ECMRightsModifiedById = LoginDTO.UserId;
ECMRightsDTO.ECMRightsModifiedOn = DateTime.UtcNow;
int initialId = ECMRightsDTO.ECMRightsId;
int count = 1 + initialId;
if (initialId == 0)
{
AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "ECMRights", LoginDTO);
ECMRightsDTO.ECMRightsId = AutoNumberDTO.StartNumber;
ECMRightsDTO.ECMRightsCreatedById = LoginDTO.UserId;
ECMRightsDTO.ECMRightsCreatedOn = DateTime.UtcNow;
await _EcmRightsDAL.SaveECMRights(ECMRightsDTO, LoginDTO);
return $"{SuccessResponse.SaveSuccessMessage} {ECMRightsDTO.ECMRightsId}";
}
else
{
await _EcmRightsDAL.UpdateECMRights(ECMRightsDTO, LoginDTO);
return $"{SuccessResponse.UpdateSuccessMessage} {ECMRightsDTO.ECMRightsId}";
}
}
catch (ValidationException vex)
{
throw new Exception(vex.Message);
}
catch (Exception)
{
if (autoNumberDTO != null && ECMRightsDTO.ECMRightsId > 0)
await _AutoNumber.RollbackAutoNumber("ECMRights", ECMRightsDTO.ECMRightsId, LoginDTO);
throw;
}
}
///
/// Retrieves a paginated list of ECMRights records based on specified filter criteria.
/// Can optionally return the total count of records instead of the list.
///
/// The starting index of records to fetch (for pagination).
/// The maximum number of records to fetch.
/// Filter criteria for selecting ECMRights.
/// The current user's login details.
/// If true, returns the total count of matching records instead of the list.
/// A JSON string representing either the list of ECMRights records or the total count.
public async Task GetSelectListECMRights(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO LoginDTO, bool isCount = false)
{
try
{
return await _EcmRightsDAL.GetSelectListECMRights(firstNumber, maxResult, criteriaDTO, LoginDTO);
}
catch (Exception)
{
throw;
}
}
///
/// Deletes an ECMRights record by its unique identifier.
///
public async Task DeleteECMRights(int ECMRightsId, LoginDTO LoginDTO)
{
try
{
return await _EcmRightsDAL.DeleteECMRights(ECMRightsId, LoginDTO);
}
catch (Exception)
{
throw;
}
}
}
}