-- 创建个人客户手机号绑定表
CREATE TABLE IF NOT EXISTS personal_customer_phone (
    id SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    phone VARCHAR(20) NOT NULL,
    is_primary BOOLEAN DEFAULT false,
    verified_at TIMESTAMP,
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

COMMENT ON TABLE personal_customer_phone IS '个人客户手机号绑定表';
COMMENT ON COLUMN personal_customer_phone.id IS '主键ID';
COMMENT ON COLUMN personal_customer_phone.customer_id IS '个人客户ID';
COMMENT ON COLUMN personal_customer_phone.phone IS '手机号';
COMMENT ON COLUMN personal_customer_phone.is_primary IS '是否主手机号';
COMMENT ON COLUMN personal_customer_phone.verified_at IS '验证通过时间';
COMMENT ON COLUMN personal_customer_phone.status IS '状态 0=禁用 1=启用';
COMMENT ON COLUMN personal_customer_phone.created_at IS '创建时间';
COMMENT ON COLUMN personal_customer_phone.updated_at IS '更新时间';
COMMENT ON COLUMN personal_customer_phone.deleted_at IS '删除时间（软删除）';

-- 创建索引
CREATE UNIQUE INDEX idx_personal_customer_phone_customer_phone ON personal_customer_phone(customer_id, phone) WHERE deleted_at IS NULL;
CREATE INDEX idx_personal_customer_phone_phone ON personal_customer_phone(phone);
CREATE INDEX idx_personal_customer_phone_deleted_at ON personal_customer_phone(deleted_at);

-- 创建个人客户 ICCID 绑定表
CREATE TABLE IF NOT EXISTS personal_customer_iccid (
    id SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    iccid VARCHAR(20) NOT NULL,
    bind_at TIMESTAMP,
    last_used_at TIMESTAMP,
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

COMMENT ON TABLE personal_customer_iccid IS '个人客户ICCID绑定表';
COMMENT ON COLUMN personal_customer_iccid.id IS '主键ID';
COMMENT ON COLUMN personal_customer_iccid.customer_id IS '个人客户ID';
COMMENT ON COLUMN personal_customer_iccid.iccid IS 'ICCID（20位数字）';
COMMENT ON COLUMN personal_customer_iccid.bind_at IS '绑定时间';
COMMENT ON COLUMN personal_customer_iccid.last_used_at IS '最后使用时间';
COMMENT ON COLUMN personal_customer_iccid.status IS '状态 0=禁用 1=启用';
COMMENT ON COLUMN personal_customer_iccid.created_at IS '创建时间';
COMMENT ON COLUMN personal_customer_iccid.updated_at IS '更新时间';
COMMENT ON COLUMN personal_customer_iccid.deleted_at IS '删除时间（软删除）';

-- 创建索引
CREATE UNIQUE INDEX idx_personal_customer_iccid_customer_iccid ON personal_customer_iccid(customer_id, iccid) WHERE deleted_at IS NULL;
CREATE INDEX idx_personal_customer_iccid_iccid ON personal_customer_iccid(iccid);
CREATE INDEX idx_personal_customer_iccid_deleted_at ON personal_customer_iccid(deleted_at);

-- 创建个人客户设备号绑定表
CREATE TABLE IF NOT EXISTS personal_customer_device (
    id SERIAL PRIMARY KEY,
    customer_id INTEGER NOT NULL,
    device_no VARCHAR(50) NOT NULL,
    bind_at TIMESTAMP,
    last_used_at TIMESTAMP,
    status INTEGER DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_at TIMESTAMP
);

COMMENT ON TABLE personal_customer_device IS '个人客户设备号绑定表';
COMMENT ON COLUMN personal_customer_device.id IS '主键ID';
COMMENT ON COLUMN personal_customer_device.customer_id IS '个人客户ID';
COMMENT ON COLUMN personal_customer_device.device_no IS '设备号/IMEI';
COMMENT ON COLUMN personal_customer_device.bind_at IS '绑定时间';
COMMENT ON COLUMN personal_customer_device.last_used_at IS '最后使用时间';
COMMENT ON COLUMN personal_customer_device.status IS '状态 0=禁用 1=启用';
COMMENT ON COLUMN personal_customer_device.created_at IS '创建时间';
COMMENT ON COLUMN personal_customer_device.updated_at IS '更新时间';
COMMENT ON COLUMN personal_customer_device.deleted_at IS '删除时间（软删除）';

-- 创建索引
CREATE UNIQUE INDEX idx_personal_customer_device_customer_device ON personal_customer_device(customer_id, device_no) WHERE deleted_at IS NULL;
CREATE INDEX idx_personal_customer_device_device_no ON personal_customer_device(device_no);
CREATE INDEX idx_personal_customer_device_deleted_at ON personal_customer_device(deleted_at);
