SysWechatCustomerBusinessServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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.SysUser;
  5. import com.ydtech.modules.admin.model.po.SysWechatCustomer;
  6. import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
  7. import com.ydtech.modules.admin.model.po.SysWechatCustomerRecording;
  8. import com.ydtech.modules.admin.model.vo.wechat.*;
  9. import com.ydtech.modules.admin.service.SysWechatCustomerBusinessService;
  10. import com.ydtech.modules.admin.service.SysWechatCustomerDutyService;
  11. import com.ydtech.modules.admin.service.SysWechatCustomerRecordingService;
  12. import com.ydtech.modules.admin.service.SysWechatCustomerService;
  13. import com.ydtech.modules.order.entity.BasePageResult;
  14. import com.ydtech.modules.order.entity.po.InsUploadFiles;
  15. import com.ydtech.modules.order.service.InsUploadFilesService;
  16. import com.ydtech.utils.DateTimeUtil;
  17. import lombok.RequiredArgsConstructor;
  18. import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
  19. import me.chanjar.weixin.common.error.WxErrorException;
  20. import me.chanjar.weixin.cp.api.WxCpDepartmentService;
  21. import me.chanjar.weixin.cp.api.WxCpKfService;
  22. import me.chanjar.weixin.cp.api.WxCpMediaService;
  23. import me.chanjar.weixin.cp.api.WxCpUserService;
  24. import me.chanjar.weixin.cp.bean.WxCpBaseResp;
  25. import me.chanjar.weixin.cp.bean.WxCpDepart;
  26. import me.chanjar.weixin.cp.bean.WxCpUser;
  27. import me.chanjar.weixin.cp.bean.kf.*;
  28. import org.springframework.data.redis.core.StringRedisTemplate;
  29. import org.springframework.stereotype.Service;
  30. import org.springframework.util.CollectionUtils;
  31. import org.springframework.util.ObjectUtils;
  32. import org.springframework.util.StringUtils;
  33. import java.io.File;
  34. import java.time.LocalDateTime;
  35. import java.util.*;
  36. import java.util.stream.Collectors;
  37. @Service
  38. @RequiredArgsConstructor
  39. public class SysWechatCustomerBusinessServiceImpl implements SysWechatCustomerBusinessService {
  40. private final WxCpKfService wxCpKfService;
  41. private final WxCpMediaService wxCpMediaService;
  42. private final WxCpUserService wxCpUserService;
  43. private final WxCpDepartmentService wxCpDepartmentService;
  44. private final InsUploadFilesService insUploadFilesService;
  45. private final SysWechatCustomerService sysWechatCustomerService;
  46. private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
  47. private final SysWechatCustomerRecordingService sysWechatCustomerRecordingService;
  48. @Override
  49. public List<SysWechatCustomerDeptVo> getDeptByDeptId() throws WxErrorException {
  50. List<WxCpDepart> list = wxCpDepartmentService.list(null);
  51. List<SysWechatCustomerDeptVo> deptVos = list.stream()
  52. .map(SysWechatCustomerDeptVo::new)
  53. .collect(Collectors.toList());
  54. Map<String, List<SysWechatCustomerDeptVo>> parentGroup = deptVos.stream()
  55. .collect(Collectors.groupingBy(SysWechatCustomerDeptVo::getParentId));
  56. Set<String> strings = parentGroup.keySet();
  57. deptVos.forEach(node -> node.setChildDept(parentGroup.getOrDefault(node.getDeptId(), Collections.emptyList())));
  58. return deptVos.stream()
  59. .filter(node -> node.getParentId() != null && strings.contains(node.getDeptId()))
  60. .collect(Collectors.toList());
  61. }
  62. @Override
  63. public List<SysWeComUserVo> getUser(String deptId) throws WxErrorException {
  64. List<WxCpUser> wxCpUsers = wxCpUserService.listSimpleByDepartment(Long.valueOf(deptId), true, 0);
  65. return wxCpUsers.stream().map(x -> new SysWeComUserVo(x.getUserId(), x.getName())).collect(Collectors.toList());
  66. }
  67. @Override
  68. public BasePageResult<SysWechatCustomerVo> pageQuery(SysWechatCustomerQueryVo sysWechatCustomerQuery) {
  69. PageInfo<SysWechatCustomerVo> sysWechatCustomerVoPageInfo = sysWechatCustomerService.getByNickname(sysWechatCustomerQuery);
  70. // 查询本页的值班人员
  71. List<String> customer = sysWechatCustomerVoPageInfo.getList()
  72. .stream()
  73. .map(SysWechatCustomerVo::getId)
  74. .collect(Collectors.toList());
  75. if (!customer.isEmpty()) {
  76. // 获取值班时间列表
  77. Map<Long, List<SysWechatCustomerDuty>> dutyMap = sysWechatCustomerDutyService.lambdaQuery()
  78. .in(SysWechatCustomerDuty::getCustomerId, customer)
  79. .list()
  80. .stream()
  81. .collect(Collectors.groupingBy(SysWechatCustomerDuty::getCustomerId));
  82. // 设置值班时间
  83. sysWechatCustomerVoPageInfo.getList().forEach(sysWechatCustomerVo -> {
  84. List<SysWechatCustomerDuty> sysWechatCustomerDuties = dutyMap.get(Long.valueOf(sysWechatCustomerVo.getId()));
  85. if (!CollectionUtils.isEmpty(sysWechatCustomerDuties)) {
  86. List<String> collect = sysWechatCustomerDuties.stream()
  87. .map(x -> DateTimeUtil.startAndEndTimeFormatting(x.getStartTime(), x.getEndTime()))
  88. .collect(Collectors.toList());
  89. sysWechatCustomerVo.setDutyDatetime(collect);
  90. }
  91. });
  92. }
  93. return new BasePageResult<>(sysWechatCustomerQuery.getPageNum(), sysWechatCustomerQuery.getPageSize(), sysWechatCustomerVoPageInfo.getTotal(), sysWechatCustomerVoPageInfo.getPages(), sysWechatCustomerVoPageInfo.getList());
  94. }
  95. @Override
  96. public SysWechatCustomerAddVo getWechatCustomer(String customerId) {
  97. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getById(customerId);
  98. return Optional.ofNullable(sysWechatCustomer)
  99. .map(SysWechatCustomerAddVo::new)
  100. .orElse(new SysWechatCustomerAddVo());
  101. }
  102. @Override
  103. public void addWechatCustomer(SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException {
  104. sysWechatCustomerService.isNicknameRepeat(sysWechatCustomerAddVo.getNickname());
  105. WxCpKfAccountAdd wxCpKfAccountAdd = new WxCpKfAccountAdd();
  106. wxCpKfAccountAdd.setName(sysWechatCustomerAddVo.getNickname());
  107. // 头像
  108. if (StringUtils.hasLength(sysWechatCustomerAddVo.getAvatar())) {
  109. InsUploadFiles insUploadFiles = insUploadFilesService.getById(sysWechatCustomerAddVo.getAvatar());
  110. Optional.ofNullable(insUploadFiles).map(InsUploadFiles::getFilePath).ifPresent(filePath -> {
  111. File file = new File(filePath);
  112. // 上传素材
  113. WxMediaUploadResult image;
  114. try {
  115. image = wxCpMediaService.upload("image", file);
  116. } catch (WxErrorException e) {
  117. throw new SystemException("图片上传失败");
  118. }
  119. String mediaId = image.getMediaId();
  120. wxCpKfAccountAdd.setMediaId(mediaId);
  121. });
  122. }
  123. // 添加
  124. WxCpKfAccountAddResp wxCpKfAccountAddResp = wxCpKfService.addAccount(wxCpKfAccountAdd);
  125. SysWechatCustomer sysWechatCustomer = new SysWechatCustomer(sysWechatCustomerAddVo);
  126. sysWechatCustomer.setOpenKfId(wxCpKfAccountAddResp.getOpenKfid());
  127. List<String> list = new ArrayList<>();
  128. list.add(sysWechatCustomerAddVo.getReceptionistId());
  129. WxCpKfServicerOpResp wxCpKfServicerOpResp = wxCpKfService.addServicer(wxCpKfAccountAddResp.getOpenKfid(), list);
  130. if (wxCpKfServicerOpResp.getErrcode() == 0) {
  131. sysWechatCustomerService.save(sysWechatCustomer);
  132. }
  133. }
  134. @Override
  135. public void deleteWechatCustomer(String customerId) throws WxErrorException {
  136. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
  137. sysWechatCustomerService.isDutyCustomerId(sysWechatCustomer.getId());
  138. WxCpKfAccountDel wxCpKfAccountDel = new WxCpKfAccountDel();
  139. wxCpKfAccountDel.setOpenKfid(sysWechatCustomer.getOpenKfId());
  140. WxCpBaseResp wxCpBaseResp;
  141. wxCpBaseResp = wxCpKfService.delAccount(wxCpKfAccountDel);
  142. if (wxCpBaseResp.getErrcode() == 0) {
  143. sysWechatCustomerService.deleteAllInfo(customerId);
  144. sysWechatCustomerDutyService.clearCustomerDuty(customerId);
  145. }
  146. }
  147. @Override
  148. public void updateWechatCustomer(String customerId, SysWechatCustomerAddVo sysWechatCustomerAddVo) throws WxErrorException {
  149. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByIdExist(customerId, "客服不存在");
  150. if(!sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname())){
  151. sysWechatCustomerService.isNicknameRepeat(sysWechatCustomerAddVo.getNickname());
  152. }
  153. WxCpKfAccountUpd wxCpKfAccountUpd = new WxCpKfAccountUpd();
  154. wxCpKfAccountUpd.setOpenKfid(sysWechatCustomer.getOpenKfId());
  155. boolean isAvatarSame = StringUtils.hasLength(sysWechatCustomer.getAvatar()) && !sysWechatCustomer.getAvatar()
  156. .equals(sysWechatCustomerAddVo.getAvatar());
  157. // 头像是否更换
  158. if (isAvatarSame) {
  159. InsUploadFiles insUploadFiles = insUploadFilesService.getById(sysWechatCustomerAddVo.getAvatar());
  160. File file = new File(insUploadFiles.getFilePath());
  161. WxMediaUploadResult image = wxCpMediaService.upload("image", file);
  162. String mediaId = image.getMediaId();
  163. wxCpKfAccountUpd.setMediaId(mediaId);
  164. sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar());
  165. }
  166. // 昵称是否相同
  167. boolean isNicknameSame = !sysWechatCustomer.getNickname().equals(sysWechatCustomerAddVo.getNickname());
  168. if (isNicknameSame) {
  169. wxCpKfAccountUpd.setName(sysWechatCustomerAddVo.getNickname());
  170. }
  171. // 客服人员是否替换
  172. if (!sysWechatCustomer.getReceptionistId().equals(sysWechatCustomerAddVo.getReceptionistId())) {
  173. List<String> list = new ArrayList<>();
  174. list.add(sysWechatCustomer.getReceptionistId());
  175. wxCpKfService.delServicer(sysWechatCustomer.getOpenKfId(), list);
  176. list = new ArrayList<>();
  177. list.add(sysWechatCustomerAddVo.getReceptionistId());
  178. wxCpKfService.addServicer(sysWechatCustomer.getOpenKfId(), list);
  179. sysWechatCustomer.setReceptionistId(sysWechatCustomerAddVo.getReceptionistId());
  180. sysWechatCustomer.setReceptionist(sysWechatCustomerAddVo.getReceptionist());
  181. }
  182. // 昵称和头像是否跟换
  183. if (isNicknameSame || isAvatarSame) {
  184. wxCpKfService.updAccount(wxCpKfAccountUpd);
  185. }
  186. sysWechatCustomer.setNickname(sysWechatCustomerAddVo.getNickname());
  187. sysWechatCustomer.setAvatar(sysWechatCustomerAddVo.getAvatar());
  188. sysWechatCustomer.setUserId(sysWechatCustomerAddVo.getUserId());
  189. sysWechatCustomerService.updateById(sysWechatCustomer);
  190. }
  191. @Override
  192. public String getWeChatCustomer(SysUser user) throws WxErrorException {
  193. SysWechatCustomer sysWechatCustomer = sysWechatCustomerService.getByUserId(user.getLeaderId());
  194. List<SysWechatCustomer> sysWechatCustomerList = sysWechatCustomerService.list();
  195. List<Long> sysWechatCustomerIdList = sysWechatCustomerList.stream()
  196. .map(SysWechatCustomer::getId)
  197. .collect(Collectors.toList());
  198. if (ObjectUtils.isEmpty(sysWechatCustomer)) {
  199. // 获取最近一次的客服记录
  200. SysWechatCustomerRecording sysWechatCustomerRecording = sysWechatCustomerRecordingService.lambdaQuery()
  201. .in(!sysWechatCustomerIdList.isEmpty(), SysWechatCustomerRecording::getCustomerId, sysWechatCustomerIdList)
  202. .eq(SysWechatCustomerRecording::getUserId, user.getId())
  203. .orderByDesc(SysWechatCustomerRecording::getCreateTime)
  204. .last("limit 1")
  205. .one();
  206. if (ObjectUtils.isEmpty(sysWechatCustomerRecording)) {
  207. sysWechatCustomerService.getCacheCustomerId(sysWechatCustomer);
  208. } else {
  209. // 使用上一次的客服
  210. sysWechatCustomer = sysWechatCustomerService.lambdaQuery()
  211. .eq(SysWechatCustomer::getId, sysWechatCustomerRecording.getCustomerId())
  212. .one();
  213. if (ObjectUtils.isEmpty(sysWechatCustomer)) {
  214. sysWechatCustomerService.getCacheCustomerId(sysWechatCustomer);
  215. }
  216. }
  217. SysWechatCustomerRecording customerRecording = new SysWechatCustomerRecording();
  218. customerRecording.setCustomerId(sysWechatCustomer.getId());
  219. customerRecording.setUserId(user.getId());
  220. sysWechatCustomerRecordingService.save(customerRecording);
  221. }
  222. List<SysWechatCustomerDuty> list = sysWechatCustomerDutyService.lambdaQuery()
  223. .eq(SysWechatCustomerDuty::getCustomerId, sysWechatCustomer.getId())
  224. .le(SysWechatCustomerDuty::getStartTime, LocalDateTime.now())
  225. .ge(SysWechatCustomerDuty::getEndTime, LocalDateTime.now())
  226. .list();
  227. if (sysWechatCustomer.getDutyCustomerId() > 0 && list != null && !list.isEmpty()) {
  228. sysWechatCustomer = sysWechatCustomerService.getById(sysWechatCustomer.getDutyCustomerId());
  229. }
  230. if (ObjectUtils.isEmpty(sysWechatCustomer)) {
  231. throw new SystemException("请联系管理员,配置客服");
  232. }
  233. String openKfId = sysWechatCustomer.getOpenKfId();
  234. WxCpKfAccountLink wxCpKfAccountLink = new WxCpKfAccountLink();
  235. wxCpKfAccountLink.setOpenKfid(openKfId);
  236. wxCpKfAccountLink.setScene(user.getId());
  237. WxCpKfAccountLinkResp accountLink = wxCpKfService.getAccountLink(wxCpKfAccountLink);
  238. return accountLink.getUrl();
  239. }
  240. }