Просмотр исходного кода

commit 世系查询展示优化

林海 7 часов назад
Родитель
Сommit
ef215e26b5
1 измененных файлов с 244 добавлено и 28 удалено
  1. 244 28
      app.py

+ 244 - 28
app.py

@@ -1842,6 +1842,21 @@ def get_lineage(member_id):
                 if mode == 'blood':
                     # 血脉追溯:亲生父优先(出继亦沿亲生路径)
                     parent = normal_parent or bio_parent or adoptive_parent
+                    # 血脉模式下若存在养父,仍标注出继/入继关系
+                    if adoptive_parent is not None:
+                        _bio_src = bio_parent or normal_parent
+                        bio_name  = (_bio_src.get('simplified_name') or _bio_src.get('name')) if _bio_src else None
+                        adop_name = adoptive_parent.get('simplified_name') or adoptive_parent.get('name')
+                        if bio_name and adop_name:
+                            adopt_label = f"出继自{bio_name},入继至{adop_name}"
+                        elif adop_name:
+                            adopt_label = f"入继至{adop_name}"
+                        else:
+                            adopt_label = "出继"
+                        if depth == 0:
+                            center['adoption_label'] = adopt_label
+                        elif generations:
+                            generations[-1]['ancestor']['adoption_label'] = adopt_label
                 else:
                     # 香火传承(默认):入继养父优先
                     parent = adoptive_parent or normal_parent or bio_parent
@@ -1881,7 +1896,7 @@ def get_lineage(member_id):
                 else:
                     gp_order = "CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END"
                 cursor.execute(f"""
-                    SELECT gp.id
+                    SELECT gp.id, gp.name, gp.simplified_name
                     FROM family_relation_info r
                     JOIN family_member_info gp ON r.parent_mid = gp.id
                     WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
@@ -1926,19 +1941,43 @@ def get_lineage(member_id):
                         LIMIT 30
                     """, (grandparent['id'], parent['id']))
                     parent_siblings = cursor.fetchall()
-                    # 为入继兄弟补充"出继自xx,入继至yy"标注
+                    # 生成兄弟节点标注(入继/出继双向)
+                    # 兄弟是祖父的子女,所以"入继至/出继自"用祖父名,不是当前祖先名
+                    gp_display = grandparent.get('simplified_name') or grandparent.get('name') or ''
                     for sib in parent_siblings:
                         if sib.get('sub_relation_type') == 3:
+                            # 入继兄弟:出继自bio,入继至祖父(祖父是收养方)
                             cursor.execute("""
                                 SELECT p.simplified_name, p.name
                                 FROM family_relation_info r
                                 JOIN family_member_info p ON r.parent_mid = p.id
-                                WHERE r.child_mid = %s AND r.sub_relation_type = 2 LIMIT 1
+                                WHERE r.child_mid = %s AND r.sub_relation_type != 3
+                                ORDER BY r.sub_relation_type DESC LIMIT 1
                             """, (sib['id'],))
                             sbp = cursor.fetchone()
