using AccountsDAL.CustomCode.ChequeBook; using AccountsDAL.DTO.Account; using AccountsDAL.DTO.ChequeBook; using Dapr.Client; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.HybridCache; using GB5Shared.DTO.Framework.Login; using GB5Shared.DTO.Qualifier; using GB5Shared.EntityHandler; using GB5Shared.GB5Library.Qualifier; using GB5Shared.GenerateAutoNumber; using GB5Shared.PubSub.OutBox; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.WorkFlow.WorkFlowEngine; using static GB5Shared.GB5Constant.Constant; namespace AccountsBLL.ChequeBook { public class ChequeBookBLL : IChequeBookBLL { private readonly IChequeBookDAL _ChequeBookDAL; private readonly AutoNumber _AutoNumber; private readonly IWorkFlowEngine _WorkFlowEngine; private readonly IQueryExecutor _QueryExecutor; private readonly IOutBox _OutBox; private readonly KeyInvalidate _KeyInvalidate; private readonly DaprClient _DaprClient; private readonly BaseEntityAppService _BaseEntityAppService; public ChequeBookBLL( IChequeBookDAL chequeBookDAL, AutoNumber autoNumber, IWorkFlowEngine workFlowEngine, IQueryExecutor queryExecutor, IOutBox outBox, KeyInvalidate keyInvalidate, DaprClient daprClient, BaseEntityAppService baseEntityAppService ) { _ChequeBookDAL = chequeBookDAL; _AutoNumber = autoNumber; _WorkFlowEngine = workFlowEngine; _QueryExecutor = queryExecutor; _OutBox = outBox; _KeyInvalidate = keyInvalidate; _DaprClient = daprClient; _BaseEntityAppService = baseEntityAppService; } public async Task GetChequeBook(int chequeBookId, LoginDTO loginDTO) { try { return await _ChequeBookDAL.GetChequeBook(chequeBookId, loginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } public async Task SaveChequeBook(ChequeBookDTO dto, LoginDTO loginDTO) { var Trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); try { bool isNew = dto.ChequeBookId == 0; var details = dto.ChequeBookDetailArray ?? []; int detailCount = details.Count; if (isNew) { var headerAuto = await _AutoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.CHEQUEBOOK, loginDTO) .ConfigureAwait(false); dto.ChequeBookId = headerAuto.StartNumber; } // Pre-fetch detail IDs for the new/replaced detail rows //int detailStartId = 0; //if (detailCount > 0) //{ // var detailAuto = await _AutoNumber.GetNumberAsync(detailCount, AUTONUMBERCONSTANT.CHEQUEBOOKDETAIL, loginDTO) // .ConfigureAwait(false); // detailStartId = detailAuto.StartNumber; //} //// Assign IDs to detail rows before the callback //int detailIdx = detailStartId; //foreach (var detail in details) //{ // detail.ChequeBookDetailId = detailIdx++; // detail.ChequeBookId = dto.ChequeBookId; //} //var definitions = new List(); //var executors = new List(); //var engine = new QualifierEngine((IQualifierDefinitionLoader)definitions, executors); //var facade = new QualifierFacade(engine); //var baseSvc = new BaseEntityAppService( // facade, _WorkFlowEngine, _OutBox, _QueryExecutor,_DaprClient); foreach (var detail in details) { detail.ChequeBookId = dto.ChequeBookId; detail.TenantId = loginDTO.ClientId; } await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTCHEQUEBOOK, EventTypeConstant.SAVECHEQUEBOOKEVENTTYPEID, dto, loginDTO, async tx => { if (isNew) await _ChequeBookDAL.SaveChequeBook(dto, loginDTO, tx).ConfigureAwait(false); else await _ChequeBookDAL.UpdateChequeBook(dto, loginDTO, tx).ConfigureAwait(false); // Replace detail rows: delete existing then re-insert await _ChequeBookDAL.DeleteChequeBookDetails(dto.ChequeBookId, loginDTO, tx) .ConfigureAwait(false); foreach (var detail in details) await _ChequeBookDAL.SaveChequeBookDetail(detail, loginDTO, tx) .ConfigureAwait(false); return dto.ChequeBookId; }, null, -1, -1, Trans); await _QueryExecutor.CommitAsync(Trans); var cacheKey = CacheKeyGenerator.KeyGeneration( dto.ChequeBookId, EntityConstant.OBJECTCHEQUEBOOK, CacheKeyLevel.CLIENT_LEVEL, string.Empty, // ← SourceData loginDTO); // await _KeyInvalidate.AllInvalidateCache(cacheKey); return isNew ? SuccessResponse.SaveSuccess : SuccessResponse.UpdateSuccess; } catch (Exception) { await _QueryExecutor.RollbackAsync(Trans); throw; } } public async Task DeleteChequeBook(int chequeBookId, LoginDTO loginDTO) { var Trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); try { await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTCHEQUEBOOK, EventTypeConstant.DELETECHEQUEBOOKEVENTTYPEID, new ChequeBookDTO { ChequeBookId = chequeBookId }, loginDTO, async tx => { await _ChequeBookDAL.DeleteChequeBook(chequeBookId, loginDTO, tx) .ConfigureAwait(false); return chequeBookId; }, null, -1, -1, Trans); await _QueryExecutor.CommitAsync(Trans); var cacheKey = CacheKeyGenerator.KeyGeneration( chequeBookId, EntityConstant.OBJECTCHEQUEBOOK, CacheKeyLevel.CLIENT_LEVEL, string.Empty, // ← SourceData loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return SuccessResponse.DeleteSuccessMessage; } catch (Exception) { await _QueryExecutor.RollbackAsync(Trans); throw; } } public async Task GetSelectListChequeBook(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _ChequeBookDAL.GetSelectListChequeBook(criteriaDTO, loginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } } }