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.Defect; using MMDAL.DTO.Defect; namespace MMBLL.Defect { public class DefectBLL : IDefectBLL { private readonly IDefectDAL _DefectDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public DefectBLL(IDefectDAL DefectDAL, AutoNumber AutoNumber, IValidation Validation) { _DefectDAL = DefectDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetDefect(int DefectId, LoginDTO LoginDTO) { try { return await _DefectDAL.GetDefect(DefectId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveDefect(DefectDTO DefectDTO, LoginDTO LoginDTO) { if (DefectDTO == null) throw new ArgumentNullException(nameof(DefectDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(DefectDTO.DefectCode, nameof(DefectDTO.DefectCode)); await _Validation.NotEmpty(DefectDTO.DefectName, nameof(DefectDTO.DefectName)); DefectDTO.DefectModifiedById = LoginDTO.UserId; DefectDTO.DefectModifiedOn = DateTime.UtcNow; int initialId = DefectDTO.DefectId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "DEFECT", LoginDTO); DefectDTO.DefectId = AutoNumberDTO.StartNumber; DefectDTO.DefectCreatedById = LoginDTO.UserId; DefectDTO.DefectCreatedOn = DateTime.UtcNow; await _DefectDAL.SaveDefect(DefectDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessCode} {DefectDTO.DefectCode}"; } else { await _DefectDAL.UpdateDefect(DefectDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessCode} {DefectDTO.DefectCode}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && DefectDTO.DefectId > 0) await _AutoNumber.RollbackAutoNumber("DEFECT", DefectDTO.DefectId, LoginDTO); throw; } } public async Task GetSelectListDefect(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _DefectDAL.GetSelectListDefect(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteDefect(int DefectId, LoginDTO LoginDTO) { try { return await _DefectDAL.DeleteDefect(DefectId, LoginDTO); } catch (Exception) { throw; } } } }