53 lines
1.7 KiB
C#
53 lines
1.7 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using SPMS.Domain.Entities;
|
|
using SPMS.Domain.Interfaces;
|
|
|
|
namespace SPMS.Infrastructure.Persistence.Repositories;
|
|
|
|
public class DailyStatRepository : Repository<DailyStat>, IDailyStatRepository
|
|
{
|
|
public DailyStatRepository(AppDbContext context) : base(context) { }
|
|
|
|
public async Task<IReadOnlyList<DailyStat>> GetByDateRangeAsync(long serviceId, DateOnly startDate, DateOnly endDate)
|
|
{
|
|
return await _dbSet
|
|
.Where(s => s.ServiceId == serviceId && s.StatDate >= startDate && s.StatDate <= endDate)
|
|
.OrderByDescending(s => s.StatDate)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<DailyStat?> GetByDateAsync(long serviceId, DateOnly date)
|
|
{
|
|
return await _dbSet
|
|
.FirstOrDefaultAsync(s => s.ServiceId == serviceId && s.StatDate == date);
|
|
}
|
|
|
|
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
|
|
});
|
|
}
|
|
}
|
|
}
|