-- =============================================
-- 电话管理系统 - 人大金仓数据库表结构
-- 创建日期: 2026-06-09
-- =============================================

-- 1. 电话记录表
CREATE TABLE IF NOT EXISTS t_phone_record (
    id BIGINT PRIMARY KEY,
    call_id VARCHAR(100) NOT NULL,
    caller_number VARCHAR(50),
    caller_name VARCHAR(100),
    callee_number VARCHAR(50),
    callee_name VARCHAR(100),
    call_direction VARCHAR(20),
    call_start_time TIMESTAMP,
    call_end_time TIMESTAMP,
    call_duration INTEGER,
    call_status VARCHAR(20),
    recording_url VARCHAR(500),
    has_recording INTEGER DEFAULT 0,
    call_type VARCHAR(20),
    remark TEXT,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted INTEGER DEFAULT 0
);

COMMENT ON TABLE t_phone_record IS '电话记录表';
COMMENT ON COLUMN t_phone_record.id IS '主键ID';
COMMENT ON COLUMN t_phone_record.call_id IS '通话ID（呼叫系统唯一标识）';
COMMENT ON COLUMN t_phone_record.caller_number IS '主叫号码';
COMMENT ON COLUMN t_phone_record.caller_name IS '主叫姓名';
COMMENT ON COLUMN t_phone_record.callee_number IS '被叫号码';
COMMENT ON COLUMN t_phone_record.callee_name IS '被叫姓名';
COMMENT ON COLUMN t_phone_record.call_direction IS '通话方向：IN-呼入, OUT-呼出';
COMMENT ON COLUMN t_phone_record.call_start_time IS '通话开始时间';
COMMENT ON COLUMN t_phone_record.call_end_time IS '通话结束时间';
COMMENT ON COLUMN t_phone_record.call_duration IS '通话时长（秒）';
COMMENT ON COLUMN t_phone_record.call_status IS '通话状态：ANSWERED-已接通, MISSED-未接听, BUSY-忙线, REJECTED-拒接';
COMMENT ON COLUMN t_phone_record.recording_url IS '录音文件URL';
COMMENT ON COLUMN t_phone_record.has_recording IS '是否有录音：0-无, 1-有';
COMMENT ON COLUMN t_phone_record.call_type IS '通话类型：NORMAL-普通, TRANSFER-转接, CONFERENCE-会议';
COMMENT ON COLUMN t_phone_record.remark IS '备注';
COMMENT ON COLUMN t_phone_record.create_time IS '创建时间';
COMMENT ON COLUMN t_phone_record.update_time IS '更新时间';
COMMENT ON COLUMN t_phone_record.deleted IS '逻辑删除：0-未删除, 1-已删除';

-- 创建索引
CREATE INDEX idx_phone_record_call_id ON t_phone_record(call_id);
CREATE INDEX idx_phone_record_caller_number ON t_phone_record(caller_number);
CREATE INDEX idx_phone_record_call_start_time ON t_phone_record(call_start_time);
CREATE INDEX idx_phone_record_call_status ON t_phone_record(call_status);

-- 序列（用于主键自增）
CREATE SEQUENCE IF NOT EXISTS seq_phone_record_id START WITH 1 INCREMENT BY 1;