using FrameworkDAL.DTO.EIPConversation; using FrameworkDAL.Query.EIPConversation.EIPActionRegistryAdmin; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using static GB5Shared.GB5Constant.Constant; namespace FrameworkDAL.CustomCode.EIPConversation.EIPActionRegistryAdmin { public class EIPActionRegistryAdminDAL : IEIPActionRegistryAdminDAL { private readonly IQueryExecutor _qe; private readonly AutoNumber _autoNumber; private readonly ILogger _logger; public EIPActionRegistryAdminDAL(IQueryExecutor qe, AutoNumber autoNumber, ILogger logger) { _qe = qe ?? throw new ArgumentNullException(nameof(qe)); _autoNumber = autoNumber ?? throw new ArgumentNullException(nameof(autoNumber)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public async Task> GetActionRegistryListAsync( string? search, LoginDTO login, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); try { _logger.LogInformation("DAL | GetActionRegistryList | Tenant={TenantId} | Search={Search}", login.ClientId, search); var result = await _qe.QueryAsync( login, EIPActionRegistryAdminQB.GET_ACTIONREGISTRY_LIST, new { TenantId = login.ClientId, Search = search }, cancellationToken: ct).ConfigureAwait(false); return result ?? []; } catch (OperationCanceledException) { _logger.LogWarning("DAL | GetActionRegistryList | Cancelled | Tenant={TenantId}", login.ClientId); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | GetActionRegistryList | Error | Tenant={TenantId}", login.ClientId); throw; } } public async Task GetActionRegistryAsync( int actionId, LoginDTO login, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); try { _logger.LogInformation("DAL | GetActionRegistry | Tenant={TenantId} | Id={Id}", login.ClientId, actionId); return await _qe.QuerySingleAsync( login, EIPActionRegistryAdminQB.GET_ACTIONREGISTRY, new { ActionId = actionId, TenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); } catch (OperationCanceledException) { _logger.LogWarning("DAL | GetActionRegistry | Cancelled | Id={Id}", actionId); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | GetActionRegistry | Error | Id={Id}", actionId); throw; } } public async Task InsertActionRegistryAsync( EIPActionRegistryAdminDTO dto, LoginDTO login, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); try { _logger.LogInformation("DAL | InsertActionRegistry | Tenant={TenantId} | Code={Code}", login.ClientId, dto.ActionCode); // ACTIONID is a plain INT PK (not IDENTITY) — must be generated here. var auto = await _autoNumber.GetNumberAsync(1, AUTONUMBERCONSTANT.EIPACTIONREGISTRY, login) .ConfigureAwait(false); var newId = auto.StartNumber; await _qe.ExecuteAsync( login, EIPActionRegistryAdminQB.INSERT_ACTIONREGISTRY, new { ActionId = newId, TenantId = login.ClientId, dto.ActionCode, dto.ActionName, dto.AssemblyName, dto.ClassName, dto.IsIdempotent, dto.TimeoutMs, dto.ActionVersion, dto.IsActive, dto.Version, CreatedById = login.UserId }, cancellationToken: ct).ConfigureAwait(false); _logger.LogInformation("DAL | InsertActionRegistry | Completed | NewId={NewId}", newId); return newId; } catch (OperationCanceledException) { _logger.LogWarning("DAL | InsertActionRegistry | Cancelled | Code={Code}", dto.ActionCode); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | InsertActionRegistry | Error | Code={Code}", dto.ActionCode); throw; } } public async Task UpdateActionRegistryAsync( EIPActionRegistryAdminDTO dto, LoginDTO login, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); try { _logger.LogInformation("DAL | UpdateActionRegistry | Tenant={TenantId} | Id={Id}", login.ClientId, dto.ActionId); await _qe.ExecuteAsync( login, EIPActionRegistryAdminQB.UPDATE_ACTIONREGISTRY, new { dto.ActionId, TenantId = login.ClientId, dto.ActionCode, dto.ActionName, dto.AssemblyName, dto.ClassName, dto.IsIdempotent, dto.TimeoutMs, dto.ActionVersion, dto.IsActive, dto.Version, ModifiedById = login.UserId }, cancellationToken: ct).ConfigureAwait(false); } catch (OperationCanceledException) { _logger.LogWarning("DAL | UpdateActionRegistry | Cancelled | Id={Id}", dto.ActionId); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | UpdateActionRegistry | Error | Id={Id}", dto.ActionId); throw; } } public async Task DeleteActionRegistryAsync( int actionId, LoginDTO login, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); try { _logger.LogInformation("DAL | DeleteActionRegistry | Tenant={TenantId} | Id={Id}", login.ClientId, actionId); await _qe.ExecuteAsync( login, EIPActionRegistryAdminQB.DELETE_ACTIONREGISTRY, new { ActionId = actionId, TenantId = login.ClientId, ModifiedById = login.UserId }, cancellationToken: ct).ConfigureAwait(false); } catch (OperationCanceledException) { _logger.LogWarning("DAL | DeleteActionRegistry | Cancelled | Id={Id}", actionId); throw; } catch (Exception ex) { _logger.LogError(ex, "DAL | DeleteActionRegistry | Error | Id={Id}", actionId); throw; } } public async Task ActionCodeExistsAsync( string actionCode, int excludeActionId, LoginDTO login, CancellationToken ct = default) { ct.ThrowIfCancellationRequested(); var count = await _qe.ExecuteScalarAsync( login, EIPActionRegistryAdminQB.EXISTS_ACTIONCODE, new { ActionCode = actionCode, ActionId = excludeActionId }, cancellationToken: ct).ConfigureAwait(false); return count == 1; } } }