190 lines
6.4 KiB
C#
190 lines
6.4 KiB
C#
using System.Linq;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TakeoutSaaS.Domain.Stores.Entities;
|
|
using TakeoutSaaS.Domain.Stores.Enums;
|
|
using TakeoutSaaS.Domain.Stores.Repositories;
|
|
using TakeoutSaaS.Infrastructure.App.Persistence;
|
|
|
|
namespace TakeoutSaaS.Infrastructure.App.Repositories;
|
|
|
|
/// <summary>
|
|
/// 门店聚合的 EF Core 仓储实现。
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// 初始化仓储。
|
|
/// </remarks>
|
|
public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepository
|
|
{
|
|
/// <inheritdoc />
|
|
public Task<Store?> FindByIdAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.Stores
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.Id == storeId)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<Store>> SearchAsync(long tenantId, StoreStatus? status, CancellationToken cancellationToken = default)
|
|
{
|
|
var query = context.Stores
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId);
|
|
|
|
if (status.HasValue)
|
|
{
|
|
query = query.Where(x => x.Status == status.Value);
|
|
}
|
|
|
|
var stores = await query
|
|
.OrderBy(x => x.Name)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return stores;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<StoreBusinessHour>> GetBusinessHoursAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var hours = await context.StoreBusinessHours
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
|
.OrderBy(x => x.DayOfWeek)
|
|
.ThenBy(x => x.StartTime)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return hours;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<StoreDeliveryZone>> GetDeliveryZonesAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var zones = await context.StoreDeliveryZones
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
|
.OrderBy(x => x.SortOrder)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return zones;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<StoreHoliday>> GetHolidaysAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var holidays = await context.StoreHolidays
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
|
.OrderBy(x => x.Date)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return holidays;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<StoreTableArea>> GetTableAreasAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var areas = await context.StoreTableAreas
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
|
.OrderBy(x => x.SortOrder)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return areas;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<StoreTable>> GetTablesAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var tables = await context.StoreTables
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
|
.OrderBy(x => x.TableCode)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return tables;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<IReadOnlyList<StoreEmployeeShift>> GetShiftsAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var shifts = await context.StoreEmployeeShifts
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
|
|
.OrderBy(x => x.ShiftDate)
|
|
.ThenBy(x => x.StartTime)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
return shifts;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddStoreAsync(Store store, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.Stores.AddAsync(store, cancellationToken).AsTask();
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddBusinessHoursAsync(IEnumerable<StoreBusinessHour> hours, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.StoreBusinessHours.AddRangeAsync(hours, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddDeliveryZonesAsync(IEnumerable<StoreDeliveryZone> zones, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.StoreDeliveryZones.AddRangeAsync(zones, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddHolidaysAsync(IEnumerable<StoreHoliday> holidays, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.StoreHolidays.AddRangeAsync(holidays, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddTableAreasAsync(IEnumerable<StoreTableArea> areas, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.StoreTableAreas.AddRangeAsync(areas, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddTablesAsync(IEnumerable<StoreTable> tables, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.StoreTables.AddRangeAsync(tables, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task AddShiftsAsync(IEnumerable<StoreEmployeeShift> shifts, CancellationToken cancellationToken = default)
|
|
{
|
|
return context.StoreEmployeeShifts.AddRangeAsync(shifts, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
return context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task UpdateStoreAsync(Store store, CancellationToken cancellationToken = default)
|
|
{
|
|
context.Stores.Update(store);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task DeleteStoreAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
|
|
{
|
|
var existing = await context.Stores
|
|
.Where(x => x.TenantId == tenantId && x.Id == storeId)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
|
|
if (existing == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
context.Stores.Remove(existing);
|
|
}
|
|
}
|