# 预约时段为空问题诊断指南 ## 问题描述 访问接口 `/adminapi/doctor.appointment/availableSlots?doctor_id=3&appointment_date=2026-03-06&period=all` 返回空数组。 ## 可能的原因 ### 原因 1:医生没有排班数据 **检查方法:** ```sql SELECT * FROM zyt_doctor_roster WHERE doctor_id = 3 AND date = '2026-03-06'; ``` **如果返回空:** - 说明医生在该日期没有排班 - 需要先添加排班数据 **解决方案:** ```sql -- 添加排班数据 INSERT INTO zyt_doctor_roster ( doctor_id, date, period, status, quota, max_patients, booked_count, create_time, update_time ) VALUES (3, '2026-03-06', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 上午出诊 (3, '2026-03-06', 2, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()); -- 下午出诊 ``` ### 原因 2:排班状态不是"出诊" **检查方法:** ```sql SELECT 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 = 3 AND date = '2026-03-06'; ``` **如果 status 不是 1:** - status = 2(停诊)→ 不会生成时段 - status = 3(休息)→ 不会生成时段 - status = 4(请假)→ 不会生成时段 **解决方案:** ```sql -- 修改排班状态为出诊 UPDATE zyt_doctor_roster SET status = 1 WHERE doctor_id = 3 AND date = '2026-03-06'; ``` ### 原因 3:period 值不正确 **检查方法:** ```sql SELECT id, date, period, CASE period WHEN 1 THEN '上午' WHEN 2 THEN '下午' ELSE CONCAT('未知(', period, ')') END as period_name, status FROM zyt_doctor_roster WHERE doctor_id = 3 AND date = '2026-03-06'; ``` **如果 period 不是 1 或 2:** - 代码只处理 period = 1(上午)和 period = 2(下午) - 其他值会被跳过 **解决方案:** ```sql -- 修改 period 为正确的值 UPDATE zyt_doctor_roster SET period = 1 -- 或 2 WHERE doctor_id = 3 AND date = '2026-03-06' AND period NOT IN (1, 2); ``` ### 原因 4:医生ID不存在 **检查方法:** ```sql -- 检查医生是否存在 SELECT a.id, a.name, a.account, ar.role_id FROM zyt_admin a INNER JOIN zyt_admin_role ar ON a.id = ar.admin_id WHERE a.id = 3 AND ar.role_id = 1; ``` **如果返回空:** - 医生ID=3不存在 - 或者该用户不是医生角色 ## 快速诊断步骤 ### 步骤 1:检查排班数据 ```sql SELECT id, doctor_id, date, period, CASE period WHEN 1 THEN '上午' WHEN 2 THEN '下午' ELSE '未知' END as period_name, status, CASE status WHEN 1 THEN '出诊' WHEN 2 THEN '停诊' WHEN 3 THEN '休息' WHEN 4 THEN '请假' ELSE '未知' END as status_name FROM zyt_doctor_roster WHERE doctor_id = 3 AND date = '2026-03-06'; ``` **预期结果:** ``` +----+-----------+------------+--------+-------------+--------+-------------+ | id | doctor_id | date | period | period_name | status | status_name | +----+-----------+------------+--------+-------------+--------+-------------+ | 1 | 3 | 2026-03-06 | 1 | 上午 | 1 | 出诊 | | 2 | 3 | 2026-03-06 | 2 | 下午 | 1 | 出诊 | +----+-----------+------------+--------+-------------+--------+-------------+ ``` ### 步骤 2:查看日志 ```bash # 查看应用日志 tail -f runtime/log/202403/04.log | grep "获取可用时段" ``` **日志示例:** ``` [info] 获取可用时段 - 请求参数 {"doctor_id":3,"date":"2026-03-06","period":"all"} [info] 查询到的排班数据 {"count":2,"rosters":[...]} [info] 处理排班时段 {"roster_id":1,"period":1} [info] 处理排班时段 {"roster_id":2,"period":2} [info] 生成的时间段数量 {"count":28} ``` ### 步骤 3:测试接口 ```bash # 使用 curl 测试 curl -X GET "http://your-domain/adminapi/doctor.appointment/availableSlots?doctor_id=3&appointment_date=2026-03-06&period=all" \ -H "token: your-token" ``` **预期响应(有排班):** ```json { "code": 1, "msg": "success", "data": { "slots": [ {"time": "09:00", "available": true, "quota": 1, "period": 1}, {"time": "09:15", "available": true, "quota": 1, "period": 1}, ... ] } } ``` **实际响应(无排班):** ```json { "code": 1, "msg": "success", "data": { "slots": [] } } ``` ## 解决方案 ### 方案 1:添加排班数据 如果医生没有排班,需要先添加: ```sql -- 添加2026-03-06的排班 INSERT INTO zyt_doctor_roster ( doctor_id, date, period, status, quota, max_patients, booked_count, remark, create_time, update_time ) VALUES -- 上午出诊 (3, '2026-03-06', 1, 1, 20, 20, 0, '上午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 下午出诊 (3, '2026-03-06', 2, 1, 20, 20, 0, '下午出诊', UNIX_TIMESTAMP(), UNIX_TIMESTAMP()); ``` ### 方案 2:修改排班状态 如果排班存在但状态不对: ```sql -- 将休息/停诊改为出诊 UPDATE zyt_doctor_roster SET status = 1, update_time = UNIX_TIMESTAMP() WHERE doctor_id = 3 AND date = '2026-03-06' AND status != 1; ``` ### 方案 3:批量添加排班 为医生添加未来7天的排班: ```sql -- 添加未来7天的排班 INSERT INTO zyt_doctor_roster ( doctor_id, date, period, status, quota, max_patients, booked_count, create_time, update_time ) VALUES -- 2026-03-04 (3, '2026-03-04', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), (3, '2026-03-04', 2, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 2026-03-05 (3, '2026-03-05', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), (3, '2026-03-05', 2, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 2026-03-06 (3, '2026-03-06', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), (3, '2026-03-06', 2, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 2026-03-07 (3, '2026-03-07', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), (3, '2026-03-07', 2, 3, 0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 下午休息 -- 2026-03-08 (3, '2026-03-08', 1, 3, 0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 上午休息 (3, '2026-03-08', 2, 3, 0, 0, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 下午休息 -- 2026-03-09 (3, '2026-03-09', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), (3, '2026-03-09', 2, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), -- 2026-03-10 (3, '2026-03-10', 1, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()), (3, '2026-03-10', 2, 1, 20, 20, 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()); ``` ## 验证修复 ### 1. 检查数据 ```sql 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 '休息' END as status_name FROM zyt_doctor_roster WHERE doctor_id = 3 AND date BETWEEN '2026-03-04' AND '2026-03-10' ORDER BY date, period; ``` ### 2. 测试接口 ```bash curl -X GET "http://your-domain/adminapi/doctor.appointment/availableSlots?doctor_id=3&appointment_date=2026-03-06&period=all" \ -H "token: your-token" ``` ### 3. 前端测试 1. 登录管理后台 2. 进入诊单管理 3. 点击"挂号" 4. 选择医生ID=3 5. 选择日期2026-03-06 6. 查看是否显示时段 ## 常见错误 ### 错误 1:日期格式不对 ```sql -- ❌ 错误:使用了错误的日期格式 INSERT INTO zyt_doctor_roster (doctor_id, date, ...) VALUES (3, '2026/03/06', ...); -- ✅ 正确:使用 YYYY-MM-DD 格式 INSERT INTO zyt_doctor_roster (doctor_id, date, ...) VALUES (3, '2026-03-06', ...); ``` ### 错误 2:status 值错误 ```sql -- ❌ 错误:使用了字符串 UPDATE zyt_doctor_roster SET status = '出诊'; -- ✅ 正确:使用数字 UPDATE zyt_doctor_roster SET status = 1; ``` ### 错误 3:period 值错误 ```sql -- ❌ 错误:使用了字符串 INSERT INTO zyt_doctor_roster (period, ...) VALUES ('上午', ...); -- ✅ 正确:使用数字 INSERT INTO zyt_doctor_roster (period, ...) VALUES (1, ...); ``` ## 调试工具 ### 使用提供的SQL脚本 ```bash # 在数据库客户端中运行 source CHECK_ROSTER_DATA.sql ``` ### 查看详细日志 代码中已添加详细日志,可以通过以下方式查看: ```bash # 实时查看日志 tail -f runtime/log/202403/04.log # 搜索特定医生的日志 grep "doctor_id.*3" runtime/log/202403/04.log # 搜索特定日期的日志 grep "2026-03-06" runtime/log/202403/04.log ``` ## 总结 最常见的原因是: 1. ✅ 医生没有排班数据 2. ✅ 排班状态不是"出诊"(status != 1) 3. ✅ period 值不是 1 或 2 解决方法: 1. 添加排班数据 2. 修改排班状态为出诊(status = 1) 3. 确保 period 值为 1(上午)或 2(下午) --- **创建日期:** 2024-03-04 **版本:** 1.0