admin接入数据库
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
-- =====================================================================================
|
||||
-- Schema: 分销代理商申请表
|
||||
-- 位置:docs/sql/10_schema/distribution/ak_distribution_agent_applications_v1.sql
|
||||
-- 对象类型:TABLE
|
||||
-- 版本:v1
|
||||
-- 依赖:ak_users, ak_distribution_divisions
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.ak_distribution_agent_applications (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
uid UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
division_uid UUID NOT NULL REFERENCES public.ak_distribution_divisions(uid),
|
||||
|
||||
agent_name TEXT NOT NULL,
|
||||
agent_phone TEXT NULL,
|
||||
|
||||
proof_images JSONB NULL, -- 申请凭证图片列表
|
||||
|
||||
status TEXT NOT NULL DEFAULT 'pending', -- pending/approved/rejected
|
||||
refusal_reason TEXT NULL,
|
||||
|
||||
approved_at TIMESTAMPTZ NULL,
|
||||
approved_by UUID NULL REFERENCES public.ak_users(id),
|
||||
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_dist_agent_applications_uid ON public.ak_distribution_agent_applications(uid);
|
||||
CREATE INDEX IF NOT EXISTS idx_dist_agent_applications_division_uid ON public.ak_distribution_agent_applications(division_uid);
|
||||
CREATE INDEX IF NOT EXISTS idx_dist_agent_applications_status ON public.ak_distribution_agent_applications(status);
|
||||
|
||||
COMMENT ON TABLE public.ak_distribution_agent_applications IS '分销代理商申请记录表';
|
||||
COMMENT ON COLUMN public.ak_distribution_agent_applications.proof_images IS '申请图片列表(JSON)';
|
||||
@@ -1,41 +1,29 @@
|
||||
-- =====================================================================================
|
||||
-- Schema: 分销代理商管理表
|
||||
-- 位置:docs/sql/10_schema/distribution/ak_distribution_agents_v1.sql
|
||||
-- 说明:管理事业部旗下的代理商,按商家隔离。
|
||||
-- 对象类型:TABLE
|
||||
-- 版本:v1
|
||||
-- 依赖:ak_users, ak_distribution_divisions
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.ak_distribution_agents (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
division_id UUID NOT NULL REFERENCES public.ak_distribution_divisions(id) ON DELETE CASCADE,
|
||||
uid UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
|
||||
name TEXT NOT NULL, -- 代理商名称(或备注名)
|
||||
status BOOLEAN DEFAULT true, -- 状态: true开启, false关闭
|
||||
|
||||
uid UUID PRIMARY KEY REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
division_uid UUID NOT NULL REFERENCES public.ak_distribution_divisions(uid), -- 所属事业部
|
||||
name TEXT NOT NULL,
|
||||
commission_ratio NUMERIC(5,2) DEFAULT 0 CHECK (commission_ratio >= 0 AND commission_ratio <= 100),
|
||||
is_enabled BOOLEAN DEFAULT TRUE,
|
||||
end_time TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
|
||||
-- 约束:一个用户在一个商家下只能成为一个代理商
|
||||
UNIQUE(merchant_id, uid)
|
||||
created_by UUID REFERENCES public.ak_users(id),
|
||||
updated_by UUID REFERENCES public.ak_users(id)
|
||||
);
|
||||
|
||||
-- 启用 RLS
|
||||
ALTER TABLE public.ak_distribution_agents ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- 权限策略:商家仅能管理自己的代理商
|
||||
CREATE POLICY "Merchants manage their own agents"
|
||||
ON public.ak_distribution_agents FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
|
||||
-- 允许查看
|
||||
CREATE POLICY "Authenticated users view active agents"
|
||||
ON public.ak_distribution_agents FOR SELECT
|
||||
TO authenticated
|
||||
USING (status = true);
|
||||
|
||||
-- 索引
|
||||
CREATE INDEX IF NOT EXISTS idx_agents_merchant ON public.ak_distribution_agents(merchant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_agents_division ON public.ak_distribution_agents(division_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_distribution_agents_division_uid ON public.ak_distribution_agents(division_uid);
|
||||
|
||||
-- 注释
|
||||
COMMENT ON TABLE public.ak_distribution_agents IS '分销代理商信息表';
|
||||
COMMENT ON COLUMN public.ak_distribution_agents.uid IS '用户ID(关联代理商本人)';
|
||||
COMMENT ON COLUMN public.ak_distribution_agents.division_uid IS '所属事业部UID';
|
||||
COMMENT ON COLUMN public.ak_distribution_agents.commission_ratio IS '代理商固定分佣比例(%)';
|
||||
|
||||
@@ -1,46 +1,30 @@
|
||||
-- =====================================================================================
|
||||
-- Schema: 分销事业部管理表
|
||||
-- 位置:docs/sql/10_schema/distribution/ak_distribution_divisions_v1.sql
|
||||
-- 说明:管理分销体系中的事业部,支持独立分销比例、邀请码及有效期,按商家隔离。
|
||||
-- 对象类型:TABLE
|
||||
-- 版本:v1
|
||||
-- 依赖:ak_users
|
||||
-- =====================================================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS public.ak_distribution_divisions (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
merchant_id UUID NOT NULL REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
|
||||
uid UUID NOT NULL REFERENCES public.ak_users(id), -- 事业部负责人UID
|
||||
name TEXT NOT NULL, -- 事业部名称
|
||||
invite_code TEXT UNIQUE NOT NULL, -- 事业部专属邀请码
|
||||
|
||||
ratio DECIMAL(5,2) DEFAULT 0, -- 事业部额外分销比例 (%)
|
||||
agent_count INTEGER DEFAULT 0, -- 下属代理商数量 (由程序或触发器维护)
|
||||
|
||||
end_time TIMESTAMPTZ, -- 协议截止时间
|
||||
status BOOLEAN DEFAULT true, -- 状态: true开启, false关闭
|
||||
|
||||
uid UUID PRIMARY KEY REFERENCES public.ak_users(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
invite_code TEXT UNIQUE NOT NULL,
|
||||
commission_ratio NUMERIC(5,2) DEFAULT 0 CHECK (commission_ratio >= 0 AND commission_ratio <= 100),
|
||||
is_enabled BOOLEAN DEFAULT TRUE,
|
||||
end_time TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||||
|
||||
-- 约束:一个用户在一个商家下只能负责一个事业部
|
||||
UNIQUE(merchant_id, uid)
|
||||
created_by UUID REFERENCES public.ak_users(id),
|
||||
updated_by UUID REFERENCES public.ak_users(id)
|
||||
);
|
||||
|
||||
-- 启用 RLS
|
||||
ALTER TABLE public.ak_distribution_divisions ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- 权限策略
|
||||
CREATE POLICY "Merchants manage their own divisions"
|
||||
ON public.ak_distribution_divisions FOR ALL
|
||||
TO authenticated
|
||||
USING (merchant_id = auth.uid())
|
||||
WITH CHECK (merchant_id = auth.uid());
|
||||
|
||||
-- 允许查看
|
||||
CREATE POLICY "Authenticated users view active divisions"
|
||||
ON public.ak_distribution_divisions FOR SELECT
|
||||
TO authenticated
|
||||
USING (status = true);
|
||||
|
||||
-- 索引
|
||||
CREATE INDEX IF NOT EXISTS idx_divisions_merchant ON public.ak_distribution_divisions(merchant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_divisions_uid ON public.ak_distribution_divisions(uid);
|
||||
CREATE INDEX IF NOT EXISTS idx_distribution_divisions_invite_code ON public.ak_distribution_divisions(invite_code);
|
||||
|
||||
-- 注释
|
||||
COMMENT ON TABLE public.ak_distribution_divisions IS '分销事业部信息表';
|
||||
COMMENT ON COLUMN public.ak_distribution_divisions.uid IS '用户ID(关联事业部负责人)';
|
||||
COMMENT ON COLUMN public.ak_distribution_divisions.invite_code IS '事业部专属邀请码';
|
||||
COMMENT ON COLUMN public.ak_distribution_divisions.commission_ratio IS '事业部固定分佣比例(%)';
|
||||
COMMENT ON COLUMN public.ak_distribution_divisions.end_time IS '事业部有效截止时间';
|
||||
|
||||
Reference in New Issue
Block a user