-- 快速修复医生ID=4的排班数据

-- 1. 检查当前排班状态
SELECT 
    '=== 当前排班状态 ===' as info;

SELECT 
    date,
    period,
    CASE period WHEN 1 THEN '上午' WHEN 2 THEN '下午' END as period_name,
    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 = '2026-03-04';

-- 2. 如果没有数据，添加排班
SELECT 
    '=== 添加排班数据 ===' as info;

INSERT INTO zyt_doctor_roster (
    doctor_id, 
    date, 
    period, 
    status, 
    quota, 
    max_patients, 
    booked_count,
    remark,
    create_time, 
    update_time
) 
SELECT 4, '2026-03-04', 1, 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (
    SELECT 1 FROM zyt_doctor_roster 
    WHERE doctor_id = 4 AND date = '2026-03-04' AND period = 1
);

INSERT INTO zyt_doctor_roster (
    doctor_id, 
    date, 
    period, 
    status, 
    quota, 
    max_patients, 
    booked_count,
    remark,
    create_time, 
    update_time
) 
SELECT 4, '2026-03-04', 2, 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()
WHERE NOT EXISTS (
    SELECT 1 FROM zyt_doctor_roster 
    WHERE doctor_id = 4 AND date = '2026-03-04' AND period = 2
);

-- 3. 如果有数据但状态不对，修改状态
SELECT 
    '=== 修改排班状态为出诊 ===' as info;

UPDATE zyt_doctor_roster 
SET status = 1,
    quota = 20,
    max_patients = 20,
    remark = '修改为出诊',
    update_time = UNIX_TIMESTAMP()
WHERE doctor_id = 4 
  AND date = '2026-03-04'
  AND status != 1;

-- 4. 验证修复结果
SELECT 
    '=== 修复后的排班状态 ===' as info;

SELECT 
    date,
    period,
    CASE period WHEN 1 THEN '上午' WHEN 2 THEN '下午' END as period_name,
    status,
    CASE status 
        WHEN 1 THEN '出诊' 
        WHEN 2 THEN '停诊' 
        WHEN 3 THEN '休息' 
        WHEN 4 THEN '请假' 
    END as status_name,
    quota,
    max_patients,
    remark
FROM zyt_doctor_roster
WHERE doctor_id = 4
  AND date = '2026-03-04';

-- 5. 添加更多日期的排班（可选）
SELECT 
    '=== 添加一周的排班数据（可选）===' as info;

-- 取消下面的注释来添加一周的排班

/*
-- 清空旧数据
DELETE FROM zyt_doctor_roster 
WHERE doctor_id = 4 
  AND date BETWEEN '2026-03-02' AND '2026-03-08';

-- 添加一周的排班
INSERT INTO zyt_doctor_roster (
    doctor_id, 
    date, 
    period, 
    status, 
    quota, 
    max_patients, 
    booked_count,
    remark,
    create_time, 
    update_time
) VALUES 
-- 周一全天出诊
(4, '2026-03-02', 1, 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-02', 2, 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
-- 周二上午出诊下午休息
(4, '2026-03-03', 1, 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-03', 2, 3, 0, 0, 0, '下午休息', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
-- 周三全天出诊
(4, '2026-03-04', 1, 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-04', 2, 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
-- 周四上午停诊下午出诊
(4, '2026-03-05', 1, 2, 0, 0, 0, '上午停诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-05', 2, 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
-- 周五全天休息
(4, '2026-03-06', 1, 3, 0, 0, 0, '上午休息', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-06', 2, 3, 0, 0, 0, '下午休息', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
-- 周六全天出诊
(4, '2026-03-07', 1, 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-07', 2, 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
-- 周日全天休息
(4, '2026-03-08', 1, 3, 0, 0, 0, '上午休息', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(4, '2026-03-08', 2, 3, 0, 0, 0, '下午休息', UNIX_TIMESTAMP(), UNIX_TIMESTAMP());
*/

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

SELECT 
    '应该看到2条记录，status都是1（出诊）' as expected;

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