zoujiajian hai 1 ano
pai
achega
3a4a27776a

+ 138 - 114
netflix-service/src/main/java/com/cyksj/service/corp/impl/CorpServiceImpl.java

@@ -541,9 +541,9 @@ public class CorpServiceImpl implements CorpService {
 				return null;
 			}
 			//账号会话设置
-			CorpKfAccountSessionConfig corpKfAccountSessionConfig = corpKfAccountSessionConfigMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccountSessionConfig.class)
+			CorpKfAccountSessionConfig corpKfAccountSessionConfig = Optional.ofNullable(corpKfAccountSessionConfigMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccountSessionConfig.class)
 					.eq(CorpKfAccountSessionConfig::getOpenKfId, openKfId)
-					.last("limit 1"));
+					.last("limit 1"))).orElse(new CorpKfAccountSessionConfig());
 			//同步消息进度
 			CorpKfAccountSyncMsgProgress corpKfAccountSyncMsgProgress = Optional.ofNullable(corpKfAccountSyncMsgProgressMapper.selectOne(Wrappers.lambdaQuery(CorpKfAccountSyncMsgProgress.class)
 					.eq(CorpKfAccountSyncMsgProgress::getOpenKfId, openKfId)
@@ -584,8 +584,8 @@ public class CorpServiceImpl implements CorpService {
 					sessionStatusChange(corpId, openKfId, item, corpKfAccountSessionConfig);
 				}
 			}
-			//同步账号信息
-			syncKfChatMsg(item);
+			//同步微信客服账号信息
+			syncKfChatMsg(corpId, openKfId, corpKfAccountSessionConfig, item);
 		}
 		cursor = wxCpKfMsgListResp.getNextCursor();
 		corpKfAccountSyncMsgProgress.setNextCursor(cursor);
@@ -595,7 +595,7 @@ public class CorpServiceImpl implements CorpService {
 		}
 	}
 
