using AccountsDAL.DTO.Party; using AccountsDAL.DTO.PartyOU; using AccountsDAL.Query.PartyOU; using Dapper; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using Microsoft.Identity.Client; using Newtonsoft.Json; using System.Collections; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Security.Cryptography; namespace AccountsDAL.CustomCode.PartyOU { public class PartyOUDAL : IPartyOUDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public PartyOUDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public PartyOUDAL() { } public async Task GetPartyOU(int partyOUId, LoginDTO loginDTO) { try { var dto = await _QueryExecutor .QuerySingleAsync(loginDTO, PartyOUQB.GET_PARTYOU, new { PartyOUId = partyOUId, TenantId = loginDTO.ClientId }) .ConfigureAwait(false); return JsonConvert.SerializeObject(dto); } catch (Exception) { throw; } } public async Task> SavePartyOU(PartyOUDTO dto, LoginDTO loginDTO, DbTransaction tx) { try { dto.PartyOUCreatedById = loginDTO.UserId; dto.PartyOUCreatedOn = DateTime.UtcNow; dto.PartyOUModifiedById = loginDTO.UserId; dto.PartyOUModifiedOn = DateTime.UtcNow; int rows = await _QueryExecutor .ExecuteAsync(loginDTO, PartyOUQB.SAVE_PARTYOU, dto, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Party OU saved successfully.") : Result.Failure("Failed to save Party OU."); } catch (Exception) { throw; } } public async Task> UpdatePartyOU(PartyOUDTO dto, LoginDTO loginDTO, DbTransaction tx) { try { dto.PartyOUModifiedById = loginDTO.UserId; dto.PartyOUModifiedOn = DateTime.UtcNow; int rows = await _QueryExecutor .ExecuteAsync(loginDTO, PartyOUQB.UPDATE_PARTYOU, dto, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Party OU updated successfully.") : Result.Failure("Failed to update Party OU."); } catch (Exception) { throw; } } public async Task> DeletePartyOU(int partyOUId, LoginDTO loginDTO, DbTransaction tx) { try { int rows = await _QueryExecutor .ExecuteAsync(loginDTO, PartyOUQB.DELETE_PARTYOU, new { PartyOUId = partyOUId }, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Party OU deleted successfully.") : Result.Failure("Party OU not found or already deleted."); } catch (Exception) { throw; } } public async Task GetSelectListPartyOU(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { var result = await _QueryExecutor .QueryAsync(loginDTO, PartyOUQB.GET_SELECTLIST_PARTYOU, null!) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task GetPartyAccountOU(int AccountId, int OuId, int PartyId, int PartyBranchId, LoginDTO loginDTO, DbTransaction tx) { return await tx.Connection.QueryFirstOrDefaultAsync( PartyOUQB.GET_PARTY_ACCOUNT_OU, new { AccountId = AccountId, PartyId = PartyId, OUId = OuId, PartyBranchId = PartyBranchId }, transaction: tx); } public async Task> GetPartyOUsForParty(int partyId, LoginDTO loginDTO) { var list = await _QueryExecutor .QueryAsync(loginDTO, PartyOUQB.GET_PARTYOU_FOR_PARTY, new { PartyId = partyId }) .ConfigureAwait(false); return list.ToList(); } } }