-- 验证后端统计接口的查询逻辑

-- 1. 后端当前的待派单查询逻辑（修改后）
-- 条件：pay_status=1 AND teacher_id IS NULL AND deleted=0
SELECT COUNT(*) as pending_count
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NULL 
  AND deleted = 0;

-- 2. 列出所有符合条件的待派单订单
SELECT 
    id,
    order_no,
    status,
    pay_status,
    teacher_id,
    create_time
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NULL 
  AND deleted = 0
ORDER BY create_time DESC;

-- 3. 如果后端还在使用旧的查询逻辑（包含status=0）
-- 条件：pay_status=1 AND status=0 AND teacher_id IS NULL AND deleted=0
SELECT COUNT(*) as pending_count_with_status
FROM `order`
WHERE pay_status = 1 
  AND status = 0
  AND teacher_id IS NULL 
  AND deleted = 0;

-- 4. 对比两种查询的差异
SELECT 
    '不限制status' as query_type,
    COUNT(*) as count
FROM `order`
WHERE pay_status = 1 
  AND teacher_id IS NULL 
  AND deleted = 0

UNION ALL

SELECT 
    '限制status=0' as query_type,
    COUNT(*) as count
FROM `order`
WHERE pay_status = 1 
  AND status = 0
  AND teacher_id IS NULL 
  AND deleted = 0;

-- 5. 查看待派单订单的status分布
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 teacher_id IS NULL 
  AND deleted = 0
GROUP BY status
ORDER BY status;
