@ -3,6 +3,8 @@ package com.supervision.police.service.impl;
import cn.hutool.core.bean.BeanUtil ;
import cn.hutool.core.bean.copier.CopyOptions ;
import cn.hutool.core.collection.CollUtil ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.date.TimeInterval ;
import cn.hutool.core.lang.Assert ;
import cn.hutool.core.util.NumberUtil ;
import cn.hutool.core.util.StrUtil ;
@ -14,8 +16,6 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.supervision.chat.client.LangChainChatService ;
import com.supervision.chat.client.dto.CreateBaseDTO ;
import com.supervision.chat.client.dto.LangChainChatRes ;
import com.supervision.common.domain.R ;
import com.supervision.common.enums.ResultStatusEnum ;
import com.supervision.common.exception.CustomException ;
@ -28,6 +28,7 @@ import com.supervision.police.dto.*;
import com.supervision.police.mapper.ModelCaseMapper ;
import com.supervision.police.service.* ;
import com.supervision.police.vo.ModelCaseVO ;
import com.supervision.utils.DifyApiUtil ;
import com.supervision.utils.IndexRuleUtil ;
import lombok.RequiredArgsConstructor ;
import lombok.extern.slf4j.Slf4j ;
@ -76,6 +77,8 @@ public class ModelCaseServiceImpl extends ServiceImpl<ModelCaseMapper, ModelCase
private final EvidenceDirectoryService directoryService ;
private final DifyApiUtil difyApiUtil ;
/ * *
* 查 询 列 表
*
@ -166,6 +169,12 @@ public class ModelCaseServiceImpl extends ServiceImpl<ModelCaseMapper, ModelCase
Long count = super . lambdaQuery ( ) . eq ( ModelCase : : getCaseNo , modelCase . getCaseNo ( ) ) . count ( ) ;
Assert . isTrue ( count = = 0 , "案件编号已存在,请更换案件编号!" ) ;
// 这里需要调用知识库的接口,去保存知识库
String databaseId = difyApiUtil . createDataset ( modelCase . getCaseName ( ) , modelCase . getCaseDetail ( ) ) ;
if ( StrUtil . isEmpty ( databaseId ) ) {
throw new BusinessException ( "保存知识库失败" ) ;
}
modelCase . setKnowledgeBaseId ( databaseId ) ;
Long num = modelCaseMapper . selectCount ( null ) ;
modelCase . setIndexNum ( Integer . parseInt ( num . toString ( ) ) + 1 ) ;
i = modelCaseMapper . insert ( modelCase ) ;
@ -173,15 +182,6 @@ public class ModelCaseServiceImpl extends ServiceImpl<ModelCaseMapper, ModelCase
// 保存案件行为人
casePersonService . saveCaseActor ( modelCase . getId ( ) , modelCaseBase . getCaseActorName ( ) , modelCaseBase . getCaseActorIdCard ( ) ) ;
caseEvidenceService . initCaseEvidenceDirectory ( modelCase . getId ( ) , modelCase . getCaseType ( ) ) ;
// 这里需要调用知识库的接口,去保存知识库
CreateBaseDTO createBaseDTO = new CreateBaseDTO ( ) ;
createBaseDTO . setKnowledge_base_name ( modelCase . getCaseNo ( ) ) ;
LangChainChatRes chat = langChainChatService . createBase ( createBaseDTO ) ;
log . info ( "创建知识库:{}" , chat ) ;
if ( 200 ! = chat . getCode ( ) ) {
throw new BusinessException ( "保存知识库失败" ) ;
}
}
if ( i > 0 ) {
return R . okMsg ( "保存成功" ) ;
@ -208,13 +208,9 @@ public class ModelCaseServiceImpl extends ServiceImpl<ModelCaseMapper, ModelCase
public R < ? > realDeleteByIds ( List < String > ids ) {
List < ModelCase > modelCases = modelCaseMapper . selectBatchIds ( ids ) ;
if ( modelCases ! = null & & ! modelCases . isEmpty ( ) ) {
modelCases . forEach ( modelCase - > {
LangChainChatRes < Object > langChainChatRes = langChainChatService . deleteBase ( modelCase . getCaseNo ( ) ) ;
if ( 200 ! = langChainChatRes . getCode ( ) ) {
log . error ( "删除知识库失败:{}, caseNo:{}" , langChainChatRes . getMsg ( ) , modelCase . getCaseNo ( ) ) ;
// throw new BusinessException("删除知识库失败");
}
} ) ;
modelCases . stream ( )
. filter ( modelCase - > StrUtil . isNotEmpty ( modelCase . getKnowledgeBaseId ( ) ) )
. forEach ( modelCase - > difyApiUtil . deleteDataset ( modelCase . getKnowledgeBaseId ( ) ) ) ;
}
noteRecordService . list ( new QueryWrapper < NoteRecord > ( ) . in ( "case_id" , ids ) ) . forEach ( noteRecord - > noteRecordSplitService . delRecords ( noteRecord . getId ( ) ) ) ;
List < String > personIds = casePersonService . list ( new QueryWrapper < CasePerson > ( ) . in ( "case_id" , ids ) ) . stream ( ) . map ( CasePerson : : getId ) . collect ( Collectors . toList ( ) ) ;
@ -362,7 +358,7 @@ public class ModelCaseServiceImpl extends ServiceImpl<ModelCaseMapper, ModelCase
}
private Map < String , Object > errorMapBuilder ( ModelCase modelCase , String errorText ) {
return new HashMap < String , Object > ( 2 ) { {
return new HashMap < > ( 2 ) { {
put ( "caseNo" , modelCase . getCaseNo ( ) ) ;
put ( "caseName" , modelCase . getCaseName ( ) ) ;
put ( "errorText" , errorText ) ;
@ -384,6 +380,56 @@ public class ModelCaseServiceImpl extends ServiceImpl<ModelCaseMapper, ModelCase
return iPage ;
}
@Override
public void initCaseKnowledgeBase ( String caseId ) {
ModelCase modelCase = this . getById ( caseId ) ;
if ( StrUtil . isEmpty ( modelCase . getKnowledgeBaseId ( ) ) ) {
log . info ( "案件:{} 尚未创建知识库,即将创建知识库库..." , modelCase . getCaseName ( ) ) ;
String knowledgeBaseId = difyApiUtil . createDataset (
StrUtil . join ( "_" , modelCase . getCaseName ( ) , modelCase . getCaseNo ( ) ) , modelCase . getCaseDetail ( ) ) ;
if ( StrUtil . isEmpty ( knowledgeBaseId ) ) {
log . error ( "案件:{} 创建支持库失败..." , modelCase . getCaseName ( ) ) ;
return ;
}
modelCase . setKnowledgeBaseId ( knowledgeBaseId ) ;
modelCaseMapper . updateById ( modelCase ) ;
log . info ( "案件:{} 创建支持库成功, 知识库id为: {}" , modelCase . getCaseName ( ) , modelCase . getKnowledgeBaseId ( ) ) ;
} else {
log . info ( "案件:{} 已创建知识库,不进行创建..." , modelCase . getCaseName ( ) ) ;
}
log . info ( "案件:{} 开始上传笔录到支持库..." , modelCase . getCaseName ( ) ) ;
noteRecordService . uploadRecordFileToDifyKnowledgeBase ( caseId ) ;
log . info ( "案件:{} 上传笔录到支持库成功..." , modelCase . getCaseName ( ) ) ;
}
@Override
public void migrateRecordKnowledgeBase ( ) {
// 获取所有案件
int success = 0 ;
int fail = 0 ;
// 只 同步 已经分析的案件
List < ModelCase > allModelCase = this . lambdaQuery ( ) . isNotNull ( ModelCase : : getCaseAnalysisSuccessTime ) . list ( ) ;
log . info ( "===========>>>>>案件总数:{},开始迁移笔录到支持库...<<<<<===========" , allModelCase . size ( ) ) ;
TimeInterval timer = DateUtil . timer ( ) ;
timer . start ( ) ;
for ( ModelCase modelCase : allModelCase ) {
try {
log . info ( "开始迁移案件:{} 笔录到支持库,当前总体进度:{}..." , modelCase . getCaseName ( ) , NumberUtil . formatPercent ( ( success + fail ) / ( allModelCase . size ( ) * 1.0 ) , 2 ) ) ;
timer . start ( modelCase . getId ( ) ) ;
initCaseKnowledgeBase ( modelCase . getId ( ) ) ;
log . info ( "案件:{} 迁移笔录到支持库成功,耗时:{}秒..." , modelCase . getCaseName ( ) , timer . intervalSecond ( modelCase . getId ( ) ) ) ;
success + + ;
} catch ( Exception e ) {
log . error ( "案件:{} 迁移笔录到支持库失败.." , modelCase . getCaseName ( ) , e ) ;
fail + + ;
}
}
log . info ( "===========>>>>>迁移笔录到支持库完成,成功个数:{},失败个数:{},总耗时:{}秒...<<<<<===========" , success , allModelCase . size ( ) - success , timer . intervalSecond ( ) ) ;
}
/ * *
* 添 加 指 标 结 果 附 属 内 容