using AccountsDAL.CustomCode.CostCenterPattern; using AccountsDAL.DTO.CostCenter; using AccountsDAL.DTO.CostCenterPattern; using AccountsDAL.DTO.CostCenterPatternDetail; using Dapr.Client; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.AutoNumber; 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.CostCenterPattern { public class CostCenterPatternBLL : ICostCenterPatternBLL { private readonly ICostCenterPatternDAL _CostCenterPatternDAL; 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 CostCenterPatternBLL( ICostCenterPatternDAL costCenterPatternDAL, AutoNumber autoNumber, IWorkFlowEngine workFlowEngine, IQueryExecutor queryExecutor, IOutBox outBox, KeyInvalidate keyInvalidate, DaprClient daprClient, BaseEntityAppService baseEntityAppService ) { _CostCenterPatternDAL = costCenterPatternDAL; _AutoNumber = autoNumber; _WorkFlowEngine = workFlowEngine; _QueryExecutor = queryExecutor; _OutBox = outBox; _KeyInvalidate = keyInvalidate; _DaprClient = daprClient; _BaseEntityAppService = baseEntityAppService; } public async Task GetCostCenterPattern(int costCenterPatternId, LoginDTO loginDTO) { try { return await _CostCenterPatternDAL.GetCostCenterPattern(costCenterPatternId, loginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } public async Task SaveCostCenterPattern( CostCenterPatternDTO dto, LoginDTO loginDTO, CancellationToken ct) { if (dto == null) throw new ArgumentNullException(nameof(dto)); dto.TenantId = loginDTO.ClientId; AutoNumberDTO? autoNumberDTO = null; var trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); try { bool isNew = dto.CostCenterPatternId == 0; if (isNew) { int detailCount = dto.CostCenterPatternDetailArray?.Count ?? 0; autoNumberDTO = await _AutoNumber.GetNumberAsync( 1 + detailCount, AUTONUMBERCONSTANT.COSTCENTERPATTERN, loginDTO); int nextId = autoNumberDTO.StartNumber; dto.CostCenterPatternId = nextId++; if (dto.CostCenterPatternDetailArray?.Any() == true) { foreach (var detail in dto.CostCenterPatternDetailArray) { detail.CostCenterPatternDetailId = nextId++; detail.CostCenterPatternId = dto.CostCenterPatternId; } } } else { var newDetails = dto.CostCenterPatternDetailArray? .Where(x => x.CostCenterPatternDetailId == -1) .ToList(); if (newDetails?.Any() == true) { autoNumberDTO = await _AutoNumber.GetNumberAsync( newDetails.Count, AUTONUMBERCONSTANT.COSTCENTERPATTERN, loginDTO); int nextId = autoNumberDTO.StartNumber; foreach (var detail in newDetails) { detail.CostCenterPatternDetailId = nextId++; detail.CostCenterPatternId = dto.CostCenterPatternId; } } } await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTCOSTCENTERPATTERN, EventTypeConstant.SAVECOSTCENTERPATTERNEVENTTYPEID, dto, loginDTO, async tx => { // Header if (isNew) { await _CostCenterPatternDAL .SaveCostCenterPattern(dto, loginDTO, tx) .ConfigureAwait(false); } else { await _CostCenterPatternDAL .UpdateCostCenterPattern(dto, loginDTO, tx) .ConfigureAwait(false); await _CostCenterPatternDAL .DeleteCostCenterPatternDetails( dto.CostCenterPatternId, loginDTO, tx) .ConfigureAwait(false); } // Details if (dto.CostCenterPatternDetailArray?.Any() == true) { foreach (var detail in dto.CostCenterPatternDetailArray) { detail.CostCenterPatternId = dto.CostCenterPatternId; await _CostCenterPatternDAL .SaveCostCenterPatternDetail( detail, loginDTO, tx) .ConfigureAwait(false); } } return dto.CostCenterPatternId; }, null, -1, -1, trans); await _QueryExecutor.CommitAsync(trans); var cacheKey = CacheKeyGenerator.KeyGeneration( dto.CostCenterPatternId, EntityConstant.OBJECTCOSTCENTERPATTERN, CacheKeyLevel.CLIENT_LEVEL, string.Empty, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey).ConfigureAwait(false); return isNew ? $"{SuccessResponse.SaveSuccessMessage} {dto.CostCenterPatternId}" : $"{SuccessResponse.UpdateSuccessMessage} {dto.CostCenterPatternId}"; } catch { await _QueryExecutor.RollbackAsync(trans); if (autoNumberDTO != null) { await _AutoNumber.RollbackAutoNumber( AUTONUMBERCONSTANT.COSTCENTERPATTERN, autoNumberDTO.StartNumber, loginDTO); } throw; } } public async Task DeleteCostCenterPattern(int costCenterPatternId, 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.OBJECTCOSTCENTERPATTERN, EventTypeConstant.DELETECOSTCENTERPATTERNEVENTTYPEID, new CostCenterPatternDTO { CostCenterPatternId = costCenterPatternId }, loginDTO, async tx => { await _CostCenterPatternDAL.DeleteCostCenterPattern(costCenterPatternId, loginDTO, tx) .ConfigureAwait(false); return costCenterPatternId; }, null, -1, -1, Trans); await _QueryExecutor.CommitAsync(Trans); var cacheKey = CacheKeyGenerator.KeyGeneration( costCenterPatternId, EntityConstant.OBJECTCOSTCENTERPATTERN, CacheKeyLevel.CLIENT_LEVEL, string.Empty, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey); return SuccessResponse.DeleteSuccessMessage; } catch (Exception) { await _QueryExecutor.RollbackAsync(Trans); throw; } } public async Task GetSelectListCostCenterPattern(CriteriaDTO criteriaDTO, LoginDTO loginDTO) { try { return await _CostCenterPatternDAL.GetSelectListCostCenterPattern(criteriaDTO, loginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } public async Task GetCostCenterPatternList(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _CostCenterPatternDAL.GetCostCenterPatternList(CriteriaDTO, LoginDTO) .ConfigureAwait(false); } catch (Exception) { throw; } } } }