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.MProcessGroup; using MMDAL.DTO.MprocessGroup; namespace MMBLL.MProcessGroup { public class MProcessGroupBLL : IMProcessGroupBLL { private readonly IMProcessGroupDAL _MProcessGroupDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public MProcessGroupBLL(IMProcessGroupDAL MProcessGroupDAL, AutoNumber AutoNumber, IValidation Validation) { _MProcessGroupDAL = MProcessGroupDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetMProcessGroup(int MProcessGroupId, LoginDTO LoginDTO) { try { return await _MProcessGroupDAL.GetMProcessGroup(MProcessGroupId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveMProcessGroup(MProcessGroupDTO MProcessGroupDTO, LoginDTO LoginDTO) { if (MProcessGroupDTO == null) throw new ArgumentNullException(nameof(MProcessGroupDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(MProcessGroupDTO.MProcessGroupCode, nameof(MProcessGroupDTO.MProcessGroupCode)); await _Validation.NotEmpty(MProcessGroupDTO.MProcessGroupName, nameof(MProcessGroupDTO.MProcessGroupName)); MProcessGroupDTO.MProcessGroupModifiedById = LoginDTO.UserId; MProcessGroupDTO.MProcessGroupModifiedOn = DateTime.UtcNow; int initialId = MProcessGroupDTO.MProcessGroupId; int count = 1 + initialId; if (initialId == 0) { AutoNumberDTO AutoNumberDTO = await _AutoNumber.GetNumberAsync(count, "MPROCESSGROUP", LoginDTO); MProcessGroupDTO.MProcessGroupId = AutoNumberDTO.StartNumber; MProcessGroupDTO.MProcessGroupCreatedById = LoginDTO.UserId; MProcessGroupDTO.MProcessGroupCreatedOn = DateTime.UtcNow; await _MProcessGroupDAL.SaveMProcessGroup(MProcessGroupDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {MProcessGroupDTO.MProcessGroupId}"; } else { await _MProcessGroupDAL.UpdateMProcessGroup(MProcessGroupDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {MProcessGroupDTO.MProcessGroupId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && MProcessGroupDTO.MProcessGroupId > 0) await _AutoNumber.RollbackAutoNumber("MPROCESSGROUP", MProcessGroupDTO.MProcessGroupId, LoginDTO); throw; } } public async Task GetSelectListMProcessGroup(int FirstNumber, int MaxResult, CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _MProcessGroupDAL.GetSelectListMProcessGroup(FirstNumber, MaxResult, criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task DeleteMProcessGroup(int MProcessGroupId, LoginDTO LoginDTO) { try { return await _MProcessGroupDAL.DeleteMProcessGroup(MProcessGroupId, LoginDTO); } catch (Exception) { throw; } } } }