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.MachineType; using MMDAL.DTO.Machine; namespace MMBLL.MachineType { public class MachineTypeBLL : IMachineTypeBLL { private readonly IMachineTypeDAL _MachineTypeDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public MachineTypeBLL(IMachineTypeDAL MachineTypeDAL, AutoNumber AutoNumber, IValidation Validation) { _MachineTypeDAL = MachineTypeDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetMachineType(int MachineTypeId, LoginDTO LoginDTO) { try { return await _MachineTypeDAL.GetMachineType(MachineTypeId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveMachineType(MachineTypeDTO MachineTypeDTO, LoginDTO LoginDTO) { if (MachineTypeDTO == null) throw new ArgumentNullException(nameof(MachineTypeDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(MachineTypeDTO.MachineTypeCode, nameof(MachineTypeDTO.MachineTypeCode)); await _Validation.NotEmpty(MachineTypeDTO.MachineTypeName, nameof(MachineTypeDTO.MachineTypeName)); MachineTypeDTO.MachineTypeModifiedById = LoginDTO.UserId; MachineTypeDTO.MachineTypeModifiedOn = DateTime.UtcNow; int initialId = MachineTypeDTO.MachineTypeId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "MACHINETYPE", LoginDTO); MachineTypeDTO.MachineTypeId = AutoNumberDTO.StartNumber; MachineTypeDTO.MachineTypeCreatedById = LoginDTO.UserId; MachineTypeDTO.MachineTypeCreatedOn = DateTime.UtcNow; await _MachineTypeDAL.SaveMachineType(MachineTypeDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessCode} {MachineTypeDTO.MachineTypeCode}"; } else { await _MachineTypeDAL.UpdateMachineType(MachineTypeDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessCode} {MachineTypeDTO.MachineTypeCode}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && MachineTypeDTO.MachineTypeId > 0) await _AutoNumber.RollbackAutoNumber("MACHINETYPE", MachineTypeDTO.MachineTypeId, LoginDTO); throw; } } public async Task GetSelectListMachineType(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _MachineTypeDAL.GetSelectListMachineType(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteMachineType(int MachineTypeId, LoginDTO LoginDTO) { try { return await _MachineTypeDAL.DeleteMachineType(MachineTypeId, LoginDTO); } catch (Exception) { throw; } } } }