using Microsoft.EntityFrameworkCore; using SPMS.Domain.Entities; using SPMS.Domain.Interfaces; namespace SPMS.Infrastructure.Persistence.Repositories; public class DailyStatRepository : Repository, IDailyStatRepository { public DailyStatRepository(AppDbContext context) : base(context) { } public async Task> GetByDateRangeAsync(long? serviceId, DateOnly startDate, DateOnly endDate) { var query = _dbSet.Where(s => s.StatDate >= startDate && s.StatDate <= endDate); if (serviceId.HasValue) query = query.Where(s => s.ServiceId == serviceId.Value); return await query.OrderByDescending(s => s.StatDate).ToListAsync(); } public async Task GetByDateAsync(long? serviceId, DateOnly date) { var query = _dbSet.Where(s => s.StatDate == date); if (serviceId.HasValue) query = query.Where(s => s.ServiceId == serviceId.Value); return await query.FirstOrDefaultAsync(); } public async Task UpsertAsync(long serviceId, DateOnly statDate, int sentCnt, int successCnt, int failCnt, int openCnt) { var existing = await _dbSet .FirstOrDefaultAsync(s => s.ServiceId == serviceId && s.StatDate == statDate); if (existing != null) { existing.SentCnt = sentCnt; existing.SuccessCnt = successCnt; existing.FailCnt = failCnt; existing.OpenCnt = openCnt; _context.Entry(existing).State = EntityState.Modified; } else { await _dbSet.AddAsync(new DailyStat { ServiceId = serviceId, StatDate = statDate, SentCnt = sentCnt, SuccessCnt = successCnt, FailCnt = failCnt, OpenCnt = openCnt, CreatedAt = DateTime.UtcNow }); } } }