SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

DROP TABLE IF EXISTS `conversation`;
CREATE TABLE `conversation`  (
                                 `id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
                                 `title` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '标题',
                                 `type` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '类型',
                                 `case_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '案件ID',
                                 `user_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '用户ID',
                                 `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
                                 `update_time` datetime NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
                                 PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '会话记录表' ROW_FORMAT = DYNAMIC;

DROP TABLE IF EXISTS `conversation_qa`;
CREATE TABLE `conversation_qa`  (
                                    `id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
                                    `type` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '问答类型 0：DIFY问答 1：NX_LLM问答',
                                    `intent_type` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '意图类型 0：指标结果 1：案件分析结果 2：案件概况 3：案件证据指引',
                                    `dialog_count` int(11) NULL DEFAULT NULL COMMENT '多轮对话轮数',
                                    `question` text CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
                                    `complete_question` text CHARACTER SET utf8 COLLATE utf8_bin NULL,
                                    `question_time` datetime NOT NULL,
                                    `answer` text CHARACTER SET utf8 COLLATE utf8_bin NULL,
                                    `answer_map` text CHARACTER SET utf8 COLLATE utf8_bin NULL,
                                    `segment_list` text CHARACTER SET utf8 COLLATE utf8_bin NULL COMMENT 'RAG片段',
                                    `complete_answer` text CHARACTER SET utf8 COLLATE utf8_bin NULL,
                                    `answer_time` datetime NULL DEFAULT NULL,
                                    `time_interval` int(11) NULL DEFAULT NULL,
                                    `user_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
                                    `case_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL,
                                    `conversation_id` varchar(64) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
                                    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
                                    `update_time` datetime NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
                                    PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = DYNAMIC;

alter table model_case
    add knowledge_base_id varchar(64) null comment '知识库id' after id;

ALTER TABLE `nx_llm`.`note_record_split`
    ADD COLUMN `note_file_id` varchar(64) NULL COMMENT '笔录文件ID' AFTER `person_name`;

create table audit_log
(
    id             varchar(64)   not null comment '主键id'
        primary key,
    record_type    varchar(64)   null comment '记录类型：0-正常操作记录；1-异常记录',
    user_id        varchar(64)   null comment '用户id',
    user_name      varchar(128)  null comment '用户名',
    ip             varchar(255)  null comment '操作人ip地址',
    method         varchar(64)   null comment '请求类型 GET POST PUT DELETE',
    url            varchar(1024) null comment 'url地址',
    request_params text          null comment '请求参数',
    cost_time      int           null comment '操作耗时 单位ms',
    response       text          null comment '响应结果',
    exception_desc text          null comment '异常描述',
    create_time    datetime      null comment '创建时间',
    create_user_id varchar(64)   null comment '创建人',
    update_time    datetime      null comment '更新时间',
    update_user_id varchar(255)  null comment '更新用户id'
)
    comment '日志审计表';
create index audit_log_record_type_index
    on audit_log (record_type);
create index audit_log_user_id_index
    on audit_log (user_id);