using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using MMDAL.DTO.StockValuation; using MMDAL.Query.StockValuation; namespace MMDAL.CustomCode.StockValuation; public class StockValuationDAL : IStockValuationDAL { private readonly IQueryExecutor _qe; public StockValuationDAL(IQueryExecutor qe) => _qe = qe; public async Task InsertValuationRunAsync(StockValuationRunDTO run, LoginDTO login, CancellationToken ct) { return (long)await _qe.ExecuteIdentityAsync( login, StockValuationQB.INSERT_VALUATION_RUN, run).ConfigureAwait(false); } public async Task UpdateRunDoneAsync(long runId, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.UPDATE_RUN_DONE, new { StockValuationRunId = runId, CompletedAt = DateTime.UtcNow }, cancellationToken: ct).ConfigureAwait(false); } public async Task UpdateRunFailedAsync(long runId, string errorMessage, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.UPDATE_RUN_FAILED, new { StockValuationRunId = runId, ErrorMessage = errorMessage, CompletedAt = DateTime.UtcNow }, cancellationToken: ct).ConfigureAwait(false); } public async Task MarkStuckRunsFailedAsync(LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.MARK_STUCK_RUNS_FAILED, new { CutoffMinutes = 120 }, cancellationToken: ct).ConfigureAwait(false); } public async Task IsRunActiveAsync( int ouid, byte runType, DateOnly periodFrom, DateOnly periodTo, LoginDTO login, CancellationToken ct) { int count = await _qe.ExecuteScalarAsync( login, StockValuationQB.CHECK_RUN_ACTIVE, new { OUID = ouid, RunType = runType, PeriodFrom = periodFrom.ToDateTime(TimeOnly.MinValue), PeriodTo = periodTo.ToDateTime(TimeOnly.MaxValue) }, cancellationToken: ct).ConfigureAwait(false); return count > 0; } public async Task> GetValuationRunsAsync( int ouid, DateOnly fromDate, DateOnly toDate, LoginDTO login, CancellationToken ct) { var result = await _qe.QueryAsync( login, StockValuationQB.GET_VALUATION_RUNS, new { OUID = ouid, FromDate = fromDate.ToDateTime(TimeOnly.MinValue), ToDate = toDate.ToDateTime(TimeOnly.MaxValue) }, cancellationToken: ct).ConfigureAwait(false); return result.ToList(); } public async Task IsPeriodLockedAsync(int ouid, DateOnly date, LoginDTO login, CancellationToken ct) { int count = await _qe.ExecuteScalarAsync( login, StockValuationQB.CHECK_PERIOD_LOCKED, new { OUID = ouid, Date = date.ToDateTime(TimeOnly.MinValue) }, cancellationToken: ct).ConfigureAwait(false); return count > 0; } public async Task ApplyMakeItemCostAsync(int ouid, DateOnly fromDate, DateOnly toDate, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.APPLY_MAKE_ITEM_COST, new { OUID = ouid, FromDate = fromDate.ToDateTime(TimeOnly.MinValue), ToDate = toDate.ToDateTime(TimeOnly.MaxValue) }, cancellationToken: ct).ConfigureAwait(false); } public async Task SyncStockPositionFromLedgerAsync(int ouid, DateOnly fromDate, DateOnly toDate, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.SYNC_STOCK_POSITION_FROM_LEDGER, new { OUID = ouid, FromDate = fromDate.ToDateTime(TimeOnly.MinValue), ToDate = toDate.ToDateTime(TimeOnly.MaxValue) }, cancellationToken: ct).ConfigureAwait(false); } public async Task InsertBalanceSnapshotAsync(int ouid, DateOnly snapshotDate, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.INSERT_BALANCE_SNAPSHOT, new { OUID = ouid, SnapshotDate = snapshotDate.ToDateTime(TimeOnly.MinValue) }, cancellationToken: ct).ConfigureAwait(false); } public async Task CertifySnapshotAsync(int ouid, DateOnly snapshotDate, LoginDTO login, CancellationToken ct) { await _qe.ExecuteAsync( login, StockValuationQB.CERTIFY_SNAPSHOT, new { OUID = ouid, SnapshotDate = snapshotDate.ToDateTime(TimeOnly.MinValue) }, cancellationToken: ct).ConfigureAwait(false); } public async Task GetLastCertifiedSnapshotAsync( int ouid, DateOnly beforeDate, LoginDTO login, CancellationToken ct) { return await _qe.QuerySingleAsync( login, StockValuationQB.GET_LAST_CERTIFIED_SNAPSHOT, new { OUID = ouid, BeforeDate = beforeDate.ToDateTime(TimeOnly.MinValue) }, cancellationToken: ct).ConfigureAwait(false); } public async Task> GetCostWorkingsAsync( int itemId, int skuId, int storeId, int ouid, DateOnly asOfDate, LoginDTO login, CancellationToken ct) { return await _qe.QueryAsync( login, StockValuationQB.GET_COST_WORKINGS_WA, new { ItemId = itemId, SKUId = skuId, StoreId = storeId, OUID = ouid, AsOfDate = asOfDate.ToDateTime(TimeOnly.MaxValue) }, cancellationToken: ct).ConfigureAwait(false); } }