-- =====================================================
-- 评价功能部署验证脚本
-- 执行日期：2026-01-23
-- 用途：验证评价功能是否正确部署
-- =====================================================

USE peixue;

-- =====================================================
-- 第一步：检查review表是否存在
-- =====================================================
SELECT 
  '=== 检查review表 ===' AS step,
  CASE 
    WHEN COUNT(*) > 0 THEN '✅ review表已存在'
    ELSE '❌ review表不存在，请执行 create_review_table_simple_2026-01-23.sql'
  END AS result
FROM information_schema.TABLES 
WHERE TABLE_SCHEMA = 'peixue' 
  AND TABLE_NAME = 'review';

-- =====================================================
-- 第二步：检查order表的reviewed字段
-- =====================================================
SELECT 
  '=== 检查order.reviewed字段 ===' AS step,
  CASE 
    WHEN COUNT(*) > 0 THEN '✅ reviewed字段已存在'
    ELSE '❌ reviewed字段不存在，请执行 create_review_table_simple_2026-01-23.sql'
  END AS result
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' 
  AND TABLE_NAME = 'order' 
  AND COLUMN_NAME = 'reviewed';

-- =====================================================
-- 第三步：检查review表结构
-- =====================================================
SELECT 
  '=== review表结构 ===' AS info;

SHOW COLUMNS FROM review;

-- =====================================================
-- 第四步：检查测试数据
-- =====================================================
SELECT 
  '=== 测试评价数据 ===' AS info;

SELECT 
  id,
  order_id,
  user_id,
  teacher_id,
  service_score,
  quality_score,
  punctuality_score,
  overall_score,
  content,
  is_anonymous,
  create_time
FROM review
ORDER BY create_time DESC
LIMIT 5;

-- =====================================================
-- 第五步：检查订单评价状态
-- =====================================================
SELECT 
  '=== 订单评价状态 ===' AS info;

SELECT 
  id,
  order_no,
  status,
  CASE 
    WHEN reviewed IS NULL THEN '❌ reviewed字段不存在'
    WHEN reviewed = 1 THEN '✅ 已评价'
    WHEN reviewed = 0 THEN '⏳ 未评价'
    ELSE '❓ 未知状态'
  END AS review_status,
  service_name,
  service_date
FROM `order`
WHERE status = 4
ORDER BY id DESC
LIMIT 10;

-- =====================================================
-- 第六步：统计评价数据
-- =====================================================
SELECT 
  '=== 评价统计 ===' AS info;

SELECT 
  COUNT(*) as total_reviews,
  COUNT(DISTINCT user_id) as total_users,
  COUNT(DISTINCT teacher_id) as total_teachers,
  ROUND(AVG(overall_score), 2) as avg_overall_score,
  ROUND(AVG(service_score), 2) as avg_service_score,
  ROUND(AVG(quality_score), 2) as avg_quality_score,
  ROUND(AVG(punctuality_score), 2) as avg_punctuality_score,
  SUM(CASE WHEN is_anonymous = 1 THEN 1 ELSE 0 END) as anonymous_count
FROM review
WHERE deleted = 0 AND status = 1;

-- =====================================================
-- 第七步：检查陪伴员评价统计
-- =====================================================
SELECT 
  '=== 陪伴员评价统计 ===' AS info;

SELECT 
  r.teacher_id,
  t.name as teacher_name,
  COUNT(*) as review_count,
  ROUND(AVG(r.overall_score), 1) as avg_score,
  ROUND(AVG(r.service_score), 1) as avg_service_score,
  ROUND(AVG(r.quality_score), 1) as avg_quality_score,
  ROUND(AVG(r.punctuality_score), 1) as avg_punctuality_score,
  MAX(r.create_time) as last_review_time
FROM review r
LEFT JOIN teacher t ON r.teacher_id = t.id
WHERE r.deleted = 0 AND r.status = 1
GROUP BY r.teacher_id, t.name
ORDER BY review_count DESC;

-- =====================================================
-- 第八步：检查用户评价统计
-- =====================================================
SELECT 
  '=== 用户评价统计 ===' AS info;

SELECT 
  r.user_id,
  u.nickname as user_name,
  COUNT(*) as review_count,
  ROUND(AVG(r.overall_score), 1) as avg_score,
  SUM(CASE WHEN r.is_anonymous = 1 THEN 1 ELSE 0 END) as anonymous_count,
  MAX(r.create_time) as last_review_time
FROM review r
LEFT JOIN user u ON r.user_id = u.id
WHERE r.deleted = 0 AND r.status = 1
GROUP BY r.user_id, u.nickname
ORDER BY review_count DESC;

-- =====================================================
-- 第九步：检查待评价订单
-- =====================================================
SELECT 
  '=== 待评价订单（已完成但未评价）===' AS info;

SELECT 
  o.id,
  o.order_no,
  o.user_id,
  o.teacher_id,
  o.service_name,
  o.service_date,
  o.complete_time,
  CASE 
    WHEN o.reviewed = 1 THEN '✅ 已评价'
    WHEN o.reviewed = 0 THEN '⏳ 待评价'
    ELSE '❓ 未知'
  END AS review_status
FROM `order` o
WHERE o.status = 4
  AND (o.reviewed = 0 OR o.reviewed IS NULL)
ORDER BY o.complete_time DESC
LIMIT 10;

-- =====================================================
-- 第十步：验证评价与订单的关联
-- =====================================================
SELECT 
  '=== 评价与订单关联验证 ===' AS info;

SELECT 
  r.id as review_id,
  r.order_id,
  o.order_no,
  o.reviewed as order_reviewed,
  CASE 
    WHEN o.id IS NULL THEN '❌ 订单不存在'
    WHEN o.reviewed = 1 THEN '✅ 订单已标记为已评价'
    ELSE '❌ 订单未标记为已评价'
  END AS validation_result
FROM review r
LEFT JOIN `order` o ON r.order_id = o.id
ORDER BY r.create_time DESC
LIMIT 10;

-- =====================================================
-- 验证完成提示
-- =====================================================
SELECT 
  '✅ 验证脚本执行完成！' AS message,
  '请检查上面的验证结果' AS next_step,
  '如果发现问题，请执行 create_review_table_simple_2026-01-23.sql' AS solution;
