using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GB5Exception; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.Response; using MMDAL.CustomCode.InspectionParamSetOverride; using MMDAL.DTO.InspectionParamSetOverride; using MMDAL.DTO.InspectionType; using MMDAL.DTO.Item; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; namespace MMBLL.InspectionParamSetOverride { public class InspectionParamSetOverrideBLL : IInspectionParamSetOverrideBLL { private readonly IInspectionParamSetOverrideDAL _dal; private readonly AutoNumber _autoNumber; public InspectionParamSetOverrideBLL(IInspectionParamSetOverrideDAL dal, AutoNumber autoNumber) { _dal = dal; _autoNumber = autoNumber; } public async Task GetInspectionParamSetOverride(int inspectionTypeParameterSetId, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetInspectionParamSetOverride(inspectionTypeParameterSetId, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetInspectionParamSetOverrideList(InspectionParamSetOverrideListRequestDTO request, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetInspectionParamSetOverrideList(request, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task SaveInspectionParamSetOverride(InspectionTypeParameterSetDTO dto, LoginDTO loginDTO, CancellationToken ct) { if (dto == null) throw new ArgumentNullException(nameof(dto)); if (dto.ParameterSetId == 0) throw new ArgumentException("ParameterSetId is required.", nameof(dto.ParameterSetId)); if (dto.InspectionTypeId == 0) throw new ArgumentException("InspectionTypeId is required.", nameof(dto.InspectionTypeId)); // Reserved for the InspectionType master screen — never let this endpoint // create/update what would look like a default (org-wide) bridge row. if (dto.PartyId == -1 && dto.ItemId == -1) throw new MethodNotAllowedException( "PartyId=-1 and ItemId=-1 together is reserved for the InspectionType master screen. " + "At least one of PartyId/ItemId must be a concrete value to create an override."); AutoNumberDTO? autoNumberDTO = null; bool isNew = dto.InspectionTypeParameterSetId == 0; try { dto.ModifiedById = loginDTO.UserId; dto.ModifiedOn = DateTime.UtcNow; if (isNew) { // Same table, same ID space as InspectionType's own bridge rows — // reuse the existing "INSPECTIONTYPE" AutoNumber sequence. autoNumberDTO = await _autoNumber.GetNumberAsync(1, "INSPECTIONTYPE", loginDTO); dto.InspectionTypeParameterSetId = autoNumberDTO.StartNumber; dto.Status = 1; dto.Version = 1; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; // SlNo is computed here, not client-supplied — matches UX_MITPS_SCOPE_SLNO // (INSPECTIONTYPEID, PARTYID, ITEMID, SLNO), so two clients guessing the // same SlNo can never collide. int maxSlNo = await _dal.GetMaxSlNo(dto.InspectionTypeId, dto.PartyId, dto.ItemId, loginDTO, ct).ConfigureAwait(false); dto.SlNo = (short)(maxSlNo + 1); await _dal.SaveInspectionParamSetOverride(dto, loginDTO, ct).ConfigureAwait(false); return $"{SuccessResponse.SaveSuccess}"; } else { await _dal.UpdateInspectionParamSetOverride(dto, loginDTO, ct).ConfigureAwait(false); return $"{SuccessResponse.UpdateSuccess}"; } } catch { if (autoNumberDTO != null && dto.InspectionTypeParameterSetId > 0) await _autoNumber.RollbackAutoNumber("INSPECTIONTYPE", autoNumberDTO.StartNumber, loginDTO); throw; } } public async Task DeleteInspectionParamSetOverride(int inspectionTypeParameterSetId, LoginDTO loginDTO, CancellationToken ct) { try { await _dal.DeleteInspectionParamSetOverride(inspectionTypeParameterSetId, loginDTO, ct).ConfigureAwait(false); return $"{SuccessResponse.DeleteSuccessMessage}"; } catch (Exception ex) { throw new Exception($"Error deleting InspectionParamSetOverride ID {inspectionTypeParameterSetId}", ex); } } public async Task GetPicklistInspectionParamSetOverride(int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetPicklistInspectionParamSetOverride(firstNumber, maxResult, criteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task> GetInspectionParamSetOverrideByCriteria( int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { if (criteriaDTO?.SectionCriteriaList == null || criteriaDTO.SectionCriteriaList.Count == 0) throw new ArgumentException("At least one criteria section is required to fetch InspectionParamSetOverride."); try { return await _dal.GetInspectionParamSetOverrideByCriteria(firstNumber, maxResult, criteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task> GetInspectionParamSetOverrideDetailByCriteria( int firstNumber, int maxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO, CancellationToken ct) { if (criteriaDTO?.SectionCriteriaList == null || criteriaDTO.SectionCriteriaList.Count == 0) throw new ArgumentException("At least one criteria section is required to fetch InspectionParamSetOverride parameter details."); try { return await _dal.GetInspectionParamSetOverrideDetailByCriteria(firstNumber, maxResult, criteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } } }