103 lines
3.4 KiB
C#
103 lines
3.4 KiB
C#
using System.Linq;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TakeoutSaaS.Domain.Deliveries.Entities;
|
|
using TakeoutSaaS.Domain.Deliveries.Repositories;
|
|
using TakeoutSaaS.Infrastructure.App.Persistence;
|
|
|
|
namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
|
|
|
/// <summary>
|
|
/// 配送聚合的 EF Core 仓储实现。
|
|
/// </summary>
|
|
public sealed class EfDeliveryRepository : IDeliveryRepository
|
|
{
|
|
private readonly TakeoutAppDbContext _context;
|
|
|
|
/// <summary>
|
|
/// 初始化仓储。
|
|
/// </summary>
|
|
public EfDeliveryRepository(TakeoutAppDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<DeliveryOrder?> FindByIdAsync(long deliveryOrderId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
return _context.DeliveryOrders
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.Id == deliveryOrderId)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<DeliveryOrder?> FindByOrderIdAsync(long orderId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
return _context.DeliveryOrders
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.OrderId == orderId)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<DeliveryEvent>> GetEventsAsync(long deliveryOrderId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var events = await _context.DeliveryEvents
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.DeliveryOrderId == deliveryOrderId)
|
|
.OrderBy(x => x.CreatedAt)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return events;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddDeliveryOrderAsync(DeliveryOrder deliveryOrder, CancellationToken cancellationToken = default)
|
|
{
|
|
return _context.DeliveryOrders.AddAsync(deliveryOrder, cancellationToken).AsTask();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddEventAsync(DeliveryEvent deliveryEvent, CancellationToken cancellationToken = default)
|
|
{
|
|
return _context.DeliveryEvents.AddAsync(deliveryEvent, cancellationToken).AsTask();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return _context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task UpdateDeliveryOrderAsync(DeliveryOrder deliveryOrder, CancellationToken cancellationToken = default)
|
|
{
|
|
_context.DeliveryOrders.Update(deliveryOrder);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task DeleteDeliveryOrderAsync(long deliveryOrderId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var events = await _context.DeliveryEvents
|
|
.Where(x => x.TenantId == tenantId && x.DeliveryOrderId == deliveryOrderId)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
if (events.Count > 0)
|
|
{
|
|
_context.DeliveryEvents.RemoveRange(events);
|
|
}
|
|
|
|
var existing = await _context.DeliveryOrders
|
|
.Where(x => x.TenantId == tenantId && x.Id == deliveryOrderId)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
if (existing == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_context.DeliveryOrders.Remove(existing);
|
|
}
|
|
}
|