using System.Data.Common; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.ResponseStandard; using PIEDAL.DTO.Module; using PIEDAL.Query.Module; namespace PIEDAL.CustomCode.Module; public class PIEModuleDAL : IPIEModuleDAL { private readonly IQueryExecutor _QueryExecutor; public PIEModuleDAL(IQueryExecutor queryExecutor) { _QueryExecutor = queryExecutor; } // ── Reads ───────────────────────────────────────────────────────────────── public async Task GetPIEModuleAsync( Guid moduleId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, PIEModuleQB.GET_BY_ID, new { ModuleId = moduleId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetPIEModuleDependenciesAsync( Guid moduleId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, PIEModuleQB.GET_DEPENDENCIES, new { ModuleId = moduleId }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task> GetAllDependenciesForCycleCheckAsync( LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, PIEModuleQB.GET_ALL_DEPENDENCIES_FOR_CYCLE_CHECK, new { }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task> GetPIEModuleSelectListAsync( LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, PIEModuleQB.GET_SELECT_LIST, new { }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); public async Task GetPIEKpiAsync( Guid kpiId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QuerySingleAsync( login, PIEKpiQB.GET_BY_ID, new { KpiId = kpiId }, cancellationToken: ct).ConfigureAwait(false); public async Task> GetKpisByModuleAsync( Guid moduleId, LoginDTO login, CancellationToken ct) => await _QueryExecutor.QueryAsync( login, PIEKpiQB.GET_BY_MODULE, new { ModuleId = moduleId }, cancellationToken: ct).ConfigureAwait(false) ?? Enumerable.Empty(); // ── Module writes ───────────────────────────────────────────────────────── public async Task> SavePIEModuleAsync( PIEModuleDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.CreatedBy = login.UserId; dto.CreatedAt = DateTime.UtcNow; dto.UpdatedBy = login.UserId; dto.UpdatedAt = DateTime.UtcNow; int rows = await _QueryExecutor.ExecuteAsync( login, PIEModuleQB.INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("PIE module saved.") : Result.Failure("Failed to save PIE module."); } public async Task> UpdatePIEModuleAsync( PIEModuleDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.UpdatedBy = login.UserId; dto.UpdatedAt = DateTime.UtcNow; int rows = await _QueryExecutor.ExecuteAsync( login, PIEModuleQB.UPDATE, dto, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("PIE module updated.") : Result.Failure("PIE module not found or not updated."); } public async Task> SoftDeletePIEModuleAsync( Guid moduleId, LoginDTO login, DbTransaction tx, CancellationToken ct) { var param = new { ModuleId = moduleId, UpdatedAt = DateTime.UtcNow, UpdatedBy = login.UserId }; int rows = await _QueryExecutor.ExecuteAsync( login, PIEModuleQB.SOFT_DELETE, param, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("PIE module deleted.") : Result.Failure("PIE module not found or already inactive."); } // ── Dependency writes ───────────────────────────────────────────────────── public async Task DeleteModuleDependenciesAsync( Guid moduleId, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, PIEModuleQB.DELETE_DEPENDENCIES, new { ModuleId = moduleId }, tx, cancellationToken: ct).ConfigureAwait(false); public async Task InsertModuleDependencyAsync( PIEModuleDependencyDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) => await _QueryExecutor.ExecuteAsync( login, PIEModuleQB.INSERT_DEPENDENCY, dto, tx, cancellationToken: ct).ConfigureAwait(false); // ── KPI writes ──────────────────────────────────────────────────────────── public async Task> SavePIEKpiAsync( PIEKpiDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.CreatedBy = login.UserId; dto.CreatedAt = DateTime.UtcNow; dto.UpdatedBy = login.UserId; dto.UpdatedAt = DateTime.UtcNow; int rows = await _QueryExecutor.ExecuteAsync( login, PIEKpiQB.INSERT, dto, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("PIE KPI saved.") : Result.Failure("Failed to save PIE KPI."); } public async Task> UpdatePIEKpiAsync( PIEKpiDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.UpdatedBy = login.UserId; dto.UpdatedAt = DateTime.UtcNow; int rows = await _QueryExecutor.ExecuteAsync( login, PIEKpiQB.UPDATE, dto, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("PIE KPI updated.") : Result.Failure("PIE KPI not found or not updated."); } public async Task> SoftDeletePIEKpiAsync( Guid kpiId, LoginDTO login, DbTransaction tx, CancellationToken ct) { var param = new { KpiId = kpiId, UpdatedAt = DateTime.UtcNow, UpdatedBy = login.UserId }; int rows = await _QueryExecutor.ExecuteAsync( login, PIEKpiQB.SOFT_DELETE, param, tx, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("PIE KPI deleted.") : Result.Failure("PIE KPI not found or already inactive."); } }