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 {