zoujiajian il y a 1 an
Parent
commit
8e45ea1b32

+ 28 - 33
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpServiceImpl.java

@@ -647,17 +647,6 @@ public class CorpServiceImpl implements CorpService {
 			String msgType = item.getMsgType();
 			corpKfAccountSessionChat.setOpenKfId(openKfid);
 			corpKfAccountSessionChat.setMsgid(msgId);
-			if (StrUtil.isEmpty(servicerUserId)) {
-				//寻找接待记录
-				CorpKfServicerSessionRecord corpKfServicerSessionRecord = corpKfServicerSessionRecordMapper.selectOne(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
-						.eq(CorpKfServicerSessionRecord::getOpenKfId, openKfId)
-						.eq(CorpKfServicerSessionRecord::getExternalUserid, externalUserId)
-						.eq(CorpKfServicerSessionRecord::getServiceState, 0)
-						.last("limit 1"));
-				if (corpKfServicerSessionRecord != null) {
-					corpKfAccountSessionChat.setSessionId(corpKfServicerSessionRecord.getMsgid());
-				}
-			}
 			corpKfAccountSessionChat.setServicerUserid(servicerUserId);
 			corpKfAccountSessionChat.setExternalUserid(externalUserId);
 			corpKfAccountSessionChat.setOrign(origin);
@@ -717,15 +706,6 @@ public class CorpServiceImpl implements CorpService {
 		String msgId = wxCpKfMsgItem.getMsgId();
 		String msgCode = wxCpKfMsgItem.getEvent().getMsgCode();
 		if (changeType == 1) {
-			CorpKfServicerSessionRecord corpKfServicerSessionRecord = corpKfServicerSessionRecordMapper.selectOne(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
-					.eq(CorpKfServicerSessionRecord::getOpenKfId, openKfId)
-					.eq(CorpKfServicerSessionRecord::getExternalUserid, externalUserId)
-					.le(CorpKfServicerSessionRecord::getStartTime, sendDate)
-					.eq(CorpKfServicerSessionRecord::getServicerUserid, 0).last("limit 1"));
-			//存在上一个错误接待会话
-			if (corpKfServicerSessionRecord != null) {
-				corpKfServicerSessionRecordMapper.deleteById(corpKfServicerSessionRecord.getId());
-			}
 			CorpKfServicerSessionRecord newSession = new CorpKfServicerSessionRecord();
 			newSession.setOpenKfId(openKfId);
 			newSession.setServicerUserid(newServicerUserId);
@@ -780,18 +760,6 @@ public class CorpServiceImpl implements CorpService {
 				}
 			}
 		}
-		Integer selectCount = corpKfServicerSessionRecordMapper.selectCount(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
-				.eq(CorpKfServicerSessionRecord::getMsgid, msgid));
-		//新增接待记录
-		if (selectCount == 0) {
-			CorpKfServicerSessionRecord corpKfServicerSessionRecord = new CorpKfServicerSessionRecord();
-			corpKfServicerSessionRecord.setOpenKfId(openKfId);
-			corpKfServicerSessionRecord.setMsgid(msgid);
-			corpKfServicerSessionRecord.setExternalUserid(event.getExternalUserId());
-			corpKfServicerSessionRecord.setStartTime(sendDate);
-			corpKfServicerSessionRecord.setServiceState(0);
-			corpKfServicerSessionRecordMapper.insert(corpKfServicerSessionRecord);
-		}
 		//分配接待人员
 		assignKfSession(corpId, openKfId, event.getExternalUserId(), corpKfAccountSessionConfig, sendDate, msgid);
 	}
@@ -856,6 +824,10 @@ public class CorpServiceImpl implements CorpService {
 	 * 会话状态为待处理 则分配接待人员
 	 */
 	public void assignKfSession(String corpId, String openKfId, String externalUserid, CorpKfAccountSessionConfig corpKfAccountSessionConfig, Date sendDate, String msgid) throws Exception {
+		//超过15分钟 进入会话时间不处理
+		if (sendDate.compareTo(DateUtil.offsetMinute(DateTime.now(), -15)) <= 0) {
+			return;
+		}
 		//查询用户会话状态 0待处理 	1智能助手接待
 		ServiceStateResponse serviceStateResponse = wxCorpOps.getCorpKfSessionStatus(corpId, openKfId, externalUserid);
 		Integer state = serviceStateResponse.getService_state();
@@ -959,10 +931,11 @@ public class CorpServiceImpl implements CorpService {
 				.eq(CorpKfServicerSessionRecord::getOpenKfId, openKfId)
 				.eq(CorpKfServicerSessionRecord::getServicerUserid, servicerUserid)
 				.eq(CorpKfServicerSessionRecord::getExternalUserid, externalUserId)
-				.le(CorpKfServicerSessionRecord::getStartTime, sendDate)
 				.eq(CorpKfServicerSessionRecord::getServiceState, 0)
 				.last("limit 1"));
 		if (corpKfServicerSessionRecord != null) {
+			//同步用户该次接待记录
+			syncUserChatSession(corpKfServicerSessionRecord.getId(), externalUserId);
 			corpKfServicerSessionRecord.setCloseTime(sendDate);
 			corpKfServicerSessionRecord.setServiceState(changeType != 2 ? 1 : changeType);
 			if (changeType != 2) {
@@ -982,6 +955,17 @@ public class CorpServiceImpl implements CorpService {
 			corpKfServicerSessionRecord.setMsgid(msgid);
 			corpKfServicerSessionRecord.setCloseTime(sendDate);
 			corpKfServicerSessionRecordMapper.insert(corpKfServicerSessionRecord);
+			//同步用户该次接待记录
+			syncUserChatSession(corpKfServicerSessionRecord.getId(), externalUserId);
+			//同步会话最早德发送时间
+			CorpKfAccountSessionChat beginChat = corpKfAccountSessionChatMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccountSessionChat.class)
+					.eq(CorpKfAccountSessionChat::getSessionId, corpKfServicerSessionRecord.getId())
+					.eq(CorpKfAccountSessionChat::getExternalUserid, externalUserId)
+					.orderByAsc(CorpKfAccountSessionChat::getId)
+					.last("limit 1"));
+			if (beginChat != null) {
+				corpKfServicerSessionRecord.setStartTime(beginChat.getSendDate());
+			}
 		}
 	}
 
@@ -1022,4 +1006,15 @@ public class CorpServiceImpl implements CorpService {
 		}
 		return thisHourDutyFollows;
 	}
+
+	/**
+	 * 同步接待用户聊天会话id
+	 */
+	public void syncUserChatSession(Long sessionId, String externalUserid) {
+		//同步用户该次接待记录
+		corpKfAccountSessionChatMapper.update(null, Wrappers.lambdaUpdate(CorpKfAccountSessionChat.class)
+				.set(CorpKfAccountSessionChat::getSessionId, sessionId)
+				.eq(CorpKfAccountSessionChat::getExternalUserid, externalUserid)
+				.isNull(CorpKfAccountSessionChat::getSessionId));
+	}
 }