@@ -1,13 +1,352 @@
package com.volvo.ai.analytic.center.service.impl ;
import cn.hutool.core.date.DateUtil ;
import com.alibaba.cloud.commons.lang.StringUtils ;
import com.alibaba.fastjson.JSON ;
import com.alibaba.fastjson.JSONObject ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.volvo.ai.analytic.center.constant.Constant ;
import com.volvo.ai.analytic.center.dto.req.* ;
import com.volvo.ai.analytic.center.dto.resp.* ;
import com.volvo.ai.analytic.center.entity.DataMaskingRule ;
import com.volvo.ai.analytic.center.entity.DiffdefeatApprove ;
import com.volvo.ai.analytic.center.entity.MqMessageRecord ;
import com.volvo.ai.analytic.center.enums.MqTaskStatusEnum ;
import com.volvo.ai.analytic.center.enums.SubSinceTypeEnum ;
import com.volvo.ai.analytic.center.mapper.MqMessageRecordMapper ;
import com.volvo.ai.analytic.center.service.DataMaskingRuleService ;
import com.volvo.ai.analytic.center.service.DiFyService ;
import com.volvo.ai.analytic.center.service.DiffdefeatApproveService ;
import com.volvo.ai.analytic.center.service.MqMessageRecordService ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.util.CollectionUtils ;
import java.util.Date ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.stream.Collectors ;
@Slf4j
@Service
public class MqMessageRecordServiceImpl extends ServiceImpl < MqMessageRecordMapper , MqMessageRecord > implements MqMessageRecordService {
@Autowired
private DataMaskingRuleService dataMaskingRuleService ;
@Autowired
private DiffdefeatApproveService diffdefeatApproveService ;
@Autowired
private DiFyService diFyService ;
/**
* 处理Mq消息
* @param message
* @return
*/
@Override
public boolean processMessageByMQ ( String message ) {
log . info ( " message: {} " , message ) ;
RabbitMqFormData oldItem = JSONObject . parseObject ( message , RabbitMqFormData . class ) ;
log . info ( " 真假战败数据已获取 {} " , oldItem . getFormId ( ) ) ;
MqMessageRecord curMQMessageRecord = new MqMessageRecord ( ) ;
try {
curMQMessageRecord . setSinceType ( oldItem . getSinceType ( ) ) ;
curMQMessageRecord . setSubSinceType ( oldItem . getSubSinceType ( ) ) ;
curMQMessageRecord . setBizNo ( oldItem . getFormId ( ) ) ;
curMQMessageRecord . setSubBizNo ( oldItem . getFormId ( ) ) ;
curMQMessageRecord . setMessageContent ( message ) ;
curMQMessageRecord . setRetryCount ( 0 ) ;
curMQMessageRecord . setLastRetryTime ( new Date ( ) ) ;
curMQMessageRecord . setTaskStatus ( 0 ) ;
this . save ( curMQMessageRecord ) ;
log . info ( " 数据入库成功(MQMessageRecord) " ) ;
} catch ( Exception ex ) {
log . error ( " 数据入库失败(MQMessageRecord) " , ex . getMessage ( ) ) ;
return false ;
}
if ( oldItem . getSinceType ( ) = = 2 & & oldItem . getSubSinceType ( ) = = SubSinceTypeEnum . SinceType51 . getCode ( ) ) {
log . info ( " 辨别数据为分析请求 " ) ;
DiffDefeatResult diffDefeatResult = null ;
DiffDefeatanAlysis diffDefeatanAlysis = JSON . parseObject ( JSON . toJSONString ( oldItem . getData ( ) ) , DiffDefeatanAlysis . class ) ;
DiffDefeatAnalyseOutputResult response = this . processChatRecord ( diffDefeatanAlysis ) ;
DiffDefeatAnalyseOutput output = response . getDiffDefeatAnalyseOutput ( ) ;
log . info ( " 数据分析完成 " ) ;
if ( output . getHandleStatus ( ) = = 200 ) {
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) ;
diffDefeatResult = JSONObject . parseObject ( output . getResultStr ( ) , DiffDefeatResult . class ) ;
} else if ( output . getHandleStatus ( ) = = 400 ) {
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) ;
diffDefeatResult = new DiffDefeatResult ( ) ;
diffDefeatResult . setUserStatus ( " 无效通话 " ) ;
diffDefeatResult . setAppointmentResult ( " 因为通话时间太短没有ASR转义文本, 因此判断为无效通话 " ) ;
} else {
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Failure . getCode ( ) ) ;
}
curMQMessageRecord . setRespCode ( output . getHandleStatus ( ) + " " ) ;
curMQMessageRecord . setRespContent ( output . getResultStr ( ) ) ;
this . updateById ( curMQMessageRecord ) ;
log . info ( " 数据更改分析状态完成 " ) ;
if ( curMQMessageRecord . getTaskStatus ( ) = = MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) {
log . info ( " 数据分析结果正常 " ) ;
DiffdefeatApprove curDiffdefeatApprove = new DiffdefeatApprove ( ) ;
String userStatus = getUserStatus ( diffDefeatResult . getUserStatus ( ) ) ;
curDiffdefeatApprove . setFormId ( oldItem . getFormId ( ) ) ;
curDiffdefeatApprove . setBusinessId ( diffDefeatanAlysis . getBusinessId ( ) ) ;
curDiffdefeatApprove . setUserStatus ( userStatus ) ;
curDiffdefeatApprove . setAppointmentResult ( diffDefeatResult . getAppointmentResult ( ) ) ;
curDiffdefeatApprove . setInputStr ( response . getContentStr ( ) ) ;
curDiffdefeatApprove . setDefeatTime ( diffDefeatanAlysis . getDefeatTime ( ) ) ;
diffdefeatApproveService . save ( curDiffdefeatApprove ) ;
log . info ( " 插入数据记录完成 " ) ;
DiffDefeatanCallbak curDiffDefeatanCallbak = new DiffDefeatanCallbak ( ) ;
curDiffDefeatanCallbak . setBusinessId ( diffDefeatanAlysis . getBusinessId ( ) ) ;
curDiffDefeatanCallbak . setResponseCode ( " 200 " ) ;
curDiffDefeatanCallbak . setLabel ( userStatus ) ;
curDiffDefeatanCallbak . setDescribe ( diffDefeatResult . getAppointmentResult ( ) ) ;
RabbitMqToData rabbitMqToData = new RabbitMqToData ( ) ;
rabbitMqToData . setFormId ( oldItem . getFormId ( ) ) ;
rabbitMqToData . setSinceType ( 2 ) ;
rabbitMqToData . setSubSinceType ( SubSinceTypeEnum . SinceType53 . getCode ( ) ) ;
rabbitMqToData . setData ( curDiffDefeatanCallbak ) ;
String callbakInput = JSONObject . toJSONString ( rabbitMqToData ) ;
//todo 发Mq给LTO
log . info ( " 发送回调MQ完成: {} " , callbakInput ) ;
} else {
log . info ( " 数据分析结果不正常,等待重试 " ) ;
return false ;
}
}
if ( oldItem . getSinceType ( ) = = 2 & & oldItem . getSubSinceType ( ) = = SubSinceTypeEnum . SinceType52 . getCode ( ) ) {
log . info ( " 辨别数据为审批结果 " ) ;
com . volvo . ai . analytic . center . dto . req . DiffDefeatanApprove curDiffDefeatanApprove = JSON . parseObject ( JSON . toJSONString ( oldItem . getData ( ) ) , com . volvo . ai . analytic . center . dto . req . DiffDefeatanApprove . class ) ;
DiffdefeatApprove approveEntity = diffdefeatApproveService . lambdaQuery ( ) . eq ( DiffdefeatApprove : : getFormId , oldItem . getFormId ( ) ) . last ( " limit 1 " ) . one ( ) ;
if ( approveEntity ! = null ) {
try {
approveEntity . setApproveCode ( curDiffDefeatanApprove . getApproveCode ( ) ) ;
approveEntity . setApproveResult ( curDiffDefeatanApprove . getApproveResult ( ) ) ;
approveEntity . setApproveOpinion ( curDiffDefeatanApprove . getApproveOpinion ( ) ) ;
diffdefeatApproveService . updateById ( approveEntity ) ;
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) ;
curMQMessageRecord . setRespCode ( " 200 " ) ;
this . updateById ( curMQMessageRecord ) ;
log . info ( " 审批结果更新完成 " ) ;
} catch ( Exception e ) {
log . error ( " 审批结果更新失败 " , e . getMessage ( ) ) ;
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Failure . getCode ( ) ) ;
curMQMessageRecord . setRespContent ( e . getMessage ( ) ) ;
curMQMessageRecord . setRespCode ( " 500 " ) ;
this . updateById ( curMQMessageRecord ) ;
}
}
}
return true ;
}
/**
* 处理聊天和语音数据
* @return
*/
@Override
public DiffDefeatAnalyseOutputResult processChatRecord ( DiffDefeatanAlysis input ) {
DiffDefeatAnalyseOutput output = new DiffDefeatAnalyseOutput ( ) ;
String contentStr = " " ;
//获取脱敏配置信息
List < DataMaskingRule > maskingRuleItems = dataMaskingRuleService . getDataMaskingRuleListByApplicationChannel ( Constant . CHANNEL_DCC ) ;
// 根据SourceId查询通话信息
List < String > sourceIds = input . getCallList ( ) . stream ( ) . map ( CallItem : : getSourceId ) . collect ( Collectors . toList ( ) ) ;
if ( ! sourceIds . isEmpty ( ) ) {
int asrCount = 0 ;
//todo 查询未完成解析的通话信息
if ( asrCount > 0 ) {
output . setHandleStatus ( 500 ) ;
output . setResultStr ( " 存在未完成解析的通话信息 " ) ;
return new DiffDefeatAnalyseOutputResult ( output , contentStr ) ;
}
//todo 查询到的通话数据
}
// 查询企微的数据
if ( input . getVdqwUserId ( ) = = null | | input . getVdqwCustomerId ( ) = = null ) {
log . info ( " 企微用户信息为空,不附加企微数据 " ) ;
} else {
//todo 查询到的企微数据
}
//todo 组装数据
if ( StringUtils . isEmpty ( contentStr ) ) {
output . setHandleStatus ( 400 ) ;
output . setResultStr ( " 语料信息为空 " ) ;
return new DiffDefeatAnalyseOutputResult ( output , contentStr ) ;
}
log . info ( " 开始脱敏 {} " , contentStr ) ;
RunMaskingRuleInput runMaskingRuleInput = new RunMaskingRuleInput ( ) ;
runMaskingRuleInput . setOpinionId ( input . getBusinessId ( ) ) ;
runMaskingRuleInput . setOldStr ( contentStr ) ;
runMaskingRuleInput . setDataMaskingRules ( maskingRuleItems ) ;
String summaryText = dataMaskingRuleService . runMaskingRule ( runMaskingRuleInput ) ;
log . info ( " 脱敏结果 {} " , summaryText ) ;
Map < String , Object > record = new HashMap < > ( ) ;
record . put ( " record " , summaryText ) ;
DiFyReq diFyReq = new DiFyReq ( ) ;
diFyReq . setUser ( " voc " ) ;
diFyReq . setFlowId ( " app-MVGxogM08CDCg7jMo7GNF6eS " ) ;
diFyReq . setInputs ( record ) ;
JSONObject difyResult = ( JSONObject ) diFyService . getDiFyObject ( diFyReq ) ;
output . setHandleStatus ( 200 ) ;
output . setResultStr ( difyResult . getString ( " result " ) ) ;
log . info ( " DiFy平台处理结果:{} " , output . getResultStr ( ) ) ;
return new DiffDefeatAnalyseOutputResult ( output , contentStr ) ;
}
@Override
public void processMessageByTask ( ) {
log . info ( " 开始处理任务 " ) ;
Date currTime = new Date ( ) ;
Date startTime = DateUtil . offsetDay ( currTime , - 3 ) ;
Date stopTime = DateUtil . offsetMinute ( currTime , - 5 ) ;
List < MqMessageRecord > mqMessageRecords = this . lambdaQuery ( )
. eq ( MqMessageRecord : : getTaskStatus , MqTaskStatusEnum . Analysis_Failure . getCode ( ) )
. eq ( MqMessageRecord : : getSinceType , 2 )
. le ( MqMessageRecord : : getRetryCount , 3 )
. ge ( MqMessageRecord : : getCreateTime , startTime )
. lt ( MqMessageRecord : : getLastRetryTime , stopTime )
. list ( ) ;
if ( CollectionUtils . isEmpty ( mqMessageRecords ) ) {
return ;
}
for ( MqMessageRecord curMQMessageRecord : mqMessageRecords ) {
log . info ( " 辨别数据为分析请求 " ) ;
RabbitMqFormData oldItem = JSONObject . parseObject ( curMQMessageRecord . getMessageContent ( ) , RabbitMqFormData . class ) ;
if ( curMQMessageRecord . getSinceType ( ) = = 2 & & curMQMessageRecord . getSubSinceType ( ) = = SubSinceTypeEnum . SinceType51 . getCode ( ) ) {
try {
DiffDefeatResult diffDefeatResult = null ;
DiffDefeatanAlysis diffDefeatanAlysis = JSON . parseObject ( JSON . toJSONString ( oldItem . getData ( ) ) , DiffDefeatanAlysis . class ) ;
DiffDefeatAnalyseOutputResult response = this . processChatRecord ( diffDefeatanAlysis ) ;
DiffDefeatAnalyseOutput output = response . getDiffDefeatAnalyseOutput ( ) ;
log . info ( " 数据分析完成 " ) ;
if ( output . getHandleStatus ( ) = = 200 ) {
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) ;
diffDefeatResult = JSONObject . parseObject ( output . getResultStr ( ) , DiffDefeatResult . class ) ;
} else if ( output . getHandleStatus ( ) = = 400 ) {
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) ;
diffDefeatResult = new DiffDefeatResult ( ) ;
diffDefeatResult . setUserStatus ( " 无效通话 " ) ;
diffDefeatResult . setAppointmentResult ( " 因为通话时间太短没有ASR转义文本, 因此判断为无效通话 " ) ;
} else {
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Failure . getCode ( ) ) ;
}
if ( curMQMessageRecord . getTaskStatus ( ) = = MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) {
String userStatus = getUserStatus ( diffDefeatResult . getUserStatus ( ) ) ;
DiffdefeatApprove curDiffdefeatApprove = diffdefeatApproveService . lambdaQuery ( )
. eq ( DiffdefeatApprove : : getFormId , oldItem . getFormId ( ) )
. eq ( DiffdefeatApprove : : getBusinessId , curMQMessageRecord . getSubBizNo ( ) )
. orderByDesc ( DiffdefeatApprove : : getId )
. last ( " limit 1 " ) . one ( ) ;
if ( curDiffdefeatApprove ! = null ) {
curDiffdefeatApprove . setAppointmentResult ( diffDefeatResult . getAppointmentResult ( ) ) ;
curDiffdefeatApprove . setUserStatus ( userStatus ) ;
curDiffdefeatApprove . setInputStr ( response . getContentStr ( ) ) ;
} else {
log . info ( " 数据分析结果正常 " ) ;
curDiffdefeatApprove = new DiffdefeatApprove ( ) ;
curDiffdefeatApprove . setFormId ( oldItem . getFormId ( ) ) ;
curDiffdefeatApprove . setBusinessId ( diffDefeatanAlysis . getBusinessId ( ) ) ;
curDiffdefeatApprove . setUserStatus ( userStatus ) ;
curDiffdefeatApprove . setAppointmentResult ( diffDefeatResult . getAppointmentResult ( ) ) ;
curDiffdefeatApprove . setInputStr ( response . getContentStr ( ) ) ;
curDiffdefeatApprove . setDefeatTime ( diffDefeatanAlysis . getDefeatTime ( ) ) ;
diffdefeatApproveService . save ( curDiffdefeatApprove ) ;
log . info ( " 插入数据记录完成 " ) ;
}
DiffDefeatanCallbak curDiffDefeatanCallbak = new DiffDefeatanCallbak ( ) ;
curDiffDefeatanCallbak . setBusinessId ( diffDefeatanAlysis . getBusinessId ( ) ) ;
curDiffDefeatanCallbak . setResponseCode ( " 200 " ) ;
curDiffDefeatanCallbak . setLabel ( userStatus ) ;
curDiffDefeatanCallbak . setDescribe ( diffDefeatResult . getAppointmentResult ( ) ) ;
RabbitMqToData rabbitMqToData = new RabbitMqToData ( ) ;
rabbitMqToData . setFormId ( oldItem . getFormId ( ) ) ;
rabbitMqToData . setSinceType ( 2 ) ;
rabbitMqToData . setSubSinceType ( SubSinceTypeEnum . SinceType53 . getCode ( ) ) ;
rabbitMqToData . setData ( curDiffDefeatanCallbak ) ;
String callbakInput = JSONObject . toJSONString ( rabbitMqToData ) ;
//todo 发Mq给LTO
log . info ( " 发送回调MQ完成: {} " , callbakInput ) ;
}
curMQMessageRecord . setRespCode ( output . getHandleStatus ( ) + " " ) ;
curMQMessageRecord . setRespContent ( output . getResultStr ( ) ) ;
curMQMessageRecord . setLastRetryTime ( new Date ( ) ) ;
curMQMessageRecord . setRetryCount ( curMQMessageRecord . getRetryCount ( ) + 1 ) ;
this . updateById ( curMQMessageRecord ) ;
log . info ( " 数据更改分析状态完成 " ) ;
} catch ( Exception e ) {
log . error ( " 数据解析异常: {} " , e . getMessage ( ) ) ;
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Failure . getCode ( ) ) ;
curMQMessageRecord . setRespCode ( " 500 " ) ;
curMQMessageRecord . setRespContent ( e . getMessage ( ) ) ;
curMQMessageRecord . setLastRetryTime ( new Date ( ) ) ;
curMQMessageRecord . setRetryCount ( curMQMessageRecord . getRetryCount ( ) + 1 ) ;
this . updateById ( curMQMessageRecord ) ;
}
}
if ( curMQMessageRecord . getSinceType ( ) = = 2 & & curMQMessageRecord . getSubSinceType ( ) = = SubSinceTypeEnum . SinceType52 . getCode ( ) ) {
try {
DiffdefeatApprove approveEntity = diffdefeatApproveService . lambdaQuery ( )
. eq ( DiffdefeatApprove : : getFormId , oldItem . getFormId ( ) )
. eq ( DiffdefeatApprove : : getBusinessId , curMQMessageRecord . getSubBizNo ( ) )
. orderByDesc ( DiffdefeatApprove : : getId )
. last ( " limit 1 " ) . one ( ) ;
DiffDefeatanApprove curDiffDefeatanApprove = JSON . parseObject ( JSON . toJSONString ( oldItem . getData ( ) ) , DiffDefeatanApprove . class ) ;
if ( approveEntity ! = null ) {
approveEntity . setApproveCode ( curDiffDefeatanApprove . getApproveCode ( ) ) ;
approveEntity . setApproveResult ( curDiffDefeatanApprove . getApproveResult ( ) ) ;
approveEntity . setApproveOpinion ( curDiffDefeatanApprove . getApproveOpinion ( ) ) ;
diffdefeatApproveService . updateById ( approveEntity ) ;
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Completion . getCode ( ) ) ;
curMQMessageRecord . setRespCode ( " 200 " ) ;
curMQMessageRecord . setRespContent ( " " ) ;
curMQMessageRecord . setLastRetryTime ( new Date ( ) ) ;
curMQMessageRecord . setRetryCount ( curMQMessageRecord . getRetryCount ( ) + 1 ) ;
this . updateById ( curMQMessageRecord ) ;
}
} catch ( Exception e ) {
log . error ( " 数据解析异常: {} " , e . getMessage ( ) ) ;
curMQMessageRecord . setTaskStatus ( MqTaskStatusEnum . Analysis_Failure . getCode ( ) ) ;
curMQMessageRecord . setRespCode ( " 500 " ) ;
curMQMessageRecord . setRespContent ( e . getMessage ( ) ) ;
curMQMessageRecord . setLastRetryTime ( new Date ( ) ) ;
curMQMessageRecord . setRetryCount ( curMQMessageRecord . getRetryCount ( ) + 1 ) ;
this . updateById ( curMQMessageRecord ) ;
}
}
}
}
private String getUserStatus ( String oldStr )
{
if ( oldStr = = " 确认到店 " ) {
return " 客户确认将到店 " ;
} else if ( oldStr = = " 继续观望 " ) {
return " 客户继续观望中 " ;
} else if ( oldStr = = " 放弃购车 " ) {
return " 客户放弃购车 " ;
} else if ( oldStr = = " 无效通话 " ) {
return " 近三天无有效通话记录 " ;
}
return oldStr ;
}
}