using System.Data.Common; using AccountsDAL.DTO.BankBranch; using AccountsDAL.DTO.ChequeBook; using AccountsDAL.Query.BankBranch; using AccountsDAL.Query.ChequeBook; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using Newtonsoft.Json; namespace AccountsDAL.CustomCode.ChequeBook { public class ChequeBookDAL : IChequeBookDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public ChequeBookDAL(IQueryExecutor queryExecutor, IValidation validation) { _QueryExecutor = queryExecutor; _Validation = validation; } public async Task GetChequeBook(int chequeBookId, LoginDTO loginDTO) { try { var dto = await _QueryExecutor .QuerySingleAsync(loginDTO, ChequeBookQB.GET_CHEQUEBOOK, new { ChequeBookId = chequeBookId }) .ConfigureAwait(false); var details = await _QueryExecutor .QueryAsync(loginDTO, ChequeBookQB.GET_CHEQUEBOOK_DETAILS, new { ChequeBookId = chequeBookId }) .ConfigureAwait(false); if (dto != null) dto.ChequeBookDetailArray = details?.ToList() ?? []; return JsonConvert.SerializeObject(dto); } catch (Exception) { throw; } } public async Task> SaveChequeBook(ChequeBookDTO dto, LoginDTO loginDTO, DbTransaction tx) { try { dto.TenantId = loginDTO.ClientId; dto.ChequeBookCreatedById = loginDTO.UserId; dto.ChequeBookCreatedOn = DateTime.UtcNow; dto.ChequeBookModifiedById = loginDTO.UserId; dto.ChequeBookModifiedOn = DateTime.UtcNow; int rows = await _QueryExecutor .ExecuteAsync(loginDTO, ChequeBookQB.SAVE_CHEQUEBOOK, dto, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Cheque book saved successfully.") : Result.Failure("Failed to save cheque book."); } catch (Exception) { throw; } } public async Task> UpdateChequeBook(ChequeBookDTO dto, LoginDTO loginDTO, DbTransaction tx) { try { dto.ChequeBookModifiedById = loginDTO.UserId; dto.ChequeBookModifiedOn = DateTime.UtcNow; int rows = await _QueryExecutor .ExecuteAsync(loginDTO, ChequeBookQB.UPDATE_CHEQUEBOOK, dto, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Cheque book updated successfully.") : Result.Failure("Failed to update cheque book."); } catch (Exception) { throw; } } public async Task> SaveChequeBookDetail( ChequeBookDetailDTO detail, LoginDTO loginDTO, DbTransaction tx) { try { detail.TenantId = loginDTO.ClientId; int rows = await _QueryExecutor .ExecuteAsync(loginDTO, ChequeBookQB.SAVE_CHEQUEBOOK_DETAIL, detail, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Cheque book detail saved.") : Result.Failure("Failed to save cheque book detail."); } catch (Exception) { throw; } } public async Task> DeleteChequeBookDetails(int chequeBookId, LoginDTO loginDTO, DbTransaction tx) { try { await _QueryExecutor .ExecuteAsync(loginDTO, ChequeBookQB.DELETE_CHEQUEBOOK_DETAILS, new { ChequeBookId = chequeBookId, TenantId = loginDTO.ClientId }, tx) .ConfigureAwait(false); return Result.Success("Cheque book details deleted."); } catch (Exception) { throw; } } public async Task> DeleteChequeBook(int chequeBookId, LoginDTO loginDTO, DbTransaction tx) { try { int rows = await _QueryExecutor .ExecuteAsync(loginDTO, ChequeBookQB.DELETE_CHEQUEBOOK, new { ChequeBookId = chequeBookId }, tx) .ConfigureAwait(false); return rows > 0 ? Result.Success("Cheque book deleted successfully.") : Result.Failure("Cheque book not found or already deleted."); } catch (Exception) { throw; } } public async Task GetSelectListChequeBook(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { //try //{ // var result = await _QueryExecutor // .QueryAsync(loginDTO, ChequeBookQB.GET_SELECTLIST_CHEQUEBOOK, null!) // .ConfigureAwait(false); // return JsonConvert.SerializeObject(result); //} //catch (Exception) //{ // throw; //} try { var param = new { TenantId = loginDTO.ClientId }; var result = await _QueryExecutor .QueryAsync( loginDTO, ChequeBookQB.GET_SELECTLIST_CHEQUEBOOK, param ) .ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } } }