InsertItemsAsync 批量插入后按 MessageId 回填自增 Id;消费者支持 JobItemId=0 时按 JobId+MessageId 解析子项。 Co-authored-by: Cursor <cursoragent@cursor.com>
115 lines
4.0 KiB
C#
115 lines
4.0 KiB
C#
using Fake.DependencyInjection;
|
||
using Oms.Domain.OrdersAggregate.Entities;
|
||
using Oms.Domain.OrdersAggregate.Repositories;
|
||
using Oms.SqlSugarCore.Repositories;
|
||
|
||
namespace Oms.SqlSugarCore.Orders;
|
||
|
||
public class OrderBatchJobRepository : OmsSqlSugarRepository<OrderBatchJob>, IOrderBatchJobRepository, IScopedDependency
|
||
{
|
||
public Task<OrderBatchJob?> GetByIdAsync(int id, CancellationToken cancellationToken = default)
|
||
{
|
||
return FirstOrDefaultAsync(x => x.Id == id, cancellationToken);
|
||
}
|
||
|
||
public Task<OrderBatchJob?> GetByRequestIdAsync(string requestId, CancellationToken cancellationToken = default)
|
||
{
|
||
return FirstOrDefaultAsync(x => x.RequestId == requestId, cancellationToken);
|
||
}
|
||
|
||
public async Task<List<OrderBatchJobItem>> GetItemsByJobIdAsync(int jobId, CancellationToken cancellationToken = default)
|
||
{
|
||
var db = await GetDbContextAsync(cancellationToken);
|
||
return await db.Queryable<OrderBatchJobItem>()
|
||
.Where(x => x.JobId == jobId)
|
||
.OrderBy(x => x.Id)
|
||
.ToListAsync(cancellationToken);
|
||
}
|
||
|
||
public async Task<OrderBatchJobItem?> GetItemByIdAsync(int itemId, CancellationToken cancellationToken = default)
|
||
{
|
||
var db = await GetDbContextAsync(cancellationToken);
|
||
var list = await db.Queryable<OrderBatchJobItem>()
|
||
.Where(it => it.Id == itemId)
|
||
.Take(1)
|
||
.ToListAsync(cancellationToken);
|
||
return list.FirstOrDefault();
|
||
}
|
||
|
||
public async Task<OrderBatchJobItem?> GetItemByJobAndMessageIdAsync(
|
||
int jobId,
|
||
string messageId,
|
||
CancellationToken cancellationToken = default)
|
||
{
|
||
if (jobId <= 0 || string.IsNullOrWhiteSpace(messageId))
|
||
{
|
||
return null;
|
||
}
|
||
|
||
var db = await GetDbContextAsync(cancellationToken);
|
||
var list = await db.Queryable<OrderBatchJobItem>()
|
||
.Where(it => it.JobId == jobId && it.MessageId == messageId)
|
||
.Take(1)
|
||
.ToListAsync(cancellationToken);
|
||
return list.FirstOrDefault();
|
||
}
|
||
|
||
public async Task InsertItemsAsync(IEnumerable<OrderBatchJobItem> items, CancellationToken cancellationToken = default)
|
||
{
|
||
var list = items.ToArray();
|
||
if (list.Length == 0)
|
||
{
|
||
return;
|
||
}
|
||
|
||
var db = await GetDbContextAsync(cancellationToken);
|
||
await db.Insertable(list).ExecuteCommandAsync(cancellationToken);
|
||
|
||
// 批量 Insert 不会回填自增 Id,需重新加载以便 MQ 消息携带正确的 JobItemId。
|
||
var jobId = list[0].JobId;
|
||
var persisted = await db.Queryable<OrderBatchJobItem>()
|
||
.Where(x => x.JobId == jobId)
|
||
.OrderBy(x => x.Id)
|
||
.ToListAsync(cancellationToken);
|
||
|
||
var byMessageId = persisted
|
||
.Where(x => !string.IsNullOrWhiteSpace(x.MessageId))
|
||
.ToDictionary(x => x.MessageId!, x => x.Id);
|
||
|
||
foreach (var item in list)
|
||
{
|
||
if (string.IsNullOrWhiteSpace(item.MessageId))
|
||
{
|
||
continue;
|
||
}
|
||
|
||
if (byMessageId.TryGetValue(item.MessageId, out var persistedId))
|
||
{
|
||
item.Id = persistedId;
|
||
}
|
||
}
|
||
|
||
var unmatched = list.Where(x => x.Id == 0).ToList();
|
||
if (unmatched.Count > 0)
|
||
{
|
||
var assignedIds = list.Where(x => x.Id > 0).Select(x => x.Id).ToHashSet();
|
||
var spare = persisted.Where(x => !assignedIds.Contains(x.Id)).OrderBy(x => x.Id).ToList();
|
||
for (var i = 0; i < unmatched.Count && i < spare.Count; i++)
|
||
{
|
||
unmatched[i].Id = spare[i].Id;
|
||
}
|
||
}
|
||
}
|
||
|
||
public async Task UpdateItemAsync(OrderBatchJobItem item, CancellationToken cancellationToken = default)
|
||
{
|
||
var db = await GetDbContextAsync(cancellationToken);
|
||
await db.Updateable(item).ExecuteCommandAsync(cancellationToken);
|
||
}
|
||
|
||
public Task UpdateJobAsync(OrderBatchJob job, CancellationToken cancellationToken = default)
|
||
{
|
||
return UpdateAsync(job, cancellationToken);
|
||
}
|
||
}
|