| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- package com.ydtech.modules.admin.service.impl;
- import com.github.pagehelper.PageInfo;
- import com.ydtech.exception.SystemException;
- import com.ydtech.modules.admin.model.po.SysWechatCustomer;
- import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
- 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.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.stereotype.Service;
- import org.springframework.util.StringUtils;
- import java.io.File;
- 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;
- @Override
- public List<SysWechatCustomerDeptVo> getDeptByDeptId() throws WxErrorException {
- List<WxCpDepart> list = wxCpDepartmentService.list(null);
- List<SysWechatCustomerDeptVo> deptVos = list.stream()
- .map(SysWechatCustomerDeptVo::new)
- .collect(Collectors.toList());
- Map<String, List<SysWechatCustomerDeptVo>> parentGroup = deptVos.stream()
- .collect(Collectors.groupingBy(SysWechatCustomerDeptVo::getParentId));
- Set<String> 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<SysWeComUserVo> getUser(String deptId) throws WxErrorException {
- List<WxCpUser> 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<SysWechatCustomerVo> pageQuery(SysWechatCustomerQueryVo sysWechatCustomerQuery) {
- PageInfo<SysWechatCustomerVo> sysWechatCustomerVoPageInfo = sysWechatCustomerService.getByNickname(sysWechatCustomerQuery);
- // 查询本页的值班人员
- List<String> customer = sysWechatCustomerVoPageInfo.getList()
- .stream()
- .map(SysWechatCustomerVo::getId)
- .collect(Collectors.toList());
- if(!customer.isEmpty()){
- // 获取值班时间列表
- Map<Long, List<SysWechatCustomerDuty>> dutyMap = sysWechatCustomerDutyService.lambdaQuery()
- .in(SysWechatCustomerDuty::getId, customer)
- .list()
- .stream()
- .collect(Collectors.groupingBy(SysWechatCustomerDuty::getCustomerId));
- // 设置值班时间
- sysWechatCustomerVoPageInfo.getList().forEach(sysWechatCustomerVo -> {
- List<SysWechatCustomerDuty> sysWechatCustomerDuties = dutyMap.get(Long.valueOf(sysWechatCustomerVo.getId()));
- List<String> 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 {
- WxCpKfAccountAdd wxCpKfAccountAdd = new WxCpKfAccountAdd();
- wxCpKfAccountAdd.setName(sysWechatCustomerAddVo.getNickname());
- // 头像
- 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<String> 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, "客服不存在");
- WxCpKfAccountDel wxCpKfAccountDel = new WxCpKfAccountDel();
- wxCpKfAccountDel.setOpenKfid(sysWechatCustomer.getOpenKfId());
- WxCpBaseResp wxCpBaseResp;
- wxCpBaseResp = wxCpKfService.delAccount(wxCpKfAccountDel);
- if (wxCpBaseResp.getErrcode() == 0) {
- sysWechatCustomerService.deleteAllInfo(customerId);
- }
- }
- @Override
- public void updateWechatCustomer(String customerId, SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException {
- SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
- 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<String> 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());
- }
- // 昵称和头像是否跟换
- if (isNicknameSame || isAvatarSame) {
- wxCpKfService.updAccount(wxCpKfAccountUpd);
- }
- sysWechatCustomer.setNickname(sysWechatCustomerAddVo.getNickname());
- sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar());
- sysWechatCustomerService.updateById(sysWechatCustomer);
- }
- }
|