完成门店管理后端接口与任务

This commit is contained in:
2026-01-01 07:26:14 +08:00
parent dc9f6136d6
commit fc55003d3d
131 changed files with 15333 additions and 201 deletions

View File

@@ -36,17 +36,51 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos
}
/// <inheritdoc />
public async Task<IReadOnlyList<Store>> SearchAsync(long tenantId, StoreStatus? status, CancellationToken cancellationToken = default)
public async Task<IReadOnlyList<Store>> SearchAsync(
long tenantId,
long? merchantId,
StoreStatus? status,
StoreAuditStatus? auditStatus,
StoreBusinessStatus? businessStatus,
StoreOwnershipType? ownershipType,
string? keyword,
CancellationToken cancellationToken = default)
{
var query = context.Stores
.AsNoTracking()
.Where(x => x.TenantId == tenantId);
if (merchantId.HasValue)
{
query = query.Where(x => x.MerchantId == merchantId.Value);
}
if (status.HasValue)
{
query = query.Where(x => x.Status == status.Value);
}
if (auditStatus.HasValue)
{
query = query.Where(x => x.AuditStatus == auditStatus.Value);
}
if (businessStatus.HasValue)
{
query = query.Where(x => x.BusinessStatus == businessStatus.Value);
}
if (ownershipType.HasValue)
{
query = query.Where(x => x.OwnershipType == ownershipType.Value);
}
if (!string.IsNullOrWhiteSpace(keyword))
{
var trimmed = keyword.Trim();
query = query.Where(x => x.Name.Contains(trimmed) || x.Code.Contains(trimmed));
}
var stores = await query
.OrderBy(x => x.Name)
.ToListAsync(cancellationToken);
@@ -54,6 +88,43 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos
return stores;
}
/// <inheritdoc />
public async Task<bool> ExistsStoreWithinDistanceAsync(
long merchantId,
long tenantId,
double longitude,
double latitude,
double distanceMeters,
CancellationToken cancellationToken = default)
{
// 1. 校验距离阈值
if (distanceMeters <= 0)
{
return false;
}
// 2. (空行后) 拉取候选坐标
var coordinates = await context.Stores
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.MerchantId == merchantId)
.Where(x => x.Longitude.HasValue && x.Latitude.HasValue)
.Select(x => new { Longitude = x.Longitude!.Value, Latitude = x.Latitude!.Value })
.ToListAsync(cancellationToken);
// 3. (空行后) 计算距离并判断是否命中
foreach (var coordinate in coordinates)
{
var distance = CalculateDistanceMeters(latitude, longitude, coordinate.Latitude, coordinate.Longitude);
if (distance <= distanceMeters)
{
return true;
}
}
// 4. (空行后) 返回未命中结果
return false;
}
/// <inheritdoc />
public async Task<Dictionary<long, int>> GetStoreCountsAsync(long? tenantId, IReadOnlyCollection<long> merchantIds, CancellationToken cancellationToken = default)
{
@@ -92,6 +163,91 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos
return hours;
}
/// <inheritdoc />
public Task<StoreFee?> GetStoreFeeAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
{
return context.StoreFees
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task AddStoreFeeAsync(StoreFee storeFee, CancellationToken cancellationToken = default)
{
return context.StoreFees.AddAsync(storeFee, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task UpdateStoreFeeAsync(StoreFee storeFee, CancellationToken cancellationToken = default)
{
context.StoreFees.Update(storeFee);
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task<IReadOnlyList<StoreQualification>> GetQualificationsAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
{
var qualifications = await context.StoreQualifications
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
.OrderBy(x => x.SortOrder)
.ThenBy(x => x.QualificationType)
.ToListAsync(cancellationToken);
return qualifications;
}
/// <inheritdoc />
public Task<StoreQualification?> FindQualificationByIdAsync(long qualificationId, long tenantId, CancellationToken cancellationToken = default)
{
return context.StoreQualifications
.Where(x => x.TenantId == tenantId && x.Id == qualificationId)
.FirstOrDefaultAsync(cancellationToken);
}
/// <inheritdoc />
public Task AddQualificationAsync(StoreQualification qualification, CancellationToken cancellationToken = default)
{
return context.StoreQualifications.AddAsync(qualification, cancellationToken).AsTask();
}
/// <inheritdoc />
public Task UpdateQualificationAsync(StoreQualification qualification, CancellationToken cancellationToken = default)
{
context.StoreQualifications.Update(qualification);
return Task.CompletedTask;
}
/// <inheritdoc />
public async Task DeleteQualificationAsync(long qualificationId, long tenantId, CancellationToken cancellationToken = default)
{
var existing = await context.StoreQualifications
.Where(x => x.TenantId == tenantId && x.Id == qualificationId)
.FirstOrDefaultAsync(cancellationToken);
if (existing != null)
{
context.StoreQualifications.Remove(existing);
}
}
/// <inheritdoc />
public Task AddAuditRecordAsync(StoreAuditRecord record, CancellationToken cancellationToken = default)
{
return context.StoreAuditRecords.AddAsync(record, cancellationToken).AsTask();
}
/// <inheritdoc />
public async Task<IReadOnlyList<StoreAuditRecord>> GetAuditRecordsAsync(long storeId, long tenantId, CancellationToken cancellationToken = default)
{
var records = await context.StoreAuditRecords
.AsNoTracking()
.Where(x => x.TenantId == tenantId && x.StoreId == storeId)
.OrderByDescending(x => x.CreatedAt)
.ToListAsync(cancellationToken);
return records;
}
/// <inheritdoc />
public Task<StoreBusinessHour?> FindBusinessHourByIdAsync(long businessHourId, long tenantId, CancellationToken cancellationToken = default)
{
@@ -476,4 +632,20 @@ public sealed class EfStoreRepository(TakeoutAppDbContext context) : IStoreRepos
context.Stores.Remove(existing);
}
private static double CalculateDistanceMeters(double latitude1, double longitude1, double latitude2, double longitude2)
{
const double earthRadius = 6371000d;
var latRad1 = DegreesToRadians(latitude1);
var latRad2 = DegreesToRadians(latitude2);
var deltaLat = DegreesToRadians(latitude2 - latitude1);
var deltaLon = DegreesToRadians(longitude2 - longitude1);
var sinLat = Math.Sin(deltaLat / 2);
var sinLon = Math.Sin(deltaLon / 2);
var a = sinLat * sinLat + Math.Cos(latRad1) * Math.Cos(latRad2) * sinLon * sinLon;
var c = 2 * Math.Asin(Math.Min(1, Math.Sqrt(a)));
return earthRadius * c;
}
private static double DegreesToRadians(double degrees) => degrees * (Math.PI / 180d);
}