using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.GenerateAutoNumber; using GB5Shared.QueryExecutor; using PERMDAL.DTOs; using PERMDAL.Interfaces; using PERMDAL.QueryBuilders; namespace PERMDAL.Implementations; public class AppraisalPlanDAL : IAppraisalPlanDAL { private readonly IQueryExecutor _qe; private readonly AutoNumber _autoNumber; public AppraisalPlanDAL(IQueryExecutor queryExecutor, AutoNumber autoNumber) { _qe = queryExecutor; _autoNumber = autoNumber; } public async Task GetAppraisalPlanAsync(int planId, LoginDTO login, CancellationToken ct) => await _qe.QuerySingleAsync( login, AppraisalPlanQB.GET_BY_ID, new { AppraisalPlanId = planId, TenantId = login.ClientId }) .ConfigureAwait(false); public async Task> GetAppraisalPlanListAsync( short? status, int page, int pageSize, LoginDTO login, CancellationToken ct) => await _qe.QueryAsync(login, AppraisalPlanQB.GET_LIST, new { TenantId = login.ClientId, Status = status, Offset = (page - 1) * pageSize, PageSize = pageSize }, cancellationToken: ct) .ConfigureAwait(false) ?? Enumerable.Empty(); public async Task> GetAppraisalPlanSelectListAsync(CriteriaDTO criteriaDTO, LoginDTO login, CancellationToken ct) => await _qe.QueryWithCriteriaAsync( login, AppraisalPlanQB.GET_SELECT_LIST, new { TenantId = login.ClientId }, criteriaDTO, cancellationToken: ct) .ConfigureAwait(false) ?? Enumerable.Empty(); public async Task InsertAppraisalPlanAsync(AppraisalPlanDTO dto, LoginDTO login, CancellationToken ct) { var auto = await _autoNumber.GetAutoNumber(1, "APPRAISALPLAN", login).ConfigureAwait(false); dto.AppraisalPlanId = auto.StartNumber; await _qe.ExecuteAsync(login, AppraisalPlanQB.INSERT, new { dto.AppraisalPlanId, dto.Name, dto.Description, dto.AppraisalTypeId, dto.PerformanceFrom, dto.PerformanceTo, dto.EntryFrom, dto.EntryTo, dto.Nature, dto.SelfAppraisal, dto.SourceType, TenantId = login.ClientId, dto.SecurityLevel, CreatedById = login.UserId, ModifiedById = login.UserId, dto.SortOrder }) .ConfigureAwait(false); return dto.AppraisalPlanId; } public async Task UpdateAppraisalPlanAsync(AppraisalPlanDTO dto, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync(login, AppraisalPlanQB.UPDATE, new { dto.AppraisalPlanId, dto.Name, dto.Description, dto.AppraisalTypeId, dto.PerformanceFrom, dto.PerformanceTo, dto.EntryFrom, dto.EntryTo, dto.Nature, dto.SelfAppraisal, TenantId = login.ClientId, ModifiedById = login.UserId }, cancellationToken: ct) .ConfigureAwait(false); public async Task ActivateAppraisalPlanAsync(int planId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync(login, AppraisalPlanQB.ACTIVATE, new { AppraisalPlanId = planId, TenantId = login.ClientId, ModifiedById = login.UserId }, cancellationToken: ct) .ConfigureAwait(false); public async Task CloseAppraisalPlanAsync(int planId, LoginDTO login, CancellationToken ct) => await _qe.ExecuteAsync(login, AppraisalPlanQB.CLOSE, new { AppraisalPlanId = planId, TenantId = login.ClientId, ModifiedById = login.UserId }, cancellationToken: ct) .ConfigureAwait(false); public async Task NameExistsAsync(string name, int excludePlanId, LoginDTO login, CancellationToken ct) { int count = await _qe.ExecuteScalarAsync(login, AppraisalPlanQB.EXISTS_NAME, new { Name = name, TenantId = login.ClientId, AppraisalPlanId = excludePlanId }, cancellationToken: ct) .ConfigureAwait(false); return count > 0; } public async Task ActivePlanExistsForTypeAsync(int appraisalTypeId, int excludePlanId, LoginDTO login, CancellationToken ct) { int count = await _qe.ExecuteScalarAsync(login, AppraisalPlanQB.EXISTS_ACTIVE_PLAN_FOR_TYPE, new { AppraisalTypeId = appraisalTypeId, TenantId = login.ClientId, ExcludePlanId = excludePlanId }, cancellationToken: ct) .ConfigureAwait(false); return count > 0; } }