using FAMDAL.DTO.Depreciation; using FAMDAL.Query.Depreciation; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.ResponseStandard; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Data.Common; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace FAMDAL.CustomCode.Depreciation { public class DepreciationDAL : IDepreciationDAL { private readonly IQueryExecutor _QueryExecutor; private readonly ILogger _Logger; public DepreciationDAL(IQueryExecutor IQueryExecutor, ILogger ILogger) { _QueryExecutor = IQueryExecutor; _Logger = ILogger; } public async Task GetDepreciation(int DepreciationId, LoginDTO LoginDTO) { try { string sql = DepreciationQB.GET_DEPRECIATION; var parameters = new { depreciationid = DepreciationId }; DepreciationDTO DepreciationDTO = await _QueryExecutor.QuerySingleAsync(LoginDTO, sql, parameters); return JsonConvert.SerializeObject(DepreciationDTO); } catch (Exception ex) { _Logger.LogError(ex, "Error fetching cost analysis with ID {DepreciationId}", DepreciationId); throw; } } public async Task GetSelectListDepreciation(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO) { try { string sql = DepreciationQB.GET_SELECTLIST_DEPRECIATION; var Criteria = CriteriaParser.Parse(CriteriaDTO); string DepreciationName = CriteriaParser.GetString(Criteria, "DepreciationName"); var parameters = new { firstnumber = FirstNumber, maxresult = MaxResult, depreciationname = string.IsNullOrWhiteSpace(DepreciationName) ? null : DepreciationName }; var result = await _QueryExecutor.QueryAsync(LoginDTO, sql, parameters); return JsonConvert.SerializeObject(result); } catch (Exception ex) { _Logger.LogError(ex, "Error fetching Depreciation select list"); throw; } } public async Task GetSelectListDepreciation(LoginDTO login, CancellationToken ct) { try { var result = await _QueryExecutor.QueryAsync(login, DepreciationQB.GET_SELECTLIST_DEPRECIATION_ALL, new { DepreciationTenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); return JsonConvert.SerializeObject(result); } catch (Exception ex) { _Logger.LogError(ex, "Error fetching Depreciation select list"); throw; } } public async Task SaveDepreciation(DepreciationDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.DepreciationCreatedOn = DateTime.UtcNow; dto.DepreciationCreatedById = login.UserId; dto.DepreciationModifiedOn = DateTime.UtcNow; dto.DepreciationModifiedById = login.UserId; await _QueryExecutor.ExecuteAsync(login, DepreciationQB.SAVE_DEPRECIATION, dto, tx, cancellationToken: ct).ConfigureAwait(false); return dto.DepreciationId; } public async Task UpdateDepreciation(DepreciationDTO dto, LoginDTO login, DbTransaction tx, CancellationToken ct) { dto.DepreciationModifiedOn = DateTime.UtcNow; dto.DepreciationModifiedById = login.UserId; await _QueryExecutor.ExecuteAsync(login, DepreciationQB.UPDATE_DEPRECIATION, dto, tx, cancellationToken: ct).ConfigureAwait(false); } public async Task> DeleteDepreciation(int depreciationId, LoginDTO login, CancellationToken ct) { var rows = await _QueryExecutor.ExecuteAsync(login, DepreciationQB.DELETE_DEPRECIATION, new { DepreciationId = depreciationId, DepreciationTenantId = login.ClientId }, cancellationToken: ct).ConfigureAwait(false); return rows > 0 ? Result.Success("Depreciation Details Deleted Successfully.") : Result.Failure("Depreciation not found or could not be deleted."); } } }