SysWechatCustomerBusinessServiceImpl.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package com.ydtech.modules.admin.service.impl;
  2. import com.github.pagehelper.PageInfo;
  3. import com.ydtech.exception.SystemException;
  4. import com.ydtech.modules.admin.model.po.SysWechatCustomer;
  5. import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
  6. import com.ydtech.modules.admin.model.vo.wechat.*;
  7. import com.ydtech.modules.admin.service.SysWechatCustomerBusinessService;
  8. import com.ydtech.modules.admin.service.SysWechatCustomerDutyService;
  9. import com.ydtech.modules.admin.service.SysWechatCustomerService;
  10. import com.ydtech.modules.order.entity.BasePageResult;
  11. import com.ydtech.modules.order.entity.po.InsUploadFiles;
  12. import com.ydtech.modules.order.service.InsUploadFilesService;
  13. import com.ydtech.utils.DateTimeUtil;
  14. import lombok.RequiredArgsConstructor;
  15. import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
  16. import me.chanjar.weixin.common.error.WxErrorException;
  17. import me.chanjar.weixin.cp.api.WxCpDepartmentService;
  18. import me.chanjar.weixin.cp.api.WxCpKfService;
  19. import me.chanjar.weixin.cp.api.WxCpMediaService;
  20. import me.chanjar.weixin.cp.api.WxCpUserService;
  21. import me.chanjar.weixin.cp.bean.WxCpBaseResp;
  22. import me.chanjar.weixin.cp.bean.WxCpDepart;
  23. import me.chanjar.weixin.cp.bean.WxCpUser;
  24. import me.chanjar.weixin.cp.bean.kf.*;
  25. import org.springframework.stereotype.Service;
  26. import org.springframework.util.StringUtils;
  27. import java.io.File;
  28. import java.util.*;
  29. import java.util.stream.Collectors;
  30. @Service
  31. @RequiredArgsConstructor
  32. public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBusinessService {
  33. private final WxCpKfService wxCpKfService;
  34. private final WxCpMediaService wxCpMediaService;
  35. private final WxCpUserService wxCpUserService;
  36. private final WxCpDepartmentService wxCpDepartmentService;
  37. private final InsUploadFilesService insUploadFilesService;
  38. private final SysWechatCustomerService sysWechatCustomerService;
  39. private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
  40. @Override
  41. public List<SysWechatCustomerDeptVo> getDeptByDeptId() throws WxErrorException {
  42. List<WxCpDepart> list = wxCpDepartmentService.list(null);
  43. List<SysWechatCustomerDeptVo> deptVos = list.stream()
  44. .map(SysWechatCustomerDeptVo::new)
  45. .collect(Collectors.toList());
  46. Map<String, List<SysWechatCustomerDeptVo>> parentGroup = deptVos.stream()
  47. .collect(Collectors.groupingBy(SysWechatCustomerDeptVo::getParentId));
  48. Set<String> strings = parentGroup.keySet();
  49. deptVos.forEach(node -> node.setChildDept(parentGroup.getOrDefault(node.getDeptId(), Collections.emptyList())));
  50. return deptVos.stream()
  51. .filter(node -> node.getParentId() != null && strings.contains(node.getDeptId()))
  52. .collect(Collectors.toList());
  53. }
  54. @Override
  55. public List<SysWeComUserVo> getUser(String deptId) throws WxErrorException {
  56. List<WxCpUser> wxCpUsers = wxCpUserService.listSimpleByDepartment(Long.valueOf(deptId), true, 0);
  57. return wxCpUsers.stream()
  58. .map(x -> new SysWeComUserVo(x.getUserId(), x.getName()))
  59. .collect(Collectors.toList());
  60. }
  61. @Override
  62. public BasePageResult<SysWechatCustomerVo> pageQuery(SysWechatCustomerQueryVo sysWechatCustomerQuery) {
  63. PageInfo<SysWechatCustomerVo> sysWechatCustomerVoPageInfo = sysWechatCustomerService.getByNickname(sysWechatCustomerQuery);
  64. // 查询本页的值班人员
  65. List<String> customer = sysWechatCustomerVoPageInfo.getList()
  66. .stream()
  67. .map(SysWechatCustomerVo::getId)
  68. .collect(Collectors.toList());
  69. if(!customer.isEmpty()){
  70. // 获取值班时间列表
  71. Map<Long, List<SysWechatCustomerDuty>> dutyMap = sysWechatCustomerDutyService.lambdaQuery()
  72. .in(SysWechatCustomerDuty::getId, customer)
  73. .list()
  74. .stream()
  75. .collect(Collectors.groupingBy(SysWechatCustomerDuty::getCustomerId));
  76. // 设置值班时间
  77. sysWechatCustomerVoPageInfo.getList().forEach(sysWechatCustomerVo -> {
  78. List<SysWechatCustomerDuty> sysWechatCustomerDuties = dutyMap.get(Long.valueOf(sysWechatCustomerVo.getId()));
  79. List<String> collect = sysWechatCustomerDuties.stream()
  80. .map(x -> DateTimeUtil.startAndEndTimeFormatting(x.getStartTime(), x.getEndTime()))
  81. .collect(Collectors.toList());
  82. sysWechatCustomerVo.setDutyDatetime(collect);
  83. });
  84. }
  85. return new BasePageResult<>(sysWechatCustomerQuery.getPageNum(), sysWechatCustomerQuery.getPageSize(), sysWechatCustomerVoPageInfo.getTotal(),
  86. sysWechatCustomerVoPageInfo.getPages(), sysWechatCustomerVoPageInfo.getList());
  87. }
  88. @Override
  89. public SysWechatCustomerAddVo getWechatCustomer(String customerId) {
  90. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getById(customerId);
  91. return Optional.ofNullable(sysWechatCustomer)
  92. .map(SysWechatCustomerAddVo::new)
  93. .orElse(new SysWechatCustomerAddVo());
  94. }
  95. @Override
  96. public void addWechatCustomer(SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException {
  97. WxCpKfAccountAdd wxCpKfAccountAdd = new WxCpKfAccountAdd();
  98. wxCpKfAccountAdd.setName(sysWechatCustomerAddVo.getNickname());
  99. // 头像
  100. InsUploadFiles insUploadFiles = insUploadFilesService.getById(sysWechatCustomerAddVo.getAvatar());
  101. Optional.ofNullable(insUploadFiles)
  102. .map(InsUploadFiles::getFilePath)
  103. .ifPresent(filePath -> {
  104. File file = new File(filePath);
  105. // 上传素材
  106. WxMediaUploadResult image;
  107. try {
  108. image = wxCpMediaService.upload("image", file);
  109. } catch (WxErrorException e) {
  110. throw new SystemException("图片上传失败");
  111. }
  112. String mediaId = image.getMediaId();
  113. wxCpKfAccountAdd.setMediaId(mediaId);
  114. });
  115. // 添加
  116. WxCpKfAccountAddResp wxCpKfAccountAddResp = wxCpKfService.addAccount(wxCpKfAccountAdd);
  117. SysWechatCustomer sysWechatCustomer = new SysWechatCustomer(sysWechatCustomerAddVo);
  118. sysWechatCustomer.setOpenKfId(wxCpKfAccountAddResp.getOpenKfid());
  119. List<String> list = new ArrayList<>();
  120. list.add(sysWechatCustomerAddVo.getReceptionistId());
  121. WxCpKfServicerOpResp wxCpKfServicerOpResp = wxCpKfService.addServicer(wxCpKfAccountAddResp.getOpenKfid(), list);
  122. if (wxCpKfServicerOpResp.getErrcode() == 0) {
  123. sysWechatCustomerService.save(sysWechatCustomer);
  124. }
  125. }
  126. @Override
  127. public void deleteWechatCustomer(String customerId) throws WxErrorException {
  128. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
  129. WxCpKfAccountDel wxCpKfAccountDel = new WxCpKfAccountDel();
  130. wxCpKfAccountDel.setOpenKfid(sysWechatCustomer.getOpenKfId());
  131. WxCpBaseResp wxCpBaseResp;
  132. wxCpBaseResp = wxCpKfService.delAccount(wxCpKfAccountDel);
  133. if (wxCpBaseResp.getErrcode() == 0) {
  134. sysWechatCustomerService.deleteAllInfo(customerId);
  135. }
  136. }
  137. @Override
  138. public void updateWechatCustomer(String customerId, SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException {
  139. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
  140. WxCpKfAccountUpd wxCpKfAccountUpd = new WxCpKfAccountUpd();
  141. wxCpKfAccountUpd.setOpenKfid(sysWechatCustomer.getOpenKfId());
  142. boolean isAvatarSame = StringUtils.hasLength(sysWechatCustomer.getAvatar()) && !sysWechatCustomer.getAvatar()
  143. .equals(sysWechatCustomerAddVo.getAvatar());
  144. // 头像是否更换
  145. if (isAvatarSame) {
  146. InsUploadFiles insUploadFiles = insUploadFilesService.getById(sysWechatCustomerAddVo.getAvatar());
  147. File file = new File(insUploadFiles.getFilePath());
  148. WxMediaUploadResult image = wxCpMediaService.upload("image", file);
  149. String mediaId = image.getMediaId();
  150. wxCpKfAccountUpd.setMediaId(mediaId);
  151. sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar());
  152. }
  153. // 昵称是否相同
  154. boolean isNicknameSame = !sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname());
  155. if (isNicknameSame) {
  156. wxCpKfAccountUpd.setName(sysWechatCustomerAddVo.getNickname());
  157. }
  158. // 客服人员是否替换
  159. if (!sysWechatCustomer.getReceptionistId().equals(sysWechatCustomerAddVo.getReceptionistId())) {
  160. List<String> list = new ArrayList<>();
  161. list.add(sysWechatCustomer.getReceptionistId());
  162. wxCpKfService.delServicer(sysWechatCustomer.getOpenKfId(), list);
  163. list = new ArrayList<>();
  164. list.add(sysWechatCustomerAddVo.getReceptionistId());
  165. wxCpKfService.addServicer(sysWechatCustomer.getOpenKfId(), list);
  166. sysWechatCustomer.setReceptionistId(sysWechatCustomerAddVo.getReceptionistId());
  167. }
  168. // 昵称和头像是否跟换
  169. if (isNicknameSame || isAvatarSame) {
  170. wxCpKfService.updAccount(wxCpKfAccountUpd);
  171. }
  172. sysWechatCustomer.setNickname(sysWechatCustomerAddVo.getNickname());
  173. sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar());
  174. sysWechatCustomerService.updateById(sysWechatCustomer);
  175. }
  176. }