|
@@ -6,28 +6,32 @@ import com.ydtech.modules.admin.components.WechatCustomerComponents;
|
|
|
import com.ydtech.modules.admin.components.wechat.customer.request.CustomerAddContactWayRequest;
|
|
import com.ydtech.modules.admin.components.wechat.customer.request.CustomerAddContactWayRequest;
|
|
|
import com.ydtech.modules.admin.components.wechat.customer.request.CustomerListRequest;
|
|
import com.ydtech.modules.admin.components.wechat.customer.request.CustomerListRequest;
|
|
|
import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSendMsgOnEventRequest;
|
|
import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSendMsgOnEventRequest;
|
|
|
-import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSyncMsgRequest;
|
|
|
|
|
import com.ydtech.modules.admin.components.wechat.customer.response.CustomerAddContactWayResponse;
|
|
import com.ydtech.modules.admin.components.wechat.customer.response.CustomerAddContactWayResponse;
|
|
|
import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
|
|
import com.ydtech.modules.admin.components.wechat.customer.response.CustomerListResponse;
|
|
|
-import com.ydtech.modules.admin.components.wechat.customer.response.CustomerSyncMsgResponse;
|
|
|
|
|
import com.ydtech.modules.admin.model.SysUser;
|
|
import com.ydtech.modules.admin.model.SysUser;
|
|
|
import com.ydtech.modules.admin.model.po.SysWechatCustomer;
|
|
import com.ydtech.modules.admin.model.po.SysWechatCustomer;
|
|
|
|
|
+import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
|
|
|
import com.ydtech.modules.admin.model.vo.wechat.WechatCustomerCallbackVo;
|
|
import com.ydtech.modules.admin.model.vo.wechat.WechatCustomerCallbackVo;
|
|
|
import com.ydtech.modules.admin.model.vo.wechat.WechatEncryptVo;
|
|
import com.ydtech.modules.admin.model.vo.wechat.WechatEncryptVo;
|
|
|
-import com.ydtech.modules.admin.service.SysUserService;
|
|
|
|
|
-import com.ydtech.modules.admin.service.SysWechatCustomerService;
|
|
|
|
|
-import com.ydtech.modules.admin.service.WechatCustomerService;
|
|
|
|
|
|
|
+import com.ydtech.modules.admin.service.*;
|
|
|
import com.ydtech.utils.StringUtils;
|
|
import com.ydtech.utils.StringUtils;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
|
|
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
|
|
|
import me.chanjar.weixin.cp.api.WxCpKfService;
|
|
import me.chanjar.weixin.cp.api.WxCpKfService;
|
|
|
|
|
+import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgListResp;
|
|
|
|
|
+import me.chanjar.weixin.cp.bean.kf.WxCpKfMsgSendRequest;
|
|
|
import me.chanjar.weixin.cp.bean.kf.WxCpKfServiceStateResp;
|
|
import me.chanjar.weixin.cp.bean.kf.WxCpKfServiceStateResp;
|
|
|
|
|
+import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfEventMsg;
|
|
|
|
|
+import me.chanjar.weixin.cp.bean.kf.msg.WxCpKfTextMsg;
|
|
|
|
|
+import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
@@ -45,6 +49,11 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
|
|
|
|
|
|
|
|
private final SysWechatCustomerService sysWechatCustomerService;
|
|
private final SysWechatCustomerService sysWechatCustomerService;
|
|
|
|
|
|
|
|
|
|
+ private final IWorkScheduleConfigService workScheduleConfigService;
|
|
|
|
|
+
|
|
|
|
|
+ private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
|
|
|
|
|
+
|
|
|
|
|
+ private final StringRedisTemplate redisTemplate;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) {
|
|
public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) {
|
|
@@ -63,32 +72,63 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
|
|
|
WechatCustomerCallbackVo wechatCustomerCallbackVo = xmlMapper.readValue(encrypt, WechatCustomerCallbackVo.class);
|
|
WechatCustomerCallbackVo wechatCustomerCallbackVo = xmlMapper.readValue(encrypt, WechatCustomerCallbackVo.class);
|
|
|
|
|
|
|
|
// 获取消息
|
|
// 获取消息
|
|
|
- CustomerSyncMsgRequest customerSyncMsgRequest = new CustomerSyncMsgRequest();
|
|
|
|
|
- customerSyncMsgRequest.setToken(wechatCustomerCallbackVo.getToken());
|
|
|
|
|
- customerSyncMsgRequest.setOpenKfid(wechatCustomerCallbackVo.getOpenKfId());
|
|
|
|
|
- CustomerSyncMsgResponse customerSyncMsgResponse = wechatCustomerComponents.syncMsg(customerSyncMsgRequest);
|
|
|
|
|
- while (customerSyncMsgResponse.getHasMore() == 1) {
|
|
|
|
|
- String nextCursor = customerSyncMsgResponse.getNextCursor();
|
|
|
|
|
- customerSyncMsgRequest.setCursor(nextCursor);
|
|
|
|
|
- customerSyncMsgResponse = wechatCustomerComponents.syncMsg(customerSyncMsgRequest);
|
|
|
|
|
|
|
+ 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<CustomerSyncMsgResponse.MsgListDTO> msgList = customerSyncMsgResponse.getMsgList();
|
|
|
|
|
- CustomerSyncMsgResponse.MsgListDTO msgListDTO = customerSyncMsgResponse.getMsgList().get(msgList.size() - 1);
|
|
|
|
|
|
|
+ List<WxCpKfMsgListResp.WxCpKfMsgItem> msgList = wxCpKfMsgListResp.getMsgList();
|
|
|
|
|
+ WxCpKfMsgListResp.WxCpKfMsgItem wxCpKfMsgItem = wxCpKfMsgListResp.getMsgList().get(msgList.size() - 1);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, wechatCustomerCallbackVo.getOpenKfId()).one();
|
|
|
|
|
+ boolean workingHours = workScheduleConfigService.isWorkingHours();
|
|
|
|
|
+ List<SysWechatCustomerDuty> sysWechatCustomerDuties = sysWechatCustomerDutyService.lambdaQuery()
|
|
|
|
|
+ .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId())
|
|
|
|
|
+ .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
|
|
|
|
|
+ .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
|
|
|
|
|
+ .list();
|
|
|
|
|
+
|
|
|
|
|
+ // 改变会话状态
|
|
|
|
|
+ if (wxCpKfMsgItem.getOrigin() == 3) {
|
|
|
|
|
+ // 1 管理人是否一致
|
|
|
|
|
+ // 2 休息日
|
|
|
|
|
+ // 3 工作时间内 安排了值班客服
|
|
|
|
|
+ if (!sysWechatCustomerService.whetherTheManagersAreConsistent(wxCpKfMsgItem.getExternalUserId(), sysWechatCustomer) || !workingHours || !sysWechatCustomerDuties.isEmpty()) {
|
|
|
|
|
+ WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId());
|
|
|
|
|
+ Integer state1 = serviceState.getServiceState();
|
|
|
|
|
+ if (state1 == 0) {
|
|
|
|
|
+ state1 = 1;
|
|
|
|
|
+ wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
|
|
|
|
|
+ restTimeSendMsg(wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid());
|
|
|
|
|
+ } else if (state1 == 1) {
|
|
|
|
|
+ String s = redisTemplate.opsForValue().get(wxCpKfMsgItem.getMsgId());
|
|
|
|
|
+ if (!ObjectUtils.isEmpty(s)) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ restTimeSendMsg(wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid());
|
|
|
|
|
+ redisTemplate.opsForValue().set(wxCpKfMsgItem.getMsgId(), wxCpKfMsgItem.getMsgId(), 1, TimeUnit.MINUTES);
|
|
|
|
|
+ } else if (state1 == 3) {
|
|
|
|
|
+ state1 = 4;
|
|
|
|
|
+ wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (msgListDTO.getOrigin() == 3) {
|
|
|
|
|
- List<CustomerSyncMsgResponse.MsgListDTO> msgListDTOList = msgList.stream()
|
|
|
|
|
- .filter(x -> "event".equals(x.getMsgtype()))
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
|
|
+ List<WxCpKfMsgListResp.WxCpKfMsgItem> msgListDTOList = msgList.stream().filter(x -> "event".equals(x.getMsgType())).collect(Collectors.toList());
|
|
|
msgListDTOList.forEach(x -> {
|
|
msgListDTOList.forEach(x -> {
|
|
|
try {
|
|
try {
|
|
|
- CustomerSyncMsgResponse.MsgListDTO.EventDTO xEvent = x.getEvent();
|
|
|
|
|
- WxCpKfServiceStateResp serviceState = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserid());
|
|
|
|
|
- if (serviceState.getServiceState() == 0) {
|
|
|
|
|
- List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery()
|
|
|
|
|
- .eq(SysWechatCustomer::getOpenKfId, xEvent.getOpenKfid())
|
|
|
|
|
- .list();
|
|
|
|
|
- wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserid(), 3, list.get(0)
|
|
|
|
|
- .getReceptionistId());
|
|
|
|
|
|
|
+ WxCpKfEventMsg xEvent = x.getEvent();
|
|
|
|
|
+ WxCpKfServiceStateResp wxCpKfServiceStateResp = wxCpKfService.getServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId());
|
|
|
|
|
+ Integer state = wxCpKfServiceStateResp.getServiceState();
|
|
|
|
|
+ // 休息时间
|
|
|
|
|
+ List<SysWechatCustomer> list = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, xEvent.getOpenKfid()).list();
|
|
|
|
|
+ String receptionistId = list.get(0).getReceptionistId();
|
|
|
|
|
+ if (workScheduleConfigService.isWorkingHours() && (state == 0 || state == 1)) {
|
|
|
|
|
+ state = 3;
|
|
|
|
|
+ wxCpKfService.transServiceState(xEvent.getOpenKfid(), xEvent.getExternalUserId(), state, receptionistId);
|
|
|
}
|
|
}
|
|
|
} catch (WxErrorException e) {
|
|
} catch (WxErrorException e) {
|
|
|
throw new RuntimeException(e);
|
|
throw new RuntimeException(e);
|
|
@@ -96,7 +136,7 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- CustomerSyncMsgResponse.MsgListDTO.EventDTO event = msgListDTO.getEvent();
|
|
|
|
|
|
|
+ WxCpKfEventMsg event = wxCpKfMsgItem.getEvent();
|
|
|
if (ObjectUtils.isEmpty(event)) {
|
|
if (ObjectUtils.isEmpty(event)) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
@@ -105,14 +145,31 @@ public class WechatCustomerServiceImpl implements WechatCustomerService {
|
|
|
if (StringUtils.isEmpty(scene)) {
|
|
if (StringUtils.isEmpty(scene)) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- if (msgListDTO.getOrigin() == 4) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 发送欢迎语
|
|
|
|
|
+ if (wxCpKfMsgItem.getOrigin() == 4) {
|
|
|
SysUser sysUser = sysUserService.getById(scene);
|
|
SysUser sysUser = sysUserService.getById(scene);
|
|
|
- CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, msgListDTO.getMsgid(), event.getWelcomeCode());
|
|
|
|
|
|
|
+ sysUser.setExternalUserId(event.getExternalUserId());
|
|
|
|
|
+ sysUserService.updateById(sysUser);
|
|
|
|
|
+ CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, event.getWelcomeCode(), workingHours);
|
|
|
wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
|
|
wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void restTimeSendMsg(String externalUserId, String openKfId) throws WxErrorException {
|
|
|
|
|
+ String message = "您咨询的客服目前暂时不在服务时间,请您前往“晋掌柜App”点击“微信客服”,将为您安排新的客服为您提供服务~(客服服务时间:周一至周五 8:30-18:30,周六至周日 8:30-18:30,午间 12:00-14:00 休息)";
|
|
|
|
|
+ WxCpKfMsgSendRequest wxCpKfMsgSendRequest = new WxCpKfMsgSendRequest();
|
|
|
|
|
+ wxCpKfMsgSendRequest.setToUser(externalUserId);
|
|
|
|
|
+ wxCpKfMsgSendRequest.setOpenKfid(openKfId);
|
|
|
|
|
+ wxCpKfMsgSendRequest.setMsgType("text");
|
|
|
|
|
+ WxCpKfTextMsg wxCpKfTextMsg = new WxCpKfTextMsg();
|
|
|
|
|
+ wxCpKfTextMsg.setContent(message);
|
|
|
|
|
+ wxCpKfMsgSendRequest.setText(wxCpKfTextMsg);
|
|
|
|
|
+ wxCpKfService.sendMsg(wxCpKfMsgSendRequest);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public List<CustomerListResponse.AccountListDTO> list() {
|
|
public List<CustomerListResponse.AccountListDTO> list() {
|
|
|
CustomerListRequest customerListResponse = new CustomerListRequest(0, 100);
|
|
CustomerListRequest customerListResponse = new CustomerListRequest(0, 100);
|