-	private void syncKfChatMsg(WxCpKfMsgListResp.WxCpKfMsgItem item) throws Exception {
+	private void syncKfChatMsg(String corpId, String openKfId, CorpKfAccountSessionConfig corpKfAccountSessionConfig, WxCpKfMsgListResp.WxCpKfMsgItem item) throws Exception {
 		CorpKfAccountSessionChat corpKfAccountSessionChat = new CorpKfAccountSessionChat();
 		Boolean record = false;
 		//文本
@@ -604,7 +604,6 @@ public class CorpServiceImpl implements CorpService {
 			record = true;
 		} else if (item.getMergedMsg() != null) {
 			//聊天记录 转发
-			corpKfAccountSessionChat.setLinkTitle(item.getMergedMsg().getTitle());
 			corpKfAccountSessionChat.setMsgContent(Jsons.toJson(item.getMergedMsg().getItem()));
 			record = true;
 		} else if (item.getVideo() != null) {
@@ -656,6 +655,11 @@ public class CorpServiceImpl implements CorpService {
 			corpKfAccountSessionChat.setSendTime(sendTime);
 			corpKfAccountSessionChat.setSendDate(DateTime.of(sendTime * 1000));
 			saveChatMsg(corpKfAccountSessionChat);
+
+			if (StrUtil.isNotEmpty(externalUserId)) {
+				//会话状态为待处理 则分配接待人员
+				assignKfSession(corpId, openKfId, externalUserId, corpKfAccountSessionConfig);
+			}
 		}
 	}
 
@@ -729,30 +733,34 @@ public class CorpServiceImpl implements CorpService {
 	}
 
 	private void sessionStatusChange(String corpId, String openKfId, WxCpKfMsgListResp.WxCpKfMsgItem wxCpKfMsgItem, CorpKfAccountSessionConfig corpKfAccountSessionConfig) throws Exception {
-		CorpKfServicerSessionRecord corpKfServicerSessionRecord = corpKfServicerSessionRecordMapper.selectOne(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
-				.eq(CorpKfServicerSessionRecord::getOpenKfId, openKfId)
-				.eq(CorpKfServicerSessionRecord::getMsgId, wxCpKfMsgItem.getMsgId())
-				.last("limit 1"));
-		Integer changeType = null;
-		if (corpKfServicerSessionRecord != null) {
-			WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
-			//变更类型,均为接待人员在企业微信客户端操作触发。1-从接待池接入会话 2-转接会话 3-结束会话 4-重新接入已结束/已转接会话
-			changeType = event.getChangeType();
-			//老会话已结束
-			if (changeType != 1) {
-				corpKfServicerSessionRecord.setServiceState(changeType == 3 ? 1 : 2);
-				//关闭
-				corpKfServicerSessionRecord.setCloseTime(DateTime.now());
-				corpKfServicerSessionRecordMapper.updateById(corpKfServicerSessionRecord);
-				//优先分配等待池中的用户
-				assignWaitingQueueUser(corpId, openKfId, wxCpKfMsgItem.getServicerUserId(), corpKfAccountSessionConfig);
-			}
-		}
-		//转接会话
-		String newServicerUserid = wxCpKfMsgItem.getNewServicerUserid();
-		if (StrUtil.isNotEmpty(newServicerUserid)) {
+		WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
+		Integer changeType = event.getChangeType();
+		String servicerUserId = event.getServicerUserId();
+		String externalUserId = event.getExternalUserId();
+		String msgId = wxCpKfMsgItem.getMsgId();
+		Long sendTime = wxCpKfMsgItem.getSendTime();
+		//1-从接待池接入会话 2-转接会话 3-结束会话 4-重新接入已结束/已转接会话
+		if (changeType == 1) {
+			CorpKfServicerSessionRecord newSession = new CorpKfServicerSessionRecord();
+			newSession.setOpenKfId(openKfId);
+			newSession.setServicerUserid(servicerUserId);
+			newSession.setExternalUserid(externalUserId);
+			newSession.setChangeType(changeType);
+			newSession.setServiceState(0);
+			newSession.setMsgId(msgId);
+			corpKfServicerSessionRecordMapper.insert(newSession);
+		}
+		//2-转接会话
+		if (changeType == 2) {
+			//设置表中会话状态
+			setDbSessionState(openKfId, externalUserId, sendTime);
 			//记录新的客服会话
-			transCorpKfSessionToServicer(corpId, openKfId, newServicerUserid, wxCpKfMsgItem.getExternalUserId(), changeType, 3);
+			String newServicerUserid = wxCpKfMsgItem.getNewServicerUserid();
+			transCorpKfSessionToServicer(corpId, openKfId, newServicerUserid, externalUserId, changeType, 3);
+		}
+		//3-结束会话 4-重新接入已结束/已转接会话
+		if (changeType == 3 || changeType == 4) {
+			setDbSessionState(openKfId, externalUserId, sendTime);
 		}
 	}
 
@@ -795,7 +803,7 @@ public class CorpServiceImpl implements CorpService {
 			wxCpKfMsgSendRequest.setMsgType("text");
 			wxCpKfMsgSendRequest.setCode(welcomeCode);
 			WxCorpKfMsgSendRequest.WxCpKfTextMsg textMsg = new WxCorpKfMsgSendRequest.WxCpKfTextMsg();
-			if (corpKfAccountSessionConfig != null && StrUtil.isNotEmpty(corpKfAccountSessionConfig.getWelcomeContent())) {
+			if (StrUtil.isNotEmpty(corpKfAccountSessionConfig.getWelcomeContent())) {
 				textMsg.setContent(corpKfAccountSessionConfig.getWelcomeContent());
 				wxCpKfMsgSendRequest.setText(textMsg);
 				wxCpKfMsgSendRequest.setToUser(welcomeCode);
@@ -807,41 +815,88 @@ public class CorpServiceImpl implements CorpService {
 				}
 			}
 		}
+		//分配接待人员
+		assignKfSession(corpId, openKfId, event.getExternalUserId(), corpKfAccountSessionConfig);
+	}
+
+	/**
+	 * 变更用户会话状态
+	 * serviceState 0未处理 1由智能助手接待 2待接入池排队中 3由人工接待 4 已结束/未开始
+	 *
+	 */
+	private void transCorpKfSessionToServicer(String corpId, String openKfId, String userid, String externalUserId, Integer changeType, Integer serviceState) throws Exception {
+		//记录接待人员会话
+		CorpKfServicerSessionRecord corpKfServicerSessionRecord = new CorpKfServicerSessionRecord();
+		corpKfServicerSessionRecord.setOpenKfId(openKfId);
+		corpKfServicerSessionRecord.setServiceState(0);
+		corpKfServicerSessionRecord.setServicerUserid(userid);
+		corpKfServicerSessionRecord.setExternalUserid(externalUserId);
+		corpKfServicerSessionRecord.setChangeType(changeType);
+		corpKfServicerSessionRecordMapper.insert(corpKfServicerSessionRecord);
+		//调用微信客服接口 变更会话状态
+		wxCorpOps.transCorpKfSessionToServicer(corpId, openKfId, userid, externalUserId, serviceState);
+	}
+
+	/**
+	 * 发送微信客服会话文本消息
+	 */
+	public void sendCorpKfTextTips(String corpId, String externalUserid, String openKfId, String content) throws Exception {
+		if (StrUtil.isEmpty(content)) {
+			return;
+		}
+		//查询会话状态
+		Integer state = wxCorpOps.getCorpKfSessionStatus(corpId, openKfId, externalUserid);
+		//0.未处理 1.人工智能接待 可调用api发送消息
+		if (state == null || state != 0 || state != 1) {
+			log.info("目前openKfId:{}用户eid:{}会话处于状态中state:{},无法发送消息", openKfId, externalUserid, state);
+			return;
+		}
+		WxCorpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCorpKfMsgSendRequest();
+		wxCpKfMsgSendRequest.setMsgType("text");
+		wxCpKfMsgSendRequest.setToUser(externalUserid);
+		wxCpKfMsgSendRequest.setOpenKfid(openKfId);
+		WxCorpKfMsgSendRequest.WxCpKfTextMsg textMsg = new WxCorpKfMsgSendRequest.WxCpKfTextMsg();
+		textMsg.setContent(content);
+		wxCpKfMsgSendRequest.setText(textMsg);
+		wxCorpOps.sendTextMsg(corpId, wxCpKfMsgSendRequest);
+	}
+
+	/**
+	 * 会话状态为待处理 则分配接待人员
+	 */
+	public void assignKfSession(String corpId, String openKfId, String externalUserid, CorpKfAccountSessionConfig corpKfAccountSessionConfig) throws Exception {
 		//查询用户会话状态 0待处理就进行处理
-		Integer sessionStatus = wxCorpOps.getCorpKfSessionStatus(corpId, openKfId, event.getExternalUserId());
+		Integer sessionStatus = wxCorpOps.getCorpKfSessionStatus(corpId, openKfId, externalUserid);
 		if (sessionStatus != 0) {
 			return;
 		}
 		//用户是否已有会话
 		Integer selectCount = corpKfServicerSessionRecordMapper.selectCount(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
 				.eq(CorpKfServicerSessionRecord::getOpenKfId, openKfId)
-				.eq(CorpKfServicerSessionRecord::getExternalUserid, event.getExternalUserId())
+				.eq(CorpKfServicerSessionRecord::getExternalUserid, externalUserid)
 				.eq(CorpKfServicerSessionRecord::getServiceState, 0));
 		if (selectCount > 0) {
-			log.info("external_userid:{}已有未结束的会话,无需分配", event.getExternalUserId());
+			log.info("external_userid:{}已有未结束的会话,无需分配", externalUserid);
 			return;
 		}
-		//是否设置非值班提示
-		if (corpKfAccountSessionConfig != null && StrUtil.isNotEmpty(corpKfAccountSessionConfig.getNoDutyTips())) {
-			DateTime now = DateTime.now();
-			String thisHourDate = DateUtil.format(now, "HH:mm");
-			//星期天为1
-			int dayOfWeek = DateUtil.dayOfWeek(now);
-			//是否值班期
-			Integer dutyHourCount = corpKfAccountFollowMapper.selectCount(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
-					.eq(CorpKfAccountFollow::getOpenKfId, openKfId)
-					.le(CorpKfAccountFollow::getStartTime, thisHourDate)
-					.ge(CorpKfAccountFollow::getEndTime, thisHourDate)
-					.eq(CorpKfAccountFollow::getDutyWeek, dayOfWeek));
-			//非值班时间 发送通知
-			if (dutyHourCount == 0) {
-				//非值班时间 移入等待池并发送进入等待池消息
-				log.info("当前客服账号:{}处于非值班时间:{},将客服:{}移入等待池", openKfId, thisHourDate, event.getExternalUserId());
-				String queueTips = corpKfAccountSessionConfig.getQueueTips();
-				sendCorpKfTextTips(corpId, event.getExternalUserId(), openKfId, queueTips);
-				moveWaitingQueue(corpId, openKfId, event.getExternalUserId());
-				return;
-			}
+		DateTime now = DateTime.now();
+		String thisHourDate = DateUtil.format(now, "HH:mm");
+		//星期天为1
+		int dayOfWeek = DateUtil.dayOfWeek(now);
+		//是否值班期
+		Integer dutyHourCount = corpKfAccountFollowMapper.selectCount(Wrappers.lambdaQuery(CorpKfAccountFollow.class)
+				.eq(CorpKfAccountFollow::getOpenKfId, openKfId)
+				.le(CorpKfAccountFollow::getStartTime, thisHourDate)
+				.ge(CorpKfAccountFollow::getEndTime, thisHourDate)
+				.eq(CorpKfAccountFollow::getDutyWeek, dayOfWeek));
+		//非值班
+		if (dutyHourCount == 0) {
+			//非值班时间 移入等待池
+			log.info("当前客服账号:{}处于非值班时间:{},将客户:{}移入等待池", openKfId, thisHourDate, externalUserid);
+			moveWaitingQueue(corpId, openKfId, externalUserid);
+			//发送进入等待池消息
+			sendCorpKfTextTips(corpId, externalUserid, openKfId, corpKfAccountSessionConfig.getQueueTips());
+			return;
 		}
 		//获取接待人员列表
 		CorpKfServicerResp corpKfServicerResp = wxCorpOps.getKfServicerList(corpId, openKfId);
@@ -856,13 +911,12 @@ public class CorpServiceImpl implements CorpService {
 			return false;
 		}).collect(Collectors.toList());
 		//客服是否已经开始值班
-		//没有值班人员 接入等待池
+		//没有值班人员
 		if (CollUtil.isEmpty(corpKfServicers)) {
-			if (corpKfAccountSessionConfig != null) {
-				String queueTips = corpKfAccountSessionConfig.getQueueTips();
-				sendCorpKfTextTips(corpId, event.getExternalUserId(), openKfId, queueTips);
-			}
-			moveWaitingQueue(corpId, openKfId, event.getExternalUserId());
+			log.info("当前客服账号:{}无值班人员,将客户:{}移入等待池", openKfId, externalUserid);
+			//接入等待池
+			moveWaitingQueue(corpId, openKfId, externalUserid);
+			sendCorpKfTextTips(corpId, externalUserid, openKfId, corpKfAccountSessionConfig.getQueueTips());
 			return;
 		}
 		//是否有空闲接待人员
@@ -872,71 +926,26 @@ public class CorpServiceImpl implements CorpService {
 			//随机分配一个客服
 			CorpKfServicerResp.CorpKfServicer corpKfServicer = corpKfServicers.get(RandomUtil.randomInt(corpKfServicers.size()));
 			//变更会话状态
-			transCorpKfSessionToServicer(corpId, openKfId, corpKfServicer.getUserid(), event.getExternalUserId(), null, 3);
+			transCorpKfSessionToServicer(corpId, openKfId, corpKfServicer.getUserid(), externalUserid, null, 3);
 			return;
 		}
 		//筛选符合条件的客服
 		//默认接入最少的优先分配 接待人最少的在集合前面
-		//不超过最多接待人数
-		Integer maxNum = corpKfAccountSessionConfig.getMaxNum();
+		//不超过最多接待人数 默认30
+		Integer maxNum = Optional.ofNullable(corpKfAccountSessionConfig.getMaxNum()).orElse(30);
 		List<CorpKfServicerDto> filterKfCs = corpKfServicerDtos.stream().filter(kfCs -> corpKfServiceIds.contains(kfCs.getServicerUserid()) && kfCs.getNum() < maxNum).collect(Collectors.toList());
 		if (CollUtil.isEmpty(filterKfCs)) {
-			String queueTips = corpKfAccountSessionConfig.getQueueTips();
-			sendCorpKfTextTips(corpId, event.getExternalUserId(), openKfId, queueTips);
-			//未有可接待的客服 移入等待池并发送进入等待池消息
-			moveWaitingQueue(corpId, openKfId, event.getExternalUserId());
+			log.info("当前客服账号:{}值班接待上限:{},将客户:{}移入等待池", openKfId, maxNum, externalUserid);
+			//移入等待池
+			moveWaitingQueue(corpId, openKfId, externalUserid);
+			//发送进入等待池消息
+			sendCorpKfTextTips(corpId, externalUserid, openKfId, corpKfAccountSessionConfig.getQueueTips());
 			return;
 		}
 		//选择第一个分配接待
 		CorpKfServicerDto corpKfServicerDto = filterKfCs.get(0);
 		//变更会话状态
-		transCorpKfSessionToServicer(corpId, openKfId, corpKfServicerDto.getServicerUserid(), event.getExternalUserId(), null, 3);
-	}
-
-	/**
-	 * 变更用户会话状态
-	 * serviceState 0未处理 1由智能助手接待 2待接入池排队中 3由人工接待 4 已结束/未开始
-	 *
-	 */
-	private void transCorpKfSessionToServicer(String corpId, String openKfId, String userid, String externalUserId, Integer changeType, Integer serviceState) throws Exception {
-		try {
-			//记录接待人员会话
-			CorpKfServicerSessionRecord corpKfServicerSessionRecord = new CorpKfServicerSessionRecord();
-			corpKfServicerSessionRecord.setOpenKfId(openKfId);
-			corpKfServicerSessionRecord.setServiceState(0);
-			corpKfServicerSessionRecord.setServicerUserid(userid);
-			corpKfServicerSessionRecord.setExternalUserid(externalUserId);
-			corpKfServicerSessionRecord.setChangeType(changeType);
-			corpKfServicerSessionRecordMapper.insert(corpKfServicerSessionRecord);
-			//调用微信客服接口 变更会话状态
-			wxCorpOps.transCorpKfSessionToServicer(corpId, openKfId, userid, externalUserId, serviceState);
-		} catch (DuplicateKeyException e) {
-		}
-
-	}
-
-	/**
-	 * 发送微信客服会话文本消息
-	 */
-	public void sendCorpKfTextTips(String corpId, String externalUserid, String openKfId, String content) throws Exception {
-		if (StrUtil.isEmpty(content)) {
-			return;
-		}
-		//查询会话状态
-		Integer state = wxCorpOps.getCorpKfSessionStatus(corpId, openKfId, externalUserid);
-		//0.未处理 1.人工智能接待 可调用api发送消息
-		if (state == null || state != 0 || state != 1) {
-			log.info("目前openKfId:{}用户eid:{}会话处于状态中state:{},无法发送消息", openKfId, externalUserid, state);
-			return;
-		}
-		WxCorpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCorpKfMsgSendRequest();
-		wxCpKfMsgSendRequest.setMsgType("text");
-		wxCpKfMsgSendRequest.setToUser(externalUserid);
-		wxCpKfMsgSendRequest.setOpenKfid(openKfId);
-		WxCorpKfMsgSendRequest.WxCpKfTextMsg textMsg = new WxCorpKfMsgSendRequest.WxCpKfTextMsg();
-		textMsg.setContent(content);
-		wxCpKfMsgSendRequest.setText(textMsg);
-		wxCorpOps.sendTextMsg(corpId, wxCpKfMsgSendRequest);
+		transCorpKfSessionToServicer(corpId, openKfId, corpKfServicerDto.getServicerUserid(), externalUserid, null, 3);
 	}
 
 	private void moveWaitingQueue(String corpId, String openKfId, String externalUserId) throws Exception {
@@ -966,4 +975,19 @@ public class CorpServiceImpl implements CorpService {
 	public void saveChatMsg(CorpKfAccountSessionChat chat) {
 		corpKfAccountSessionChatMapper.insert(chat);
 	}
+
+	/**
+	 * 设置会话记录状态
+	 */
+	public void setDbSessionState(String openKfId, String externalUserId, Long sendTime) {
+		CorpKfServicerSessionRecord corpKfServicerSessionRecord = corpKfServicerSessionRecordMapper.selectOne(Wrappers.lambdaQuery(CorpKfServicerSessionRecord.class)
+				.eq(CorpKfServicerSessionRecord::getOpenKfId, openKfId)
+				.eq(CorpKfServicerSessionRecord::getExternalUserid, externalUserId)
+				.last("limit 1"));
+		if (corpKfServicerSessionRecord != null) {
+			corpKfServicerSessionRecord.setCloseTime(DateTime.of(sendTime * 1000));
+			corpKfServicerSessionRecord.setServiceState(1);
+			corpKfServicerSessionRecordMapper.updateById(corpKfServicerSessionRecord);
+		}
+	}
 }