using AdminDAL.CustomCode.AllocationType; using AdminDAL.DTO.Allocation; using AdminDAL.DTO.Period; 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 System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AdminBLL.AllocationType { public class AllocationTypeBLL : IAllocationTypeBLL { private readonly IAllocationTypeDAL _AllocationTypeDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public AllocationTypeBLL(IAllocationTypeDAL AllocationTypeDAL, AutoNumber AutoNumber, IValidation validation) { _AllocationTypeDAL = AllocationTypeDAL; _AutoNumber = AutoNumber; _Validation = validation; } public async Task GetSelectListAllocationType(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _AllocationTypeDAL.GetSelectListAllocationType(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task GetAllocationType(int AllocationTypeId, LoginDTO LoginDTO) { try { return await _AllocationTypeDAL.GetAllocationType(AllocationTypeId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveAllocationType(AllocationTypeDTO AllocationTypeDTO, LoginDTO LoginDTO) { if (AllocationTypeDTO == null) throw new ArgumentNullException(nameof(AllocationTypeDTO)); AutoNumberDTO? AutoNumberDTO = null; try { await _Validation.NotEmpty(AllocationTypeDTO.AllocationTypeCode, nameof(AllocationTypeDTO.AllocationTypeCode)); await _Validation.NotEmpty(AllocationTypeDTO.AllocationTypeName, nameof(AllocationTypeDTO.AllocationTypeName)); AllocationTypeDTO.AllocationTypeModifiedById = LoginDTO.UserId; AllocationTypeDTO.AllocationTypeModifiedOn = DateTime.UtcNow; int initialId = AllocationTypeDTO.AllocationTypeId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO autoNumberDTO = await _AutoNumber.GetNumberAsync(count, "ALLOCATIONTYPE", LoginDTO); AllocationTypeDTO.AllocationTypeId = autoNumberDTO.StartNumber; AllocationTypeDTO.AllocationTypeCreatedById = LoginDTO.UserId; AllocationTypeDTO.AllocationTypeCreatedOn = DateTime.UtcNow; await _AllocationTypeDAL.SaveAllocationType(AllocationTypeDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {AllocationTypeDTO.AllocationTypeId}"; } else { await _AllocationTypeDAL.UpdateAllocationType(AllocationTypeDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {AllocationTypeDTO.AllocationTypeId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (AutoNumberDTO != null && AllocationTypeDTO.AllocationTypeId > 0) await _AutoNumber.RollbackAutoNumber("ALLOCATIONTYPE", AllocationTypeDTO.AllocationTypeId, LoginDTO); throw; } } public async Task DeleteAllocationType(int AllocationTypeId, LoginDTO LoginDTO) { try { return await _AllocationTypeDAL.DeleteAllocationType(AllocationTypeId, LoginDTO); } catch (Exception) { throw; } } } }