package com.ydtech.modules.admin.service.impl; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.ydtech.components.DistributedLockComponent; 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.CustomerListRequest; import com.ydtech.modules.admin.components.wechat.customer.request.CustomerSendMsgOnEventRequest; 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.model.SysUser; 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.WechatEncryptVo; import com.ydtech.modules.admin.service.*; import com.ydtech.utils.StringUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.util.crypto.WxCryptUtil; 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.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.util.ObjectUtils; import java.time.LocalDateTime; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Slf4j @Service @RequiredArgsConstructor public class WechatCustomerServiceImpl implements WechatCustomerService { private final WechatCustomerComponents wechatCustomerComponents; private final SysUserService sysUserService; private final XmlMapper xmlMapper = new XmlMapper(); private final WxCpKfService wxCpKfService; private final SysWechatCustomerService sysWechatCustomerService; private final IWorkScheduleConfigService workScheduleConfigService; private final SysWechatCustomerDutyService sysWechatCustomerDutyService; private final DistributedLockComponent distributedLockComponent; @Override public String customerCallback(String msgSignature, String timestamp, String nonce, String echoStr) { WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil(); return wxCryptUtil.decrypt(echoStr); } @Override public void callbackSendMsgOnEvent(String wechatEncrypt) throws Exception { log.info("回调参数为{}", wechatEncrypt); WechatEncryptVo wechatEncryptVo = xmlMapper.readValue(wechatEncrypt, WechatEncryptVo.class); WxCryptUtil wxCryptUtil = wechatCustomerComponents.getWxCryptUtil(); 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 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 sysWechatCustomerDuties = sysWechatCustomerDutyService.lambdaQuery() .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId()) .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now()) .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now()) .list(); distributedLockComponent.executeWithLock(wxCpKfMsgItem.getMsgId(), () -> { // 改变会话状态 if (wxCpKfMsgItem.getOrigin() == 3) { // 1 管理人是否一致 // 2 休息日 // 3 工作时间内 安排了值班客服 boolean unanimous = !sysWechatCustomerService.whetherTheManagersAreConsistent(wxCpKfMsgItem.getExternalUserId(), sysWechatCustomer, sysWechatCustomerDuties); WxCpKfServiceStateResp serviceState; try { serviceState = wxCpKfService.getServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId()); } catch (WxErrorException e) { throw new RuntimeException(e); } Integer state1 = serviceState.getServiceState(); if (unanimous || !workingHours) { if (state1 == 0) { state1 = 1; try { wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId()); } catch (WxErrorException e) { throw new RuntimeException(e); } restTimeSendMsg(workingHours, wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid()); } else if (state1 == 1) { restTimeSendMsg(workingHours, wxCpKfMsgItem.getExternalUserId(), wxCpKfMsgItem.getOpenKfid()); } else if (state1 == 3) { state1 = 4; try { wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId()); } catch (WxErrorException e) { throw new RuntimeException(e); } } } else { if (state1 == 0 || state1 == 1) { state1 = 3; try { wxCpKfService.transServiceState(wxCpKfMsgItem.getOpenKfid(), wxCpKfMsgItem.getExternalUserId(), state1, sysWechatCustomer.getReceptionistId()); } catch (WxErrorException e) { throw new RuntimeException(e); } } } } else if (wxCpKfMsgItem.getOrigin() == 4) { // 发送欢迎语 WxCpKfEventMsg event = wxCpKfMsgItem.getEvent(); WxCpKfServiceStateResp serviceState = null; try { serviceState = wxCpKfService.getServiceState(event.getOpenKfid(), event.getExternalUserId()); } catch (WxErrorException e) { throw new RuntimeException(e); } Integer state = serviceState.getServiceState(); if (ObjectUtils.isEmpty(event)) { return; } String scene = event.getScene(); if (!StringUtils.isEmpty(scene)) { SysUser sysUser = sysUserService.getById(scene); sysUser.setExternalUserId(event.getExternalUserId()); sysUserService.updateById(sysUser); CustomerSendMsgOnEventRequest customerSendMsgOnEventRequest = CustomerSendMsgOnEventRequest.sendMsgOnEvent(sysUser, event.getWelcomeCode(), workingHours); wechatCustomerComponents.sendMsgOnEvent(customerSendMsgOnEventRequest); } if (workingHours && (state == 0 || state == 1)) { state = 3; List list = sysWechatCustomerService.lambdaQuery().eq(SysWechatCustomer::getOpenKfId, wxCpKfMsgItem.getEvent().getOpenKfid()).list(); String receptionistId = list.get(0).getReceptionistId(); try { wxCpKfService.transServiceState(wxCpKfMsgItem.getEvent().getOpenKfid(), wxCpKfMsgItem.getEvent().getExternalUserId(), state, receptionistId); } catch (WxErrorException e) { throw new RuntimeException(e); } } } }, 2, 2); } private void restTimeSendMsg(boolean workingHours, String externalUserId, String openKfId) { String message; if(!workingHours){ message = "您好~当前客服暂时不在服务时间,服务时间为:周一至周五:8:30-12:00,14:00-18:30,周六至周日:9:00-12:00,14:00-18:30,当前未到服务时间,请您在服务时段内咨询~"; }else { message = "您咨询的客服目前暂时不在服务时间,请您前往“晋掌柜App”点击“微信客服”,将为您安排新的客服为您提供服务~(客服服务时间:周一至周五 8:30-18:30,周六至周日 9:00-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); try { wxCpKfService.sendMsg(wxCpKfMsgSendRequest); } catch (WxErrorException e) { throw new RuntimeException(e); } } @Override public List list() { CustomerListRequest customerListResponse = new CustomerListRequest(0, 100); return wechatCustomerComponents.list(customerListResponse).getAccountList(); } @Override public String addContactWay(String openKfId) { CustomerAddContactWayRequest customerAddContactWayRequest = new CustomerAddContactWayRequest(openKfId, "WELCOME_MESSAGE"); CustomerAddContactWayResponse customerAddContactWayResponse = wechatCustomerComponents.addContactWay(customerAddContactWayRequest); return customerAddContactWayResponse.getUrl(); } }