Files
chinese-family-tree-2/GENERATION_RECALCULATION_FIX.md
T
freedakgmail 5c8c70097c 0.8.0.0
2025-12-21 17:32:33 +08:00

56 lines
1.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 世代重新计算修复说明
## 问题描述
给始祖(第1代成员)添加父母后,始祖及其所有后代的世代没有自动+1。
## 根本原因
`app/api/trees/[treeId]/members/route.ts` 的 POST 处理器中,代数重新计算的触发条件有问题:
```typescript
// 原来的逻辑
if (minGeneration !== null && data.generation < minGeneration) {
needRecalculate = true
generationOffset = minGeneration - data.generation
}
```
当给第1代成员添加父母时:
- 新父母的 `generation` 设置为 1(我们之前修复的)
- 当前最小代数 `minGeneration` 也是 1
- 条件 `data.generation < minGeneration` 为 false1 不小于 1
- 因此不会触发代数重新计算
## 解决方案
添加额外的检查逻辑,专门处理"给第1代成员添加父母"的情况:
1. **检测是否给第1代成员添加父母**
- 检查新成员的 `childrenIds` 中是否包含第1代成员
- 如果是,设置 `isAddingParentToFounder = true`
2. **触发代数重新计算**
-`isAddingParentToFounder && minGeneration === 1`
- 设置 `needRecalculate = true``generationOffset = 1`
3. **执行重新计算**
- 将所有现有成员的代数+1(排除新创建的父母)
- 新父母保持代数为1,成为新的始祖
- 更新家族树的 `rootMemberId` 为新父母
## 修改的文件
- `app/api/trees/[treeId]/members/route.ts`
## 测试建议
1. 创建一个家族树,添加第1代成员(始祖)
2. 给始祖添加子女(第2代)
3. 给始祖添加父亲或母亲
4. 验证:
- 新父母的代数为1
- 原始祖的代数变为2
- 所有后代的代数都+1
- 家族树的 rootMemberId 更新为新父母
## 相关功能
- 前端已有代数重新计算警告对话框(GenerationWarningDialog
- 警告会在添加父母到第1代成员时显示
- 用户确认后,后端会自动执行代数重新计算