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 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::getId, customer) .list() .stream() .collect(Collectors.groupingBy(SysWechatCustomerDuty::getCustomerId)); // 设置值班时间 sysWechatCustomerVoPageInfo.getList().forEach(sysWechatCustomerVo -> { List sysWechatCustomerDuties = dutyMap.get(Long.valueOf(sysWechatCustomerVo.getId())); 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 { 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 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 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); } }