-- 创建店铺角色关联表
CREATE TABLE tb_shop_role (
    id SERIAL PRIMARY KEY,
    shop_id INT NOT NULL,
    role_id INT NOT NULL,
    status INT NOT NULL DEFAULT 1,  -- 0=禁用 1=启用
    creator INT NOT NULL,
    updater INT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

-- 创建唯一索引
CREATE UNIQUE INDEX idx_shop_role_shop_id_role_id ON tb_shop_role(shop_id, role_id) WHERE deleted_at IS NULL;

-- 创建索引
CREATE INDEX idx_shop_role_shop_id ON tb_shop_role(shop_id) WHERE deleted_at IS NULL;
CREATE INDEX idx_shop_role_role_id ON tb_shop_role(role_id) WHERE deleted_at IS NULL;
CREATE INDEX idx_shop_role_deleted_at ON tb_shop_role(deleted_at);

-- 添加注释
COMMENT ON TABLE tb_shop_role IS '店铺角色关联表';
COMMENT ON COLUMN tb_shop_role.shop_id IS '店铺ID';
COMMENT ON COLUMN tb_shop_role.role_id IS '角色ID';
COMMENT ON COLUMN tb_shop_role.status IS '状态 0=禁用 1=启用';
COMMENT ON COLUMN tb_shop_role.creator IS '创建人ID';
COMMENT ON COLUMN tb_shop_role.updater IS '更新人ID';
COMMENT ON COLUMN tb_shop_role.created_at IS '创建时间';
COMMENT ON COLUMN tb_shop_role.updated_at IS '更新时间';
COMMENT ON COLUMN tb_shop_role.deleted_at IS '软删除时间';
