573 lines
22 KiB
SQL
573 lines
22 KiB
SQL
-- ===================================================================
|
||
-- 消息系统测试数据生成脚本(简化版)
|
||
-- 适用于运动训练监测与AI评估平台
|
||
-- 教师ID: 7bf7378e-a027-473e-97ac-3460ed3f170a
|
||
-- 学生ID: eed3824b-bba1-4309-8048-19d17367c084
|
||
-- ===================================================================
|
||
|
||
-- 1. 清理现有测试数据
|
||
DELETE FROM ak_message_recipients
|
||
WHERE message_id IN (
|
||
SELECT id FROM ak_messages
|
||
WHERE metadata->>'test_data' = 'true'
|
||
);
|
||
|
||
DELETE FROM ak_messages
|
||
WHERE metadata->>'test_data' = 'true';
|
||
|
||
DELETE FROM ak_message_group_members
|
||
WHERE group_id IN (
|
||
SELECT id FROM ak_message_groups
|
||
WHERE name LIKE '%测试%' OR name LIKE '%训练群%' OR name LIKE '%交流群%'
|
||
);
|
||
|
||
DELETE FROM ak_message_groups
|
||
WHERE name LIKE '%测试%' OR name LIKE '%训练群%' OR name LIKE '%交流群%';
|
||
|
||
-- 2. 创建测试群组
|
||
INSERT INTO ak_message_groups (
|
||
id, name, description, group_type, owner_id, is_public, member_limit
|
||
) VALUES (
|
||
'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||
'高一(1)班运动训练群',
|
||
'高一(1)班运动训练讨论群',
|
||
'class',
|
||
'7bf7378e-a027-473e-97ac-3460ed3f170a',
|
||
false,
|
||
50
|
||
);
|
||
|
||
INSERT INTO ak_message_groups (
|
||
id, name, description, group_type, owner_id, is_public, member_limit
|
||
) VALUES (
|
||
'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
|
||
'师生交流群',
|
||
'教师与学生的日常交流群组',
|
||
'normal',
|
||
'7bf7378e-a027-473e-97ac-3460ed3f170a',
|
||
false,
|
||
100
|
||
);
|
||
|
||
-- 3. 添加群组成员
|
||
INSERT INTO ak_message_group_members (
|
||
group_id, user_id, role, status, nickname, notification_enabled
|
||
) VALUES
|
||
('f47ac10b-58cc-4372-a567-0e02b2c3d479', '7bf7378e-a027-473e-97ac-3460ed3f170a', 'owner', 'active', '李老师', true),
|
||
('f47ac10b-58cc-4372-a567-0e02b2c3d479', 'eed3824b-bba1-4309-8048-19d17367c084', 'member', 'active', '张同学', true),
|
||
('a1b2c3d4-e5f6-7890-abcd-ef1234567890', '7bf7378e-a027-473e-97ac-3460ed3f170a', 'owner', 'active', '李老师', true),
|
||
('a1b2c3d4-e5f6-7890-abcd-ef1234567890', 'eed3824b-bba1-4309-8048-19d17367c084', 'member', 'active', '张同学', true);
|
||
|
||
-- 4. 创建系统欢迎消息
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'system'),
|
||
'system', '系统',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'欢迎使用运动训练平台!',
|
||
'欢迎来到运动训练监测平台!您可以在这里记录训练数据、查看AI分析报告、与老师同学交流。祝您运动愉快!',
|
||
100,
|
||
'{"test_data": "true", "welcome_message": true}',
|
||
now() - INTERVAL '2 days'
|
||
);
|
||
|
||
-- 为系统消息创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '欢迎使用运动训练平台!' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '2 days', now() - INTERVAL '1 day'
|
||
);
|
||
|
||
-- 5. 创建设备连接消息
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, device_data, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'device'),
|
||
'device', gen_random_uuid(), '智能手环Pro',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'设备连接成功',
|
||
'您的智能手环Pro已成功连接,电量85%,可以开始记录运动数据了。',
|
||
80,
|
||
'{"device_name": "智能手环Pro", "battery_level": 85, "firmware_version": "2.1.3"}',
|
||
'{"test_data": "true", "device_type": "wearable"}',
|
||
now() - INTERVAL '1 day'
|
||
);
|
||
|
||
-- 为设备消息创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '设备连接成功' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '1 day', now() - INTERVAL '20 hours'
|
||
);
|
||
|
||
-- 6. 创建设备低电量警告(未读)
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, is_urgent, device_data, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'device'),
|
||
'device', gen_random_uuid(), '智能手环Pro',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'⚠️ 设备电量不足',
|
||
'您的智能手环Pro电量仅剩15%,请及时充电以确保正常使用。',
|
||
85, true,
|
||
'{"device_name": "智能手环Pro", "battery_level": 15, "estimated_time_left": "2小时"}',
|
||
'{"test_data": "true", "alert_type": "low_battery"}',
|
||
now() - INTERVAL '2 hours'
|
||
);
|
||
|
||
-- 为低电量警告创建未读接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '⚠️ 设备电量不足' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'delivered', now() - INTERVAL '2 hours'
|
||
);
|
||
|
||
-- 7. 创建训练完成通知
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'training'),
|
||
'system', '训练助手',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'🎉 训练完成!',
|
||
'恭喜完成跑步训练!用时25分钟,消耗180卡路里,步数3250步,平均心率145 bpm。您的表现很棒,继续保持!',
|
||
70,
|
||
'{"test_data": "true", "activity_type": "running", "duration": 25, "calories": 180, "steps": 3250}',
|
||
now() - INTERVAL '3 hours'
|
||
);
|
||
|
||
-- 为训练消息创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '🎉 训练完成!' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '3 hours', now() - INTERVAL '2 hours'
|
||
);
|
||
|
||
-- 8. 创建教师发布作业通知
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, expires_at, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'assignment'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a', '李老师',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'新作业:每日运动打卡',
|
||
'同学们好!本周的运动作业已发布:每天完成30分钟有氧运动,记录心率和步数数据。截止时间:本周日23:59。请按时完成并上传数据。',
|
||
75,
|
||
'2025-06-22 23:59:00',
|
||
'{"test_data": "true", "assignment_id": "homework_001", "subject": "体育"}',
|
||
now() - INTERVAL '1 day'
|
||
);
|
||
|
||
-- 为作业通知创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '新作业:每日运动打卡' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '1 day', now() - INTERVAL '18 hours'
|
||
);
|
||
|
||
-- 9. 创建成就解锁通知(未读)
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'achievement'),
|
||
'system', '成就系统',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'🏆 恭喜解锁新成就!',
|
||
'恭喜您解锁了"坚持不懈"成就!连续7天完成运动打卡,获得50积分奖励。继续努力,向更高目标前进!',
|
||
50,
|
||
'{"test_data": "true", "achievement_name": "坚持不懈", "points": 50, "days_streak": 7}',
|
||
now() - INTERVAL '6 hours'
|
||
);
|
||
|
||
-- 为成就通知创建未读接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '🏆 恭喜解锁新成就!' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'delivered', now() - INTERVAL '6 hours'
|
||
);
|
||
|
||
-- 10. 创建教师群组消息
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, content,
|
||
priority, conversation_id, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'chat'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a', '李老师',
|
||
'group', 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||
'同学们,下周一我们将进行体能测试,请大家提前做好准备。测试项目包括:50米跑、立定跳远、引体向上。有问题随时问我。',
|
||
90, 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||
'{"test_data": "true", "group_message": true}',
|
||
now() - INTERVAL '4 hours'
|
||
);
|
||
|
||
-- 为群组消息创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES
|
||
((SELECT id FROM ak_messages WHERE content LIKE '%体能测试%' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '4 hours', now() - INTERVAL '3 hours'),
|
||
((SELECT id FROM ak_messages WHERE content LIKE '%体能测试%' AND metadata->>'test_data' = 'true'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a',
|
||
'read', now() - INTERVAL '4 hours', now() - INTERVAL '4 hours');
|
||
|
||
-- 11. 创建学生回复群组消息
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, content,
|
||
priority, conversation_id, parent_message_id, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'chat'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084', '张同学',
|
||
'group', 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||
'好的老师,我会认真准备的!请问引体向上有什么技巧吗?',
|
||
90, 'f47ac10b-58cc-4372-a567-0e02b2c3d479',
|
||
(SELECT id FROM ak_messages WHERE content LIKE '%体能测试%' AND metadata->>'test_data' = 'true'),
|
||
'{"test_data": "true", "group_message": true, "reply": true}',
|
||
now() - INTERVAL '3 hours'
|
||
);
|
||
|
||
-- 为学生回复创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES
|
||
((SELECT id FROM ak_messages WHERE content LIKE '%引体向上有什么技巧%' AND metadata->>'test_data' = 'true'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a',
|
||
'read', now() - INTERVAL '3 hours', now() - INTERVAL '2 hours'),
|
||
((SELECT id FROM ak_messages WHERE content LIKE '%引体向上有什么技巧%' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '3 hours', now() - INTERVAL '3 hours');
|
||
|
||
-- 12. 创建教师私信学生(未读)
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, content,
|
||
priority, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'chat'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a', '李老师',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'张同学,我看到你最近的运动数据很不错,心率控制得很好。继续保持这个节奏,相信你的体能会有很大提升。',
|
||
90,
|
||
'{"test_data": "true", "private_message": true}',
|
||
now() - INTERVAL '1 hour'
|
||
);
|
||
|
||
-- 为私信创建未读接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE content LIKE '%运动数据很不错%' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'delivered', now() - INTERVAL '1 hour'
|
||
);
|
||
|
||
-- 13. 创建系统公告
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_name,
|
||
receiver_type, title, content,
|
||
priority, is_broadcast, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'announcement'),
|
||
'system', '运动平台',
|
||
'broadcast',
|
||
'平台升级通知',
|
||
'各位用户,运动训练平台将于本周六凌晨2:00-4:00进行系统升级,届时将暂停服务。升级后将新增AI运动分析功能,敬请期待!',
|
||
95, true,
|
||
'{"test_data": "true", "maintenance": true, "upgrade": true}',
|
||
now() - INTERVAL '12 hours'
|
||
);
|
||
|
||
-- 为公告创建接收记录(广播消息)
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES
|
||
((SELECT id FROM ak_messages WHERE title = '平台升级通知' AND metadata->>'test_data' = 'true'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a',
|
||
'read', now() - INTERVAL '12 hours', now() - INTERVAL '11 hours'),
|
||
((SELECT id FROM ak_messages WHERE title = '平台升级通知' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '12 hours', now() - INTERVAL '10 hours');
|
||
|
||
-- 14. 创建运动提醒(未读)
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, scheduled_at, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'reminder'),
|
||
'system', '运动助手',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'⏰ 运动时间到了!',
|
||
'根据您的运动计划,现在是跑步时间。建议运动30分钟,注意热身和拉伸。今天天气不错,适合户外运动!',
|
||
65,
|
||
now() - INTERVAL '30 minutes',
|
||
'{"test_data": "true", "reminder_type": "exercise", "activity": "running"}',
|
||
now() - INTERVAL '30 minutes'
|
||
);
|
||
|
||
-- 为提醒消息创建未读接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '⏰ 运动时间到了!' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'delivered', now() - INTERVAL '30 minutes'
|
||
);
|
||
|
||
-- 15. 创建心率异常警报
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, is_urgent, device_data, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'alert'),
|
||
'device', gen_random_uuid(), '智能手环Pro',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'⚠️ 心率异常警报',
|
||
'检测到您的心率持续偏高(185 bpm),已超过安全范围。建议立即停止运动,休息并观察。如有不适请及时就医。',
|
||
99, true,
|
||
'{"device_name": "智能手环Pro", "heart_rate": 185, "safe_range": "60-180"}',
|
||
'{"test_data": "true", "alert_type": "heart_rate", "severity": "high"}',
|
||
now() - INTERVAL '10 minutes'
|
||
);
|
||
|
||
-- 为心率警报创建已读接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '⚠️ 心率异常警报' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '10 minutes', now() - INTERVAL '8 minutes'
|
||
);
|
||
|
||
-- 16. 创建社交点赞消息
|
||
INSERT INTO ak_messages (
|
||
id, message_type_id, sender_type, sender_id, sender_name,
|
||
receiver_type, receiver_id, title, content,
|
||
priority, metadata, created_at
|
||
) VALUES (
|
||
gen_random_uuid(),
|
||
(SELECT id FROM ak_message_types WHERE code = 'social'),
|
||
'user', '7bf7378e-a027-473e-97ac-3460ed3f170a', '李老师',
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'👍 老师为您点赞了',
|
||
'李老师为您的运动记录"今日跑步5公里"点赞了,并评论:"坚持得很好,继续加油!"',
|
||
60,
|
||
'{"test_data": "true", "social_action": "like", "post_id": "post_001"}',
|
||
now() - INTERVAL '5 hours'
|
||
);
|
||
|
||
-- 为社交消息创建接收记录
|
||
INSERT INTO ak_message_recipients (
|
||
message_id, recipient_type, recipient_id, status, delivered_at, read_at
|
||
) VALUES (
|
||
(SELECT id FROM ak_messages WHERE title = '👍 老师为您点赞了' AND metadata->>'test_data' = 'true'),
|
||
'user', 'eed3824b-bba1-4309-8048-19d17367c084',
|
||
'read', now() - INTERVAL '5 hours', now() - INTERVAL '4 hours'
|
||
);
|
||
|
||
-- 17. 更新消息统计
|
||
UPDATE ak_messages
|
||
SET
|
||
total_recipients = (
|
||
SELECT COUNT(*)
|
||
FROM ak_message_recipients mr
|
||
WHERE mr.message_id = ak_messages.id
|
||
),
|
||
delivered_count = (
|
||
SELECT COUNT(*)
|
||
FROM ak_message_recipients mr
|
||
WHERE mr.message_id = ak_messages.id
|
||
AND mr.status IN ('delivered', 'read')
|
||
),
|
||
read_count = (
|
||
SELECT COUNT(*)
|
||
FROM ak_message_recipients mr
|
||
WHERE mr.message_id = ak_messages.id
|
||
AND mr.status = 'read'
|
||
)
|
||
WHERE metadata->>'test_data' = 'true';
|
||
|
||
-- 18. 输出测试数据统计
|
||
SELECT
|
||
'测试数据统计' as info,
|
||
(SELECT COUNT(*) FROM ak_messages WHERE metadata->>'test_data' = 'true') as total_messages,
|
||
(SELECT COUNT(*) FROM ak_message_recipients mr
|
||
JOIN ak_messages m ON mr.message_id = m.id
|
||
WHERE m.metadata->>'test_data' = 'true') as total_recipients,
|
||
(SELECT COUNT(*) FROM ak_message_recipients mr
|
||
JOIN ak_messages m ON mr.message_id = m.id
|
||
WHERE m.metadata->>'test_data' = 'true'
|
||
AND mr.recipient_id = 'eed3824b-bba1-4309-8048-19d17367c084'
|
||
AND mr.status != 'read') as student_unread_count;
|
||
|
||
-- ===================================================================
|
||
-- 常用测试查询示例
|
||
-- ===================================================================
|
||
|
||
-- 查询学生的所有消息
|
||
/*
|
||
SELECT
|
||
m.title,
|
||
m.content,
|
||
mt.name as message_type,
|
||
m.sender_name,
|
||
mr.status,
|
||
m.created_at,
|
||
m.is_urgent
|
||
FROM ak_messages m
|
||
JOIN ak_message_types mt ON m.message_type_id = mt.id
|
||
JOIN ak_message_recipients mr ON m.id = mr.message_id
|
||
WHERE mr.recipient_id = 'eed3824b-bba1-4309-8048-19d17367c084'
|
||
AND m.metadata->>'test_data' = 'true'
|
||
ORDER BY m.created_at DESC;
|
||
*/
|
||
|
||
-- 查询学生未读消息
|
||
/*
|
||
SELECT
|
||
m.title,
|
||
m.content,
|
||
mt.name as message_type,
|
||
m.sender_name,
|
||
m.created_at,
|
||
m.is_urgent
|
||
FROM ak_messages m
|
||
JOIN ak_message_types mt ON m.message_type_id = mt.id
|
||
JOIN ak_message_recipients mr ON m.id = mr.message_id
|
||
WHERE mr.recipient_id = 'eed3824b-bba1-4309-8048-19d17367c084'
|
||
AND mr.status != 'read'
|
||
AND m.metadata->>'test_data' = 'true'
|
||
ORDER BY m.priority DESC, m.created_at DESC;
|
||
*/
|
||
|
||
-- 查询教师发送的消息
|
||
/*
|
||
SELECT
|
||
m.title,
|
||
m.content,
|
||
mt.name as message_type,
|
||
m.receiver_type,
|
||
m.created_at,
|
||
m.total_recipients,
|
||
m.read_count
|
||
FROM ak_messages m
|
||
JOIN ak_message_types mt ON m.message_type_id = mt.id
|
||
WHERE m.sender_id = '7bf7378e-a027-473e-97ac-3460ed3f170a'
|
||
AND m.metadata->>'test_data' = 'true'
|
||
ORDER BY m.created_at DESC;
|
||
*/
|
||
|
||
-- 查询群组消息
|
||
/*
|
||
SELECT
|
||
m.content,
|
||
m.sender_name,
|
||
mg.name as group_name,
|
||
m.created_at,
|
||
CASE WHEN m.parent_message_id IS NOT NULL THEN '回复' ELSE '原始消息' END as message_type
|
||
FROM ak_messages m
|
||
JOIN ak_message_groups mg ON m.receiver_id = mg.id
|
||
WHERE m.receiver_type = 'group'
|
||
AND m.metadata->>'test_data' = 'true'
|
||
ORDER BY m.created_at DESC;
|
||
*/
|
||
|
||
-- 查询设备消息
|
||
/*
|
||
SELECT
|
||
m.title,
|
||
m.content,
|
||
m.device_data,
|
||
m.is_urgent,
|
||
mr.status,
|
||
m.created_at
|
||
FROM ak_messages m
|
||
JOIN ak_message_recipients mr ON m.id = mr.message_id
|
||
WHERE m.sender_type = 'device'
|
||
AND mr.recipient_id = 'eed3824b-bba1-4309-8048-19d17367c084'
|
||
AND m.metadata->>'test_data' = 'true'
|
||
ORDER BY m.created_at DESC;
|
||
*/
|
||
|
||
-- 查询未读消息统计
|
||
/*
|
||
SELECT
|
||
mt.name as message_type,
|
||
COUNT(*) as unread_count
|
||
FROM ak_message_recipients mr
|
||
JOIN ak_messages m ON mr.message_id = m.id
|
||
JOIN ak_message_types mt ON m.message_type_id = mt.id
|
||
WHERE mr.recipient_id = 'eed3824b-bba1-4309-8048-19d17367c084'
|
||
AND mr.status != 'read'
|
||
AND m.metadata->>'test_data' = 'true'
|
||
GROUP BY mt.name, mt.priority
|
||
ORDER BY mt.priority DESC;
|
||
*/
|
||
|
||
-- 标记消息为已读的示例
|
||
/*
|
||
UPDATE ak_message_recipients
|
||
SET status = 'read', read_at = now()
|
||
WHERE recipient_id = 'eed3824b-bba1-4309-8048-19d17367c084'
|
||
AND message_id = '消息ID'
|
||
AND status != 'read';
|
||
*/
|
||
|
||
SELECT '====================================================================' as separator
|
||
UNION ALL
|
||
SELECT '测试数据生成完成!' as info
|
||
UNION ALL
|
||
SELECT '教师ID: 7bf7378e-a027-473e-97ac-3460ed3f170a' as info
|
||
UNION ALL
|
||
SELECT '学生ID: eed3824b-bba1-4309-8048-19d17367c084' as info
|
||
UNION ALL
|
||
SELECT '班级群组ID: f47ac10b-58cc-4372-a567-0e02b2c3d479' as info
|
||
UNION ALL
|
||
SELECT '师生群组ID: a1b2c3d4-e5f6-7890-abcd-ef1234567890' as info
|
||
UNION ALL
|
||
SELECT '已创建各类型测试消息,包含已读和未读状态' as info
|
||
UNION ALL
|
||
SELECT '可使用注释中的查询语句测试各种消息场景' as info
|
||
UNION ALL
|
||
SELECT '====================================================================' as separator;
|