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.ResponseStandard; using GB5Shared.Validation; using MMDAL.CustomCode.LotType; using MMDAL.DTO.LotType; namespace MMBLL.LotType { public class LotTypeBLL : ILotTypeBLL { private readonly ILotTypeDAL _LotTypeDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public LotTypeBLL(ILotTypeDAL ILotTypeDAL, AutoNumber AutoNumber, IValidation Validation) { _LotTypeDAL = ILotTypeDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetLotType(int LotTypeId, LoginDTO LoginDTO) { try { return await _LotTypeDAL.GetLotType(LotTypeId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListLotType(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _LotTypeDAL.GetSelectListLotType(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SaveLotType(LotTypeDTO LotTypeDTO, LoginDTO LoginDTO) { if (LotTypeDTO == null) throw new ArgumentNullException(nameof(LotTypeDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(LotTypeDTO.LotTypeName, nameof(LotTypeDTO.LotTypeName)); LotTypeDTO.LotTypeModifiedById = LoginDTO.UserId; LotTypeDTO.LotTypeModifiedOn = DateTime.UtcNow; int count = 1; if (LotTypeDTO.LotTypeId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "LOTTYPE", LoginDTO); LotTypeDTO.LotTypeId = autoNumberDTO.StartNumber; LotTypeDTO.LotTypeCreatedById = LoginDTO.UserId; LotTypeDTO.LotTypeCreatedOn = DateTime.UtcNow; await _LotTypeDAL.SaveLotType(LotTypeDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {LotTypeDTO.LotTypeId}"; } else { await _LotTypeDAL.UpdateLotType(LotTypeDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {LotTypeDTO.LotTypeId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && LotTypeDTO.LotTypeId > 0) await _AutoNumber.RollbackAutoNumber("LOTTYPE", LotTypeDTO.LotTypeId, LoginDTO); throw; } } public async Task DeleteLotType(int LotTypeId, LoginDTO LoginDTO) { try { return await _LotTypeDAL.DeleteLotType(LotTypeId, LoginDTO); } catch (Exception) { throw; } } } }