Files
medical-mall/doc_mall/consumer/sql/06_setup_coupons.sql
2026-02-04 17:35:46 +08:00

132 lines
4.1 KiB
SQL
Raw Permalink 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.
-- 1. 创建优惠券模板数据 (Templates)
-- 修正版:适配实际表结构 ml_coupon_templates(coupon_type, discount_type, discount_value...)
DO $$
DECLARE
v_merchant_id UUID;
v_shop_id UUID;
v_product_id UUID;
v_template_shop_id UUID;
v_template_product_id UUID;
v_template_platform_id UUID;
v_user_id UUID := 'b653fded-7d5e-4950-aa0d-725595543e3c'; -- 默认测试用户ID
BEGIN
-- 获取一个商户和店铺
SELECT id, merchant_id INTO v_shop_id, v_merchant_id FROM public.ml_shops LIMIT 1;
-- 获取一个商品
SELECT id INTO v_product_id FROM public.ml_products WHERE merchant_id = v_merchant_id LIMIT 1;
-- A. 创建店铺满减券模板 (满100减10)
-- coupon_type: 1(满减), discount_type: 1(固定金额), discount_value: 10
INSERT INTO public.ml_coupon_templates (
merchant_id,
name,
coupon_type,
discount_type,
discount_value,
min_order_amount,
total_quantity,
start_time,
end_time,
status,
applicable_products
) VALUES (
v_merchant_id,
'店铺新人礼 - 满100减10',
1, -- 满减
1, -- 固定金额
10.00, -- 减10元
100.00, -- 满100
9999,
now(),
now() + interval '1 year',
1,
'[]'::jsonb
) RETURNING id INTO v_template_shop_id;
-- B. 创建商品专属折扣券模板 (无门槛9折)
-- coupon_type: 2(折扣), discount_type: 2(百分比), discount_value: 0.9 (代表9折消费端需适配逻辑)
INSERT INTO public.ml_coupon_templates (
merchant_id,
name,
coupon_type,
discount_type,
discount_value,
min_order_amount,
total_quantity,
start_time,
end_time,
status,
applicable_products
) VALUES (
v_merchant_id,
'爆品专属9折券',
2, -- 折扣
2, -- 百分比
0.90, -- 9折
0, -- 无门槛
9999,
now(),
now() + interval '1 year',
1,
jsonb_build_array(v_product_id) -- 指定商品ID
) RETURNING id INTO v_template_product_id;
-- C. 创建平台通用红包模板 (无门槛5元)
-- merchant_id: NULL, coupon_type: 1(满减/直减), discount_type: 1(固定), discount_value: 5
INSERT INTO public.ml_coupon_templates (
merchant_id,
name,
coupon_type,
discount_type,
discount_value,
min_order_amount,
total_quantity,
start_time,
end_time,
status,
applicable_products
) VALUES (
NULL,
'新人注册红包',
1,
1,
5.00,
0,
9999,
now(),
now() + interval '1 year',
1,
'[]'::jsonb
) RETURNING id INTO v_template_platform_id;
RAISE NOTICE 'Created Templates: Shop=%, Product=%, Platform=%', v_template_shop_id, v_template_product_id, v_template_platform_id;
-- 2. 为测试直接“领取”几张优惠券 (Populate User Coupons)
-- 领取店铺券 (未使用)
INSERT INTO public.ml_user_coupons (
user_id, template_id, coupon_code, status, received_at, expire_at
) VALUES (
v_user_id, v_template_shop_id, 'SH' || substring(md5(clock_timestamp()::text) from 1 for 8), 1, now(), now() + interval '7 days'
);
-- 领取红包 (未使用)
INSERT INTO public.ml_user_coupons (
user_id, template_id, coupon_code, status, received_at, expire_at
) VALUES (
v_user_id, v_template_platform_id, 'HB' || substring(md5(clock_timestamp()::text) from 1 for 8), 1, now(), now() + interval '30 days'
);
-- 领取一张已过期的券 (用于测试展示)
INSERT INTO public.ml_user_coupons (
user_id, template_id, coupon_code, status, received_at, expire_at
) VALUES (
v_user_id, v_template_shop_id, 'EX' || substring(md5(clock_timestamp()::text) from 1 for 8), 3, now() - interval '10 days', now() - interval '3 days'
);
RAISE NOTICE 'Successfully inserted test coupons for user %', v_user_id;
END $$;