fix(planting): 使用upsert解决合同签约任务创建的并发冲突
Kafka事件重复消费时,多个消费者同时创建签约任务会导致唯一约束冲突 改用upsert确保幂等性,如果orderNo已存在则返回现有记录 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
0c6e73de85
commit
68e269b602
|
|
@ -41,9 +41,11 @@ export class ContractSigningTaskRepositoryImpl implements IContractSigningTaskRe
|
|||
});
|
||||
return this.mapToDomain(updated);
|
||||
} else {
|
||||
// 创建
|
||||
const created = await this.prisma.contractSigningTask.create({
|
||||
data: {
|
||||
// 创建 - 使用 upsert 处理并发幂等性
|
||||
// 如果 orderNo 已存在,则只返回现有记录(不更新)
|
||||
const result = await this.prisma.contractSigningTask.upsert({
|
||||
where: { orderNo: task.orderNo },
|
||||
create: {
|
||||
orderNo: task.orderNo,
|
||||
userId: task.userId,
|
||||
accountSequence: task.accountSequence,
|
||||
|
|
@ -62,8 +64,10 @@ export class ContractSigningTaskRepositoryImpl implements IContractSigningTaskRe
|
|||
status: task.status,
|
||||
expiresAt: task.expiresAt,
|
||||
},
|
||||
// 如果已存在,不更新任何字段,只返回现有记录
|
||||
update: {},
|
||||
});
|
||||
return this.mapToDomain(created);
|
||||
return this.mapToDomain(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue