-- 代理分销码与公开扫码注册:店铺全局唯一分销码、待审批注册记录。 -- 分销码由应用层随机生成且不可修改,条件唯一索引兜底;不提供人工指定或编辑入口。 -- 注册只落待审批记录,审批通过前不创建店铺、账号、钱包或上下级归属。 -- 不使用数据库外键,关联以 ID 保存并由应用层显式校验。 ALTER TABLE tb_shop ADD COLUMN IF NOT EXISTS distribution_code VARCHAR(32) NOT NULL DEFAULT ''; COMMENT ON COLUMN tb_shop.distribution_code IS '全局唯一随机分销码,创建时生成且不可修改;空字符串表示历史数据未生成'; CREATE UNIQUE INDEX IF NOT EXISTS uk_shop_distribution_code ON tb_shop (distribution_code) WHERE distribution_code <> '' AND deleted_at IS NULL; CREATE TABLE tb_agent_distribution_registration ( id BIGSERIAL PRIMARY KEY, distribution_code VARCHAR(32) NOT NULL, parent_shop_id BIGINT NOT NULL, phone VARCHAR(20) NOT NULL, password_hash VARCHAR(255) NOT NULL, shop_name VARCHAR(100) NOT NULL, shop_code VARCHAR(50) NOT NULL, username VARCHAR(50) NOT NULL, contact_name VARCHAR(50) NOT NULL DEFAULT '', province VARCHAR(50) NOT NULL DEFAULT '', city VARCHAR(50) NOT NULL DEFAULT '', district VARCHAR(50) NOT NULL DEFAULT '', address VARCHAR(255) NOT NULL DEFAULT '', status SMALLINT NOT NULL DEFAULT 0, approval_instance_id BIGINT, reject_reason VARCHAR(500) NOT NULL DEFAULT '', decided_at TIMESTAMPTZ, creator BIGINT NOT NULL DEFAULT 0, updater BIGINT NOT NULL DEFAULT 0, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT chk_agent_distribution_registration_code CHECK (distribution_code <> ''), CONSTRAINT chk_agent_distribution_registration_parent CHECK (parent_shop_id > 0), CONSTRAINT chk_agent_distribution_registration_phone CHECK (phone <> ''), CONSTRAINT chk_agent_distribution_registration_password CHECK (password_hash <> ''), CONSTRAINT chk_agent_distribution_registration_shop CHECK ( shop_name <> '' AND shop_code <> '' AND username <> '' ), CONSTRAINT chk_agent_distribution_registration_status CHECK (status IN (0, 1, 2)), CONSTRAINT chk_agent_distribution_registration_instance CHECK ( approval_instance_id IS NULL OR approval_instance_id > 0 ) ); -- 同一手机号可存在多条记录:驳回后再次扫码是新记录、新审批实例。 CREATE INDEX idx_agent_distribution_registration_parent ON tb_agent_distribution_registration (parent_shop_id, status, id); CREATE INDEX idx_agent_distribution_registration_phone ON tb_agent_distribution_registration (phone, id DESC); CREATE UNIQUE INDEX uk_agent_distribution_registration_instance ON tb_agent_distribution_registration (approval_instance_id) WHERE approval_instance_id IS NOT NULL; COMMENT ON TABLE tb_agent_distribution_registration IS '代理扫码注册待审批记录,审批通过后才创建店铺与账号'; COMMENT ON COLUMN tb_agent_distribution_registration.id IS '主键,注册记录ID,同时作为通用审批业务ID'; COMMENT ON COLUMN tb_agent_distribution_registration.distribution_code IS '提交时使用的上级店铺分销码快照,审批通过时校验其归属店铺仍启用'; COMMENT ON COLUMN tb_agent_distribution_registration.parent_shop_id IS '上级店铺ID,审批通过后作为新店铺的直接上级'; COMMENT ON COLUMN tb_agent_distribution_registration.phone IS '注册手机号,审批通过时写入代理主账号;不在此记录明文密码'; COMMENT ON COLUMN tb_agent_distribution_registration.password_hash IS '注册时生成的 bcrypt 密码哈希,审批通过时直接写入新建账号'; COMMENT ON COLUMN tb_agent_distribution_registration.shop_name IS '申请店铺名称快照'; COMMENT ON COLUMN tb_agent_distribution_registration.shop_code IS '申请店铺编号快照,审批通过时按既有唯一约束校验'; COMMENT ON COLUMN tb_agent_distribution_registration.username IS '申请代理主账号用户名快照,审批通过时按既有唯一约束校验'; COMMENT ON COLUMN tb_agent_distribution_registration.contact_name IS '联系人姓名快照'; COMMENT ON COLUMN tb_agent_distribution_registration.province IS '省份快照'; COMMENT ON COLUMN tb_agent_distribution_registration.city IS '城市快照'; COMMENT ON COLUMN tb_agent_distribution_registration.district IS '区县快照'; COMMENT ON COLUMN tb_agent_distribution_registration.address IS '详细地址快照'; COMMENT ON COLUMN tb_agent_distribution_registration.status IS '状态:0 待审批、1 已通过、2 已驳回'; COMMENT ON COLUMN tb_agent_distribution_registration.approval_instance_id IS '关联的通用审批实例ID,创建后不可修改'; COMMENT ON COLUMN tb_agent_distribution_registration.reject_reason IS '企业微信最终驳回原因,通过时为空'; COMMENT ON COLUMN tb_agent_distribution_registration.decided_at IS '审批终态到达时间'; COMMENT ON COLUMN tb_agent_distribution_registration.creator IS '创建人用户ID,0 表示系统'; COMMENT ON COLUMN tb_agent_distribution_registration.updater IS '最近更新人用户ID,0 表示系统'; COMMENT ON COLUMN tb_agent_distribution_registration.created_at IS '创建时间'; COMMENT ON COLUMN tb_agent_distribution_registration.updated_at IS '最近更新时间';