-- ========================================
-- 验证员工家庭关系表 JOIN 字符集问题修复
-- ========================================
-- 目的: 验证 ccdi_staff_fmy_relation 与 ccdi_base_staff 表的 JOIN 操作正常
-- 创建时间: 2026-02-09
-- ========================================

USE
`ccdi`;

-- 测试 1: 验证字符集统一性
-- ========================================
SELECT '=== 字符集验证 ===' AS test_section;

SELECT TABLE_NAME AS '表名', COLUMN_NAME AS '字段名', CHARACTER_SET_NAME AS '字符集', COLLATION_NAME AS '排序规则', CASE
    WHEN COLLATION_NAME = 'utf8mb4_general_ci' THEN '✅ 正确'
    ELSE '❌ 错误'
    END AS '状态'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'ccdi'
  AND TABLE_NAME IN ('ccdi_staff_fmy_relation', 'ccdi_base_staff')
  AND COLUMN_NAME IN ('person_id', 'id_card')
ORDER BY TABLE_NAME, COLUMN_NAME;

-- 测试 2: 验证 JOIN 操作（模拟实际查询）
-- ========================================
SELECT '=== JOIN 操作验证 ===' AS test_section;

SELECT r.id,
       r.person_id,
       s.name    AS person_name,
       r.relation_type,
       r.relation_name,
       r.gender,
       s.id_card AS staff_id_card,
       CASE
           WHEN r.person_id = s.id_card THEN '✅ 匹配成功'
           ELSE '❌ 匹配失败'
           END   AS join_status
FROM ccdi_staff_fmy_relation r
         LEFT JOIN ccdi_base_staff s ON r.person_id = s.id_card
WHERE r.is_emp_family = 1 LIMIT 5;

-- 测试 3: 统计数据
-- ========================================
SELECT '=== 数据统计 ===' AS test_section;

SELECT COUNT(*)                 AS total_records,
       COUNT(s.name)            AS matched_records,
       COUNT(*) - COUNT(s.name) AS unmatched_records,
       CONCAT(
               ROUND(COUNT(s.name) / COUNT(*) * 100, 2),
               '%'
       )                        AS match_rate
FROM ccdi_staff_fmy_relation r
         LEFT JOIN ccdi_base_staff s ON r.person_id = s.id_card
WHERE r.is_emp_family = 1;

-- 测试 4: 查找未匹配的记录
-- ========================================
SELECT '=== 未匹配记录 ===' AS test_section;

SELECT r.id,
       r.person_id,
       r.relation_name,
       '未找到对应员工信息' AS warning
FROM ccdi_staff_fmy_relation r
         LEFT JOIN ccdi_base_staff s ON r.person_id = s.id_card
WHERE r.is_emp_family = 1
  AND s.id_card IS NULL LIMIT 5;

-- 测试 5: 字符集转换测试
-- ========================================
SELECT '=== 字符集转换测试 ===' AS test_section;

SELECT r.person_id,
       s.id_card,
       CHAR_LENGTH(r.person_id) AS len_person_id,
       CHAR_LENGTH(s.id_card)   AS len_id_card,
       HEX(r.person_id)         AS hex_person_id,
       HEX(s.id_card)           AS hex_id_card,
       CASE
           WHEN r.person_id = s.id_card THEN '✅ 完全相等'
           ELSE '❌ 不相等'
           END                  AS compare_result
FROM ccdi_staff_fmy_relation r
         INNER JOIN ccdi_base_staff s ON r.person_id = s.id_card
WHERE r.is_emp_family = 1 LIMIT 3;

-- ========================================
-- 验证结果说明:
-- ========================================
-- 1. 字符集验证: 所有字段应显示 ✅ 正确
-- 2. JOIN 操作: 应能正常执行，无字符集冲突错误
-- 3. 数据统计: 显示匹配率
-- 4. 未匹配记录: 显示哪些员工关系记录未关联到员工
-- 5. 字符集转换: 验证字段在 JOIN 时能正确比较
-- ========================================