-                            sib['adoption_label'] = (
-                                f"出继自{sbp['simplified_name'] or sbp['name']}" if sbp else "出继"
-                            )
+                            bio_name = (sbp['simplified_name'] or sbp['name']) if sbp else None
+                            if bio_name and gp_display:
+                                sib['adoption_label'] = f"出继自{bio_name},入继至{gp_display}"
+                            elif bio_name:
+                                sib['adoption_label'] = f"出继自{bio_name}"
+                            else:
+                                sib['adoption_label'] = "出继"
+                        elif mode == 'blood' and sib.get('sub_relation_type') == 2:
+                            # 血脉模式出继兄弟:出继自祖父(祖父是出继方),入继至养父
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
+                            """, (sib['id'],))
+                            adop = cursor.fetchone()
+                            adop_name = (adop['simplified_name'] or adop['name']) if adop else None
+                            if gp_display and adop_name:
+                                sib['adoption_label'] = f"出继自{gp_display},入继至{adop_name}"
+                            elif adop_name:
+                                sib['adoption_label'] = f"入继至{adop_name}"
+                            else:
+                                sib['adoption_label'] = "出继"
 
                 # Mark sibling IDs as displayed
                 for sibling in parent_siblings:
@@ -2107,6 +2146,22 @@ def get_lineage(member_id):
                             sib['adoption_label'] = f"出继自{bio_name}"
                         else:
                             sib['adoption_label'] = "出继"
+                    elif mode == 'blood' and sib.get('sub_relation_type') == 2:
+                        # 血脉模式:出继兄弟 → 出继自当前父亲,入继至养父
+                        cursor.execute("""
+                            SELECT p.simplified_name, p.name
+                            FROM family_relation_info r
+                            JOIN family_member_info p ON r.parent_mid = p.id
+                            WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
+                        """, (sib['id'],))
+                        adop = cursor.fetchone()
+                        adop_name = (adop['simplified_name'] or adop['name']) if adop else None
+                        if parent_display and adop_name:
+                            sib['adoption_label'] = f"出继自{parent_display},入继至{adop_name}"
+                        elif adop_name:
+                            sib['adoption_label'] = f"入继至{adop_name}"
+                        else:
+                            sib['adoption_label'] = "出继"
             print(f"[Lineage Query] Step 4 - Get siblings ({len(siblings)}): {time.time() - step_start:.3f}s")
             
             total_time = time.time() - start_time
@@ -2215,6 +2270,21 @@ def get_ancestors_above(ancestor_id):
 
                 if mode == 'blood':
                     parent = normal_parent or bio_parent or adoptive_parent
+                    # 血脉模式下若存在养父,仍标注出继/入继关系
+                    if adoptive_parent is not None:
+                        _bio_src = bio_parent or normal_parent
+                        bio_name  = (_bio_src.get('simplified_name') or _bio_src.get('name')) if _bio_src else None
+                        adop_name = adoptive_parent.get('simplified_name') or adoptive_parent.get('name')
+                        if bio_name and adop_name:
+                            adopt_label = f"出继自{bio_name},入继至{adop_name}"
+                        elif adop_name:
+                            adopt_label = f"入继至{adop_name}"
+                        else:
+                            adopt_label = "出继"
+                        if depth == 0:
+                            anchor_adoption_label = adopt_label
+                        elif generations:
+                            generations[-1]['ancestor']['adoption_label'] = adopt_label
                 else:
                     parent = adoptive_parent or normal_parent or bio_parent
                     # 若走入继路径,在 current_id 对应的人物上标注"出继自xx,入继至yy"
@@ -2247,7 +2317,7 @@ def get_ancestors_above(ancestor_id):
                 else:
                     gp_order_ab = "CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END"
                 cursor.execute(f"""
-                    SELECT gp.id FROM family_relation_info r
+                    SELECT gp.id, gp.name, gp.simplified_name FROM family_relation_info r
                     JOIN family_member_info gp ON r.parent_mid = gp.id
                     WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
                     ORDER BY {gp_order_ab}, r.id
@@ -2288,8 +2358,41 @@ def get_ancestors_above(ancestor_id):
                         LIMIT 10
                     """, (grandparent['id'], parent['id']))
                     parent_siblings = cursor.fetchall()
+                    # 兄弟是祖父的子女,"入继至/出继自"用祖父名
+                    ab_gp_display = grandparent.get('simplified_name') or grandparent.get('name') or ''
                     for s in parent_siblings:
                         s['has_children'] = bool(s['has_children'])
+                        if s.get('sub_relation_type') == 3:
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type != 3
+                                ORDER BY r.sub_relation_type DESC LIMIT 1
+                            """, (s['id'],))
+                            sbp = cursor.fetchone()
+                            bio_name = (sbp['simplified_name'] or sbp['name']) if sbp else None
+                            if bio_name and ab_gp_display:
+                                s['adoption_label'] = f"出继自{bio_name},入继至{ab_gp_display}"
+                            elif bio_name:
+                                s['adoption_label'] = f"出继自{bio_name}"
+                            else:
+                                s['adoption_label'] = "出继"
+                        elif mode == 'blood' and s.get('sub_relation_type') == 2:
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
+                            """, (s['id'],))
+                            adop = cursor.fetchone()
+                            adop_name = (adop['simplified_name'] or adop['name']) if adop else None
+                            if ab_gp_display and adop_name:
+                                s['adoption_label'] = f"出继自{ab_gp_display},入继至{adop_name}"
+                            elif adop_name:
+                                s['adoption_label'] = f"入继至{adop_name}"
+                            else:
+                                s['adoption_label'] = "出继"
                 else:
                     parent['child_order'] = None
 
@@ -2396,11 +2499,11 @@ def get_descendants(parent_id):
 
             children = cursor.fetchall()
 
-            # 为入继子女生成 adoption_label
+            # 为入继/出继子女生成 adoption_label
             for child in children:
                 child['children'] = []
                 if child.get('sub_relation_type') == 3:
-                    # 查亲生父(sub_type=2 优先,否则取 sub_type=0)
+                    # 入继子女:出继自bio父,入继至当前节点
                     cursor.execute("""
                         SELECT p.simplified_name, p.name
                         FROM family_relation_info r
@@ -2417,6 +2520,22 @@ def get_descendants(parent_id):
                         child['adoption_label'] = f"出继自{bio_name}"
                     else:
                         child['adoption_label'] = "出继"
