-- 检查订单309的派单状态

-- 1. 查看订单309的详细信息
SELECT 
    id,
    order_no,
    user_id,
    teacher_id,
    status,
    pay_status,
    service_date,
    time_slot,
    service_address,
    user_remark,
    create_time,
    update_time,
    deleted
FROM `order`
WHERE id = 309;

-- 2. 查看订单309的状态变更历史（如果有日志表）
-- SELECT * FROM order_log WHERE order_id = 309 ORDER BY create_time DESC;

-- 3. 查看陪伴员10096的信息
SELECT 
    id,
    user_id,
    name,
    real_name,
    phone,
    audit_status,
    status
FROM teacher
WHERE id = 10096;

-- 4. 查看所有待派单的订单（pay_status=1 AND teacher_id IS NULL）
SELECT 
    id,
    order_no,
    user_id,
    teacher_id,
    status,
    pay_status,
    create_time,
    update_time
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NULL 
  AND deleted = 0
ORDER BY create_time DESC
LIMIT 20;

-- 5. 查看所有已派单的订单（teacher_id IS NOT NULL）
SELECT 
    id,
    order_no,
    user_id,
    teacher_id,
    status,
    pay_status,
    create_time,
    update_time
FROM `order`
WHERE teacher_id IS NOT NULL 
  AND deleted = 0
ORDER BY create_time DESC
LIMIT 20;

-- 6. 统计各状态订单数量
SELECT 
    status,
    COUNT(*) as count,
    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 '未知状态'
    END as status_name
FROM `order`
WHERE pay_status = 1 AND deleted = 0
GROUP BY status
ORDER BY status;

-- 7. 检查是否有teacher_id和status不一致的订单
SELECT 
    id,
    order_no,
    teacher_id,
    status,
    pay_status,
    CASE 
        WHEN teacher_id IS NULL AND status != 0 THEN '❌ 异常：未派单但状态不是0'
        WHEN teacher_id IS NOT NULL AND status = 0 THEN '❌ 异常：已派单但状态是0'
        ELSE '✅ 正常'
    END as check_result
FROM `order`
WHERE pay_status = 1 AND deleted = 0
  AND (
    (teacher_id IS NULL AND status != 0) OR
    (teacher_id IS NOT NULL AND status = 0)
  )
ORDER BY create_time DESC;
