|
@@ -3,10 +3,12 @@ package com.fs.fastGpt.service.impl;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.fs.common.annotation.Excel;
|
|
import com.fs.common.annotation.Excel;
|
|
|
import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.utils.PinYinUtil;
|
|
import com.fs.common.utils.PinYinUtil;
|
|
|
|
|
+import com.fs.common.utils.http.HttpUtils;
|
|
|
import com.fs.company.domain.CompanyConfig;
|
|
import com.fs.company.domain.CompanyConfig;
|
|
|
import com.fs.company.mapper.CompanyConfigMapper;
|
|
import com.fs.company.mapper.CompanyConfigMapper;
|
|
|
import com.fs.config.ai.AiHostProper;
|
|
import com.fs.config.ai.AiHostProper;
|
|
@@ -65,14 +67,13 @@ import com.vdurmont.emoji.EmojiParser;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.jetbrains.annotations.Nullable;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
|
+import org.json.JSONObject;
|
|
|
import org.redisson.api.RLock;
|
|
import org.redisson.api.RLock;
|
|
|
import org.redisson.api.RedissonClient;
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
-import org.springframework.transaction.annotation.Propagation;
|
|
|
|
|
-import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
|
import java.time.DayOfWeek;
|
|
import java.time.DayOfWeek;
|
|
@@ -80,7 +81,7 @@ import java.time.LocalDate;
|
|
|
import java.time.LocalTime;
|
|
import java.time.LocalTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
+import java.util.concurrent.*;
|
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
import java.util.regex.Pattern;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
@@ -180,6 +181,21 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
private static final String AI_REPLY = "AI_REPLY:";
|
|
private static final String AI_REPLY = "AI_REPLY:";
|
|
|
private static final String AI_REPLY_TAG = "AI_REPLY_TAG:";
|
|
private static final String AI_REPLY_TAG = "AI_REPLY_TAG:";
|
|
|
|
|
|
|
|
|
|
+ private final ExecutorService executor = new ThreadPoolExecutor(
|
|
|
|
|
+ 8, 32, 60L, TimeUnit.SECONDS,
|
|
|
|
|
+ new LinkedBlockingQueue<>(1000),
|
|
|
|
|
+ new ThreadFactory() {
|
|
|
|
|
+ private final ThreadFactory defaultFactory = Executors.defaultThreadFactory();
|
|
|
|
|
+ private int count = 1;
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Thread newThread(Runnable r) {
|
|
|
|
|
+ Thread thread = defaultFactory.newThread(r);
|
|
|
|
|
+ thread.setName("ai-im-exec-" + count++);
|
|
|
|
|
+ return thread;
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ new ThreadPoolExecutor.CallerRunsPolicy() // 拒绝策略
|
|
|
|
|
+ );
|
|
|
|
|
|
|
|
/** Ai半小时未回复提醒 **/
|
|
/** Ai半小时未回复提醒 **/
|
|
|
/**
|
|
/**
|
|
@@ -299,7 +315,7 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
if (result.isLongText()){
|
|
if (result.isLongText()){
|
|
|
//新增用户信息
|
|
//新增用户信息
|
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
|
- sendAiMsg(content,sender,uid,serverId);
|
|
|
|
|
|
|
+ sendAiMsg(content,sender,uid,serverId, user.getId(), qwExternalContacts.getExternalUserId());
|
|
|
|
|
|
|
|
}else {
|
|
}else {
|
|
|
String sa = contentKh.replaceAll("】\n", "】").replaceAll("\n【", "【");
|
|
String sa = contentKh.replaceAll("】\n", "】").replaceAll("\n【", "【");
|
|
@@ -313,7 +329,7 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
//新增用户信息
|
|
//新增用户信息
|
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
|
for (String msg : countList) {
|
|
for (String msg : countList) {
|
|
|
- sendAiMsg(msg,sender,uid,serverId);
|
|
|
|
|
|
|
+ sendAiMsg(msg,sender,uid,serverId, user.getId(), qwExternalContacts.getExternalUserId());
|
|
|
try {
|
|
try {
|
|
|
Thread.sleep(10000);
|
|
Thread.sleep(10000);
|
|
|
} catch (InterruptedException e) {
|
|
} catch (InterruptedException e) {
|
|
@@ -575,9 +591,9 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
//新增用户信息
|
|
//新增用户信息
|
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
|
if (type==16){
|
|
if (type==16){
|
|
|
- sendAiVoiceMsg(content,sender,uid,serverId,user);
|
|
|
|
|
|
|
+ sendAiVoiceMsg(content,sender,uid,serverId,user, qwExternalContacts.getExternalUserId());
|
|
|
}else {
|
|
}else {
|
|
|
- sendAiMsg(content,sender,uid,serverId);
|
|
|
|
|
|
|
+ sendAiMsg(content,sender,uid,serverId, user.getId(), qwExternalContacts.getExternalUserId());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}else {
|
|
}else {
|
|
@@ -594,9 +610,9 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
addUserInfo(contentKh, qwExternalContacts.getId(),fastGptChatSession);
|
|
|
for (String msg : countList) {
|
|
for (String msg : countList) {
|
|
|
if (type==16){
|
|
if (type==16){
|
|
|
- sendAiVoiceMsg(msg,sender,uid,serverId,user);
|
|
|
|
|
|
|
+ sendAiVoiceMsg(msg,sender,uid,serverId,user, qwExternalContacts.getExternalUserId());
|
|
|
}else {
|
|
}else {
|
|
|
- sendAiMsg(msg,sender,uid,serverId);
|
|
|
|
|
|
|
+ sendAiMsg(msg,sender,uid,serverId, user.getId(), qwExternalContacts.getExternalUserId());
|
|
|
}
|
|
}
|
|
|
try {
|
|
try {
|
|
|
Thread.sleep(10000);
|
|
Thread.sleep(10000);
|
|
@@ -961,7 +977,7 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
return maskedContent;
|
|
return maskedContent;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void sendAiVoiceMsg(String content, Long sendId , String uuid,Long serverId,QwUser user) {
|
|
|
|
|
|
|
+ private void sendAiVoiceMsg(String content, Long sendId , String uuid,Long serverId,QwUser user, String extUserId) {
|
|
|
if (content == null || content.trim().isEmpty()){
|
|
if (content == null || content.trim().isEmpty()){
|
|
|
System.out.println("输出为空格");
|
|
System.out.println("输出为空格");
|
|
|
return;
|
|
return;
|
|
@@ -1000,11 +1016,37 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
WxWorkResponseDTO<WxwSendCDNVoiceMsgRespDTO> wxwSendCDNVoiceMsgRespDTOWxWorkResponseDTO = wxWorkService.SendCDNVoiceMsg(wxwSendCDNVoiceMsgDTO, serverId);
|
|
WxWorkResponseDTO<WxwSendCDNVoiceMsgRespDTO> wxwSendCDNVoiceMsgRespDTOWxWorkResponseDTO = wxWorkService.SendCDNVoiceMsg(wxwSendCDNVoiceMsgDTO, serverId);
|
|
|
System.out.println(wxwSendCDNVoiceMsgRespDTOWxWorkResponseDTO);
|
|
System.out.println(wxwSendCDNVoiceMsgRespDTOWxWorkResponseDTO);
|
|
|
|
|
|
|
|
|
|
+ if (wxwSendCDNVoiceMsgRespDTOWxWorkResponseDTO.getErrcode() == 0) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ WxwSendCDNVoiceMsgRespDTO dtoData = wxwSendCDNVoiceMsgRespDTOWxWorkResponseDTO.getData();
|
|
|
|
|
+
|
|
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
|
|
+ json.put("url", data.getUrl());
|
|
|
|
|
+ json.put("content", content);
|
|
|
|
|
+
|
|
|
|
|
+ // 保存聊天消息
|
|
|
|
|
+ QwMessageListVO message = this.saveQwMsg(user.getId(), extUserId, json.toString(), 4, false, dtoData.getMsg_id(), dtoData.getApp_info());
|
|
|
|
|
+ if (message == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 发送webSocket
|
|
|
|
|
+ executor.execute(() -> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ HttpUtils.doPost(aiHostProper.getIpadUrl() + "/msg/sendQwImMsg", JSON.toJSONString(message));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("转发ai回复消息失败 err: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("保存ai回复消息失败 err: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Autowired
|
|
@Autowired
|
|
|
QwSopLogsMapper qwSopLogsMapper;
|
|
QwSopLogsMapper qwSopLogsMapper;
|
|
|
- private void sendAiMsg(String content, Long sendId , String uuid,Long serverId) {
|
|
|
|
|
|
|
+ private void sendAiMsg(String content, Long sendId , String uuid,Long serverId, Long qwUserId, String extUserId) {
|
|
|
if (content == null || content.trim().isEmpty()){
|
|
if (content == null || content.trim().isEmpty()){
|
|
|
System.out.println("输出为空格");
|
|
System.out.println("输出为空格");
|
|
|
return;
|
|
return;
|
|
@@ -1017,6 +1059,27 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
WxWorkResponseDTO<WxWorkSendTextMsgRespDTO> wxWorkSendTextMsgRespDTOWxWorkResponseDTO = wxWorkService.SendTextMsg(wxWorkSendTextMsgDTO,serverId);
|
|
WxWorkResponseDTO<WxWorkSendTextMsgRespDTO> wxWorkSendTextMsgRespDTOWxWorkResponseDTO = wxWorkService.SendTextMsg(wxWorkSendTextMsgDTO,serverId);
|
|
|
WxWorkSendTextMsgRespDTO data = wxWorkSendTextMsgRespDTOWxWorkResponseDTO.getData();
|
|
WxWorkSendTextMsgRespDTO data = wxWorkSendTextMsgRespDTOWxWorkResponseDTO.getData();
|
|
|
|
|
|
|
|
|
|
+ if (wxWorkSendTextMsgRespDTOWxWorkResponseDTO.getErrcode() == 0) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ WxWorkSendTextMsgRespDTO dtoData = wxWorkSendTextMsgRespDTOWxWorkResponseDTO.getData();
|
|
|
|
|
+
|
|
|
|
|
+ // 保存聊天消息
|
|
|
|
|
+ QwMessageListVO message = this.saveQwMsg(qwUserId, extUserId, content, 1, false, dtoData.getMsg_id(), dtoData.getApp_info());
|
|
|
|
|
+ if (message == null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 发送webSocket
|
|
|
|
|
+ executor.execute(() -> {
|
|
|
|
|
+ try {
|
|
|
|
|
+ HttpUtils.doPost(aiHostProper.getIpadUrl() + "/msg/sendQwImMsg", JSON.toJSONString(message));
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("转发ai回复消息失败 err: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ log.error("保存ai回复消息失败 err: {}", e.getMessage(), e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
/**
|
|
/**
|
|
@@ -2145,9 +2208,11 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
* @param isRoom 是否群聊
|
|
* @param isRoom 是否群聊
|
|
|
* @param chatId 会话ID(群聊才有)
|
|
* @param chatId 会话ID(群聊才有)
|
|
|
* @param chatAvatar 群头像(群聊才有)
|
|
* @param chatAvatar 群头像(群聊才有)
|
|
|
|
|
+ * @param qwMsgId 企微消息ID
|
|
|
|
|
+ * @param qwAppInfo 企微appInfo
|
|
|
*/
|
|
*/
|
|
|
@Override
|
|
@Override
|
|
|
- public QwMessageListVO saveQwMsg(Long qwUserId, Long senderVid, Long receiverVid, Long serverId, String content, String uuid, String json, int msgType, boolean isRoom, String chatId, String chatAvatar) {
|
|
|
|
|
|
|
+ public QwMessageListVO saveQwMsg(Long qwUserId, Long senderVid, Long receiverVid, Long serverId, String content, String uuid, String json, int msgType, boolean isRoom, String chatId, String chatAvatar, Long qwMsgId, String qwAppInfo) {
|
|
|
// 查询企微用户
|
|
// 查询企微用户
|
|
|
QwUser qwUser = qwUserService.selectQwUserById(qwUserId);
|
|
QwUser qwUser = qwUserService.selectQwUserById(qwUserId);
|
|
|
if (Objects.isNull(qwUser)) {
|
|
if (Objects.isNull(qwUser)) {
|
|
@@ -2214,9 +2279,16 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
}
|
|
}
|
|
|
qwMsg.setAvatar(sender.getAvatar());
|
|
qwMsg.setAvatar(sender.getAvatar());
|
|
|
qwMsg.setNickName(sender.getUserName());
|
|
qwMsg.setNickName(sender.getUserName());
|
|
|
|
|
+ qwMsg.setQwMsgId(qwMsgId);
|
|
|
|
|
+ qwMsg.setQwAppInfo(qwAppInfo);
|
|
|
qwMsgMapper.insertQwMsg(qwMsg);
|
|
qwMsgMapper.insertQwMsg(qwMsg);
|
|
|
log.debug("保存企微聊天记录 msgId: {}", qwMsg.getMsgId());
|
|
log.debug("保存企微聊天记录 msgId: {}", qwMsg.getMsgId());
|
|
|
|
|
|
|
|
|
|
+ qwSession.setLastMsgId(qwMsg.getMsgId());
|
|
|
|
|
+ qwSession.setLastSendTime(qwMsg.getCreateTime().getTime());
|
|
|
|
|
+ qwSession.setLastContent(qwMsg.getContent());
|
|
|
|
|
+ qwSessionMapper.updateQwSession(qwSession);
|
|
|
|
|
+
|
|
|
// 组装返回消息结构
|
|
// 组装返回消息结构
|
|
|
QwMessageListVO listVO = new QwMessageListVO();
|
|
QwMessageListVO listVO = new QwMessageListVO();
|
|
|
QWFromUser qwFromUser = new QWFromUser();
|
|
QWFromUser qwFromUser = new QWFromUser();
|
|
@@ -2243,12 +2315,90 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
return listVO;
|
|
return listVO;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存企微聊天信息
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public QwMessageListVO saveQwMsg(Long qwUserId, String exId, String content, int msgType, boolean isRoom, Long qwMsgId, String qwAppInfo) {
|
|
|
|
|
+ QwUser qwUser = qwUserService.selectQwUserById(qwUserId);
|
|
|
|
|
+ if (Objects.isNull(qwUser)) {
|
|
|
|
|
+ log.warn("企微用户不存在 qwUserId: {}", qwUserId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查询会话
|
|
|
|
|
+ QwSession qwSession;
|
|
|
|
|
+ if (isRoom) {
|
|
|
|
|
+ qwSession = qwSessionMapper.selectQwSessionByChatIdAndQwUserId(exId, qwUser.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ QwExternalContact externalContact = qwExternalContactMapper.selectQwExternalContactByExternalUserIdAndQwUserId(exId, qwUser.getCorpId(), qwUser.getQwUserId());
|
|
|
|
|
+ if (Objects.isNull(externalContact)) {
|
|
|
|
|
+ log.warn("企微外部联系人不存在 extId: {}", exId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+ qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(externalContact.getId(), qwUser.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (qwSession == null) {
|
|
|
|
|
+ log.warn("获取session失败 qwUserId: {}, extId: {}", qwUserId, exId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 保存聊天消息
|
|
|
|
|
+ QwMsg qwMsg = new QwMsg();
|
|
|
|
|
+ qwMsg.setContent(content);
|
|
|
|
|
+ qwMsg.setSessionId(qwSession.getSessionId());
|
|
|
|
|
+ qwMsg.setSendType(2);
|
|
|
|
|
+ qwMsg.setCompanyId(qwUser.getCompanyId());
|
|
|
|
|
+ qwMsg.setCompanyUserId(qwUser.getCompanyUserId());
|
|
|
|
|
+ qwMsg.setMsgType(msgType);
|
|
|
|
|
+ qwMsg.setMsgJson(content);
|
|
|
|
|
+ qwMsg.setStatus(0);
|
|
|
|
|
+ qwMsg.setQwUserId(qwSession.getQwUserId());
|
|
|
|
|
+ qwMsg.setCreateTime(new Date());
|
|
|
|
|
+ qwMsg.setAvatar(qwUser.getAvatar());
|
|
|
|
|
+ qwMsg.setNickName(qwUser.getQwUserName());
|
|
|
|
|
+ qwMsg.setQwMsgId(qwMsgId);
|
|
|
|
|
+ qwMsg.setQwAppInfo(qwAppInfo);
|
|
|
|
|
+ qwMsgMapper.insertQwMsg(qwMsg);
|
|
|
|
|
+ log.debug("保存企微聊天记录 msgId: {}", qwMsg.getMsgId());
|
|
|
|
|
+
|
|
|
|
|
+ qwSession.setLastMsgId(qwMsg.getMsgId());
|
|
|
|
|
+ qwSession.setLastSendTime(qwMsg.getCreateTime().getTime());
|
|
|
|
|
+ qwSession.setLastContent(qwMsg.getContent());
|
|
|
|
|
+ qwSessionMapper.updateQwSession(qwSession);
|
|
|
|
|
+
|
|
|
|
|
+ // 组装返回消息结构
|
|
|
|
|
+ QwMessageListVO listVO = new QwMessageListVO();
|
|
|
|
|
+ QWFromUser qwFromUser = new QWFromUser();
|
|
|
|
|
+ qwFromUser.setId(qwUser.getId());
|
|
|
|
|
+ qwFromUser.setAvatar(qwUser.getAvatar());
|
|
|
|
|
+ qwFromUser.setDisplayName(qwUser.getQwUserName());
|
|
|
|
|
+
|
|
|
|
|
+ listVO.setCompanyUserId(qwUser.getCompanyUserId());
|
|
|
|
|
+ String type = "text";
|
|
|
|
|
+ MsgType messageType = MsgType.getMsgType(msgType);
|
|
|
|
|
+ if (Objects.nonNull(messageType)){
|
|
|
|
|
+ type = messageType.getValue();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ listVO.setType(type);
|
|
|
|
|
+ listVO.setStatus("succeed");
|
|
|
|
|
+ listVO.setExtId(qwMsg.getQwExtId());
|
|
|
|
|
+ listVO.setFromUser(qwFromUser);
|
|
|
|
|
+ listVO.setSendTime(qwMsg.getCreateTime().getTime());
|
|
|
|
|
+ listVO.setId(qwMsg.getMsgId().toString());
|
|
|
|
|
+ listVO.setContent(qwMsg.getContent());
|
|
|
|
|
+ listVO.setToContactId(String.valueOf(qwSession.getSessionId()));
|
|
|
|
|
+ listVO.setAppKey(qwUser.getAppKey());
|
|
|
|
|
+ return listVO;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获取单聊session
|
|
* 获取单聊session
|
|
|
*/
|
|
*/
|
|
|
private QwSession getQwSession(QwImUserDTO extUser, QwUser qwUser, Long extWxId) {
|
|
private QwSession getQwSession(QwImUserDTO extUser, QwUser qwUser, Long extWxId) {
|
|
|
QwSession qwSession;
|
|
QwSession qwSession;
|
|
|
- String chatId;
|
|
|
|
|
qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(extUser.getUserId(), qwUser.getId());
|
|
qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(extUser.getUserId(), qwUser.getId());
|
|
|
String firstLetter = PinYinUtil.getFirstLetter(extUser.getUserName());
|
|
String firstLetter = PinYinUtil.getFirstLetter(extUser.getUserName());
|
|
|
if (qwSession == null) {
|
|
if (qwSession == null) {
|
|
@@ -2264,7 +2414,7 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(extUser.getUserId(), qwUser.getId());
|
|
qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(extUser.getUserId(), qwUser.getId());
|
|
|
if (qwSession == null) {
|
|
if (qwSession == null) {
|
|
|
qwSession = new QwSession();
|
|
qwSession = new QwSession();
|
|
|
- chatId = UUID.randomUUID().toString();
|
|
|
|
|
|
|
+ String chatId = UUID.randomUUID().toString();
|
|
|
qwSession.setChatId(chatId);
|
|
qwSession.setChatId(chatId);
|
|
|
qwSession.setCorpId(qwUser.getCorpId());
|
|
qwSession.setCorpId(qwUser.getCorpId());
|
|
|
qwSession.setQwExtWxId(String.valueOf(extWxId));
|
|
qwSession.setQwExtWxId(String.valueOf(extWxId));
|
|
@@ -2404,4 +2554,96 @@ public class AiHookServiceImpl implements AiHookService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 修改消息
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public QwMessageListVO updateQwMsg(Long qwUserId, Long senderVid, Long receiverVid, Long serverId, String uuid, boolean isRoom, String chatId, String qwAppInfo) {
|
|
|
|
|
+ QwUser qwUser = qwUserService.selectQwUserById(qwUserId);
|
|
|
|
|
+ if (Objects.isNull(qwUser)) {
|
|
|
|
|
+ log.warn("企微用户不存在 qwUserId: {}", qwUserId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取发送人
|
|
|
|
|
+ QwImUserDTO sender = getUserByVid(senderVid, uuid, serverId, qwUser.getCorpId(), qwUser.getQwUserId());
|
|
|
|
|
+ if (Objects.isNull(sender)) {
|
|
|
|
|
+ log.warn("sender用户不存在 senderVid: {}", senderVid);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 获取接收者
|
|
|
|
|
+ QwImUserDTO receiver = getUserByVid(receiverVid, uuid, serverId, qwUser.getCorpId(), qwUser.getQwUserId());
|
|
|
|
|
+
|
|
|
|
|
+ // 查询会话
|
|
|
|
|
+ QwSession qwSession = null;
|
|
|
|
|
+ if (isRoom) {
|
|
|
|
|
+ QwGroupChat qwGroupChat = qwGroupChatMapper.selectQwGroupChatByChatId(chatId);
|
|
|
|
|
+ if (Objects.isNull(qwGroupChat)){
|
|
|
|
|
+ log.warn("群聊不存在 serverId: {}, corpId: {}, qwUserId: {}, chatId: {}", qwUser.getServerId(), qwUser.getCorpId(), qwUser.getQwUserId(), chatId);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ qwSession = qwSessionMapper.selectQwSessionByChatIdAndQwUserId(chatId, qwUser.getId());
|
|
|
|
|
+ } else {
|
|
|
|
|
+ QwImUserDTO extUser = null;
|
|
|
|
|
+
|
|
|
|
|
+ // 获取外部联系人
|
|
|
|
|
+ if (sender.getUserType() == 1) {
|
|
|
|
|
+ extUser = sender;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (receiver != null && receiver.getUserType() == 1) {
|
|
|
|
|
+ extUser = receiver;
|
|
|
|
|
+ }
|
|
|
|
|
+ if (extUser != null) {
|
|
|
|
|
+ qwSession = qwSessionMapper.selectQwSessionByExtIdAndQwUserId(extUser.getUserId(), qwUser.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (qwSession == null) {
|
|
|
|
|
+ log.warn("获取session失败 senderVid: {}, receiverVid: {}", senderVid, receiverVid);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ QwMsg qwMsg = qwMsgMapper.selectQwMsgBySessionIdAndQwAppInfo(qwSession.getSessionId(), qwAppInfo);
|
|
|
|
|
+ if (qwMsg == null) {
|
|
|
|
|
+ log.warn("消息不存在 sessionId: {}, appInfo: {}", qwSession.getSessionId(), qwAppInfo);
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ qwMsg.setMsgType(6);
|
|
|
|
|
+ qwMsgMapper.updateQwMsg(qwMsg);
|
|
|
|
|
+
|
|
|
|
|
+ qwSession.setLastMsgId(qwMsg.getMsgId());
|
|
|
|
|
+ qwSession.setLastSendTime(qwMsg.getCreateTime().getTime());
|
|
|
|
|
+ qwSession.setLastContent("");
|
|
|
|
|
+ qwSessionMapper.updateQwSession(qwSession);
|
|
|
|
|
+
|
|
|
|
|
+ // 组装返回消息结构
|
|
|
|
|
+ QwMessageListVO listVO = new QwMessageListVO();
|
|
|
|
|
+ QWFromUser qwFromUser = new QWFromUser();
|
|
|
|
|
+ qwFromUser.setId(sender.getUserId());
|
|
|
|
|
+ qwFromUser.setAvatar(sender.getAvatar());
|
|
|
|
|
+ qwFromUser.setDisplayName(sender.getUserName());
|
|
|
|
|
+
|
|
|
|
|
+ listVO.setCompanyUserId(qwUser.getCompanyUserId());
|
|
|
|
|
+ String type = "text";
|
|
|
|
|
+ MsgType messageType = MsgType.getMsgType(qwMsg.getMsgType());
|
|
|
|
|
+ if (Objects.nonNull(messageType)){
|
|
|
|
|
+ type = messageType.getValue();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ listVO.setType(type);
|
|
|
|
|
+ listVO.setStatus("succeed");
|
|
|
|
|
+ listVO.setExtId(qwMsg.getQwExtId());
|
|
|
|
|
+ listVO.setFromUser(qwFromUser);
|
|
|
|
|
+ listVO.setSendTime(qwMsg.getCreateTime().getTime());
|
|
|
|
|
+ listVO.setId(qwMsg.getMsgId().toString());
|
|
|
|
|
+ listVO.setContent(qwMsg.getContent());
|
|
|
|
|
+ listVO.setToContactId(String.valueOf(qwSession.getSessionId()));
|
|
|
|
|
+ listVO.setAppKey(qwUser.getAppKey());
|
|
|
|
|
+ return listVO;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|