|
@@ -1,5 +1,6 @@
|
|
|
package cn.tr.module.smart.common.service.impl;
|
|
package cn.tr.module.smart.common.service.impl;
|
|
|
|
|
|
|
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.tr.core.annotation.TenantIgnore;
|
|
import cn.tr.core.annotation.TenantIgnore;
|
|
@@ -9,25 +10,26 @@ import cn.tr.module.smart.common.controller.vo.BizReasearchQueuePushVO;
|
|
|
import cn.tr.module.smart.common.controller.vo.BizReasearchQueueQuestionAnswerVO;
|
|
import cn.tr.module.smart.common.controller.vo.BizReasearchQueueQuestionAnswerVO;
|
|
|
import cn.tr.module.smart.common.dto.BizReasearchQueueAddClinicDTO;
|
|
import cn.tr.module.smart.common.dto.BizReasearchQueueAddClinicDTO;
|
|
|
import cn.tr.module.smart.common.dto.BizReasearchQueueClinicQuestionAnswerRecordQueryDTO;
|
|
import cn.tr.module.smart.common.dto.BizReasearchQueueClinicQuestionAnswerRecordQueryDTO;
|
|
|
|
|
+import cn.tr.module.smart.common.enums.MpPublishTaskTypeEnums;
|
|
|
|
|
+import cn.tr.module.smart.common.enums.RabbitMQConstant;
|
|
|
import cn.tr.module.smart.common.mapper.BizQuestionAnswerMapper;
|
|
import cn.tr.module.smart.common.mapper.BizQuestionAnswerMapper;
|
|
|
-import cn.tr.module.smart.common.po.BizClinicRoomPO;
|
|
|
|
|
-import cn.tr.module.smart.common.po.BizQuestionAnswerPO;
|
|
|
|
|
-import cn.tr.module.smart.common.po.BizReasearchQueuePO;
|
|
|
|
|
-import cn.tr.module.smart.common.repository.BizClinicRoomRepository;
|
|
|
|
|
-import cn.tr.module.smart.common.repository.BizQuestionAnswerRepository;
|
|
|
|
|
-import cn.tr.module.smart.common.repository.BizReasearchQueueRepository;
|
|
|
|
|
|
|
+import cn.tr.module.smart.common.po.*;
|
|
|
|
|
+import cn.tr.module.smart.common.repository.*;
|
|
|
|
|
+import cn.tr.module.smart.wx.config.WxTemplateSendHelper;
|
|
|
|
|
+import cn.tr.module.smart.wx.dto.BizMpPublishInfoDTO;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
import cn.tr.core.exception.ServiceException;
|
|
import cn.tr.core.exception.ServiceException;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
-import cn.tr.module.smart.common.repository.BizReasearchQueueClinicRepository;
|
|
|
|
|
-import cn.tr.module.smart.common.po.BizReasearchQueueClinicPO;
|
|
|
|
|
import cn.tr.module.smart.common.dto.BizReasearchQueueClinicQueryDTO;
|
|
import cn.tr.module.smart.common.dto.BizReasearchQueueClinicQueryDTO;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
import cn.tr.module.smart.common.service.IBizReasearchQueueClinicService;
|
|
import cn.tr.module.smart.common.service.IBizReasearchQueueClinicService;
|
|
@@ -38,6 +40,7 @@ import cn.tr.module.smart.common.service.IBizReasearchQueueClinicService;
|
|
|
* @date 2025/10/15 08:27
|
|
* @date 2025/10/15 08:27
|
|
|
**/
|
|
**/
|
|
|
@Service
|
|
@Service
|
|
|
|
|
+@Slf4j
|
|
|
public class BizReasearchQueueClinicServiceImpl extends ServiceImpl<BizReasearchQueueClinicRepository,BizReasearchQueueClinicPO> implements IBizReasearchQueueClinicService {
|
|
public class BizReasearchQueueClinicServiceImpl extends ServiceImpl<BizReasearchQueueClinicRepository,BizReasearchQueueClinicPO> implements IBizReasearchQueueClinicService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private BizQuestionAnswerRepository questionAnswerRepository;
|
|
private BizQuestionAnswerRepository questionAnswerRepository;
|
|
@@ -45,6 +48,12 @@ public class BizReasearchQueueClinicServiceImpl extends ServiceImpl<BizReasearch
|
|
|
private BizReasearchQueueRepository reasearchQueueRepository;
|
|
private BizReasearchQueueRepository reasearchQueueRepository;
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private BizClinicRoomRepository clinicRoomRepository;
|
|
private BizClinicRoomRepository clinicRoomRepository;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RabbitTemplate rabbitTemplate;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private WxTemplateSendHelper wxTemplateSendHelper;
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private BizMpPublishTaskRepository bizMpPublishTaskRepository;
|
|
|
/**
|
|
/**
|
|
|
* 根据条件查询队列-临床手术关联表
|
|
* 根据条件查询队列-临床手术关联表
|
|
|
* @param query 查询参数
|
|
* @param query 查询参数
|
|
@@ -126,16 +135,113 @@ public class BizReasearchQueueClinicServiceImpl extends ServiceImpl<BizReasearch
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
@TenantIgnore
|
|
@TenantIgnore
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
public void pushReasearchQueueQuestion(String id){
|
|
public void pushReasearchQueueQuestion(String id){
|
|
|
- if(StrUtil.isBlank(id)){
|
|
|
|
|
|
|
+ // 添加日志记录
|
|
|
|
|
+ log.info("开始处理研究队列推送任务,ID: {}", id);
|
|
|
|
|
+
|
|
|
|
|
+ // 查询研究队列信息
|
|
|
|
|
+ BizReasearchQueuePushVO reasearchQueuePushVO = this.baseMapper.stdSelectBizReasearchQueueClinicById(id);
|
|
|
|
|
+ if (ObjectUtil.isNull(reasearchQueuePushVO)) {
|
|
|
|
|
+ log.warn("未找到对应的研究队列信息,ID: {}", id);
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- BizReasearchQueuePushVO reasearchQueuePushVO=this.baseMapper.stdSelectBizReasearchQueueClinicById(id);
|
|
|
|
|
- if(ObjectUtil.isNull(reasearchQueuePushVO)){
|
|
|
|
|
|
|
+ // 获取当前时间和手术结束时间
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ Date clinicEndTime = reasearchQueuePushVO.getClinicEndTime();
|
|
|
|
|
+ log.info("当前时间: {}, 手术结束时间: {}", now, clinicEndTime);
|
|
|
|
|
+
|
|
|
|
|
+ if (ObjectUtil.isNull(clinicEndTime)) {
|
|
|
|
|
+ log.info("手术尚未结束,24小时后再次判断,ID: {}", id);
|
|
|
|
|
+ sendDelayMessage(id, TimeUnit.DAYS.toMillis(1));
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
- //先判断当前时间与手术结束时间的时间间隔是否满足研究队列的最小时间间隔要求
|
|
|
|
|
|
|
|
|
|
|
|
+ // 计算时间差
|
|
|
|
|
+ long timeDiffMillion = now.getTime() - clinicEndTime.getTime();
|
|
|
|
|
+ log.info("当前时间与手术结束时间的差值(毫秒): {}", timeDiffMillion);
|
|
|
|
|
+
|
|
|
|
|
+ // 获取上次推送间隔和时间点配置
|
|
|
|
|
+ Long lastPushInterval = ObjectUtil.isNull(reasearchQueuePushVO.getLastPushInterval()) ? 0 : reasearchQueuePushVO.getLastPushInterval();
|
|
|
|
|
+ List<Long> timePointsFormat = reasearchQueuePushVO.getTimePointsFormat();
|
|
|
|
|
+ log.debug("上次推送间隔: {} 毫秒", lastPushInterval);
|
|
|
|
|
+ log.debug("推送时间点配置: {}", timePointsFormat);
|
|
|
|
|
+
|
|
|
|
|
+ // 过滤出大于上次推送间隔的时间点
|
|
|
|
|
+ timePointsFormat = timePointsFormat.stream()
|
|
|
|
|
+ .filter(timeInterval -> timeInterval > lastPushInterval)
|
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
|
+ log.debug("过滤后的推送时间点: {}", timePointsFormat);
|
|
|
|
|
+
|
|
|
|
|
+ // 确定下一个推送间隔
|
|
|
|
|
+ Long nextPushInterval = CollectionUtil.getFirst(timePointsFormat);
|
|
|
|
|
+ if (CollectionUtil.size(timePointsFormat) == 1) {
|
|
|
|
|
+ nextPushInterval = CollectionUtil.getFirst(timePointsFormat);
|
|
|
|
|
+ }
|
|
|
|
|
+ log.debug("确定的下一个推送间隔: {} 毫秒", nextPushInterval);
|
|
|
|
|
+
|
|
|
|
|
+ // 如果时间点数量大于1,查找相邻时间点
|
|
|
|
|
+ if (CollectionUtil.isNotEmpty(timePointsFormat) && timePointsFormat.size() > 1) {
|
|
|
|
|
+ List<Long> sortedTimePoints = timePointsFormat.stream().sorted().collect(Collectors.toList());
|
|
|
|
|
+ Long nextPoint = sortedTimePoints.stream()
|
|
|
|
|
+ .filter(point -> point > timeDiffMillion)
|
|
|
|
|
+ .findFirst()
|
|
|
|
|
+ .orElse(null);
|
|
|
|
|
+ Long prevPoint = sortedTimePoints.stream()
|
|
|
|
|
+ .filter(point -> point < timeDiffMillion)
|
|
|
|
|
+ .max(Long::compareTo)
|
|
|
|
|
+ .orElse(null);
|
|
|
|
|
|
|
|
|
|
+ log.debug("与当前时间差值相邻的两个时间点: prevPoint={}, nextPoint={}", prevPoint, nextPoint);
|
|
|
|
|
+
|
|
|
|
|
+ if (prevPoint != null) {
|
|
|
|
|
+ log.info("发现已过期的推送时间点,立即执行推送: {}", prevPoint);
|
|
|
|
|
+ sendMessage(reasearchQueuePushVO, prevPoint);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (nextPoint != null) {
|
|
|
|
|
+ nextPushInterval = nextPoint;
|
|
|
|
|
+ log.debug("更新下一个推送间隔为: {} 毫秒", nextPushInterval);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 判断是否需要立即推送或延迟推送
|
|
|
|
|
+ if (nextPushInterval < timeDiffMillion) {
|
|
|
|
|
+ log.info("已超过推送时间,立即推送消息,ID: {}", id);
|
|
|
|
|
+ sendMessage(reasearchQueuePushVO, nextPushInterval);
|
|
|
|
|
+ } else {
|
|
|
|
|
+ long waitMillion = nextPushInterval - timeDiffMillion;
|
|
|
|
|
+ log.info("未满足推送条件,需等待 {} 毫秒后推送,ID: {}", waitMillion, id);
|
|
|
|
|
+ sendDelayMessage(id, waitMillion);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //执行推送逻辑
|
|
|
|
|
+ private void sendMessage(BizReasearchQueuePushVO source,long nextPushInterval){
|
|
|
|
|
+ String sql =String.format("last_push_interval = %d and push_count = push_count + 1 and last_push_time = %s",nextPushInterval,DateUtil.now());
|
|
|
|
|
+ baseMapper.update(null,new LambdaUpdateWrapper<BizReasearchQueueClinicPO>()
|
|
|
|
|
+ .eq(BizReasearchQueueClinicPO::getId,source.getId())
|
|
|
|
|
+ .setSql(sql));
|
|
|
|
|
+ try {
|
|
|
|
|
+ BizMpPublishInfoDTO publishInfo = baseMapper.selectPushInfo(source.getId());
|
|
|
|
|
+ wxTemplateSendHelper.sendMsg(publishInfo, MpPublishTaskTypeEnums.AFTER_QUESTION.getType());
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送延迟消息到RabbitMQ
|
|
|
|
|
+ */
|
|
|
|
|
+ private void sendDelayMessage(String id,long delayTime) {
|
|
|
|
|
+ // 发送延迟消息
|
|
|
|
|
+ rabbitTemplate.convertAndSend(
|
|
|
|
|
+ RabbitMQConstant.TOPIC_EXCHANGE_MP_NAME,
|
|
|
|
|
+ RabbitMQConstant.ROUTING_KEY_MP_PUSH,
|
|
|
|
|
+ id,
|
|
|
|
|
+ message -> {
|
|
|
|
|
+ message.getMessageProperties().getHeaders().put("x-delay", delayTime);
|
|
|
|
|
+ return message;
|
|
|
|
|
+ }
|
|
|
|
|
+ );
|
|
|
}
|
|
}
|
|
|
-}
|
|
|
|
|
|
|
+}
|