-- 创建店铺表
CREATE TABLE IF NOT EXISTS tb_shop (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    creator BIGINT NOT NULL,
    updater BIGINT NOT NULL,
    shop_name VARCHAR(100) NOT NULL,
    shop_code VARCHAR(50),
    parent_id BIGINT,
    level INT NOT NULL DEFAULT 1,
    contact_name VARCHAR(50),
    contact_phone VARCHAR(20),
    province VARCHAR(50),
    city VARCHAR(50),
    district VARCHAR(50),
    address VARCHAR(255),
    status INT NOT NULL DEFAULT 1
);

-- 添加索引
CREATE INDEX IF NOT EXISTS idx_shop_parent_id ON tb_shop(parent_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_shop_code ON tb_shop(shop_code) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_shop_deleted_at ON tb_shop(deleted_at);

-- 添加表注释
COMMENT ON TABLE tb_shop IS '店铺表';
COMMENT ON COLUMN tb_shop.id IS '主键ID';
COMMENT ON COLUMN tb_shop.created_at IS '创建时间';
COMMENT ON COLUMN tb_shop.updated_at IS '更新时间';
COMMENT ON COLUMN tb_shop.deleted_at IS '删除时间';
COMMENT ON COLUMN tb_shop.creator IS '创建人ID';
COMMENT ON COLUMN tb_shop.updater IS '更新人ID';
COMMENT ON COLUMN tb_shop.shop_name IS '店铺名称';
COMMENT ON COLUMN tb_shop.shop_code IS '店铺编号';
COMMENT ON COLUMN tb_shop.parent_id IS '上级店铺ID（NULL表示一级代理）';
COMMENT ON COLUMN tb_shop.level IS '层级（1-7）';
COMMENT ON COLUMN tb_shop.contact_name IS '联系人姓名';
COMMENT ON COLUMN tb_shop.contact_phone IS '联系人电话';
COMMENT ON COLUMN tb_shop.province IS '省份';
COMMENT ON COLUMN tb_shop.city IS '城市';
COMMENT ON COLUMN tb_shop.district IS '区县';
COMMENT ON COLUMN tb_shop.address IS '详细地址';
COMMENT ON COLUMN tb_shop.status IS '状态 0=禁用 1=启用';

-- 创建企业表
CREATE TABLE IF NOT EXISTS tb_enterprise (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    creator BIGINT NOT NULL,
    updater BIGINT NOT NULL,
    enterprise_name VARCHAR(100) NOT NULL,
    enterprise_code VARCHAR(50),
    owner_shop_id BIGINT,
    legal_person VARCHAR(50),
    contact_name VARCHAR(50),
    contact_phone VARCHAR(20),
    business_license VARCHAR(100),
    province VARCHAR(50),
    city VARCHAR(50),
    district VARCHAR(50),
    address VARCHAR(255),
    status INT NOT NULL DEFAULT 1
);

-- 添加索引
CREATE INDEX IF NOT EXISTS idx_enterprise_owner_shop_id ON tb_enterprise(owner_shop_id);
CREATE UNIQUE INDEX IF NOT EXISTS idx_enterprise_code ON tb_enterprise(enterprise_code) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_enterprise_deleted_at ON tb_enterprise(deleted_at);

-- 添加表注释
COMMENT ON TABLE tb_enterprise IS '企业表';
COMMENT ON COLUMN tb_enterprise.id IS '主键ID';
COMMENT ON COLUMN tb_enterprise.created_at IS '创建时间';
COMMENT ON COLUMN tb_enterprise.updated_at IS '更新时间';
COMMENT ON COLUMN tb_enterprise.deleted_at IS '删除时间';
COMMENT ON COLUMN tb_enterprise.creator IS '创建人ID';
COMMENT ON COLUMN tb_enterprise.updater IS '更新人ID';
COMMENT ON COLUMN tb_enterprise.enterprise_name IS '企业名称';
COMMENT ON COLUMN tb_enterprise.enterprise_code IS '企业编号';
COMMENT ON COLUMN tb_enterprise.owner_shop_id IS '归属店铺ID（NULL表示平台直属）';
COMMENT ON COLUMN tb_enterprise.legal_person IS '法人代表';
COMMENT ON COLUMN tb_enterprise.contact_name IS '联系人姓名';
COMMENT ON COLUMN tb_enterprise.contact_phone IS '联系人电话';
COMMENT ON COLUMN tb_enterprise.business_license IS '营业执照号';
COMMENT ON COLUMN tb_enterprise.province IS '省份';
COMMENT ON COLUMN tb_enterprise.city IS '城市';
COMMENT ON COLUMN tb_enterprise.district IS '区县';
COMMENT ON COLUMN tb_enterprise.address IS '详细地址';
COMMENT ON COLUMN tb_enterprise.status IS '状态 0=禁用 1=启用';

-- 创建个人客户表
CREATE TABLE IF NOT EXISTS tb_personal_customer (
    id BIGSERIAL PRIMARY KEY,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP WITH TIME ZONE,
    phone VARCHAR(20),
    nickname VARCHAR(50),
    avatar_url VARCHAR(255),
    wx_open_id VARCHAR(100),
    wx_union_id VARCHAR(100),
    status INT NOT NULL DEFAULT 1
);

-- 添加索引
CREATE UNIQUE INDEX IF NOT EXISTS idx_personal_customer_phone ON tb_personal_customer(phone) WHERE deleted_at IS NULL;
CREATE INDEX IF NOT EXISTS idx_personal_customer_wx_open_id ON tb_personal_customer(wx_open_id);
CREATE INDEX IF NOT EXISTS idx_personal_customer_wx_union_id ON tb_personal_customer(wx_union_id);
CREATE INDEX IF NOT EXISTS idx_personal_customer_deleted_at ON tb_personal_customer(deleted_at);

-- 添加表注释
COMMENT ON TABLE tb_personal_customer IS '个人客户表';
COMMENT ON COLUMN tb_personal_customer.id IS '主键ID';
COMMENT ON COLUMN tb_personal_customer.created_at IS '创建时间';
COMMENT ON COLUMN tb_personal_customer.updated_at IS '更新时间';
COMMENT ON COLUMN tb_personal_customer.deleted_at IS '删除时间';
COMMENT ON COLUMN tb_personal_customer.phone IS '手机号（唯一标识）';
COMMENT ON COLUMN tb_personal_customer.nickname IS '昵称';
COMMENT ON COLUMN tb_personal_customer.avatar_url IS '头像URL';
COMMENT ON COLUMN tb_personal_customer.wx_open_id IS '微信OpenID';
COMMENT ON COLUMN tb_personal_customer.wx_union_id IS '微信UnionID';
COMMENT ON COLUMN tb_personal_customer.status IS '状态 0=禁用 1=启用';

-- 修改 tb_account 表：添加 enterprise_id 列，移除 parent_id 列
ALTER TABLE tb_account ADD COLUMN IF NOT EXISTS enterprise_id BIGINT;
CREATE INDEX IF NOT EXISTS idx_account_enterprise_id ON tb_account(enterprise_id);
COMMENT ON COLUMN tb_account.enterprise_id IS '企业ID（企业账号必填）';

-- 移除 parent_id 列（如果存在）
ALTER TABLE tb_account DROP COLUMN IF EXISTS parent_id;

-- 更新 tb_account 表的字段注释
COMMENT ON COLUMN tb_account.user_type IS '用户类型 1=超级管理员 2=平台用户 3=代理账号 4=企业账号';
COMMENT ON COLUMN tb_account.shop_id IS '店铺ID（代理账号必填）';
