| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package com.ydtech.modules.admin.service.impl;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.github.pagehelper.Page;
- import com.github.pagehelper.PageHelper;
- import com.github.pagehelper.PageInfo;
- import com.ydtech.exception.SystemException;
- import com.ydtech.modules.admin.dao.SysWechatCustomerMapper;
- import com.ydtech.modules.admin.model.SysUser;
- import com.ydtech.modules.admin.model.po.SysWechatCustomer;
- import com.ydtech.modules.admin.model.po.SysWechatCustomerDuty;
- import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerDutyRulesVo;
- import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerQueryVo;
- import com.ydtech.modules.admin.model.vo.wechat.SysWechatCustomerVo;
- import com.ydtech.modules.admin.service.SysUserService;
- import com.ydtech.modules.admin.service.SysWechatCustomerDutyService;
- import com.ydtech.modules.admin.service.SysWechatCustomerService;
- import com.ydtech.utils.DateTimeUtil;
- import lombok.RequiredArgsConstructor;
- import org.springframework.data.redis.core.StringRedisTemplate;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.util.ObjectUtils;
- import org.springframework.util.StringUtils;
- import java.time.LocalDateTime;
- import java.util.List;
- import java.util.Map;
- import java.util.Optional;
- import java.util.stream.Collectors;
- /**
- * @author Administrator
- * @description 针对表【sys_wechat_customer(微信客服)】的数据库操作Service实现
- * @createDate 2025-04-17 10:14:27
- */
- @Service
- @RequiredArgsConstructor
- public class SysWechatCustomerServiceImpl extends ServiceImpl<SysWechatCustomerMapper, SysWechatCustomer>
- implements SysWechatCustomerService {
- private final SysWechatCustomerDutyService sysWechatCustomerDutyService;
- private final StringRedisTemplate redisTemplate;
- private final SysUserService sysUserService;
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void deleteAllInfo(String customerId) {
- removeById(customerId);
- sysWechatCustomerDutyService.clearCustomerDuty(customerId);
- }
- @Override
- public PageInfo<SysWechatCustomerVo> getByNickname(SysWechatCustomerQueryVo sysWechatCustomerQuery) {
- Page<SysWechatCustomerVo> ignored = PageHelper.startPage(sysWechatCustomerQuery.getPageNum(), sysWechatCustomerQuery.getPageSize());
- List<SysWechatCustomerVo> sysWechatCustomerVoList = baseMapper.getByNickname(sysWechatCustomerQuery.getNickname());
- return new PageInfo<>(sysWechatCustomerVoList);
- }
- @Override
- public SysWechatCustomerDutyRulesVo getCustomerDutyRules(String customerId) {
- SysWechatCustomer customer = getByIdExist(customerId, "客服不存在");
- List<SysWechatCustomerDuty> sysWechatCustomerDuties = sysWechatCustomerDutyService.getByCustomerId(customerId);
- SysWechatCustomerDutyRulesVo sysWechatCustomerDutyRulesVo = new SysWechatCustomerDutyRulesVo();
- sysWechatCustomerDutyRulesVo.setCustomerId(String.valueOf(customer.getId()));
- Map<Long, List<SysWechatCustomerDuty>> listMap = sysWechatCustomerDuties.stream()
- .collect(Collectors.groupingBy(SysWechatCustomerDuty::getDutyCustomerId));
- List<SysWechatCustomerDutyRulesVo.DutyCustomerDTO> dutyCustomerDTOS = listMap.keySet()
- .stream()
- .map(x -> new SysWechatCustomerDutyRulesVo.DutyCustomerDTO(x, listMap.get(x)))
- .collect(Collectors.toList());
- sysWechatCustomerDutyRulesVo.setDutyCustomerDTOS(dutyCustomerDTOS);
- return sysWechatCustomerDutyRulesVo;
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void saveOrUpdateCustomerDutyRules(SysWechatCustomerDutyRulesVo sysWechatCustomerDutyRules) {
- SysWechatCustomer customer = getByIdExist(sysWechatCustomerDutyRules.getCustomerId(), "客服不存在");
- // 前端传入的值班客服ID
- List<String> dutyCustomerIdList = sysWechatCustomerDutyRules.getDutyCustomerDTOS()
- .stream()
- .map(SysWechatCustomerDutyRulesVo.DutyCustomerDTO::getDutyCustomerId)
- .collect(Collectors.toList());
- // 数据库查出来的客服列表
- List<SysWechatCustomer> sysWechatCustomerList = lambdaQuery().in(SysWechatCustomer::getId, dutyCustomerIdList)
- .list();
- // 过滤不包含的列表
- List<SysWechatCustomer> sysWechatCustomers = sysWechatCustomerList.stream()
- .filter(sysWechatCustomer -> !dutyCustomerIdList.contains(String.valueOf(sysWechatCustomer.getId())))
- .collect(Collectors.toList());
- if (!sysWechatCustomers.isEmpty()) {
- throw new SystemException("值班客服不存在");
- }
- sysWechatCustomerDutyRules.getDutyCustomerDTOS().forEach(sysWechatCustomer -> {
- Optional.ofNullable(sysWechatCustomer.getDutyDateTimeList())
- .ifPresent(sysWechatCustomerDutyRulesList -> {
- sysWechatCustomerDutyRulesList.forEach(sysWechatCustomerDutyRule -> {
- if (DateTimeUtil.timeComparison(sysWechatCustomerDutyRule.getStartTime(), sysWechatCustomerDutyRule.getEndTime())) {
- throw new SystemException("结束时间必须晚于开始时间");
- }
- });
- });
- });
- // 清除再删除
- sysWechatCustomerDutyService.clearCustomerDuty(String.valueOf(customer.getId()));
- List<SysWechatCustomerDuty> collect = sysWechatCustomerDutyRules.getDutyCustomerDTOS()
- .stream()
- .flatMap(dutyCustomerDTO -> {
- String dutyCustomerId = dutyCustomerDTO.getDutyCustomerId();
- return dutyCustomerDTO.getDutyDateTimeList().stream()
- .map(dutyDateTime -> new SysWechatCustomerDuty(customer.getId(), Long.parseLong(dutyCustomerId), dutyDateTime));
- })
- .collect(Collectors.toList());
- sysWechatCustomerDutyService.saveBatch(collect);
- }
- @Override
- public SysWechatCustomer getCacheCustomerId(SysWechatCustomer sysWechatCustomer) {
- final String KEY = "sys:wechat:customer:";
- String customerCache = redisTemplate.opsForValue().get(KEY);
- String s1 = Optional.ofNullable(customerCache)
- .orElse("0");
- sysWechatCustomer = lambdaQuery()
- .gt(StringUtils.hasLength(s1), SysWechatCustomer::getId, s1)
- .orderByAsc(SysWechatCustomer::getId)
- .last("limit 1")
- .one();
- if (ObjectUtils.isEmpty(sysWechatCustomer)) {
- sysWechatCustomer = lambdaQuery()
- .orderByAsc(SysWechatCustomer::getId)
- .last("limit 1")
- .one();
- }
- redisTemplate.opsForValue().set(KEY, String.valueOf(sysWechatCustomer.getId()));
- return sysWechatCustomer;
- }
- @Override
- public boolean whetherTheManagersAreConsistent(String externalUserId, SysWechatCustomer sysWechatCustomer) {
- SysUser sysUser = sysUserService.lambdaQuery().eq(SysUser::getExternalUserId, externalUserId).one();
- if (ObjectUtils.isEmpty(sysUser)) {
- return true;
- }
- if(!StringUtils.hasLength(sysUser.getLeaderId())){
- return true;
- }
- return sysWechatCustomer.getUserId().equals(sysUser.getLeaderId());
- }
- }
|