-- 验证陪伴员待接单查询逻辑
-- 目的：检查派单后陪伴员是否能正确查询到待接单订单

-- ========================================
-- 陪伴员待接单查询逻辑（后端实现）
-- ========================================
-- 查询条件：
--   1. teacher_id = 当前陪伴员ID
--   2. status = 1（待接单）
--   3. pay_status = 1（已支付）
--   4. deleted = 0（未删除）
-- ========================================

-- 1. 查看最近派单的订单（订单306）
SELECT 
    id,
    order_no,
    teacher_id,
    status,
    CASE 
        WHEN status = 0 THEN '待派单'
        WHEN status = 1 THEN '待接单'
        WHEN status = 2 THEN '待服务'
        WHEN status = 3 THEN '服务中'
        WHEN status = 4 THEN '已完成'
        WHEN status = 5 THEN '已取消'
        ELSE CONCAT('未知(', status, ')')
    END as status_name,
    pay_status,
    CASE 
        WHEN pay_status = 0 THEN '未支付'
        WHEN pay_status = 1 THEN '已支付'
        WHEN pay_status = 2 THEN '已退款'
        ELSE CONCAT('未知(', pay_status, ')')
    END as pay_status_name,
    deleted,
    create_time,
    update_time
FROM `order`
WHERE id = 306;

-- 2. 查看陪伴员10096的所有待接单订单（模拟后端查询）
SELECT 
    id,
    order_no,
    teacher_id,
    status,
    pay_status,
    service_date,
    time_slot,
    service_address,
    pay_amount,
    create_time
FROM `order`
WHERE teacher_id = 10096
  AND status = 1
  AND pay_status = 1
  AND deleted = 0
ORDER BY create_time DESC;

-- 3. 统计陪伴员10096的待接单数量
SELECT 
    COUNT(*) as pending_count
FROM `order`
WHERE teacher_id = 10096
  AND status = 1
  AND pay_status = 1
  AND deleted = 0;

-- 4. 查看所有陪伴员的待接单订单分布
SELECT 
    teacher_id,
    COUNT(*) as pending_count,
    GROUP_CONCAT(id ORDER BY id) as order_ids
FROM `order`
WHERE status = 1
  AND pay_status = 1
  AND deleted = 0
GROUP BY teacher_id
ORDER BY pending_count DESC;

-- 5. 检查是否有订单状态不一致的情况
-- （已派单但status不是1，或未派单但status是1）
SELECT 
    '已派单但status不是1' as issue_type,
    id,
    order_no,
    teacher_id,
    status,
    pay_status,
    create_time
FROM `order`
WHERE teacher_id IS NOT NULL
  AND status != 1
  AND status != 2
  AND status != 3
  AND status != 4
  AND status != 5
  AND pay_status = 1
  AND deleted = 0
LIMIT 10;

-- 6. 检查teacher表中的user_id映射关系
-- （确认陪伴员的userId和teacherId是否正确关联）
SELECT 
    t.id as teacher_id,
    t.user_id,
    t.teacher_name,
    t.real_name,
    t.phone,
    u.id as user_table_id,
    u.nickname,
    u.phone as user_phone
FROM teacher t
LEFT JOIN user u ON t.user_id = u.id
WHERE t.id = 10096;

-- 7. 查看订单306的完整信息（包括学生和家长信息）
SELECT 
    o.id,
    o.order_no,
    o.teacher_id,
    o.status,
    o.pay_status,
    o.user_id,
    o.student_id,
    o.service_name,
    o.service_date,
    o.time_slot,
    o.service_address,
    o.pay_amount,
    o.user_remark,
    u.nickname as parent_name,
    u.phone as parent_phone,
    s.name as student_name,
    s.grade as student_grade,
    s.age as student_age
FROM `order` o
LEFT JOIN user u ON o.user_id = u.id
LEFT JOIN student s ON o.student_id = s.id
WHERE o.id = 306;

-- 8. 检查是否有重复的teacher_id（可能导致查询混乱）
SELECT 
    teacher_id,
    COUNT(*) as count
FROM `order`
WHERE teacher_id IS NOT NULL
  AND deleted = 0
GROUP BY teacher_id
HAVING count > 1
ORDER BY count DESC
LIMIT 10;

-- 9. 查看最近10条派单记录（检查派单后的状态变化）
SELECT 
    id,
    order_no,
    teacher_id,
    status,
    CASE 
        WHEN status = 0 THEN '待派单'
        WHEN status = 1 THEN '待接单'
        WHEN status = 2 THEN '待服务'
        WHEN status = 3 THEN '服务中'
        WHEN status = 4 THEN '已完成'
        WHEN status = 5 THEN '已取消'
        ELSE CONCAT('未知(', status, ')')
    END as status_name,
    pay_status,
    create_time,
    update_time
FROM `order`
WHERE teacher_id IS NOT NULL
  AND deleted = 0
ORDER BY update_time DESC
LIMIT 10;

-- 10. 检查订单306的历史状态变化（如果有日志表）
-- 注意：这个查询假设有order_log表，如果没有则跳过
-- SELECT 
--     order_id,
--     old_status,
--     new_status,
--     old_teacher_id,
--     new_teacher_id,
--     create_time,
--     operator
-- FROM order_log
-- WHERE order_id = 306
-- ORDER BY create_time DESC;
