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.Machine; using MMDAL.DTO.Machine; using MMDAL.DTO.MaterialGrade; namespace MMBLL.Machine { public class MachineBLL : IMachineBLL { private readonly IMachineDAL _MachineDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public MachineBLL(IMachineDAL MachineDAL, AutoNumber AutoNumber, IValidation Validation) { _MachineDAL = MachineDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetMachine(int MachineId, LoginDTO LoginDTO) { try { return await _MachineDAL.GetMachine(MachineId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveMachine(MachineDTO MachineDTO, LoginDTO LoginDTO) { if (MachineDTO == null) throw new ArgumentNullException(nameof(MachineDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(MachineDTO.MachineCode, nameof(MachineDTO.MachineCode)); await _Validation.NotEmpty(MachineDTO.MachineName, nameof(MachineDTO.MachineName)); MachineDTO.MachineModifiedById = LoginDTO.UserId; MachineDTO.MachineModifiedOn = DateTime.UtcNow; int initialId = MachineDTO.MachineId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "MACHINE", LoginDTO); MachineDTO.MachineId = AutoNumberDTO.StartNumber; MachineDTO.MachineCreatedById = LoginDTO.UserId; MachineDTO.MachineCreatedOn = DateTime.UtcNow; await _MachineDAL.SaveMachine(MachineDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessCode} {MachineDTO.MachineCode}"; } else { await _MachineDAL.UpdateMachine(MachineDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessCode} {MachineDTO.MachineCode}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && MachineDTO.MachineId > 0) await _AutoNumber.RollbackAutoNumber("MACHINE", MachineDTO.MachineId, LoginDTO); throw; } } public async Task GetSelectListMachine(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _MachineDAL.GetSelectListMachine(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteMachine(int MachineId, LoginDTO LoginDTO) { try { return await _MachineDAL.DeleteMachine(MachineId, LoginDTO); } catch (Exception) { throw; } } } }