using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using CRMDAL.CustomCode.CallAllocation; using CRMDAL.DTO.CallAllocation; 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; namespace CRMBLL.CallAllocation { public class CallAllocationBLL : ICallAllocationBLL { private readonly ICallAllocationDAL _CallAllocationDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public CallAllocationBLL(ICallAllocationDAL CallAllocationDAL, AutoNumber AutoNumber, IValidation Validation) { _CallAllocationDAL = CallAllocationDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetCallAllocation(int CallAllocationId, LoginDTO LoginDTO) { try { return await _CallAllocationDAL.GetCallAllocation(CallAllocationId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveCallAllocation(CallAllocationDTO CallAllocationDTO, LoginDTO LoginDTO) { if (CallAllocationDTO == null) throw new ArgumentNullException(nameof(CallAllocationDTO)); AutoNumberDTO? autoNumberDTO = null; try { int initialId = CallAllocationDTO.CallAllocationId; int count = 1 + initialId; if (initialId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "CALLALLOCATION", LoginDTO); CallAllocationDTO.CallAllocationId = autoNumberDTO.StartNumber; await _CallAllocationDAL.SaveCallAllocation(CallAllocationDTO, LoginDTO); return $"{SuccessResponse.SaveSuccess} with RuleName:{CallAllocationDTO.RuleName}"; } else { await _CallAllocationDAL.UpdateCallAllocation(CallAllocationDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccess} with RuleName:{CallAllocationDTO.RuleName}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && CallAllocationDTO.CallAllocationId > 0) await _AutoNumber.RollbackAutoNumber("CALLALLOCATION", CallAllocationDTO.CallAllocationId, LoginDTO); throw; } } public async Task GetSelectListCallAllocation(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { return await _CallAllocationDAL.GetSelectListCallAllocation(criteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task DeleteCallAllocation(int CallAllocationId, LoginDTO LoginDTO) { try { return await _CallAllocationDAL.DeleteCallAllocation(CallAllocationId, LoginDTO); } catch (Exception) { throw; } } } }