+                elif mode == 'blood' and child.get('sub_relation_type') == 2:
+                    # 血脉模式出继子女:出继自当前节点,入继至养父
+                    cursor.execute("""
+                        SELECT p.simplified_name, p.name
+                        FROM family_relation_info r
+                        JOIN family_member_info p ON r.parent_mid = p.id
+                        WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
+                    """, (child['id'],))
+                    adop_p = cursor.fetchone()
+                    adop_name = (adop_p['simplified_name'] or adop_p['name']) if adop_p else None
+                    if parent_display and adop_name:
+                        child['adoption_label'] = f"出继自{parent_display},入继至{adop_name}"
+                    elif adop_name:
+                        child['adoption_label'] = f"入继至{adop_name}"
+                    else:
+                        child['adoption_label'] = "出继"
 
             return jsonify({"success": True, "children": children})
     finally:
@@ -6237,6 +6356,21 @@ def api_get_lineage(member_id):
 
                 if mode == 'blood':
                     parent = normal_parent or bio_parent or adoptive_parent
+                    # 血脉模式下若存在养父,仍标注出继/入继关系
+                    if adoptive_parent is not None:
+                        _bio_src = bio_parent or normal_parent
+                        bio_name  = (_bio_src.get('simplified_name') or _bio_src.get('name')) if _bio_src else None
+                        adop_name = adoptive_parent.get('simplified_name') or adoptive_parent.get('name')
+                        if bio_name and adop_name:
+                            adopt_label = f"出继自{bio_name},入继至{adop_name}"
+                        elif adop_name:
+                            adopt_label = f"入继至{adop_name}"
+                        else:
+                            adopt_label = "出继"
+                        if depth == 0:
+                            center['adoption_label'] = adopt_label
+                        elif generations:
+                            generations[-1]['ancestor']['adoption_label'] = adopt_label
                 else:
                     parent = adoptive_parent or normal_parent or bio_parent
                     if parent is adoptive_parent and adoptive_parent is not None:
@@ -6269,7 +6403,7 @@ def api_get_lineage(member_id):
                 else:
                     gp_order_wx = "CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END"
                 cursor.execute(f"""
-                    SELECT gp.id FROM family_relation_info r
+                    SELECT gp.id, gp.name, gp.simplified_name FROM family_relation_info r
                     JOIN family_member_info gp ON r.parent_mid = gp.id
                     WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
                     ORDER BY {gp_order_wx}, r.id
@@ -6312,8 +6446,41 @@ def api_get_lineage(member_id):
                         LIMIT 10
                     """, (grandparent['id'], parent['id']))
                     parent_siblings = cursor.fetchall()
+                    # 兄弟是祖父的子女,"入继至/出继自"用祖父名
+                    wx_gp_display = grandparent.get('simplified_name') or grandparent.get('name') or ''
                     for s in parent_siblings:
                         s['has_children'] = bool(s['has_children'])
+                        if s.get('sub_relation_type') == 3:
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type != 3
+                                ORDER BY r.sub_relation_type DESC LIMIT 1
+                            """, (s['id'],))
+                            sbp = cursor.fetchone()
+                            bio_name = (sbp['simplified_name'] or sbp['name']) if sbp else None
+                            if bio_name and wx_gp_display:
+                                s['adoption_label'] = f"出继自{bio_name},入继至{wx_gp_display}"
+                            elif bio_name:
+                                s['adoption_label'] = f"出继自{bio_name}"
+                            else:
+                                s['adoption_label'] = "出继"
+                        elif mode == 'blood' and s.get('sub_relation_type') == 2:
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
+                            """, (s['id'],))
+                            adop = cursor.fetchone()
+                            adop_name = (adop['simplified_name'] or adop['name']) if adop else None
+                            if wx_gp_display and adop_name:
+                                s['adoption_label'] = f"出继自{wx_gp_display},入继至{adop_name}"
+                            elif adop_name:
+                                s['adoption_label'] = f"入继至{adop_name}"
+                            else:
+                                s['adoption_label'] = "出继"
                 else:
                     parent['child_order'] = None
 
@@ -6495,23 +6662,39 @@ def api_get_ancestors_above(ancestor_id):
                         bio_parent = p
                     else:
                         normal_parent = p
