SysWechatCustomerServiceImpl.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package com.ydtech.modules.admin.service.impl;
  2. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  3. import com.github.pagehelper.Page;
  4. import com.github.pagehelper.PageHelper;
  5. import com.github.pagehelper.PageInfo;
  6. import com.ydtech.exception.SystemException;
  7. import com.ydtech.modules.admin.dao.SysWechatCustomerMapper;
  8. import com.ydtech.modules.admin.model.SysUser;
  9. import com.ydtech.modules.admin.model.po.SysWechatCustomer;
  10. import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
  11. import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerDutyRulesVo;
  12. import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerQueryVo;
  13. import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerVo;
  14. import com.ydtech.modules.admin.service.SysUserService;
  15. import com.ydtech.modules.admin.service.SysWechatCustomerDutyService;
  16. import com.ydtech.modules.admin.service.SysWechatCustomerService;
  17. import com.ydtech.utils.DateTimeUtil;
  18. import lombok.RequiredArgsConstructor;
  19. import org.springframework.data.redis.core.StringRedisTemplate;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import org.springframework.util.ObjectUtils;
  23. import org.springframework.util.StringUtils;
  24. import java.time.LocalDateTime;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import java.util.stream.Collectors;
  29. /**
  30. * @author Administrator
  31. * @description 针对表【sys_wechat_customer(微信客服)】的数据库操作Service实现
  32. * @createDate 2025-04-17 10:14:27
  33. */
  34. @Service
  35. @RequiredArgsConstructor
  36. public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerMapper, SysWechatCustomer>
  37. implements SysWechatCustomerService {
  38. private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
  39. private final StringRedisTemplate redisTemplate;
  40. private final SysUserService sysUserService;
  41. @Override
  42. @Transactional(rollbackFor = Exception.class)
  43. public void deleteAllInfo(String customerId) {
  44. removeById(customerId);
  45. sysWechatCustomerDutyService.clearCustomerDuty(customerId);
  46. }
  47. @Override
  48. public PageInfo<SysWechatCustomerVo> getByNickname(SysWechatCustomerQueryVo sysWechatCustomerQuery) {
  49. Page<SysWechatCustomerVo> ignored = PageHelper.startPage(sysWechatCustomerQuery.getPageNum(), sysWechatCustomerQuery.getPageSize());
  50. List<SysWechatCustomerVo> sysWechatCustomerVoList = baseMapper.getByNickname(sysWechatCustomerQuery.getNickname());
  51. return new PageInfo<>(sysWechatCustomerVoList);
  52. }
  53. @Override
  54. public SysWechatCustomerDutyRulesVo getCustomerDutyRules(String customerId) {
  55. SysWechatCustomer customer = getByIdExist(customerId, "客服不存在");
  56. List<SysWechatCustomerDuty> sysWechatCustomerDuties = sysWechatCustomerDutyService.getByCustomerId(customerId);
  57. SysWechatCustomerDutyRulesVo sysWechatCustomerDutyRulesVo = new SysWechatCustomerDutyRulesVo();
  58. sysWechatCustomerDutyRulesVo.setCustomerId(String.valueOf(customer.getId()));
  59. Map<Long, List<SysWechatCustomerDuty>> listMap = sysWechatCustomerDuties.stream()
  60. .collect(Collectors.groupingBy(SysWechatCustomerDuty::getDutyCustomerId));
  61. List<SysWechatCustomerDutyRulesVo.DutyCustomerDTO> dutyCustomerDTOS = listMap.keySet()
  62. .stream()
  63. .map(x -> new SysWechatCustomerDutyRulesVo.DutyCustomerDTO(x, listMap.get(x)))
  64. .collect(Collectors.toList());
  65. sysWechatCustomerDutyRulesVo.setDutyCustomerDTOS(dutyCustomerDTOS);
  66. return sysWechatCustomerDutyRulesVo;
  67. }
  68. @Override
  69. @Transactional(rollbackFor = Exception.class)
  70. public void saveOrUpdateCustomerDutyRules(SysWechatCustomerDutyRulesVo sysWechatCustomerDutyRules) {
  71. SysWechatCustomer customer = getByIdExist(sysWechatCustomerDutyRules.getCustomerId(), "客服不存在");
  72. // 前端传入的值班客服ID
  73. List<String> dutyCustomerIdList = sysWechatCustomerDutyRules.getDutyCustomerDTOS()
  74. .stream()
  75. .map(SysWechatCustomerDutyRulesVo.DutyCustomerDTO::getDutyCustomerId)
  76. .collect(Collectors.toList());
  77. // 数据库查出来的客服列表
  78. List<SysWechatCustomer> sysWechatCustomerList = lambdaQuery().in(SysWechatCustomer::getId, dutyCustomerIdList)
  79. .list();
  80. // 过滤不包含的列表
  81. List<SysWechatCustomer> sysWechatCustomers = sysWechatCustomerList.stream()
  82. .filter(sysWechatCustomer -> !dutyCustomerIdList.contains(String.valueOf(sysWechatCustomer.getId())))
  83. .collect(Collectors.toList());
  84. if (!sysWechatCustomers.isEmpty()) {
  85. throw new SystemException("值班客服不存在");
  86. }
  87. sysWechatCustomerDutyRules.getDutyCustomerDTOS().forEach(sysWechatCustomer -> {
  88. Optional.ofNullable(sysWechatCustomer.getDutyDateTimeList())
  89. .ifPresent(sysWechatCustomerDutyRulesList -> {
  90. sysWechatCustomerDutyRulesList.forEach(sysWechatCustomerDutyRule -> {
  91. if (DateTimeUtil.timeComparison(sysWechatCustomerDutyRule.getStartTime(), sysWechatCustomerDutyRule.getEndTime())) {
  92. throw new SystemException("结束时间必须晚于开始时间");
  93. }
  94. });
  95. });
  96. });
  97. // 清除再删除
  98. sysWechatCustomerDutyService.clearCustomerDuty(String.valueOf(customer.getId()));
  99. List<SysWechatCustomerDuty> collect = sysWechatCustomerDutyRules.getDutyCustomerDTOS()
  100. .stream()
  101. .flatMap(dutyCustomerDTO -> {
  102. String dutyCustomerId = dutyCustomerDTO.getDutyCustomerId();
  103. return dutyCustomerDTO.getDutyDateTimeList().stream()
  104. .map(dutyDateTime -> new SysWechatCustomerDuty(customer.getId(), Long.parseLong(dutyCustomerId), dutyDateTime));
  105. })
  106. .collect(Collectors.toList());
  107. sysWechatCustomerDutyService.saveBatch(collect);
  108. }
  109. @Override
  110. public SysWechatCustomer getCacheCustomerId(SysWechatCustomer sysWechatCustomer) {
  111. final String KEY = "sys:wechat:customer:";
  112. String customerCache = redisTemplate.opsForValue().get(KEY);
  113. String s1 = Optional.ofNullable(customerCache)
  114. .orElse("0");
  115. sysWechatCustomer = lambdaQuery()
  116. .gt(StringUtils.hasLength(s1), SysWechatCustomer::getId, s1)
  117. .orderByAsc(SysWechatCustomer::getId)
  118. .last("limit 1")
  119. .one();
  120. if (ObjectUtils.isEmpty(sysWechatCustomer)) {
  121. sysWechatCustomer = lambdaQuery()
  122. .orderByAsc(SysWechatCustomer::getId)
  123. .last("limit 1")
  124. .one();
  125. }
  126. redisTemplate.opsForValue().set(KEY, String.valueOf(sysWechatCustomer.getId()));
  127. return sysWechatCustomer;
  128. }
  129. @Override
  130. public boolean whetherTheManagersAreConsistent(String externalUserId, SysWechatCustomer sysWechatCustomer) {
  131. SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getExternalUserId, externalUserId).one();
  132. if (ObjectUtils.isEmpty(sysUser)) {
  133. return true;
  134. }
  135. if(!StringUtils.hasLength(sysUser.getLeaderId())){
  136. return true;
  137. }
  138. return sysWechatCustomer.getUserId().equals(sysUser.getLeaderId());
  139. }
  140. }