using Fake.DependencyInjection; using Oms.Domain.OrdersAggregate.Entities; using Oms.Domain.OrdersAggregate.Repositories; using Oms.SqlSugarCore.Repositories; namespace Oms.SqlSugarCore.Orders; public class OrderRepository : OmsSqlSugarRepository, IOrderRepository, IScopedDependency { public Task GetByServiceNoAsync(string serviceNo, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.ServiceNo == serviceNo, cancellationToken); } public Task HasServiceNoAsync(string serviceNo, CancellationToken cancellationToken = default) { return AnyAsync(x => x.ServiceNo == serviceNo, cancellationToken); } public Task GetByTrackNoAsync(string trackNo, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.TrackNo == trackNo, cancellationToken); } public Task GetByCustomerAndClientNoAsync(int customerId, string clientNo, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.CustomerId == customerId && x.ClientNo == clientNo, cancellationToken); } public Task GetByRequestIdAsync(string requestId, CancellationToken cancellationToken = default) { return FirstOrDefaultAsync(x => x.RequestId == requestId, cancellationToken); } public async Task> GetSendersByOrderIdsAsync(IEnumerable orderIds, CancellationToken cancellationToken = default) { var ids = orderIds.Distinct().ToArray(); if (ids.Length == 0) { return []; } var db = await GetDbContextAsync(cancellationToken); return await db.Queryable() .Where(x => ids.Contains(x.OrderId)) .ToListAsync(cancellationToken); } public async Task> GetRecipientsByOrderIdsAsync(IEnumerable orderIds, CancellationToken cancellationToken = default) { var ids = orderIds.Distinct().ToArray(); if (ids.Length == 0) { return []; } var db = await GetDbContextAsync(cancellationToken); return await db.Queryable() .Where(x => ids.Contains(x.OrderId)) .ToListAsync(cancellationToken); } public async Task> GetDeclarationsByOrderIdsAsync(IEnumerable orderIds, CancellationToken cancellationToken = default) { var ids = orderIds.Distinct().ToArray(); if (ids.Length == 0) { return []; } var db = await GetDbContextAsync(cancellationToken); return await db.Queryable() .Where(x => ids.Contains(x.OrderId)) .ToListAsync(cancellationToken); } public async Task> GetWeightsByOrderIdsAsync(IEnumerable orderIds, CancellationToken cancellationToken = default) { var ids = orderIds.Distinct().ToArray(); if (ids.Length == 0) { return []; } var db = await GetDbContextAsync(cancellationToken); return await db.Queryable() .Where(x => ids.Contains(x.OrderId)) .ToListAsync(cancellationToken); } public async Task InsertSenderAsync(OrderSender sender, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); return await db.Insertable(sender).ExecuteReturnEntityAsync(); } public async Task InsertRecipientAsync(OrderRecipient recipient, CancellationToken cancellationToken = default) { var db = await GetDbContextAsync(cancellationToken); return await db.Insertable(recipient).ExecuteReturnEntityAsync(); } public async Task InsertDeclarationsAsync(IEnumerable declarations, CancellationToken cancellationToken = default) { var items = declarations.ToArray(); if (items.Length == 0) { return; } var db = await GetDbContextAsync(cancellationToken); await db.Insertable(items).ExecuteCommandAsync(cancellationToken); } public async Task InsertWeightsAsync(IEnumerable weights, CancellationToken cancellationToken = default) { var items = weights.ToArray(); if (items.Length == 0) { return; } var db = await GetDbContextAsync(cancellationToken); await db.Insertable(items).ExecuteCommandAsync(cancellationToken); } }