-- 检查订单状态分布情况

-- 1. 查看所有订单的状态分布（包括已删除的）
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 '已取消'
        WHEN status IS NULL THEN 'NULL状态'
        ELSE CONCAT('未知状态(', status, ')')
    END as status_name
FROM `order`
GROUP BY status
ORDER BY status;

-- 2. 查看未删除订单的状态分布
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 '已取消'
        WHEN status IS NULL THEN 'NULL状态'
        ELSE CONCAT('未知状态(', status, ')')
    END as status_name
FROM `order`
WHERE deleted = 0
GROUP BY status
ORDER BY status;

-- 3. 查看已支付订单的状态分布
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 '已取消'
        WHEN status IS NULL THEN 'NULL状态'
        ELSE CONCAT('未知状态(', status, ')')
    END as status_name
FROM `order`
WHERE pay_status = 1 AND deleted = 0
GROUP BY status
ORDER BY status;

-- 4. 查看已支付且未分配陪伴员的订单状态分布（这些应该是待派单）
SELECT 
    status,
    COUNT(*) as count,
    CASE 
        WHEN status = 0 THEN '✅ 待派单（正常）'
        WHEN status = 1 THEN '❌ 已派单（异常：有teacher_id应该不为NULL）'
        WHEN status = 2 THEN '❌ 待服务（异常：未派单不应该是待服务）'
        WHEN status = 3 THEN '❌ 服务中（异常：未派单不应该是服务中）'
        WHEN status = 4 THEN '❌ 已完成（异常：未派单不应该是已完成）'
        WHEN status = 5 THEN '⚠️ 已取消（可能正常）'
        WHEN status IS NULL THEN '❌ NULL状态（异常）'
        ELSE CONCAT('❌ 未知状态(', status, ')（异常）')
    END as status_check
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NULL 
  AND deleted = 0
GROUP BY status
ORDER BY status;

-- 5. 查看已支付且已分配陪伴员的订单状态分布（这些应该是已派单或更后面的状态）
SELECT 
    status,
    COUNT(*) as count,
    CASE 
        WHEN status = 0 THEN '❌ 待派单（异常：已有teacher_id不应该是待派单）'
        WHEN status = 1 THEN '✅ 已派单/待接单（正常）'
        WHEN status = 2 THEN '✅ 待服务（正常）'
        WHEN status = 3 THEN '✅ 服务中（正常）'
        WHEN status = 4 THEN '✅ 已完成（正常）'
        WHEN status = 5 THEN '⚠️ 已取消（可能正常）'
        WHEN status IS NULL THEN '❌ NULL状态（异常）'
        ELSE CONCAT('❌ 未知状态(', status, ')（异常）')
    END as status_check
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NOT NULL 
  AND deleted = 0
GROUP BY status
ORDER BY status;

-- 6. 查看支付状态和订单状态的组合分布
SELECT 
    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,
    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 '已取消'
        WHEN status IS NULL THEN 'NULL'
        ELSE CONCAT('未知(', status, ')')
    END as status_name,
    COUNT(*) as count
FROM `order`
WHERE deleted = 0
GROUP BY pay_status, status
ORDER BY pay_status, status;

-- 7. 列出所有可能存在的状态值（包括异常状态）
SELECT DISTINCT 
    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 '已取消'
        WHEN status IS NULL THEN 'NULL状态'
        ELSE CONCAT('未知状态(', status, ')')
    END as status_name
FROM `order`
ORDER BY status;

-- 8. 查看状态为NULL的订单详情（如果有的话）
SELECT 
    id,
    order_no,
    status,
    pay_status,
    teacher_id,
    create_time,
    update_time
FROM `order`
WHERE status IS NULL
LIMIT 20;

-- 9. 统计：按照后端逻辑应该显示的待派单数量
SELECT 
    '后端统计逻辑' as query_type,
    COUNT(*) as pending_count
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NULL 
  AND deleted = 0;