-                parent = adoptive_parent or normal_parent or bio_parent
-
-                # 若走入继路径,在 current_id 对应的人物上标注"出继自xx,入继至yy"
-                if parent is adoptive_parent and adoptive_parent is not None:
-                    _bio_src = bio_parent or normal_parent
-                    bio_name  = (_bio_src.get('simplified_name') or _bio_src.get('name')) if _bio_src else None
-                    adop_name = adoptive_parent.get('simplified_name') or adoptive_parent.get('name')
-                    if bio_name and adop_name:
-                        adopt_label = f"出继自{bio_name},入继至{adop_name}"
-                    elif bio_name:
-                        adopt_label = f"出继自{bio_name}"
-                    else:
-                        adopt_label = "出继"
-                    if depth == 0:
-                        anchor_adoption_label_wx = adopt_label
-                    elif generations:
-                        generations[-1]['ancestor']['adoption_label'] = adopt_label
+                if mode == 'blood':
+                    parent = normal_parent or bio_parent or adoptive_parent
+                    if adoptive_parent is not None:
+                        _bio_src = bio_parent or normal_parent
+                        bio_name  = (_bio_src.get('simplified_name') or _bio_src.get('name')) if _bio_src else None
+                        adop_name = adoptive_parent.get('simplified_name') or adoptive_parent.get('name')
+                        if bio_name and adop_name:
+                            adopt_label = f"出继自{bio_name},入继至{adop_name}"
+                        elif adop_name:
+                            adopt_label = f"入继至{adop_name}"
+                        else:
+                            adopt_label = "出继"
+                        if depth == 0:
+                            anchor_adoption_label_wx = adopt_label
+                        elif generations:
+                            generations[-1]['ancestor']['adoption_label'] = adopt_label
+                else:
+                    parent = adoptive_parent or normal_parent or bio_parent
+                    # 若走入继路径,在 current_id 对应的人物上标注"出继自xx,入继至yy"
+                    if parent is adoptive_parent and adoptive_parent is not None:
+                        _bio_src = bio_parent or normal_parent
+                        bio_name  = (_bio_src.get('simplified_name') or _bio_src.get('name')) if _bio_src else None
+                        adop_name = adoptive_parent.get('simplified_name') or adoptive_parent.get('name')
+                        if bio_name and adop_name:
+                            adopt_label = f"出继自{bio_name},入继至{adop_name}"
+                        elif bio_name:
+                            adopt_label = f"出继自{bio_name}"
+                        else:
+                            adopt_label = "出继"
+                        if depth == 0:
+                            anchor_adoption_label_wx = adopt_label
+                        elif generations:
+                            generations[-1]['ancestor']['adoption_label'] = adopt_label
 
                 # 祖先卡片不携带子辈关系类型
                 parent['sub_relation_type'] = None
@@ -6526,7 +6709,7 @@ def api_get_ancestors_above(ancestor_id):
                 else:
                     gp_order_wxab = "CASE WHEN COALESCE(r.sub_relation_type, 0) = 3 THEN 1 ELSE 0 END"
                 cursor.execute(f"""
-                    SELECT gp.id FROM family_relation_info r
+                    SELECT gp.id, gp.name, gp.simplified_name FROM family_relation_info r
                     JOIN family_member_info gp ON r.parent_mid = gp.id
                     WHERE r.child_mid = %s AND r.relation_type IN (1, 2)
                     ORDER BY {gp_order_wxab}, r.id
@@ -6567,8 +6750,41 @@ def api_get_ancestors_above(ancestor_id):
                         LIMIT 10
                     """, (grandparent['id'], parent['id']))
                     parent_siblings = cursor.fetchall()
+                    # 兄弟是祖父的子女,"入继至/出继自"用祖父名
+                    wxab_gp_display = grandparent.get('simplified_name') or grandparent.get('name') or ''
                     for s in parent_siblings:
                         s['has_children'] = bool(s['has_children'])
+                        if s.get('sub_relation_type') == 3:
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type != 3
+                                ORDER BY r.sub_relation_type DESC LIMIT 1
+                            """, (s['id'],))
+                            sbp = cursor.fetchone()
+                            bio_name = (sbp['simplified_name'] or sbp['name']) if sbp else None
+                            if bio_name and wxab_gp_display:
+                                s['adoption_label'] = f"出继自{bio_name},入继至{wxab_gp_display}"
+                            elif bio_name:
+                                s['adoption_label'] = f"出继自{bio_name}"
+                            else:
+                                s['adoption_label'] = "出继"
+                        elif mode == 'blood' and s.get('sub_relation_type') == 2:
+                            cursor.execute("""
+                                SELECT p.simplified_name, p.name
+                                FROM family_relation_info r
+                                JOIN family_member_info p ON r.parent_mid = p.id
+                                WHERE r.child_mid = %s AND r.sub_relation_type = 3 LIMIT 1
+                            """, (s['id'],))
+                            adop = cursor.fetchone()
+                            adop_name = (adop['simplified_name'] or adop['name']) if adop else None
+                            if wxab_gp_display and adop_name:
+                                s['adoption_label'] = f"出继自{wxab_gp_display},入继至{adop_name}"
+                            elif adop_name:
+                                s['adoption_label'] = f"入继至{adop_name}"
+                            else:
+                                s['adoption_label'] = "出继"
                 else:
                     parent['child_order'] = None