-- =============================================
-- 电话管理系统 - 人大金仓数据库表结构
-- 创建日期: 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;

-- =============================================
-- 民意调查样本库与评议任务表结构（人大金仓 / PostgreSQL 兼容）
-- =============================================

-- 1. 样本分类分组表
CREATE TABLE IF NOT EXISTS t_survey_sample_group (
    id BIGINT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE t_survey_sample_group IS '样本分类分组表';
COMMENT ON COLUMN t_survey_sample_group.id IS '主键ID';
COMMENT ON COLUMN t_survey_sample_group.name IS '分组名称 (如：高级精英人才库)';
COMMENT ON COLUMN t_survey_sample_group.create_time IS '创建时间';

-- 2. 样本人员名单表
CREATE TABLE IF NOT EXISTS t_survey_sample (
    id BIGINT PRIMARY KEY,
    group_id BIGINT NOT NULL,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    remarks VARCHAR(255),
    import_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE t_survey_sample IS '样本人员表';
COMMENT ON COLUMN t_survey_sample.id IS '主键ID';
COMMENT ON COLUMN t_survey_sample.group_id IS '关联分组ID';
COMMENT ON COLUMN t_survey_sample.name IS '姓名';
COMMENT ON COLUMN t_survey_sample.phone IS '联系电话';
COMMENT ON COLUMN t_survey_sample.remarks IS '分类备注';
COMMENT ON COLUMN t_survey_sample.import_time IS '导入时间';

-- 3. 调查测评任务表
CREATE TABLE IF NOT EXISTS t_survey_task (
    id BIGINT PRIMARY KEY,
    title VARCHAR(200) NOT NULL,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(20) NOT NULL,
    sjly_no VARCHAR(50),
    call_count INTEGER DEFAULT 0,
    status INTEGER DEFAULT 0,
    survey_title VARCHAR(200),
    survey_id BIGINT,
    answers TEXT,
    assign_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE t_survey_task IS '调查测评任务表';
COMMENT ON COLUMN t_survey_task.id IS '主键ID';
COMMENT ON COLUMN t_survey_task.title IS '任务名称';
COMMENT ON COLUMN t_survey_task.name IS '受访群众姓名';
COMMENT ON COLUMN t_survey_task.phone IS '联系电话';
COMMENT ON COLUMN t_survey_task.sjly_no IS '关联警务分类编码';
COMMENT ON COLUMN t_survey_task.call_count IS '呼叫次数';
COMMENT ON COLUMN t_survey_task.status IS '状态 (0待调查, 1调查成功, 2无人接听, 3明确拒访, 4已发送短信评议)';
COMMENT ON COLUMN t_survey_task.survey_title IS '关联问卷模板名称';
COMMENT ON COLUMN t_survey_task.survey_id IS '关联的问卷模板ID';
COMMENT ON COLUMN t_survey_task.answers IS '群众填报的答卷JSON详情';
COMMENT ON COLUMN t_survey_task.assign_time IS '生成时间';

-- 预置默认分组与测试样本数据
INSERT INTO t_survey_sample_group (id, name, create_time) VALUES 
(1001, '高级精英人才库', CURRENT_TIMESTAMP),
(1002, '警务窗口服务评议库', CURRENT_TIMESTAMP),
(1003, '110接处警抽样库', CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;

INSERT INTO t_survey_sample (id, group_id, name, phone, remarks, import_time) VALUES 
(2001, 1001, '张大胖', '18944445555', '高层次人才领军学者', CURRENT_TIMESTAMP),
(2002, 1001, '陈科研', '18655556666', '杰出青年科研人才', CURRENT_TIMESTAMP),
(2003, 1002, '韩梅梅', '13711112222', '户政大厅办事人员', CURRENT_TIMESTAMP),
(2004, 1002, '李雷', '13633334444', '出入境窗口满意度抽评', CURRENT_TIMESTAMP),
(2005, 1003, '王有才', '13899998888', '110处警受害者电话', CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;

-- =============================================
-- 工单一次与二次回访考核评分量化指标字段拓展升级
-- =============================================
ALTER TABLE t_czzx_wtcz ADD COLUMN IF NOT EXISTS first_feedback_score_type VARCHAR(50);
ALTER TABLE t_czzx_wtcz ADD COLUMN IF NOT EXISTS first_feedback_score_matrix VARCHAR(255);
ALTER TABLE t_czzx_wtcz ADD COLUMN IF NOT EXISTS second_feedback_score_matrix VARCHAR(255);

COMMENT ON COLUMN t_czzx_wtcz.first_feedback_score_type IS '回访评分大类类型 (如: 一般刑事行政案件)';
COMMENT ON COLUMN t_czzx_wtcz.first_feedback_score_matrix IS '一次回访不满意项考核/统计矩阵 (如: A,C,F)';
COMMENT ON COLUMN t_czzx_wtcz.second_feedback_score_matrix IS '二次回访不满意项考核/统计矩阵 (如: WZG,ZGH)';