144 lines
4.1 KiB
PL/PgSQL
144 lines
4.1 KiB
PL/PgSQL
-- =====================================================
|
||
-- 分享免单通知消息 - 数据库执行语句
|
||
-- =====================================================
|
||
|
||
-- 1. 添加分享免单相关的通知类型说明
|
||
-- ml_notifications 表的 type 字段可包含以下类型:
|
||
-- 'system' - 系统通知
|
||
-- 'promotion' - 优惠活动
|
||
-- 'order' - 订单通知
|
||
-- 'share_free' - 分享免单通知 (新增)
|
||
|
||
-- 2. 创建分享免单通知的函数
|
||
-- 当分享免单成功时,自动发送通知给用户
|
||
CREATE OR REPLACE FUNCTION notify_share_free_success(
|
||
p_user_id UUID,
|
||
p_share_record_id UUID,
|
||
p_product_name VARCHAR,
|
||
p_reward_amount DECIMAL(10,2),
|
||
p_share_code VARCHAR
|
||
)
|
||
RETURNS VOID AS $$
|
||
BEGIN
|
||
INSERT INTO ml_notifications (
|
||
user_id,
|
||
type,
|
||
title,
|
||
content,
|
||
icon_url,
|
||
link_url,
|
||
extra_data,
|
||
created_at
|
||
) VALUES (
|
||
p_user_id,
|
||
'share_free',
|
||
'恭喜!您已获得免单奖励',
|
||
'您分享的商品【' || p_product_name || '】已有4位好友购买,获得免单奖励 ¥' || p_reward_amount || '!分享码:' || p_share_code,
|
||
'/static/icons/gift.png',
|
||
'/pages/mall/consumer/share/detail?id=' || p_share_record_id,
|
||
jsonb_build_object(
|
||
'share_record_id', p_share_record_id,
|
||
'product_name', p_product_name,
|
||
'reward_amount', p_reward_amount,
|
||
'share_code', p_share_code
|
||
),
|
||
NOW()
|
||
);
|
||
END;
|
||
$$ LANGUAGE plpgsql;
|
||
|
||
-- 3. 创建二级购买进度通知函数
|
||
-- 当有人通过分享码购买时,发送进度通知
|
||
CREATE OR REPLACE FUNCTION notify_share_purchase_progress(
|
||
p_user_id UUID,
|
||
p_share_record_id UUID,
|
||
p_product_name VARCHAR,
|
||
p_current_count INT,
|
||
p_required_count INT,
|
||
p_share_code VARCHAR
|
||
)
|
||
RETURNS VOID AS $$
|
||
BEGIN
|
||
INSERT INTO ml_notifications (
|
||
user_id,
|
||
type,
|
||
title,
|
||
content,
|
||
icon_url,
|
||
link_url,
|
||
extra_data,
|
||
created_at
|
||
) VALUES (
|
||
p_user_id,
|
||
'share_free',
|
||
'分享免单进度更新',
|
||
'您分享的商品【' || p_product_name || '】已有 ' || p_current_count || '/' || p_required_count || ' 人购买,再邀请 ' || (p_required_count - p_current_count) || ' 人即可免单!',
|
||
'/static/icons/share.png',
|
||
'/pages/mall/consumer/share/detail?id=' || p_share_record_id,
|
||
jsonb_build_object(
|
||
'share_record_id', p_share_record_id,
|
||
'product_name', p_product_name,
|
||
'current_count', p_current_count,
|
||
'required_count', p_required_count,
|
||
'share_code', p_share_code
|
||
),
|
||
NOW()
|
||
);
|
||
END;
|
||
$$ LANGUAGE plpgsql;
|
||
|
||
-- 4. 创建分享创建成功通知函数
|
||
-- 当用户创建分享时,发送确认通知
|
||
CREATE OR REPLACE FUNCTION notify_share_created(
|
||
p_user_id UUID,
|
||
p_share_record_id UUID,
|
||
p_product_name VARCHAR,
|
||
p_share_code VARCHAR
|
||
)
|
||
RETURNS VOID AS $$
|
||
BEGIN
|
||
INSERT INTO ml_notifications (
|
||
user_id,
|
||
type,
|
||
title,
|
||
content,
|
||
icon_url,
|
||
link_url,
|
||
extra_data,
|
||
created_at
|
||
) VALUES (
|
||
p_user_id,
|
||
'share_free',
|
||
'分享创建成功',
|
||
'您已成功分享商品【' || p_product_name || '】,分享码:' || p_share_code || '。邀请4位好友购买即可免单!',
|
||
'/static/icons/share-success.png',
|
||
'/pages/mall/consumer/share/detail?id=' || p_share_record_id,
|
||
jsonb_build_object(
|
||
'share_record_id', p_share_record_id,
|
||
'product_name', p_product_name,
|
||
'share_code', p_share_code
|
||
),
|
||
NOW()
|
||
);
|
||
END;
|
||
$$ LANGUAGE plpgsql;
|
||
|
||
-- 5. 示例:手动插入测试通知(可选)
|
||
-- 替换 'YOUR_USER_ID' 为实际用户ID
|
||
-- INSERT INTO ml_notifications (user_id, type, title, content, icon_url, link_url, extra_data, created_at)
|
||
-- VALUES (
|
||
-- 'YOUR_USER_ID',
|
||
-- 'share_free',
|
||
-- '恭喜!您已获得免单奖励',
|
||
-- '您分享的商品【测试商品】已有4位好友购买,获得免单奖励 ¥99.00!分享码:ABCD1234',
|
||
-- '/static/icons/gift.png',
|
||
-- '/pages/mall/consumer/share/detail?id=test-share-id',
|
||
-- '{"share_record_id": "test-share-id", "product_name": "测试商品", "reward_amount": 99.00, "share_code": "ABCD1234"}',
|
||
-- NOW()
|
||
-- );
|
||
|
||
-- 6. 查询用户分享免单相关通知
|
||
-- SELECT * FROM ml_notifications
|
||
-- WHERE type = 'share_free'
|
||
-- ORDER BY created_at DESC;
|