using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.Resource.GB5Solution; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using MMDAL.CustomCode.Shift; using MMDAL.DTO.Shift; using System; using System.Threading.Tasks; namespace MMBLL.Shift { public class ShiftBLL : IShiftBLL { private readonly IShiftDAL _ShiftDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public ShiftBLL(IShiftDAL ShiftDAL, AutoNumber AutoNumber, IValidation Validation) { _ShiftDAL = ShiftDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetShift(int ShiftId, LoginDTO LoginDTO) { try { return await _ShiftDAL.GetShift(ShiftId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveShift(ShiftDTO shiftDTO, LoginDTO loginDTO) { if (shiftDTO == null) throw new ArgumentNullException(nameof(shiftDTO)); AutoNumberDTO? autoNumberDTO = null; try { shiftDTO.ShiftModifiedById = loginDTO.UserId; shiftDTO.ShiftModifiedOn = DateTime.UtcNow; if (shiftDTO.ShiftId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(1, "SHIFT", loginDTO); shiftDTO.ShiftId = autoNumberDTO.StartNumber; shiftDTO.ShiftCreatedById = loginDTO.UserId; shiftDTO.ShiftCreatedOn = DateTime.UtcNow; await _ShiftDAL.SaveShift(shiftDTO, loginDTO); return $"{SuccessResponse.SaveSuccessMessage} {shiftDTO.ShiftId}"; } else { await _ShiftDAL.UpdateShift(shiftDTO, loginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {shiftDTO.ShiftId}"; } } catch (Exception ex) { if (autoNumberDTO != null && shiftDTO.ShiftId > 0) await _AutoNumber.RollbackAutoNumber("SHIFT", shiftDTO.ShiftId, loginDTO); string error = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(error); } } public async Task GetSelectListShift( CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _ShiftDAL.GetSelectListShift( CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task DeleteShift(int ShiftId, LoginDTO LoginDTO) { try { return await _ShiftDAL.DeleteShift(ShiftId, LoginDTO); } catch (Exception) { throw; } } } }