-- 测试不同 period 格式的排班数据

-- 1. 检查当前数据库中使用的 period 格式
SELECT 
    '=== 当前使用的 period 格式 ===' as info;

SELECT 
    period,
    COUNT(*) as count,
    MIN(date) as first_date,
    MAX(date) as last_date
FROM zyt_doctor_roster
GROUP BY period
ORDER BY period;

-- 2. 查看医生ID=4的 period 格式
SELECT 
    '=== 医生ID=4的排班 ===' as info;

SELECT 
    id,
    doctor_id,
    date,
    period,
    status,
    CASE status 
        WHEN 1 THEN '出诊' 
        WHEN 2 THEN '停诊' 
        WHEN 3 THEN '休息' 
        WHEN 4 THEN '请假' 
    END as status_name
FROM zyt_doctor_roster
WHERE doctor_id = 4
  AND date BETWEEN '2026-03-02' AND '2026-03-08'
ORDER BY date, period;

-- 3. 如果 period 是字符串格式，验证修复是否生效
SELECT 
    '=== 验证：应该看到 morning 和 afternoon ===' as info;

SELECT 
    period,
    CASE 
        WHEN period = 'morning' OR period = '上午' OR period = 1 THEN '✓ 上午格式正确'
        WHEN period = 'afternoon' OR period = '下午' OR period = 2 THEN '✓ 下午格式正确'
        ELSE '✗ 未知格式'
    END as format_check
FROM zyt_doctor_roster
WHERE doctor_id = 4
  AND date = '2026-03-04'
ORDER BY period;

-- 4. 添加测试数据（如果需要）
SELECT 
    '=== 添加测试数据（取消注释来执行）===' as info;

/*
-- 清空测试数据
DELETE FROM zyt_doctor_roster WHERE doctor_id = 4 AND date = '2026-03-04';

-- 添加 morning/afternoon 格式的排班
INSERT INTO zyt_doctor_roster (
    doctor_id, 
    date, 
    period, 
    status, 
    quota, 
    max_patients, 
    booked_count,
    remark,
    create_time, 
    update_time
) VALUES 
(4, '2026-03-04', 'morning', 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-04', 'afternoon', 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
*/

-- 5. 验证添加的数据
SELECT 
    '=== 验证添加的数据 ===' as info;

SELECT 
    id,
    doctor_id,
    date,
    period,
    status,
    quota,
    max_patients,
    remark
FROM zyt_doctor_roster
WHERE doctor_id = 4
  AND date = '2026-03-04';

-- 6. 检查是否有混合格式
SELECT 
    '=== 检查是否有混合格式 ===' as info;

SELECT 
    CASE 
        WHEN period IN ('morning', 'afternoon') THEN '字符串格式'
        WHEN period IN ('上午', '下午') THEN '中文格式'
        WHEN period IN ('1', '2') THEN '数字字符串格式'
        WHEN period IN (1, 2) THEN '数字格式'
        ELSE '未知格式'
    END as format_type,
    COUNT(*) as count
FROM zyt_doctor_roster
GROUP BY format_type;

-- 7. 如果需要统一格式（可选）
SELECT 
    '=== 格式转换（可选，取消注释来执行）===' as info;

/*
-- 方案A：转换为数字格式
UPDATE zyt_doctor_roster SET period = 1 WHERE period IN ('morning', '上午');
UPDATE zyt_doctor_roster SET period = 2 WHERE period IN ('afternoon', '下午');

-- 方案B：转换为字符串格式
UPDATE zyt_doctor_roster SET period = 'morning' WHERE period IN (1, '1', '上午');
UPDATE zyt_doctor_roster SET period = 'afternoon' WHERE period IN (2, '2', '下午');
*/

-- 8. 最终验证
SELECT 
    '=== 最终验证 ===' as info;

SELECT 
    '医生ID=4在2026-03-04应该有2条出诊排班' as expected;

SELECT 
    COUNT(*) as total_count,
    SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as status_1_count,
    GROUP_CONCAT(DISTINCT period ORDER BY period) as period_values
FROM zyt_doctor_roster
WHERE doctor_id = 4
  AND date = '2026-03-04';
