From 72dc6c000638f47e46470fb5b0452b3cadfdd712 Mon Sep 17 00:00:00 2001 From: laoyang Date: Fri, 2 Oct 2026 22:08:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=B6=E8=B0=B1=E5=85=B3=E7=B3=BB=E6=8E=A8?= =?UTF-8?q?=E5=AF=BC=EF=BC=9A=E6=8A=8A=E3=80=8C=E5=8F=AA=E8=AE=B0=E4=BA=86?= =?UTF-8?q?=E4=B8=80=E5=A4=B4=E3=80=8D=E7=9A=84=E8=BE=B9=E8=A1=A5=E6=88=90?= =?UTF-8?q?=E5=8F=8C=E5=90=91=EF=BC=88=E4=BF=AE=E7=94=A8=E6=88=B7=E6=8A=A5?= =?UTF-8?q?=E7=9A=84=204=20=E5=A4=84=E5=B0=91=E4=BA=BA=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 用户指出 4 个具体错误,逐条核对线上真实数据后确认**全部是算法缺陷**: ① 杨永华只有一位家长(母亲那边整条线缺失) ② 洪博为的孩子是空的 —— 她是杨永全的配偶,杨永全的孩子就是她的 ③ 洪勇、武竹芳的配偶都是空的 —— 两人明明是同一个孩子的父母 ④ 杨景焕 / 杜浩洋 / 杜若桐 都只有一位家长 根因:本系统的设计是「只存相邻两代之间的边,其余全部推导」(见 Kinship 头注释), 但家人手工录入时只会填**最直接的那一条**: · 给几个孩子都填了「父亲 = 某人」,从没点过「母亲」→ 孩子只有一位家长; · 填了孩子父母两位,却忘了把这两位再标一次「夫妻」→ 两位家长各自孤零零。 而 graph() 只做了「parent 边反推 children」的**单向**推导,缺了两条互补推导。 修复(都在 graph() 里,内存视图,不动 relations 表): 推导① 同一个孩子的两位家长 ⇒ 认定他们是夫妻 推导② 孩子只有一位家长、且该家长**恰好只有一位配偶** ⇒ 把配偶补成另一位家长 (有多个配偶时不猜;迭代到稳定,最多 4 轮) 配套修复(实测发现的回归,必须一起上): 推导① 会把「已解除的配偶」凭共同孩子**配回去** —— 用户点"解除配偶"刷新又出现。 解法:解除配偶时把这一对记进 settings 的 `kin_undone_pairs`(不动表结构), 推导①跳过这些对;重新 addSpouse 时抹掉标记。 验证:初始有配偶 → 解除后为空(不再被推回)→ 重新加回恢复。 验证: · _fc_verify_derive.py(新):用**线上真实那 10 条边**做夹具,逐条断言用户报的 4 处 + 11 条「不许过度推导」守卫(祖父母不被推成夫妻、孩子家长不多不少、无自环…) 全绿;反向验证 2 处注入(关推导①/关推导②)各变红 6/8 条且退出码非 0。 · e2e 549/0、家谱可视化 24/0、家谱版面 10/0、桌面几何 368/0、移动端 516/0、 交互 97/0、升级专测 123/0。 · **线上真机验证**:修复后重新取线上 data-datum,4 处全部正确 —— 洪博为子女=杨景焕、洪勇⇄武竹芳、杨景焕双亲=杨永全+洪博为、 杜浩洋/杜若桐双亲=杨永华+杜元青、杨永华双亲=杨培振+刘巧莲。 · 部署脚本加 2 条断言锁住推导逻辑。 --- app/Kinship.php | 76 ++++++++++++++++++++++++++++++++++++++++++++++- app/Relations.php | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+), 1 deletion(-) diff --git a/app/Kinship.php b/app/Kinship.php index 7b0757f..67040d1 100644 --- a/app/Kinship.php +++ b/app/Kinship.php @@ -45,8 +45,82 @@ final class Kinship $spouses[$t][] = $f; } } + + // ==================== 推导:把「只记了一头」的边补成双向 ==================== + // + // ⚠️ 这一段是家谱"人少一半"的根因修复(用户 2026-10-02 报的 4 处全是它)。 + // + // 本系统的设计是「只存相邻两代之间的边,其余**全部推导**」(见文件头)。 + // 但家人手工录入时,只会填**最直接的那一条**: + // · 给几个孩子都填了「父亲 = 杨永全」,从没点过「母亲」 + // → 孩子只有一位家长;母亲那边看不出自己有孩子(用户问题②④) + // · 填了孩子的父母两位,却忘了把这两位再标一次「夫妻」 + // → 夫妻边缺失,两位家长各自孤零零(用户问题③) + // 这些**不是数据错,是数据本来就只记一头**,必须在这里补全; + // 否则下游(家谱/称呼/名册)会大面积少人。 + // + // ⚠️ 只在**信息唯一**时推导,绝不猜: + // · 补夫妻:仅当两人**同时是同一个孩子的父母** ⇒ 认定夫妻; + // · 补家长:仅当那位家长**恰好只有一个配偶**时,才把该配偶补成另一位家长; + // 有多个配偶(再婚/数据混乱)时不猜,原样保留。 + $addSpouse = static function (int $a, int $b) use (&$spouses): bool { + if ($a === $b || $a <= 0 || $b <= 0) { return false; } + $spouses[$a] ??= []; + $spouses[$b] ??= []; + $ch = false; + if (!in_array($b, $spouses[$a], true)) { $spouses[$a][] = $b; $ch = true; } + if (!in_array($a, $spouses[$b], true)) { $spouses[$b][] = $a; $ch = true; } + return $ch; + }; + $addParent = static function (int $child, int $parent, string $role) use (&$parents, &$children): bool { + if ($child === $parent || $child <= 0 || $parent <= 0) { return false; } + if (isset($parents[$child][$parent])) { return false; } + $parents[$child][$parent] = $role; + $children[$parent][] = $child; + return true; + }; + + // ---- 推导 ①:同一个孩子的两位家长 ⇒ 他们是夫妻 ---- + // ⚠️ 跳过「被显式解除过」的对:用户点了「解除配偶」,推导不能凭共同孩子把人配回来 + // (否则表现为"解除了却还在")。名单存在 settings 的 kin_undone_pairs。 + $undone = array_fill_keys(Relations::undonePairs($familyId), true); + $pairKey = static function (int $x, int $y): string { + return $x < $y ? "$x-$y" : "$y-$x"; + }; + foreach ($parents as $ps) { + if (count($ps) < 2) { continue; } + $ids = array_map('intval', array_keys($ps)); + $n = count($ids); + for ($i = 0; $i < $n; $i++) { + for ($j = $i + 1; $j < $n; $j++) { + if (isset($undone[$pairKey($ids[$i], $ids[$j])])) { continue; } + $addSpouse($ids[$i], $ids[$j]); + } + } + } + + // ---- 推导 ②:孩子只有一位家长、而该家长只有一个配偶 ⇒ 配偶即另一位家长 ---- + // 迭代到稳定:推导 ① 新加的夫妻边会让更多孩子可补(一眼看不出,跑两轮最稳)。 + for ($round = 0; $round < 4; $round++) { + $added = 0; + foreach ($parents as $child => $ps) { + if (count($ps) >= 2) { continue; } // 已有两位家长,不再补 + $known = (int)array_key_first($ps); + $role = (string)$ps[$known]; + $mates = array_values(array_unique($spouses[$known] ?? [])); + if (count($mates) !== 1) { continue; } // 有多个配偶 → 无法判定,不乱补 + $other = (int)$mates[0]; + $newRole = $role === 'father' ? 'mother' : 'father'; + if ($addParent((int)$child, $other, $newRole)) { $added++; } + } + if ($added === 0) { break; } + } + foreach ($children as $k => $v) { - $children[$k] = array_values(array_unique($v)); + $children[$k] = array_values(array_unique(array_map('intval', $v))); + } + foreach ($spouses as $k => $v) { + $spouses[$k] = array_values(array_unique(array_map('intval', $v))); } return ['parents' => $parents, 'children' => $children, 'spouses' => $spouses]; diff --git a/app/Relations.php b/app/Relations.php index 58934ba..5249b6c 100644 --- a/app/Relations.php +++ b/app/Relations.php @@ -64,6 +64,9 @@ final class Relations 'INSERT INTO relations (family_id, from_id, to_id, kind) VALUES (?,?,?,"spouse")' ); $st->execute([$familyId, $a, $b]); + // 重新加上了配偶 ⇒ 之前"解除过"的标记要撤掉, + // 否则互补推导会一直跳过这一对(连带影响他们的孩子只挂一位家长)。 + self::clearPairUndone($familyId, $a, $b); PersonRoster::syncKinForFamily($familyId); return ['ok' => true, 'error' => '']; } @@ -80,12 +83,70 @@ final class Relations public static function remove(int $familyId, int $id): int { $db = Database::get(); + // ⚠️ 删之前先读出来:如果是**配偶**边,要把它记进「显式解除」名单, + // 否则 Kinship::graph() 的互补推导会凭"共同孩子"把两人又配回夫妻, + // 用户看到的现象是「解除了却还在」。 + $row = $db->prepare('SELECT from_id, to_id, kind FROM relations WHERE id = ? AND family_id = ?'); + $row->execute([$id, $familyId]); + $r = $row->fetch(\PDO::FETCH_ASSOC); + if ($r && $r['kind'] === 'spouse') { + self::markPairUndone($familyId, (int)$r['from_id'], (int)$r['to_id']); + } + $db->prepare('DELETE FROM relations WHERE id = ? AND family_id = ?') ->execute([$id, $familyId]); $st = PersonRoster::syncKinForFamily($familyId); return (int)$st['dropped']; } + /** + * 记下「这一对已经被人为解除过夫妻关系」。 + * + * 为什么要记:家谱的关系是**推导**出来的(`Kinship::graph()` 会把 + * 「同一个孩子的两位家长」认成夫妻、把「唯一配偶」补成另一位家长)。 + * 于是「解除配偶」如果只删库里的边,推导会立刻用共同孩子把两人配回来 —— + * 用户点了解除、刷新还在,等于功能失效。 + * 记进 settings 的 `kin_undone_pairs`(不动表结构),推导时跳过这些对。 + */ + public static function markPairUndone(int $familyId, int $a, int $b): void + { + if ($a <= 0 || $b <= 0 || $a === $b) { return; } + $key = $a < $b ? "$a-$b" : "$b-$a"; + $list = self::undonePairs($familyId); + if (!in_array($key, $list, true)) { + $list[] = $key; + Setting::set($familyId, 'kin_undone_pairs', implode(',', $list)); + } + } + + /** 已被显式解除过的夫妻对('小id-大id' 形式),推导时跳过 */ + public static function undonePairs(int $familyId): array + { + $v = (string)Setting::get($familyId, 'kin_undone_pairs', ''); + if ($v === '') { return []; } + return array_values(array_filter(array_map('trim', explode(',', $v)), static fn($x) => $x !== '')); + } + + /** 这对是否被显式解除过(顺序无关) */ + public static function isPairUndone(int $familyId, int $a, int $b): bool + { + if ($a <= 0 || $b <= 0 || $a === $b) { return false; } + $key = $a < $b ? "$a-$b" : "$b-$a"; + return in_array($key, self::undonePairs($familyId), true); + } + + /** + * 重新建立关系时,把它从「显式解除」名单里抹掉 + * (不然用户重新加了配偶,推导还是把它当解除过 → 又少一处关系)。 + */ + public static function clearPairUndone(int $familyId, int $a, int $b): void + { + if ($a <= 0 || $b <= 0 || $a === $b) { return; } + $key = $a < $b ? "$a-$b" : "$b-$a"; + $list = array_values(array_filter(self::undonePairs($familyId), static fn($x) => $x !== $key)); + Setting::set($familyId, 'kin_undone_pairs', implode(',', $list)); + } + /** 解除两人之间的某种关系(与 remove() 同样的名册同步语义;当前无其他调用方) */ public static function detach(int $familyId, int $personId, string $kind, string $role = ''): void {