using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using FMDAL.CustomCode.PartyBranchLocation; using FMDAL.DTO.PartyBranchLocation; 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; namespace FMBLL.PartyBranchLocation { public class PartyBranchLocationBLL : IPartyBranchLocationBLL { private readonly IPartyBranchLocationDAL _PartyBranchLocationDAL; private readonly AutoNumber _AutoNumber; private readonly IValidation _Validation; public PartyBranchLocationBLL(IPartyBranchLocationDAL IPartyBranchLocationDAL, AutoNumber AutoNumber, IValidation Validation) { _PartyBranchLocationDAL = IPartyBranchLocationDAL; _AutoNumber = AutoNumber; _Validation = Validation; } public async Task GetPartyBranchLocation(int PartyBranchLocationId, LoginDTO LoginDTO) { try { return await _PartyBranchLocationDAL.GetPartyBranchLocation(PartyBranchLocationId, LoginDTO); } catch (Exception) { throw; } } public async Task GetSelectListPartyBranchLocation(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _PartyBranchLocationDAL.GetSelectListPartyBranchLocation(criteriaDTO, loginDTO); } catch (Exception) { throw; } } public async Task SavePartyBranchLocation(PartyBranchLocationDTO PartyBranchLocationDTO, LoginDTO LoginDTO) { if (PartyBranchLocationDTO == null) throw new ArgumentNullException(nameof(PartyBranchLocationDTO)); AutoNumberDTO? autoNumberDTO = null; try { await _Validation.NotEmpty(PartyBranchLocationDTO.PartyBranchLocationCode, nameof(PartyBranchLocationDTO.PartyBranchLocationCode)); await _Validation.NotEmpty(PartyBranchLocationDTO.PartyBranchLocationName, nameof(PartyBranchLocationDTO.PartyBranchLocationName)); PartyBranchLocationDTO.PartyBranchLocationModifiedById = LoginDTO.UserId; PartyBranchLocationDTO.PartyBranchLocationModifiedOn = DateTime.UtcNow; int count = 1; if (PartyBranchLocationDTO.PartyBranchLocationId == 0) { autoNumberDTO = await _AutoNumber.GetAutoNumber(count, "PARTYBRANCHLOCATION", LoginDTO); PartyBranchLocationDTO.PartyBranchLocationId = autoNumberDTO.StartNumber; PartyBranchLocationDTO.PartyBranchLocationCreatedById = LoginDTO.UserId; PartyBranchLocationDTO.PartyBranchLocationCreatedOn = DateTime.UtcNow; await _PartyBranchLocationDAL.SavePartyBranchLocation(PartyBranchLocationDTO, LoginDTO); return $"{SuccessResponse.SaveSuccessMessage} {PartyBranchLocationDTO.PartyBranchLocationId}"; } else { await _PartyBranchLocationDAL.UpdatePartyBranchLocation(PartyBranchLocationDTO, LoginDTO); return $"{SuccessResponse.UpdateSuccessMessage} {PartyBranchLocationDTO.PartyBranchLocationId}"; } } catch (ValidationException vex) { throw new Exception(vex.Message); } catch (Exception) { if (autoNumberDTO != null && PartyBranchLocationDTO.PartyBranchLocationId > 0) await _AutoNumber.RollbackAutoNumber("PARTYBRANCHLOCATION", PartyBranchLocationDTO.PartyBranchLocationId, LoginDTO); throw; } } public async Task DeletePartyBranchLocation(int PartyBranchLocationId, LoginDTO LoginDTO) { try { return await _PartyBranchLocationDAL.DeletePartyBranchLocation(PartyBranchLocationId, LoginDTO); } catch (Exception) { throw; } } } }