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.InspectionType; using MMDAL.DTO.InspectionType; using System; using System.Threading; using System.Threading.Tasks; namespace MMBLL.InspectionType { public class InspectionTypeBLL : IInspectionTypeBLL { private readonly IInspectionTypeDAL _dal; private readonly AutoNumber _autoNumber; private readonly IValidation _validation; public InspectionTypeBLL(IInspectionTypeDAL dal, AutoNumber autoNumber, IValidation validation) { _dal = dal; _autoNumber = autoNumber; _validation = validation; } public async Task GetInspectionType(int inspectionTypeId, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetInspectionType(inspectionTypeId, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetPicklistInspectionType(int FirstNumber, int MaxResult,CriteriaDTO CriteriaDTO,LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetPicklistInspectionType(FirstNumber, MaxResult, CriteriaDTO, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetParameterSetsByInspectionType(int inspectionTypeId, LoginDTO loginDTO, CancellationToken ct) { try { return await _dal.GetParameterSetsByInspectionType(inspectionTypeId, loginDTO, ct).ConfigureAwait(false); } catch (Exception) { throw; } } public async Task SaveInspectionType(InspectionTypeDTO dto, LoginDTO loginDTO, CancellationToken ct) { if (dto == null) throw new ArgumentNullException(nameof(dto)); AutoNumberDTO? autoNumberDTO = null; try { await _validation.NotEmpty(dto.InspectionTypeCode, nameof(dto.InspectionTypeCode)); await _validation.NotEmpty(dto.InspectionTypeName, nameof(dto.InspectionTypeName)); dto.ModifiedById = loginDTO.UserId; dto.ModifiedOn = DateTime.UtcNow; bool isNew = dto.InspectionTypeId == 0; if (isNew) { int bridgeCount = dto.InspectionTypeParameterSetArray?.Count ?? 0; autoNumberDTO = await _autoNumber.GetNumberAsync(1 + bridgeCount, "INSPECTIONTYPE", loginDTO); int nextId = autoNumberDTO.StartNumber; dto.InspectionTypeId = nextId++; dto.CreatedById = loginDTO.UserId; dto.CreatedOn = DateTime.UtcNow; if (dto.InspectionTypeParameterSetArray?.Count > 0) { foreach (var ps in dto.InspectionTypeParameterSetArray) { ps.InspectionTypeParameterSetId = nextId++; ps.Status = 1; ps.Version = 1; // Force default tier — this screen can never create an override // row, regardless of what the incoming DTO carries. ps.PartyId = -1; ps.ItemId = -1; } } await _dal.SaveInspectionType(dto, loginDTO, ct).ConfigureAwait(false); return $"{SuccessResponse.SaveSuccess}"; } else { int bridgeCount = dto.InspectionTypeParameterSetArray?.Count ?? 0; if (bridgeCount > 0) { autoNumberDTO = await _autoNumber.GetNumberAsync(bridgeCount, "INSPECTIONTYPE", loginDTO); int nextId = autoNumberDTO.StartNumber; foreach (var ps in dto.InspectionTypeParameterSetArray!) { ps.InspectionTypeParameterSetId = nextId++; ps.Status = 1; ps.Version = 1; // Force default tier — this screen can never create an override // row, regardless of what the incoming DTO carries. ps.PartyId = -1; ps.ItemId = -1; } } await _dal.SaveInspectionType(dto, loginDTO, ct).ConfigureAwait(false); return $"{SuccessResponse.UpdateSuccess}"; } } catch { if (autoNumberDTO != null && dto.InspectionTypeId > 0) await _autoNumber.RollbackAutoNumber("INSPECTIONTYPE", autoNumberDTO.StartNumber, loginDTO); throw; } } public async Task DeleteInspectionType(int inspectionTypeId, LoginDTO loginDTO, CancellationToken ct) { try { await _dal.DeleteInspectionType(inspectionTypeId, loginDTO, ct).ConfigureAwait(false); return $"{SuccessResponse.DeleteSuccessMessage}"; } catch (Exception ex) { throw new Exception($"Error deleting InspectionType ID {inspectionTypeId}", ex); } } } }