-- ============================================
-- 数据分析实时大屏 - 测试查询脚本
-- ============================================
-- 这些查询用于验证实时大屏的数据计算逻辑

-- ============================================
-- 1. 测试实时GMV查询
-- ============================================
-- 今日GMV
SELECT 
    SUM(total_amount) as today_gmv,
    COUNT(*) as today_order_count
FROM orders
WHERE created_at >= DATE_TRUNC('day', NOW())
  AND status = 2;

-- 昨日同时段GMV（当前时间往前推24小时）
SELECT 
    SUM(total_amount) as yesterday_gmv,
    COUNT(*) as yesterday_order_count
FROM orders
WHERE created_at >= DATE_TRUNC('day', NOW() - INTERVAL '1 day')
  AND created_at <= NOW() - INTERVAL '1 day'
  AND status = 2;

-- GMV增长率计算
WITH today_stats AS (
    SELECT 
        COALESCE(SUM(total_amount), 0) as gmv,
        COUNT(*) as orders
    FROM orders
    WHERE created_at >= DATE_TRUNC('day', NOW())
      AND status = 2
),
yesterday_stats AS (
    SELECT 
        COALESCE(SUM(total_amount), 0) as gmv,
        COUNT(*) as orders
    FROM orders
    WHERE created_at >= DATE_TRUNC('day', NOW() - INTERVAL '1 day')
      AND created_at <= NOW() - INTERVAL '1 day'
      AND status = 2
)
SELECT 
    today_stats.gmv as today_gmv,
    yesterday_stats.gmv as yesterday_gmv,
    CASE 
        WHEN yesterday_stats.gmv > 0 THEN 
            ROUND((today_stats.gmv - yesterday_stats.gmv) / yesterday_stats.gmv * 100, 2)
        ELSE 
            CASE WHEN today_stats.gmv > 0 THEN 100 ELSE 0 END
    END as gmv_growth_percent,
    today_stats.orders as today_orders,
    yesterday_stats.orders as yesterday_orders,
    CASE 
        WHEN yesterday_stats.orders > 0 THEN 
            ROUND((today_stats.orders - yesterday_stats.orders)::numeric / yesterday_stats.orders * 100, 2)
        ELSE 
            CASE WHEN today_stats.orders > 0 THEN 100 ELSE 0 END
    END as order_growth_percent
FROM today_stats, yesterday_stats;

-- ============================================
-- 2. 测试在线用户查询
-- ============================================
-- 最近5分钟内有活动的用户（在线用户）
SELECT COUNT(DISTINCT user_id) as online_users
FROM user_sessions
WHERE last_active_at >= NOW() - INTERVAL '5 minutes'
  AND is_active = true;

-- 备用方案：从用户表查询最近5分钟登录的用户
SELECT COUNT(*) as online_users_from_users
FROM users
WHERE last_login_at >= NOW() - INTERVAL '5 minutes';

-- ============================================
-- 3. 测试转化率查询
-- ============================================
-- 今日下单的唯一用户数
SELECT COUNT(DISTINCT user_id) as unique_order_users
FROM orders
WHERE created_at >= DATE_TRUNC('day', NOW())
  AND status = 2;

-- 今日访问用户数（从用户会话表）
SELECT COUNT(DISTINCT user_id) as today_visitors
FROM user_sessions
WHERE created_at >= DATE_TRUNC('day', NOW());

-- 转化率计算
WITH order_users AS (
    SELECT COUNT(DISTINCT user_id) as count
    FROM orders
    WHERE created_at >= DATE_TRUNC('day', NOW())
      AND status = 2
),
visitors AS (
    SELECT COUNT(DISTINCT user_id) as count
    FROM user_sessions
    WHERE created_at >= DATE_TRUNC('day', NOW())
)
SELECT 
    order_users.count as order_users,
    visitors.count as visitors,
    CASE 
        WHEN visitors.count > 0 THEN 
            ROUND(order_users.count::numeric / visitors.count * 100, 2)
        ELSE 0
    END as conversion_rate_percent
FROM order_users, visitors;

