Go to file
hailin be7ec87f05 feat(wallet): 增强认种流程可靠性 - 添加事务保护和乐观锁
## 问题背景

认种流程(冻结→确认扣款→解冻)存在以下可靠性问题:
1. 余额检查与冻结操作非原子性,存在并发竞态条件
2. 钱包更新与流水记录分开执行,可能导致数据不一致
3. 缺少乐观锁机制,并发修改时可能出现余额错误
4. Kafka consumer 错误被吞掉,消费失败无法重试

## 修复内容

### wallet-application.service.ts

1. **freezeForPlanting (冻结资金)**
   - 添加 `prisma.$transaction` 事务保护
   - 添加乐观锁 (version 字段检查)
   - 添加重试机制 (最多 3 次,指数退避)
   - 幂等性检查移入事务内,避免竞态

2. **confirmPlantingDeduction (确认扣款)**
   - 添加事务保护,确保扣款与流水原子性
   - 添加乐观锁防止并发修改
   - 添加重试机制

3. **unfreezeForPlanting (解冻资金)**
   - 添加事务保护,确保解冻与流水原子性
   - 添加乐观锁防止并发修改
   - 添加重试机制

### planting-event-consumer.service.ts

- 添加 `throw error` 重新抛出错误
- 确保消费失败时 Kafka 能感知并触发重试

## 乐观锁实现

```typescript
const updateResult = await tx.walletAccount.updateMany({
  where: {
    id: walletRecord.id,
    version: currentVersion,  // 版本检查
  },
  data: {
    usdtAvailable: newAvailable,
    version: currentVersion + 1,  // 版本递增
  },
});

if (updateResult.count === 0) {
  throw new OptimisticLockError('版本冲突');
}
```

## 测试验证

- wallet-service 构建成功
- 服务重启正常,所有 handler 注册成功

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-15 19:56:49 -08:00
.claude fix(sentry): 修复 Flutter 代码分析错误 2025-12-15 11:04:28 -08:00
backend feat(wallet): 增强认种流程可靠性 - 添加事务保护和乐观锁 2025-12-15 19:56:49 -08:00
docs docs: add planting architecture optimization documentation 2025-12-09 02:36:51 -08:00
frontend fix(android): 降级 Kotlin 版本至 1.9.22 修复构建错误 2025-12-15 19:53:01 -08:00
kubernetes . 2025-11-25 10:29:24 +08:00
scripts . 2025-11-25 10:29:24 +08:00
tests . 2025-11-25 10:29:24 +08:00
.gitignore chore: add .claude to gitignore 2025-12-09 18:37:23 -08:00
README.md first commit 2025-11-23 21:21:44 -08:00
docker-compose.yml first commit 2025-11-23 21:21:44 -08:00

README.md