using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; 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.InspectionArea; using MMDAL.DTO.InspectionArea; namespace MMBLL.InspectionArea { public class InspectionAreaBLL : IInspectionAreaBLL { private readonly IInspectionAreaDAL _InspectionAreaDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public InspectionAreaBLL(IInspectionAreaDAL InspectionAreaDAL, AutoNumber AutoNumber, IValidation Validation) { _InspectionAreaDAL = InspectionAreaDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetInspectionArea(int InspectionAreaId, LoginDTO LoginDTO) { try { return await _InspectionAreaDAL.GetInspectionArea(InspectionAreaId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveInspectionArea(InspectionAreaDTO InspectionAreaDTO, LoginDTO LoginDTO) { if (InspectionAreaDTO == null) throw new ArgumentNullException(nameof(InspectionAreaDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(InspectionAreaDTO.InspectionAreaType, nameof(InspectionAreaDTO.InspectionAreaType)); await _Validation.NotEmpty(InspectionAreaDTO.InspectionAreaArea, nameof(InspectionAreaDTO.InspectionAreaArea)); InspectionAreaDTO.InspectionAreaModifiedById = LoginDTO.UserId; InspectionAreaDTO.InspectionAreaModifiedOn = DateTime.UtcNow; int count = 1; if (InspectionAreaDTO.InspectionAreaId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "INSPECTIONAREA", LoginDTO); InspectionAreaDTO.InspectionAreaId = autoNumberDTO.StartNumber; InspectionAreaDTO.InspectionAreaCreatedById = LoginDTO.UserId; InspectionAreaDTO.InspectionAreaCreatedOn = DateTime.UtcNow; await _InspectionAreaDAL.SaveInspectionArea(InspectionAreaDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {InspectionAreaDTO.InspectionAreaId}"; } else { await _InspectionAreaDAL.UpdateInspectionArea(InspectionAreaDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {InspectionAreaDTO.InspectionAreaId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && InspectionAreaDTO.InspectionAreaId > 0) await _AutoNumber.RollbackAutoNumber("INSPECTIONAREA", InspectionAreaDTO.InspectionAreaId, LoginDTO); throw; } } public async Task DeleteInspectionArea(int InspectionAreaId, LoginDTO LoginDTO) { try { return await _InspectionAreaDAL.DeleteInspectionArea(InspectionAreaId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListInspectionArea( int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _InspectionAreaDAL.GetSelectListInspectionArea( FirstNumber, MaxResult, CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } } }