|
|
@@ -1,9 +1,9 @@
|
|
|
package com.jzg.organization.service.impl;
|
|
|
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.dataformat.xml.XmlMapper;
|
|
|
import com.jzg.commons.core.base.BaseController;
|
|
|
import com.jzg.commons.entity.po.SysUploadFiles;
|
|
|
-import com.jzg.commons.entity.vo.SysUserAccountVo;
|
|
|
import com.jzg.commons.exception.SystemException;
|
|
|
import com.jzg.commons.util.MinioUtils;
|
|
|
import com.jzg.organization.entity.WechatCustomerService;
|
|
|
@@ -22,16 +22,14 @@ import me.chanjar.weixin.cp.bean.WxCpBaseResp;
|
|
|
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
|
|
import me.chanjar.weixin.cp.bean.WxCpUser;
|
|
|
import me.chanjar.weixin.cp.bean.kf.*;
|
|
|
-import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
|
|
|
-import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
|
|
|
-import me.chanjar.weixin.cp.message.WxCpMessageRouter;
|
|
|
+import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfEventMsg;
|
|
|
+import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfTextMsg;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
-import java.io.File;
|
|
|
import java.io.InputStream;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -54,10 +52,6 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
|
|
|
private final SysUserService sysUserService;
|
|
|
|
|
|
- private final WxCpService wxCpService;
|
|
|
-
|
|
|
- private final WxCpMessageRouter wxCpMessageRouter;
|
|
|
-
|
|
|
private final WxCpMediaService wxCpMediaService;
|
|
|
|
|
|
private final WxCpKfService wxCpKfService;
|
|
|
@@ -70,6 +64,10 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
|
|
|
private final BaseController baseController;
|
|
|
|
|
|
+ private final XmlMapper xmlMapper = new XmlMapper();
|
|
|
+
|
|
|
+ private final WxCryptUtil wxCryptUtil;
|
|
|
+
|
|
|
@Value("${minio.bucket-name}")
|
|
|
private String bucketName;
|
|
|
|
|
|
@@ -190,16 +188,11 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
@Override
|
|
|
public List<WeComDeptVo> getDeptByDeptId() throws WxErrorException {
|
|
|
List<WxCpDepart> list = wxCpDepartmentService.list(null);
|
|
|
- List<WeComDeptVo> deptVos = list.stream()
|
|
|
- .map(WeComDeptVo::new)
|
|
|
- .toList();
|
|
|
- Map<String, List<WeComDeptVo>> parentGroup = deptVos.stream()
|
|
|
- .collect(Collectors.groupingBy(WeComDeptVo::getParentId));
|
|
|
+ List<WeComDeptVo> deptVos = list.stream().map(WeComDeptVo::new).toList();
|
|
|
+ Map<String, List<WeComDeptVo>> parentGroup = deptVos.stream().collect(Collectors.groupingBy(WeComDeptVo::getParentId));
|
|
|
List<String> collect = deptVos.stream().map(WeComDeptVo::getDeptId).toList();
|
|
|
deptVos.forEach(node -> node.setChildDept(parentGroup.getOrDefault(node.getDeptId(), Collections.emptyList())));
|
|
|
- return deptVos.stream()
|
|
|
- .filter(node -> node.getParentId() != null && !collect.contains(node.getParentId()))
|
|
|
- .collect(Collectors.toList());
|
|
|
+ return deptVos.stream().filter(node -> node.getParentId() != null && !collect.contains(node.getParentId())).collect(Collectors.toList());
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@@ -214,30 +207,24 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
public void saveOrUpdateCustomerServiceDutyRules(WechatCustomerServiceDutyRulesVo sysWechatCustomerDutyRules) {
|
|
|
// 删除之前的排班信息
|
|
|
wechatCustomerServiceDutyService.clearCustomerDuty(sysWechatCustomerDutyRules.getCustomerId());
|
|
|
- List<WechatCustomerServiceDuty> wechatCustomerServiceDuties = sysWechatCustomerDutyRules.getDutyList()
|
|
|
- .stream()
|
|
|
- .flatMap(duty -> duty.getDutyConfig().stream()
|
|
|
- .map(config -> {
|
|
|
- WechatCustomerServiceDuty dutyEntity = new WechatCustomerServiceDuty();
|
|
|
- dutyEntity.setCustomerServiceId(sysWechatCustomerDutyRules.getCustomerId());
|
|
|
- dutyEntity.setDutyCustomerServiceId(config.getDutyCustomerServiceId());
|
|
|
- dutyEntity.setDate(duty.getDutyDate());
|
|
|
- dutyEntity.setStartTime(config.getStartTime());
|
|
|
- dutyEntity.setEndTime(config.getEndTime());
|
|
|
- return dutyEntity;
|
|
|
- }))
|
|
|
- .collect(Collectors.toList());
|
|
|
+ List<WechatCustomerServiceDuty> wechatCustomerServiceDuties = sysWechatCustomerDutyRules.getDutyList().stream().flatMap(duty -> duty.getDutyConfig().stream().map(config -> {
|
|
|
+ WechatCustomerServiceDuty dutyEntity = new WechatCustomerServiceDuty();
|
|
|
+ dutyEntity.setCustomerServiceId(sysWechatCustomerDutyRules.getCustomerId());
|
|
|
+ dutyEntity.setDutyCustomerServiceId(config.getDutyCustomerServiceId());
|
|
|
+ dutyEntity.setDate(duty.getDutyDate());
|
|
|
+ dutyEntity.setStartTime(config.getStartTime());
|
|
|
+ dutyEntity.setEndTime(config.getEndTime());
|
|
|
+ return dutyEntity;
|
|
|
+ })).collect(Collectors.toList());
|
|
|
// 保存排班信息
|
|
|
wechatCustomerServiceDutyService.saveBatch(wechatCustomerServiceDuties);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getWeChatCustomer() throws WxErrorException {
|
|
|
-// String userName = baseController.getUserName();
|
|
|
+ String userName = baseController.getUserName();
|
|
|
WechatCustomerService wechatCustomerService = null;
|
|
|
-// String leaderId = sysUserService.getLeaderId(userName);
|
|
|
- String userName = "1930514052662677506";
|
|
|
- String leaderId = "1930514052662677506";
|
|
|
+ String leaderId = sysUserService.getLeaderId(userName);
|
|
|
if (StringUtils.hasLength(leaderId)) {
|
|
|
wechatCustomerService = wechatCustomerServiceService.getByUserId(leaderId);
|
|
|
}
|
|
|
@@ -245,23 +232,14 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
if (ObjectUtils.isEmpty(wechatCustomerService)) {
|
|
|
// 获取最近一次的客服记录
|
|
|
List<WechatCustomerService> sysWechatCustomerList = wechatCustomerServiceService.list();
|
|
|
- List<Long> sysWechatCustomerIdList = sysWechatCustomerList.stream()
|
|
|
- .map(WechatCustomerService::getId)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- WechatCustomerServiceRecording wechatCustomerServiceRecording = wechatCustomerServiceRecordingService.lambdaQuery()
|
|
|
- .in(!sysWechatCustomerIdList.isEmpty(), WechatCustomerServiceRecording::getCustomerServiceId, sysWechatCustomerIdList)
|
|
|
- .eq(WechatCustomerServiceRecording::getUserId, userName)
|
|
|
- .orderByDesc(WechatCustomerServiceRecording::getCreateTime)
|
|
|
- .last("limit 1")
|
|
|
- .one();
|
|
|
+ List<Long> sysWechatCustomerIdList = sysWechatCustomerList.stream().map(WechatCustomerService::getId).collect(Collectors.toList());
|
|
|
+
|
|
|
+ WechatCustomerServiceRecording wechatCustomerServiceRecording = wechatCustomerServiceRecordingService.lambdaQuery().in(!sysWechatCustomerIdList.isEmpty(), WechatCustomerServiceRecording::getCustomerServiceId, sysWechatCustomerIdList).eq(WechatCustomerServiceRecording::getUserId, userName).orderByDesc(WechatCustomerServiceRecording::getCreateTime).last("limit 1").one();
|
|
|
if (ObjectUtils.isEmpty(wechatCustomerServiceRecording)) {
|
|
|
wechatCustomerService = wechatCustomerServiceService.getCacheCustomerId(wechatCustomerService);
|
|
|
} else {
|
|
|
// 使用上一次的客服
|
|
|
- wechatCustomerService = wechatCustomerServiceService.lambdaQuery()
|
|
|
- .eq(WechatCustomerService::getId, wechatCustomerServiceRecording.getCustomerServiceId())
|
|
|
- .one();
|
|
|
+ wechatCustomerService = wechatCustomerServiceService.lambdaQuery().eq(WechatCustomerService::getId, wechatCustomerServiceRecording.getCustomerServiceId()).one();
|
|
|
if (ObjectUtils.isEmpty(wechatCustomerService)) {
|
|
|
wechatCustomerService = wechatCustomerServiceService.getCacheCustomerId(wechatCustomerService);
|
|
|
}
|
|
|
@@ -272,12 +250,7 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
wechatCustomerServiceRecordingService.save(customerRecording);
|
|
|
}
|
|
|
|
|
|
- List<WechatCustomerServiceDuty> list = wechatCustomerServiceDutyService.lambdaQuery()
|
|
|
- .eq(WechatCustomerServiceDuty::getDutyCustomerServiceId, wechatCustomerService.getId())
|
|
|
- .eq(WechatCustomerServiceDuty::getDate, LocalDate.now())
|
|
|
- .le(WechatCustomerServiceDuty::getStartTime, LocalTime.now())
|
|
|
- .ge(WechatCustomerServiceDuty::getEndTime, LocalTime.now())
|
|
|
- .list();
|
|
|
+ List<WechatCustomerServiceDuty> list = wechatCustomerServiceDutyService.lambdaQuery().eq(WechatCustomerServiceDuty::getDutyCustomerServiceId, wechatCustomerService.getId()).eq(WechatCustomerServiceDuty::getDate, LocalDate.now()).le(WechatCustomerServiceDuty::getStartTime, LocalTime.now()).ge(WechatCustomerServiceDuty::getEndTime, LocalTime.now()).list();
|
|
|
if (list != null && !list.isEmpty()) {
|
|
|
WechatCustomerServiceDuty wechatCustomerServiceDuty = list.get(0);
|
|
|
wechatCustomerService = wechatCustomerServiceService.getById(wechatCustomerServiceDuty.getDutyCustomerServiceId());
|
|
|
@@ -295,9 +268,62 @@ public class WechatCustomerServiceManageServiceImpl implements WechatCustomerSer
|
|
|
return accountLink.getUrl();
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
- public void callbackSendMsgOnEvent(WxCpXmlMessage wechatEncrypt) {
|
|
|
- WxCpXmlOutMessage route = wxCpMessageRouter.route(wechatEncrypt);
|
|
|
- System.out.println(route.toString());
|
|
|
+ public void callbackSendMsgOnEvent(String wechatEncrypt) throws JsonProcessingException, WxErrorException {
|
|
|
+ log.info("回调参数为{}", wechatEncrypt);
|
|
|
+ WechatEncryptVo wechatEncryptVo = xmlMapper.readValue(wechatEncrypt, WechatEncryptVo.class);
|
|
|
+ log.info("解密参数为{}", wechatEncryptVo.getEncrypt());
|
|
|
+ String encrypt = wxCryptUtil.decrypt(wechatEncryptVo.getEncrypt());
|
|
|
+ log.info("解密后的参数为{}", encrypt);
|
|
|
+ WechatCustomerCallbackVo wechatCustomerCallbackVo = xmlMapper.readValue(encrypt, WechatCustomerCallbackVo.class);
|
|
|
+
|
|
|
+ // 获取消息
|
|
|
+ WxCpKfMsgListResp wxCpKfMsgListResp = wxCpKfService.syncMsg(null, wechatCustomerCallbackVo.getToken(), null, null, wechatCustomerCallbackVo.getOpenKfId());
|
|
|
+
|
|
|
+ while (wxCpKfMsgListResp.getHasMore() == 1) {
|
|
|
+ String nextCursor = wxCpKfMsgListResp.getNextCursor();
|
|
|
+ wxCpKfMsgListResp = wxCpKfService.syncMsg(nextCursor, wechatCustomerCallbackVo.getToken(), null, null, wechatCustomerCallbackVo.getOpenKfId());
|
|
|
+ }
|
|
|
+ List<WxCpKfMsgListResp.WxCpKfMsgItem> msgList = wxCpKfMsgListResp.getMsgList();
|
|
|
+ WxCpKfMsgListResp.WxCpKfMsgItem wxCpKfMsgItem = wxCpKfMsgListResp.getMsgList().get(msgList.size() - 1);
|
|
|
+
|
|
|
+ if (wxCpKfMsgItem.getOrigin() == 3) {
|
|
|
+ List<WxCpKfMsgListResp.WxCpKfMsgItem> msgListDTOList = msgList.stream().filter(x -> "event".equals(x.getMsgType())).toList();
|
|
|
+
|
|
|
+ msgListDTOList.forEach(x -> {
|
|
|
+ try {
|
|
|
+ WxCpKfEventMsg xEvent = x.getEvent();
|
|
|
+ WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId());
|
|
|
+ if (serviceState.getServiceState() == 0) {
|
|
|
+ List<WechatCustomerService> list = wechatCustomerServiceService.lambdaQuery().eq(WechatCustomerService::getOpenKfId, xEvent.getOpenKfid()).list();
|
|
|
+ wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId(), 3, list.get(0).getReceptionistId());
|
|
|
+ }
|
|
|
+ } catch (WxErrorException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
|
|
|
+ if (ObjectUtils.isEmpty(event)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ String scene = event.getScene();
|
|
|
+ if (StringUtils.isEmpty(scene)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (wxCpKfMsgItem.getOrigin() == 4) {
|
|
|
+ String userName = new BaseController().getUserName();
|
|
|
+ WxCpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCpKfMsgSendRequest();
|
|
|
+ String messages = "你好!" + userName + "(" + userName + ")" + "很高兴为你服务";
|
|
|
+ wxCpKfMsgSendRequest.setCode(event.getWelcomeCode());
|
|
|
+ WxCpKfTextMsg wxCpKfTextMsg = new WxCpKfTextMsg();
|
|
|
+ wxCpKfTextMsg.setContent(messages);
|
|
|
+ wxCpKfMsgSendRequest.setText(wxCpKfTextMsg);
|
|
|
+ wxCpKfService.sendMsgOnEvent(wxCpKfMsgSendRequest);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|