package com.ydtech.modules.admin.service.impl; import com.github.pagehelper.PageInfo; import com.ydtech.exception.SystemException; 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.po.SysWechatCustomerRecording; import com.ydtech.modules.admin.model.vo.wechat.*; import com.ydtech.modules.admin.service.SysWechatCustomerBusinessService; import com.ydtech.modules.admin.service.SysWechatCustomerDutyService; import com.ydtech.modules.admin.service.SysWechatCustomerRecordingService; import com.ydtech.modules.admin.service.SysWechatCustomerService; import com.ydtech.modules.order.entity.BasePageResult; import com.ydtech.modules.order.entity.po.InsUploadFiles; import com.ydtech.modules.order.service.InsUploadFilesService; import com.ydtech.utils.DateTimeUtil; import lombok.RequiredArgsConstructor; import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.cp.api.WxCpDepartmentService; import me.chanjar.weixin.cp.api.WxCpKfService; import me.chanjar.weixin.cp.api.WxCpMediaService; import me.chanjar.weixin.cp.api.WxCpUserService; 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 org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import java.io.File; import java.time.LocalDateTime; import java.util.*; import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBusinessService { private final WxCpKfService wxCpKfService; private final WxCpMediaService wxCpMediaService; private final WxCpUserService wxCpUserService; private final WxCpDepartmentService wxCpDepartmentService; private final InsUploadFilesService insUploadFilesService; private final SysWechatCustomerService sysWechatCustomerService; private final SysWechatCustomerDutyService sysWechatCustomerDutyService; private final SysWechatCustomerRecordingService sysWechatCustomerRecordingService; @Override public List getDeptByDeptId() throws WxErrorException { List list = wxCpDepartmentService.list(null); List deptVos = list.stream() .map(SysWechatCustomerDeptVo::new) .collect(Collectors.toList()); Map> parentGroup = deptVos.stream() .collect(Collectors.groupingBy(SysWechatCustomerDeptVo::getParentId)); Set strings = parentGroup.keySet(); deptVos.forEach(node -> node.setChildDept(parentGroup.getOrDefault(node.getDeptId(), Collections.emptyList()))); return deptVos.stream() .filter(node -> node.getParentId() != null && strings.contains(node.getDeptId())) .collect(Collectors.toList()); } @Override public List getUser(String deptId) throws WxErrorException { List wxCpUsers = wxCpUserService.listSimpleByDepartment(Long.valueOf(deptId), true, 0); return wxCpUsers.stream().map(x -> new SysWeComUserVo(x.getUserId(), x.getName())).collect(Collectors.toList()); } @Override public BasePageResult pageQuery(SysWechatCustomerQueryVo sysWechatCustomerQuery) { PageInfo sysWechatCustomerVoPageInfo = sysWechatCustomerService.getByNickname(sysWechatCustomerQuery); // 查询本页的值班人员 List customer = sysWechatCustomerVoPageInfo.getList() .stream() .map(SysWechatCustomerVo::getId) .collect(Collectors.toList()); if (!customer.isEmpty()) { // 获取值班时间列表 Map> dutyMap = sysWechatCustomerDutyService.lambdaQuery() .in(SysWechatCustomerDuty::getCustomerId, customer) .list() .stream() .collect(Collectors.groupingBy(SysWechatCustomerDuty::getCustomerId)); // 设置值班时间 sysWechatCustomerVoPageInfo.getList().forEach(sysWechatCustomerVo -> { List sysWechatCustomerDuties = dutyMap.get(Long.valueOf(sysWechatCustomerVo.getId())); if (!CollectionUtils.isEmpty(sysWechatCustomerDuties)) { List collect = sysWechatCustomerDuties.stream() .map(x -> DateTimeUtil.startAndEndTimeFormatting(x.getStartTime(), x.getEndTime())) .collect(Collectors.toList()); sysWechatCustomerVo.setDutyDatetime(collect); } }); } return new BasePageResult<>(sysWechatCustomerQuery.getPageNum(), sysWechatCustomerQuery.getPageSize(), sysWechatCustomerVoPageInfo.getTotal(), sysWechatCustomerVoPageInfo.getPages(), sysWechatCustomerVoPageInfo.getList()); } @Override public SysWechatCustomerAddVo getWechatCustomer(String customerId) { SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getById(customerId); return Optional.ofNullable(sysWechatCustomer) .map(SysWechatCustomerAddVo::new) .orElse(new SysWechatCustomerAddVo()); } @Override public void addWechatCustomer(SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException { sysWechatCustomerService.isNicknameRepeat(sysWechatCustomerAddVo.getNickname()); WxCpKfAccountAdd wxCpKfAccountAdd = new WxCpKfAccountAdd(); wxCpKfAccountAdd.setName(sysWechatCustomerAddVo.getNickname()); // 头像 if (StringUtils.hasLength(sysWechatCustomerAddVo.getAvatar())) { InsUploadFiles insUploadFiles = insUploadFilesService.getById(sysWechatCustomerAddVo.getAvatar()); Optional.ofNullable(insUploadFiles).map(InsUploadFiles::getFilePath).ifPresent(filePath -> { File file = new File(filePath); // 上传素材 WxMediaUploadResult image; try { image = wxCpMediaService.upload("image", file); } catch (WxErrorException e) { throw new SystemException("图片上传失败"); } String mediaId = image.getMediaId(); wxCpKfAccountAdd.setMediaId(mediaId); }); } // 添加 WxCpKfAccountAddResp wxCpKfAccountAddResp = wxCpKfService.addAccount(wxCpKfAccountAdd); SysWechatCustomer sysWechatCustomer = new SysWechatCustomer(sysWechatCustomerAddVo); sysWechatCustomer.setOpenKfId(wxCpKfAccountAddResp.getOpenKfid()); List list = new ArrayList<>(); list.add(sysWechatCustomerAddVo.getReceptionistId()); WxCpKfServicerOpResp wxCpKfServicerOpResp = wxCpKfService.addServicer(wxCpKfAccountAddResp.getOpenKfid(), list); if (wxCpKfServicerOpResp.getErrcode() == 0) { sysWechatCustomerService.save(sysWechatCustomer); } } @Override public void deleteWechatCustomer(String customerId) throws WxErrorException { SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在"); sysWechatCustomerService.isDutyCustomerId(sysWechatCustomer.getId()); WxCpKfAccountDel wxCpKfAccountDel = new WxCpKfAccountDel(); wxCpKfAccountDel.setOpenKfid(sysWechatCustomer.getOpenKfId()); WxCpBaseResp wxCpBaseResp; wxCpBaseResp = wxCpKfService.delAccount(wxCpKfAccountDel); if (wxCpBaseResp.getErrcode() == 0) { sysWechatCustomerService.deleteAllInfo(customerId); sysWechatCustomerDutyService.clearCustomerDuty(customerId); } } @Override public void updateWechatCustomer(String customerId, SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException { SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在"); if(!sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname())){ sysWechatCustomerService.isNicknameRepeat(sysWechatCustomerAddVo.getNickname()); } WxCpKfAccountUpd wxCpKfAccountUpd = new WxCpKfAccountUpd(); wxCpKfAccountUpd.setOpenKfid(sysWechatCustomer.getOpenKfId()); boolean isAvatarSame = StringUtils.hasLength(sysWechatCustomer.getAvatar()) && !sysWechatCustomer.getAvatar() .equals(sysWechatCustomerAddVo.getAvatar()); // 头像是否更换 if (isAvatarSame) { InsUploadFiles insUploadFiles = insUploadFilesService.getById(sysWechatCustomerAddVo.getAvatar()); File file = new File(insUploadFiles.getFilePath()); WxMediaUploadResult image = wxCpMediaService.upload("image", file); String mediaId = image.getMediaId(); wxCpKfAccountUpd.setMediaId(mediaId); sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar()); } // 昵称是否相同 boolean isNicknameSame = !sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname()); if (isNicknameSame) { wxCpKfAccountUpd.setName(sysWechatCustomerAddVo.getNickname()); } // 客服人员是否替换 if (!sysWechatCustomer.getReceptionistId().equals(sysWechatCustomerAddVo.getReceptionistId())) { List list = new ArrayList<>(); list.add(sysWechatCustomer.getReceptionistId()); wxCpKfService.delServicer(sysWechatCustomer.getOpenKfId(), list); list = new ArrayList<>(); list.add(sysWechatCustomerAddVo.getReceptionistId()); wxCpKfService.addServicer(sysWechatCustomer.getOpenKfId(), list); sysWechatCustomer.setReceptionistId(sysWechatCustomerAddVo.getReceptionistId()); sysWechatCustomer.setReceptionist(sysWechatCustomerAddVo.getReceptionist()); } // 昵称和头像是否跟换 if (isNicknameSame || isAvatarSame) { wxCpKfService.updAccount(wxCpKfAccountUpd); } sysWechatCustomer.setNickname(sysWechatCustomerAddVo.getNickname()); sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar()); sysWechatCustomer.setUserId(sysWechatCustomerAddVo.getUserId()); sysWechatCustomerService.updateById(sysWechatCustomer); } @Override public String getWeChatCustomer(SysUser user) throws WxErrorException { SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByUserId(user.getLeaderId()); List sysWechatCustomerList = sysWechatCustomerService.list(); List sysWechatCustomerIdList = sysWechatCustomerList.stream() .map(SysWechatCustomer::getId) .collect(Collectors.toList()); if (ObjectUtils.isEmpty(sysWechatCustomer)) { // 获取最近一次的客服记录 SysWechatCustomerRecording sysWechatCustomerRecording = sysWechatCustomerRecordingService.lambdaQuery() .in(!sysWechatCustomerIdList.isEmpty(), SysWechatCustomerRecording::getCustomerId, sysWechatCustomerIdList) .eq(SysWechatCustomerRecording::getUserId, user.getId()) .orderByDesc(SysWechatCustomerRecording::getCreateTime) .last("limit 1") .one(); if (ObjectUtils.isEmpty(sysWechatCustomerRecording)) { sysWechatCustomerService.getCacheCustomerId(sysWechatCustomer); } else { // 使用上一次的客服 sysWechatCustomer = sysWechatCustomerService.lambdaQuery() .eq(SysWechatCustomer::getId, sysWechatCustomerRecording.getCustomerId()) .one(); if (ObjectUtils.isEmpty(sysWechatCustomer)) { sysWechatCustomerService.getCacheCustomerId(sysWechatCustomer); } } SysWechatCustomerRecording customerRecording = new SysWechatCustomerRecording(); customerRecording.setCustomerId(sysWechatCustomer.getId()); customerRecording.setUserId(user.getId()); sysWechatCustomerRecordingService.save(customerRecording); } List list = sysWechatCustomerDutyService.lambdaQuery() .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId()) .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now()) .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now()) .list(); if (sysWechatCustomer.getDutyCustomerId() > 0 && list != null && !list.isEmpty()) { sysWechatCustomer = sysWechatCustomerService.getById(sysWechatCustomer.getDutyCustomerId()); } if (ObjectUtils.isEmpty(sysWechatCustomer)) { throw new SystemException("请联系管理员,配置客服"); } String openKfId = sysWechatCustomer.getOpenKfId(); WxCpKfAccountLink wxCpKfAccountLink = new WxCpKfAccountLink(); wxCpKfAccountLink.setOpenKfid(openKfId); wxCpKfAccountLink.setScene(user.getId()); WxCpKfAccountLinkResp accountLink = wxCpKfService.getAccountLink(wxCpKfAccountLink); return accountLink.getUrl(); } }