peixue-dev/Archive/peidu-temp-files/sql/create_review_table_2026-01-23.sql

90 lines
3.4 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 创建评价表
-- 执行日期2026-01-23
-- 功能:家长端服务评价功能
-- 1. 创建评价表
CREATE TABLE IF NOT EXISTS `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='服务评价表';
-- 2. 为订单表添加评价状态字段(如果不存在)
ALTER TABLE `order`
ADD COLUMN IF NOT EXISTS `reviewed` TINYINT(1) NOT NULL DEFAULT 0 COMMENT '是否已评价0-未评价1-已评价)' AFTER `status`;
-- 3. 插入测试数据
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);
-- 4. 更新订单的评价状态
UPDATE `order` SET `reviewed` = 1 WHERE `id` IN (102, 103, 104);
-- 5. 查询验证
SELECT
r.id,
r.order_id,
o.order_no,
r.user_id,
r.teacher_id,
r.overall_score,
r.content,
r.is_anonymous,
r.create_time
FROM review r
LEFT JOIN `order` o ON r.order_id = o.id
ORDER BY r.create_time DESC
LIMIT 10;
-- 6. 统计陪伴员评价数据
SELECT
teacher_id,
COUNT(*) as review_count,
AVG(overall_score) as avg_score,
AVG(service_score) as avg_service_score,
AVG(quality_score) as avg_quality_score,
AVG(punctuality_score) as avg_punctuality_score
FROM review
WHERE deleted = 0 AND status = 1
GROUP BY teacher_id;