-- =====================================================
-- 家长端评价功能 - 数据库脚本（最终可执行版本）
-- 执行日期：2026-01-23
-- 数据库：peixue
-- 说明：完全兼容，可直接执行
-- =====================================================

-- 使用数据库
USE peixue;

-- =====================================================
-- 第一步：创建评价表
-- =====================================================
DROP TABLE IF EXISTS `review`;

CREATE TABLE `review` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '评价ID',
  `order_id` BIGINT NOT NULL COMMENT '订单ID',
  `user_id` BIGINT NOT NULL COMMENT '用户ID（家长）',
  `teacher_id` BIGINT NOT NULL COMMENT '陪伴员ID',
  `student_id` BIGINT DEFAULT NULL COMMENT '学生ID',
  
  -- 评分字段
  `service_score` INT NOT NULL DEFAULT 5 COMMENT '服务态度评分（1-5星）',
  `quality_score` INT NOT NULL DEFAULT 5 COMMENT '教学质量评分（1-5星）',
  `punctuality_score` INT NOT NULL DEFAULT 5 COMMENT '准时性评分（1-5星）',
  `overall_score` DECIMAL(3,1) NOT NULL DEFAULT 5.0 COMMENT '综合评分（自动计算平均分）',
  
  -- 评价内容
  `content` TEXT COMMENT '评价内容（最多200字）',
  `images` TEXT COMMENT '评价图片（JSON数组，最多9张）',
  
  -- 评价状态
  `is_anonymous` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否匿名（0-否，1-是）',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '评价状态（1-正常，0-已删除）',
  
  -- 回复信息
  `reply_content` TEXT COMMENT '陪伴员回复内容',
  `reply_time` DATETIME DEFAULT NULL COMMENT '回复时间',
  
  -- 租户和时间
  `tenant_id` BIGINT DEFAULT 1 COMMENT '租户ID',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `deleted` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '删除标记（0-未删除，1-已删除）',
  
  PRIMARY KEY (`id`),
  KEY `idx_order_id` (`order_id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_teacher_id` (`teacher_id`),
  KEY `idx_student_id` (`student_id`),
  KEY `idx_tenant_id` (`tenant_id`),
  KEY `idx_create_time` (`create_time`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='服务评价表';

-- =====================================================
-- 第二步：为order表添加reviewed字段
-- =====================================================

-- 检查字段是否存在
SET @col_exists = 0;
SELECT COUNT(*) INTO @col_exists 
FROM information_schema.COLUMNS 
WHERE TABLE_SCHEMA = 'peixue' 
  AND TABLE_NAME = 'order' 
  AND COLUMN_NAME = 'reviewed';

-- 如果字段不存在，则添加
SET @sql = IF(@col_exists = 0,
  'ALTER TABLE `order` ADD COLUMN `reviewed` TINYINT(1) NOT NULL DEFAULT 0 COMMENT ''是否已评价（0-未评价，1-已评价）'' AFTER `status`',
  'SELECT ''字段 reviewed 已存在，跳过添加'' AS message'
);

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

-- =====================================================
-- 第三步：插入测试数据
-- =====================================================

-- 插入测试数据（明确指定所有字段）
INSERT INTO `review` (
  `order_id`, 
  `user_id`, 
  `teacher_id`, 
  `student_id`,
  `service_score`, 
  `quality_score`, 
  `punctuality_score`, 
  `overall_score`,
  `content`, 
  `images`, 
  `is_anonymous`, 
  `tenant_id`
) VALUES
(102, 1, 1, 1, 5, 5, 5, 5.0, '陪伴员非常专业，孩子很喜欢，会继续选择这位老师！', '[]', 0, 1),
(103, 1, 2, 1, 4, 4, 5, 4.3, '服务态度很好，准时到达，孩子学习有进步。', '[]', 0, 1),
(104, 2, 1, 2, 5, 4, 5, 4.7, '老师很有耐心，讲解清晰，孩子听得很认真。', '[]', 1, 1);

-- =====================================================
-- 第四步：更新订单的评价状态
-- =====================================================
UPDATE `order` SET `reviewed` = 1 WHERE `id` IN (102, 103, 104);

-- =====================================================
-- 第五步：验证数据
-- =====================================================

SELECT '========================================' AS '';
SELECT '✅ 评价功能数据库脚本执行完成！' AS '执行结果';
SELECT '========================================' AS '';

-- 查询评价记录
SELECT '=== 评价记录 ===' AS '';
SELECT 
  r.id,
  r.order_id,
  r.user_id,
  r.teacher_id,
  r.student_id,
  r.overall_score,
  r.content,
  r.is_anonymous,
  r.create_time
FROM review r
ORDER BY r.create_time DESC
LIMIT 10;

-- 查询订单评价状态
SELECT '=== 订单评价状态 ===' AS '';
SELECT 
  id,
  order_no,
  status,
  reviewed,
  service_name
FROM `order`
WHERE id IN (102, 103, 104)
ORDER BY id;

-- 统计信息
SELECT '=== 统计信息 ===' AS '';
SELECT 
  COUNT(*) as '评价总数',
  COUNT(DISTINCT user_id) as '用户数',
  COUNT(DISTINCT teacher_id) as '陪伴员数',
  ROUND(AVG(overall_score), 1) as '平均评分'
FROM review
WHERE deleted = 0 AND status = 1;

SELECT '========================================' AS '';
SELECT '请检查上面的数据是否正确' AS '下一步';
SELECT '========================================' AS '';
