This commit is contained in:
2026-02-03 17:26:23 +08:00
532 changed files with 97923 additions and 20698 deletions

View File

@@ -0,0 +1,114 @@
-- =====================================================================================
-- 批量生成商品测试数据 (基于现有的店铺)
-- 说明:为 ml_shops 表中的每个店铺生成 5 个测试商品
-- 前提ml_shops 表中已有数据
-- =====================================================================================
DO $$
DECLARE
shop_rec RECORD;
cat_id UUID;
i INTEGER;
new_product_id UUID;
v_total_shops INTEGER := 0;
v_total_products INTEGER := 0;
BEGIN
-- 1. 获取一个可用的分类ID (如果没有则创建一个)
SELECT id INTO cat_id FROM public.ml_categories LIMIT 1;
IF cat_id IS NULL THEN
RAISE NOTICE '未找到分类,正在创建默认分类...';
INSERT INTO public.ml_categories (name, slug, level, path)
VALUES ('测试分类', 'test-category', 1, ARRAY['测试分类'])
RETURNING id INTO cat_id;
END IF;
-- 2. 统计现有店铺数量
SELECT COUNT(*) INTO v_total_shops FROM public.ml_shops;
RAISE NOTICE '发现 % 个店铺,准备生成商品...', v_total_shops;
-- 3. 遍历所有店铺
FOR shop_rec IN SELECT merchant_id, shop_name FROM public.ml_shops LOOP
-- 为每个店铺生成 5 个商品
FOR i IN 1..5 LOOP
-- 插入商品主表
INSERT INTO public.ml_products (
merchant_id,
category_id,
product_code,
name,
subtitle,
description,
main_image_url,
image_urls,
base_price,
market_price,
cost_price,
total_stock,
available_stock,
status, -- 1:上架
is_new,
is_hot,
sale_count, -- 初始销量
rating_avg, -- 初始评分
created_at,
updated_at
) VALUES (
shop_rec.merchant_id,
cat_id,
-- 生成唯一的商品编码: PROD + 商家ID前8位 + 序号 + 随机数
'PROD-' || substring(shop_rec.merchant_id::text, 1, 8) || '-' || i || '-' || floor(random() * 10000)::text,
shop_rec.shop_name || ' - 甄选商品 ' || i,
'测试商品副标题 ' || i || ' | 正品保证 | 极速发货',
'<h3>商品详情</h3><p>这是 ' || shop_rec.shop_name || ' 的第 ' || i || ' 款测试商品。</p><p>商品特点:优质材料,精湛工艺,性价比高。</p>',
'/static/images/product/p' || (floor(random() * 4) + 1)::int || '.jpg', -- 随机图片 p1.jpg - p5.jpg
('["/static/images/product/p1.jpg", "/static/images/product/p2.jpg", "/static/images/product/p3.jpg"]')::jsonb,
(floor(random() * 500) + 50)::decimal(10,2), -- 价格 50 - 550
(floor(random() * 200) + 600)::decimal(10,2), -- 市场价
(floor(random() * 40) + 10)::decimal(10,2), -- 成本价
999, -- 总库存
999, -- 可用库存
1, -- 状态:上架
(random() > 0.5), -- 是否新品
(random() > 0.7), -- 是否热销
(floor(random() * 1000))::int, -- 随机初始销量
(4.0 + random())::decimal(3,1), -- 随机评分 4.0 - 5.0
NOW(),
NOW()
) RETURNING id INTO new_product_id;
-- 插入 SKU (默认生成一个 SKU)
INSERT INTO public.ml_product_skus (
product_id,
sku_code,
specifications,
price,
market_price,
stock,
status
) VALUES (
new_product_id,
'SKU-' || substring(new_product_id::text, 1, 8),
'{"默认": "标准规格"}'::jsonb,
(floor(random() * 500) + 50)::decimal(10,2),
(floor(random() * 200) + 600)::decimal(10,2),
999,
1
);
v_total_products := v_total_products + 1;
END LOOP;
-- 更新店铺的商品统计数量
UPDATE public.ml_shops
SET product_count = product_count + 5,
updated_at = NOW()
WHERE merchant_id = shop_rec.merchant_id;
END LOOP;
RAISE NOTICE '生成完成!共为 % 个店铺生成了 % 个商品。', v_total_shops, v_total_products;
END $$;