using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Text; using System.Threading.Tasks; using AccountsDAL.CustomCode.Instrument; using AccountsDAL.DTO.Instrument; using GB5Shared.DaprCache; using GB5Shared.DTO.Framework.AutoNumber; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.HybridCache; using GB5Shared.DTO.Framework.Logging; using GB5Shared.DTO.Framework.Login; using GB5Shared.EntityHandler; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using static GB5Shared.GB5Constant.Constant; namespace AccountsBLL.Instrument { public class InstrumentBLL : IInstrumentBLL { private readonly IInstrumentDAL _InstrumentDAL; private readonly AutoNumber _AutoNumber; private readonly IQueryExecutor _QueryExecutor; private readonly KeyInvalidate _KeyInvalidate; private readonly BaseEntityAppService _BaseEntityAppService; public InstrumentBLL( IInstrumentDAL InstrumentDAL, AutoNumber AutoNumber, IQueryExecutor queryExecutor, KeyInvalidate keyInvalidate, BaseEntityAppService baseEntityAppService) { _InstrumentDAL = InstrumentDAL; _AutoNumber = AutoNumber; _QueryExecutor = queryExecutor; _KeyInvalidate = keyInvalidate; _BaseEntityAppService = baseEntityAppService; } public async Task GetInstrument(int InstrumentId, LoginDTO LoginDTO) { try { return await _InstrumentDAL.GetInstrument(InstrumentId, LoginDTO); } catch (Exception) { throw; } } public async Task SaveInstrument( InstrumentDTO InstrumentDTO, LoginDTO LoginDTO, Guid AttachmentHeaderGuid) { InstrumentDTO.TenantId = LoginDTO.ClientId; bool isNew = InstrumentDTO.InstrumentId == 0; var Trans = await _QueryExecutor.BeginTransactionAsync(LoginDTO); try { if (isNew) { AutoNumberDTO autoNumberDTO = await _AutoNumber.GetNumberAsync( 1, "Instrument", LoginDTO); InstrumentDTO.InstrumentId = autoNumberDTO.StartNumber; } await _BaseEntityAppService.ExecuteSaveAsync( EntityConstant.OBJECTINSTRUMENT, EventTypeConstant.SAVEINSTRUMENTID, InstrumentDTO, LoginDTO, async tx => { _ = isNew ? await _InstrumentDAL.SaveInstrument(InstrumentDTO, LoginDTO, tx) : await _InstrumentDAL.UpdateInstrument(InstrumentDTO, LoginDTO, tx); return InstrumentDTO.InstrumentId; }, null, -1, -1, Trans, isNewEntity: isNew, headerRowGuid: AttachmentHeaderGuid); await _QueryExecutor.CommitAsync(Trans); var cacheKey = CacheKeyGenerator.KeyGeneration( InstrumentDTO.InstrumentId, EntityConstant.OBJECTINSTRUMENT, CacheKeyLevel.CLIENT_LEVEL, string.Empty, LoginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey) .ConfigureAwait(false); return isNew ? "Instrument Saved Successfully." : "Instrument Updated Successfully."; } catch { await _QueryExecutor.RollbackAsync(Trans); throw; } } public async Task DeleteInstrument(int instrumentId, LoginDTO loginDTO) { var Trans = await _QueryExecutor.BeginTransactionAsync(loginDTO); try { await _InstrumentDAL.DeleteInstrument(instrumentId, loginDTO, Trans) .ConfigureAwait(false); await _QueryExecutor.CommitAsync(Trans); var cacheKey = CacheKeyGenerator.KeyGeneration( instrumentId, EntityConstant.OBJECTINSTRUMENT, CacheKeyLevel.CLIENT_LEVEL, string.Empty, loginDTO); await _KeyInvalidate.AllInvalidateCache(cacheKey) .ConfigureAwait(false); return SuccessResponse.DeleteSuccessMessage; } catch (Exception) { await _QueryExecutor.RollbackAsync(Trans); throw; } } public async Task GetSelectListInstrument(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _InstrumentDAL.GetSelectListInstrument(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } public async Task GetInstrumentList(CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { return await _InstrumentDAL.GetInstrumentList(CriteriaDTO, LoginDTO); } catch (Exception) { throw; } } } }