-- 为用户 b653fded-7d5e-4950-aa0d-725595543e3c 添加2张优惠券 -- 基于 ml_coupon_templates 表中有效的模板 INSERT INTO public.ml_user_coupons ( user_id, template_id, coupon_code, status, received_at, expire_at ) SELECT 'b653fded-7d5e-4950-aa0d-725595543e3c'::uuid, -- 目标用户ID id, -- 生成随机优惠券码 (CT + 随机字符) 'CT' || upper(substring(md5(random()::text || clock_timestamp()::text) from 1 for 10)), 1, -- 状态: 1 (未使用) now(), end_time -- 使用模板的截止时间作为过期时间 FROM public.ml_coupon_templates WHERE status = 1 -- 仅选择状态正常的模板 LIMIT 2;