using AccountsDAL.CustomCode.AccountSchedule; using AccountsDAL.DTO.Account; using AccountsDAL.DTO.AccountGroup; using AccountsDAL.DTO.AccountSchedule; using AccountsDAL.Query.AccountSchedule; using Dapr.Client; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.HybridCache; using GB5Shared.DTO.Framework.Logging; 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.AccountSchedule { public class AccountScheduleBLL : IAccountScheduleBLL { private readonly IAccountScheduleDAL _AccountScheduleDAL; 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 AccountScheduleBLL( IAccountScheduleDAL accountScheduleDAL, AutoNumber autoNumber, IWorkFlowEngine workFlowEngine, IQueryExecutor queryExecutor, IOutBox outBox, KeyInvalidate keyInvalidate, DaprClient daprClient, BaseEntityAppService baseEntityAppService ) { _AccountScheduleDAL = accountScheduleDAL; _AutoNumber = autoNumber; _WorkFlowEngine = workFlowEngine; _QueryExecutor = queryExecutor; _OutBox = outBox; _KeyInvalidate = keyInvalidate; _DaprClient = daprClient; _BaseEntityAppService = baseEntityAppService; } public async Task GetAccountSchedule(int AccountScheduleId, LoginDTO LoginDTO) { try { return await _AccountScheduleDAL.GetAccountSchedule(AccountScheduleId, LoginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } public async Task SaveAccountSchedule(AccountScheduleDTO dto, LoginDTO loginDTO) { if (dto == null) throw new ArgumentNullException(nameof(dto)); var trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); try { bool isNew = dto.AccountScheduleId == 0; if (isNew) { var auto = await _AutoNumber.GetNumberAsync( 1, AUTONUMBERCONSTANT.ACCOUNTSCHEDULE, loginDTO); dto.AccountScheduleId = auto.StartNumber; } await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTACCOUNTSCHEDULE, EventTypeConstant.SAVEACCOUNTSCHEDULEEVENTTYPEID, dto, loginDTO, async tx => { if (isNew) await _AccountScheduleDAL.SaveAccountSchedule(dto, loginDTO, tx); else await _AccountScheduleDAL.UpdateAccountSchedule(dto, loginDTO, tx); return dto.AccountScheduleId; }, null, -1, -1, trans ); await _QueryExecutor.CommitAsync(trans); // 🔥 FIX: use SINGLE consistent cache level var cacheKey = CacheKeyGenerator.KeyGeneration( dto.AccountScheduleId, EntityConstant.OBJECTACCOUNTSCHEDULE, CacheKeyLevel.USER_LEVEL, string.Empty, loginDTO ); await _KeyInvalidate.AllInvalidateCache(cacheKey); return isNew ? SuccessResponse.SaveSuccess : SuccessResponse.UpdateSuccess; } catch { await _QueryExecutor.RollbackAsync(trans); throw; } } //public async Task SaveAccountSchedule(AccountScheduleDTO dto, LoginDTO loginDTO) //{ // if (dto == null) throw new ArgumentNullException(nameof(dto)); // var Trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); // try // { // bool isNew = dto.AccountScheduleId == 0; // if (isNew) // { // var auto = await _AutoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.ACCOUNTSCHEDULE, loginDTO) // .ConfigureAwait(false); // dto.AccountScheduleId = auto.StartNumber; // } // //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); // await _BaseEntityAppService.ExecuteSaveAsync( // EntityConstant.OBJECTACCOUNTSCHEDULE, // EventTypeConstant.SAVEACCOUNTSCHEDULEEVENTTYPEID, // dto, // loginDTO, // async tx => // { // if (isNew) // await _AccountScheduleDAL.SaveAccountSchedule(dto, loginDTO, tx).ConfigureAwait(false); // else // await _AccountScheduleDAL.UpdateAccountSchedule(dto, loginDTO, tx).ConfigureAwait(false); // return dto.AccountScheduleId; // }, // null, -1, -1, Trans); // await _QueryExecutor.CommitAsync(Trans); // var cacheKey = CacheKeyGenerator.KeyGeneration( // dto.AccountScheduleId, // EntityConstant.OBJECTACCOUNTSCHEDULE, // CacheKeyLevel.USER_LEVEL, // string.Empty, // ← missing SourceData parameter // loginDTO); // Console.WriteLine($"Invalidate Cache Key = {cacheKey}"); // await _KeyInvalidate.AllInvalidateCache(cacheKey); // return isNew ? SuccessResponse.SaveSuccess : SuccessResponse.UpdateSuccess; // } // catch (Exception) // { // await _QueryExecutor.RollbackAsync(Trans); // throw; // } //} public async Task GetSelectListAccountSchedule(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _AccountScheduleDAL.GetSelectListAccountSchedule(criteriaDTO, loginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetAccountScheduleList(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _AccountScheduleDAL.GetAccountScheduleList(criteriaDTO, loginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } //public async Task DeleteAccountSchedule(int accountScheduleId, LoginDTO loginDTO) //{ // var Trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); // try // { // //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); // await _BaseEntityAppService.ExecuteSaveAsync( // EntityConstant.OBJECTACCOUNTSCHEDULE, // EventTypeConstant.DELETEACCOUNTSCHEDULEEVENTTYPEID, // new AccountScheduleDTO { AccountScheduleId = accountScheduleId }, // loginDTO, // async tx => // { // await _AccountScheduleDAL.DeleteAccountSchedule(accountScheduleId, loginDTO, tx) // .ConfigureAwait(false); // return accountScheduleId; // }, // null, -1, -1, Trans); // await _QueryExecutor.CommitAsync(Trans); // var cacheKey = CacheKeyGenerator.KeyGeneration( // accountScheduleId, // ← method parameter // EntityConstant.OBJECTACCOUNTSCHEDULE, // CacheKeyLevel.CLIENT_LEVEL, // string.Empty, // loginDTO); // //await _KeyInvalidate.AllInvalidateCache(cacheKey); // return SuccessResponse.DeleteSuccessMessage; // } // catch (Exception) // { // await _QueryExecutor.RollbackAsync(Trans); // throw; // } //} public async Task DeleteAccountSchedule(int AccountScheduleId, LoginDTO loginDTO) { var Trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); try { //var definitions = new List(); //var executors = new List(); //var engine = new QualifierEngine((IQualifierDefinitionLoader)definitions, executors); //var facade = new QualifierFacade(engine); //var baseSvc = new BaseEntityAppService( // ✅ correct generic // facade, _WorkFlowEngine, _OutBox, _QueryExecutor, _DaprClient); await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTACCOUNTSCHEDULE, EventTypeConstant.DELETEACCOUNTSCHEDULEEVENTTYPEID, new AccountScheduleDTO { AccountScheduleId = AccountScheduleId }, loginDTO, async tx => { await _AccountScheduleDAL.DeleteAccountSchedule(AccountScheduleId, loginDTO, tx) .ConfigureAwait(false); return AccountScheduleId; }, null, -1, -1, Trans); await _QueryExecutor.CommitAsync(Trans); var cacheKey = CacheKeyGenerator.KeyGeneration( AccountScheduleId, // ✅ int parameter from method signature EntityConstant.OBJECTACCOUNTSCHEDULE, CacheKeyLevel.CLIENT_LEVEL, string.Empty, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return SuccessResponse.DeleteSuccessMessage; } catch (Exception) { await _QueryExecutor.RollbackAsync(Trans); throw; } } } }