-- ============================================
-- 4. 综合实时大屏数据查询
-- ============================================
WITH 
-- 今日统计
today_stats AS (
    SELECT 
        COALESCE(SUM(total_amount), 0) as gmv,
        COUNT(*) as orders,
        COUNT(DISTINCT user_id) as order_users
    FROM orders
    WHERE created_at >= DATE_TRUNC('day', NOW())
      AND status = 2
),
-- 昨日同时段统计
yesterday_stats AS (
    SELECT 
        COALESCE(SUM(total_amount), 0) as gmv,
        COUNT(*) as orders,
        COUNT(DISTINCT user_id) as order_users
    FROM orders
    WHERE created_at >= DATE_TRUNC('day', NOW() - INTERVAL '1 day')
      AND created_at <= NOW() - INTERVAL '1 day'
      AND status = 2
),
-- 在线用户
online_users AS (
    SELECT COUNT(DISTINCT user_id) as count
    FROM user_sessions
    WHERE last_active_at >= NOW() - INTERVAL '5 minutes'
      AND is_active = true
),
-- 今日访问用户
today_visitors AS (
    SELECT COUNT(DISTINCT user_id) as count
    FROM user_sessions
    WHERE created_at >= DATE_TRUNC('day', NOW())
),
-- 昨日同时段访问用户
yesterday_visitors AS (
    SELECT COUNT(DISTINCT user_id) as count
    FROM user_sessions
    WHERE created_at >= DATE_TRUNC('day', NOW() - INTERVAL '1 day')
      AND created_at <= NOW() - INTERVAL '1 day'
)
SELECT 
    -- GMV数据
    today_stats.gmv as real_time_gmv,
    CASE 
        WHEN yesterday_stats.gmv > 0 THEN 
            ROUND((today_stats.gmv - yesterday_stats.gmv) / yesterday_stats.gmv * 100, 1)
        ELSE 
            CASE WHEN today_stats.gmv > 0 THEN 100.0 ELSE 0.0 END
    END as gmv_growth,
    
    -- 订单数据
    today_stats.orders as real_time_orders,
    CASE 
        WHEN yesterday_stats.orders > 0 THEN 
            ROUND((today_stats.orders - yesterday_stats.orders)::numeric / yesterday_stats.orders * 100, 1)
        ELSE 
            CASE WHEN today_stats.orders > 0 THEN 100.0 ELSE 0.0 END
    END as order_growth,
    
    -- 在线用户
    COALESCE(online_users.count, 0) as online_users,
    
    -- 转化率
    CASE 
        WHEN today_visitors.count > 0 THEN 
            ROUND(today_stats.order_users::numeric / today_visitors.count * 100, 1)
        ELSE 0
    END as conversion_rate,
    CASE 
        WHEN yesterday_visitors.count > 0 AND yesterday_stats.order_users > 0 THEN
            ROUND((today_stats.order_users::numeric / today_visitors.count * 100) - 
                  (yesterday_stats.order_users::numeric / yesterday_visitors.count * 100), 1)
        ELSE 0
    END as conversion_growth
FROM today_stats, yesterday_stats, online_users, today_visitors, yesterday_visitors;

-- ============================================
-- 5. 数据验证查询
-- ============================================
-- 检查今日订单数据
SELECT 
    DATE_TRUNC('hour', created_at) as hour,
    COUNT(*) as order_count,
    SUM(total_amount) as hour_gmv
FROM orders
WHERE created_at >= DATE_TRUNC('day', NOW())
  AND status = 2
GROUP BY DATE_TRUNC('hour', created_at)
ORDER BY hour DESC;

-- 检查用户会话数据
SELECT 
    DATE_TRUNC('hour', created_at) as hour,
    COUNT(DISTINCT user_id) as unique_visitors
FROM user_sessions
WHERE created_at >= DATE_TRUNC('day', NOW())
GROUP BY DATE_TRUNC('hour', created_at)
ORDER BY hour DESC;

-- 检查最近活动的用户
SELECT 
    user_id,
    last_active_at,
    NOW() - last_active_at as time_ago
FROM user_sessions
WHERE last_active_at >= NOW() - INTERVAL '10 minutes'
  AND is_active = true
ORDER BY last_active_at DESC;
