# 测试预约时间段生成逻辑 ## 测试场景 医生1在2026-03-06只有上午排班,下午未排班 ## 数据库预期状态 ```sql -- 应该只有这一条记录 SELECT * FROM zyt_doctor_roster WHERE doctor_id = 1 AND date = '2026-03-06'; -- 预期结果: -- id | doctor_id | date | period | status | quota -- 1 | 1 | 2026-03-06 | morning | 1 | 20 ``` ## API测试 ``` GET /adminapi/doctor.appointment/availableSlots?doctor_id=1&appointment_date=2026-03-06&period=all ``` ### 预期返回(正确) ```json { "slots": [ {"time": "09:00", "available": true, "quota": 1, "period": "morning"}, {"time": "09:15", "available": true, "quota": 1, "period": "morning"}, ... {"time": "11:45", "available": true, "quota": 1, "period": "morning"} ] } ``` 总共应该有 12个时间段(9:00-12:00,每15分钟一个) ### 错误返回(当前bug) ```json { "slots": [ {"time": "09:00", ...}, ... {"time": "11:45", ...}, {"time": "14:00", ...}, // ❌ 不应该有下午时段 ... {"time": "17:45", ...} // ❌ 不应该有下午时段 ] } ``` ## 调试步骤 ### 1. 检查数据库 运行SQL查看实际数据: ```sql SELECT id, doctor_id, date, period, status, quota, create_time FROM zyt_doctor_roster WHERE doctor_id = 1 AND date = '2026-03-06' ORDER BY period; ``` ### 2. 检查日志 查看 `runtime/log/` 中的日志,搜索: - "该日期所有排班数据(包括非出诊)" - "处理排班时段" ### 3. 临时调试代码 在 `AppointmentLogic.php` 的 `getAvailableSlots` 方法中,在返回前添加: ```php \think\facade\Log::info('最终生成的时间段', [ 'total_count' => count($slots), 'morning_count' => count(array_filter($slots, function($s) { return strtotime($s['time']) < strtotime('12:00'); })), 'afternoon_count' => count(array_filter($slots, function($s) { return strtotime($s['time']) >= strtotime('14:00'); })), 'first_5_slots' => array_slice($slots, 0, 5), 'last_5_slots' => array_slice($slots, -5) ]); ``` ## 可能的bug原因 ### 假设1: 数据库中实际有两条记录 如果数据库中有: - period='morning', status=1 - period='afternoon', status=1 (或其他值) 那么查询 `WHERE status=1` 可能会返回两条记录。 ### 假设2: period字段值不一致 如果数据库中的period值是其他格式,比如: - period='all' 或 period='1,2' 那么代码可能会错误地生成两个时段。 ### 假设3: 前端缓存 前端可能缓存了之前的数据。 ## 解决方案 如果确认数据库中只有上午记录,但仍然生成了下午时段,那么问题在代码逻辑中。 需要在代码中添加更详细的日志,追踪每一步的数据变化。