admin模块接入数据库
This commit is contained in:
46
docs/sql/10_schema/product/ak_product_labels_v1.sql
Normal file
46
docs/sql/10_schema/product/ak_product_labels_v1.sql
Normal file
@@ -0,0 +1,46 @@
|
||||
-- 1. 商品标签分组表
|
||||
CREATE TABLE IF NOT EXISTS public.ak_product_label_groups (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
-- 2. 商品标签表
|
||||
CREATE TABLE IF NOT EXISTS public.ak_product_labels (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
group_id UUID REFERENCES public.ak_product_label_groups(id) ON DELETE SET NULL,
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
show_in_mobile BOOLEAN DEFAULT true,
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
-- 3. 启用 RLS
|
||||
ALTER TABLE public.ak_product_label_groups ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE public.ak_product_labels ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- 4. 创建权限策略 (按 merchant_id 隔离)
|
||||
-- 分组策略
|
||||
CREATE POLICY "Users can manage their own label groups"
|
||||
ON public.ak_product_label_groups FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
|
||||
-- 标签策略
|
||||
CREATE POLICY "Users can manage their own labels"
|
||||
ON public.ak_product_labels FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
|
||||
-- 5. 索引
|
||||
CREATE INDEX IF NOT EXISTS idx_label_groups_merchant ON public.ak_product_label_groups(merchant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_labels_group ON public.ak_product_labels(group_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_labels_merchant ON public.ak_product_labels(merchant_id);
|
||||
47
docs/sql/10_schema/product/ak_product_member_prices_v1.sql
Normal file
47
docs/sql/10_schema/product/ak_product_member_prices_v1.sql
Normal file
@@ -0,0 +1,47 @@
|
||||
-- =====================================================================================
|
||||
-- Schema: 商品会员价表 (按 SKU + 等级 维度)
|
||||
-- 位置:docs/sql/10_schema/product/ak_product_member_prices_v1.sql
|
||||
-- 对象类型:Schema (DDL)
|
||||
-- 版本:v1
|
||||
-- 说明:记录特定商品 SKU 在不同会员等级下的专享价格,按商家隔离。
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.ak_product_member_prices (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
product_id UUID NOT NULL REFERENCES public.ml_products(id) ON DELETE CASCADE,
|
||||
sku_id UUID NOT NULL REFERENCES public.ml_product_skus(id) ON DELETE CASCADE,
|
||||
level_id UUID NOT NULL REFERENCES public.ak_user_levels(id) ON DELETE CASCADE,
|
||||
|
||||
member_price DECIMAL(12,2) NOT NULL CHECK (member_price >= 0),
|
||||
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
|
||||
-- 约束:同一个商家的同一个 SKU 在同一个等级下只能有一个会员价
|
||||
UNIQUE(merchant_id, sku_id, level_id)
|
||||
);
|
||||
|
||||
-- 索引
|
||||
CREATE INDEX IF NOT EXISTS idx_prod_member_prices_product ON public.ak_product_member_prices(product_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_prod_member_prices_sku ON public.ak_product_member_prices(sku_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_prod_member_prices_level ON public.ak_product_member_prices(level_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_prod_member_prices_merchant ON public.ak_product_member_prices(merchant_id);
|
||||
|
||||
-- 启用 RLS
|
||||
ALTER TABLE public.ak_product_member_prices ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- 权限策略:商家仅能管理自己的商品会员价
|
||||
CREATE POLICY "Merchants can manage their own product member prices"
|
||||
ON public.ak_product_member_prices
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
|
||||
-- 允许所有认证用户查看会员价(前台下单需计算)
|
||||
CREATE POLICY "Authenticated users can view product member prices"
|
||||
ON public.ak_product_member_prices
|
||||
FOR SELECT
|
||||
TO authenticated
|
||||
USING (true);
|
||||
24
docs/sql/10_schema/product/ak_product_protections_v1.sql
Normal file
24
docs/sql/10_schema/product/ak_product_protections_v1.sql
Normal file
@@ -0,0 +1,24 @@
|
||||
-- 商品保障/服务条款(按 merchant_id 隔离)
|
||||
CREATE TABLE IF NOT EXISTS public.ak_product_protections (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
icon_url TEXT,
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_ak_product_protections_merchant ON public.ak_product_protections(merchant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_ak_product_protections_active ON public.ak_product_protections(is_active);
|
||||
|
||||
ALTER TABLE public.ak_product_protections ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
CREATE POLICY "Users can manage their own product protections"
|
||||
ON public.ak_product_protections
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
49
docs/sql/10_schema/product/ak_product_templates_v1.sql
Normal file
49
docs/sql/10_schema/product/ak_product_templates_v1.sql
Normal file
@@ -0,0 +1,49 @@
|
||||
-- 商品规格模板表 + 商品参数模板表(按 merchant_id 隔离)
|
||||
|
||||
-- 1) 商品规格模板表
|
||||
CREATE TABLE IF NOT EXISTS public.ak_product_spec_templates (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
specs TEXT NOT NULL DEFAULT '',
|
||||
attrs TEXT NOT NULL DEFAULT '',
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_spec_templates_merchant ON public.ak_product_spec_templates(merchant_id);
|
||||
|
||||
ALTER TABLE public.ak_product_spec_templates ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
CREATE POLICY "Users can manage their own spec templates"
|
||||
ON public.ak_product_spec_templates
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
|
||||
|
||||
-- 2) 商品参数模板表
|
||||
CREATE TABLE IF NOT EXISTS public.ak_product_param_templates (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
sort_order INTEGER DEFAULT 0,
|
||||
params JSONB NOT NULL DEFAULT '[]'::jsonb,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_param_templates_merchant ON public.ak_product_param_templates(merchant_id);
|
||||
|
||||
ALTER TABLE public.ak_product_param_templates ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
CREATE POLICY "Users can manage their own param templates"
|
||||
ON public.ak_product_param_templates
|
||||
FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
Reference in New Issue
Block a user