using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using GB5Shared.Validation; using MMDAL.CustomCode.ParameterSet; using MMDAL.DTO.ParameterSet; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MMBLL.ParameterSet { public class ParameterSetBLL : IParameterSetBLL { private readonly IParameterSetDAL _ParameterSetDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ParameterSetBLL(IParameterSetDAL ParameterSetDAL, AutoNumber autoNumber, IValidation validation) { _ParameterSetDAL = ParameterSetDAL; _AutoNumber = autoNumber; _Validation = validation; } public async Task GetParameterSet(int parameterSetId, int? partyId, int? itemId, LoginDTO loginDTO) { try { return await _ParameterSetDAL.GetParameterSet(parameterSetId, partyId, itemId, loginDTO); } catch (Exception) { throw; } } public async Task GetSelectListParameterSet(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _ParameterSetDAL.GetSelectListParameterSet(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveParameterSet(ParameterSetDTO ParameterSetDTO, LoginDTO LoginDTO) { if (ParameterSetDTO == null) throw new ArgumentNullException(nameof(ParameterSetDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(ParameterSetDTO.ParameterSetCode, nameof(ParameterSetDTO.ParameterSetCode)); await _Validation.NotEmpty(ParameterSetDTO.ParameterSetName, nameof(ParameterSetDTO.ParameterSetName)); ParameterSetDTO.ParameterSetModifiedById = LoginDTO.UserId; ParameterSetDTO.ParameterSetModifiedOn = DateTime.UtcNow; bool isNew = ParameterSetDTO.ParameterSetId == 0; if (isNew) { int detailCount = ParameterSetDTO.ParameterSetDetailArray?.Count ?? 0; int totalRequired = 1 + detailCount; autoNumberDTO = await _AutoNumber.GetNumberAsync(totalRequired, "PARAMETERSET", LoginDTO); int nextId = autoNumberDTO.StartNumber; ParameterSetDTO.ParameterSetId = nextId++; ParameterSetDTO.ParameterSetCreatedById = LoginDTO.UserId; ParameterSetDTO.ParameterSetCreatedOn = DateTime.UtcNow; if (ParameterSetDTO.ParameterSetDetailArray?.Count > 0) { foreach (var detail in ParameterSetDTO.ParameterSetDetailArray) { detail.ParameterSetDetailLineId = nextId++; detail.ParameterSetId = ParameterSetDTO.ParameterSetId; } } await _ParameterSetDAL.SaveParameterSet(ParameterSetDTO, LoginDTO); return $"{SuccessResponse.SaveSuccess}"; } else { await _ParameterSetDAL.UpdateParameterSet(ParameterSetDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccess}"; } } catch { if (autoNumberDTO != null && ParameterSetDTO.ParameterSetId > 0) { await _AutoNumber.RollbackAutoNumber("PARAMETERSET", autoNumberDTO.StartNumber, LoginDTO); } throw; } } public async Task DeleteParameterSet(int ParameterSetId, LoginDTO LoginDTO) { try { var detailsResult = await _ParameterSetDAL.DeleteParameterSetDetail(ParameterSetId, LoginDTO); if (detailsResult <= 0) return $"{ErrorResponse.DeleteNotFoundMessage}"; var parentResult = await _ParameterSetDAL.DeleteParameterSet(ParameterSetId, LoginDTO); if (parentResult <= 0) return $"{ErrorResponse.DeleteNotFoundMessage}"; return $"{SuccessResponse.DeleteSuccessMessage}"; } catch (Exception ex) { throw new Exception($"Error While Deleting ParameterSet Id {ParameterSetId}", ex); } } } }