Files
medical-mall/doc_mall/consumer/sql/add_messages_and_notifications.sql
2026-02-03 17:26:23 +08:00

99 lines
4.2 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.
-- =====================================================================================
-- 补充消息 notification 和 客服 chat 相关表结构及测试数据
-- 对应用户 ID: b653fded-7d5e-4950-aa0d-725595543e3c (test@mall.com)
-- =====================================================================================
-- 1. 创建通知/消息表 (ml_notifications)
-- 用于存储: 系统通知、优惠活动、订单通知等
CREATE TABLE IF NOT EXISTS public.ml_notifications (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
type VARCHAR(50) NOT NULL, -- 'system'(系统通知), 'promotion'(优惠活动), 'order'(订单通知)
title VARCHAR(200) NOT NULL,
content TEXT,
icon_url TEXT, -- 图标/图片地址
link_url TEXT, -- 点击跳转链接
is_read BOOLEAN DEFAULT FALSE,
extra_data JSONB DEFAULT '{}', -- 扩展数据
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
COMMENT ON TABLE public.ml_notifications IS '系统通知与活动消息表';
-- 开启 RLS
ALTER TABLE public.ml_notifications ENABLE ROW LEVEL SECURITY;
-- 创建 RLS 策略 (用户只能查自己的通知)
CREATE POLICY ml_notifications_select_policy ON public.ml_notifications
FOR SELECT USING (
auth.uid() = (SELECT auth_id FROM public.ak_users WHERE id = user_id)
);
-- 2. 创建客服会话/消息表 (ml_chat_messages)
-- 用于存储: 用户与客服的聊天记录
CREATE TABLE IF NOT EXISTS public.ml_chat_messages (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
session_id UUID DEFAULT uuid_generate_v4(), -- 会话ID可用于分组
sender_id UUID REFERENCES public.ak_users(id), -- 发送者ID (NULL代表系统/自动回复)
receiver_id UUID REFERENCES public.ak_users(id), -- 接收者ID
content TEXT,
msg_type VARCHAR(20) DEFAULT 'text', -- 'text', 'image', 'product', 'order'
is_read BOOLEAN DEFAULT FALSE,
is_from_user BOOLEAN DEFAULT FALSE, -- 方便前端判断方向 (true: 用户发给客服, false: 客服发给用户)
extra_data JSONB DEFAULT '{}', -- 用于存储商品卡片信息等
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
COMMENT ON TABLE public.ml_chat_messages IS '客服聊天记录表';
-- 开启 RLS
ALTER TABLE public.ml_chat_messages ENABLE ROW LEVEL SECURITY;
-- 创建 RLS 策略 (用户只能查属于自己的消息)
CREATE POLICY ml_chat_messages_select_policy ON public.ml_chat_messages
FOR SELECT USING (
auth.uid() IN (
SELECT auth_id FROM public.ak_users WHERE id IN (sender_id, receiver_id)
)
);
-- =====================================================================================
-- 3. 插入测试数据
-- 目标用户: b653fded-7d5e-4950-aa0d-725595543e3c
-- =====================================================================================
-- 3.1 插入一条“系统通知”
INSERT INTO public.ml_notifications (user_id, type, title, content, icon_url, created_at)
VALUES (
'b653fded-7d5e-4950-aa0d-725595543e3c',
'system',
'系统安全升级通知',
'尊敬的用户,为了保障您的账户安全,我们已完成系统安全组件升级。建议您定期修改登录密码。',
'/static/icons/system-notice.png',
NOW() - INTERVAL '1 day' -- 昨天收到的
);
-- 3.2 插入一条“优惠活动”
INSERT INTO public.ml_notifications (user_id, type, title, content, icon_url, link_url, created_at)
VALUES (
'b653fded-7d5e-4950-aa0d-725595543e3c',
'promotion',
'夏日清凉大促开启',
'全场商品5折起清凉一夏好物带回家。点击立即参与抢购限量神券等你来拿',
'/static/icons/promotion.png',
'/pages/mall/activity/summer_sale',
NOW()
);
-- 3.3 插入一条“客服消息” (模仿客服发给用户的欢迎语)
INSERT INTO public.ml_chat_messages (sender_id, receiver_id, content, msg_type, is_from_user, created_at)
VALUES (
NULL, -- NULL表示系统客服
'b653fded-7d5e-4950-aa0d-725595543e3c',
'您好欢迎光临商城优选。请问有什么可以帮您的吗我们全天24小时为您服务。',
'text',
FALSE, -- 客服发的
NOW()
);