14 changed files with 414 additions and 30 deletions
@ -0,0 +1,150 @@ |
|||
package com.acupuncture.web.controller.web; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import cn.hutool.core.util.IdUtil; |
|||
import com.acupuncture.framework.datasource.DynamicDataSourceContextHolder; |
|||
import com.acupuncture.system.domain.po.FmsFollowupTask; |
|||
import com.acupuncture.system.domain.vo.FmsFollowupVo; |
|||
import com.acupuncture.system.domain.vo.UmsDataSourceVo; |
|||
import com.acupuncture.system.persist.dao.FmsFollowupDao; |
|||
import com.acupuncture.system.persist.dao.UmsDataSourceDao; |
|||
import com.acupuncture.system.persist.mapper.FmsFollowupTaskMapper; |
|||
import io.swagger.annotations.Api; |
|||
import io.swagger.annotations.ApiOperation; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.quartz.TriggerUtils; |
|||
import org.quartz.impl.triggers.CronTriggerImpl; |
|||
import org.springframework.web.bind.annotation.PostMapping; |
|||
import org.springframework.web.bind.annotation.RequestMapping; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.util.ArrayList; |
|||
import java.util.Calendar; |
|||
import java.util.Date; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Author zzc |
|||
* @Package com.acupuncture.web.controller.web |
|||
* @Date 2025/2/13 8:50 |
|||
* @description: |
|||
*/ |
|||
@Slf4j |
|||
@Api(tags = "定时任务相关") |
|||
@RestController |
|||
@RequestMapping("/task") |
|||
public class TaskController { |
|||
|
|||
@Resource |
|||
private FmsFollowupDao fmsFollowupDao; |
|||
@Resource |
|||
private FmsFollowupTaskMapper fmsFollowupTaskMapper; |
|||
@Resource |
|||
private UmsDataSourceDao umsDataSourceDao; |
|||
|
|||
|
|||
@ApiOperation("定时任务添加随访工单") |
|||
@PostMapping("/task") |
|||
public void task() { |
|||
//查询租户,根据租户循环切换数据源,定时处理所有医院的随访工单
|
|||
List<UmsDataSourceVo.Result> query = umsDataSourceDao.query(); |
|||
if (CollectionUtil.isEmpty(query)) { |
|||
return; |
|||
} |
|||
//切换数据源
|
|||
for (UmsDataSourceVo.Result result : query) { |
|||
changeDataSource(result); |
|||
{ |
|||
//获取随访患者列表,根据患者出院日时间和队列添加工单
|
|||
//1. 查询队列
|
|||
List<FmsFollowupVo.FollowupQueueVO> queueList = fmsFollowupDao.selectQueueList(null); |
|||
//查询公共队列
|
|||
changeDataSource(result); |
|||
List<FmsFollowupVo.FollowupQueueVO> queueResults = fmsFollowupDao.queryCommonQueue(null); |
|||
if (CollectionUtil.isEmpty(queueList)) { |
|||
queueList = queueResults; |
|||
} else { |
|||
queueList.addAll(queueResults); |
|||
} |
|||
for (FmsFollowupVo.FollowupQueueVO followupQueueVO : queueList) { |
|||
//2. 查询队列随访患者列表
|
|||
changeDataSource(result); |
|||
List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupDao.queryPatient(followupQueueVO.getId()); |
|||
if (CollectionUtil.isEmpty(patientList)) { |
|||
continue; |
|||
} |
|||
//随访总月数
|
|||
Integer followupMethod = followupQueueVO.getFollowupMethod(); |
|||
for (FmsFollowupVo.FollowupPatient followupPatient : patientList) { |
|||
//获取随访到期时间 出院时间+随访总月数 = 到期时间
|
|||
Calendar calendar = Calendar.getInstance(); |
|||
calendar.setTime(followupPatient.getDischargeTime()); |
|||
calendar.set(Calendar.MONTH, followupMethod); |
|||
Date time = calendar.getTime(); |
|||
|
|||
//获取队列信息
|
|||
String frequency = followupQueueVO.getFrequency(); |
|||
List<Date> dateList = new ArrayList<>(); |
|||
try { |
|||
CronTriggerImpl cronTrigger = new CronTriggerImpl(); |
|||
cronTrigger.setCronExpression(frequency); |
|||
//TriggerUtils.computeFireTimesBetween(要计算触发时间的触发器对象, 用于计算触发时间的日历对象, 计算触发时间的起始时间点, 计算触发时间的结束时间点);
|
|||
dateList = TriggerUtils.computeFireTimesBetween(cronTrigger, null, followupPatient.getDischargeTime(), time); |
|||
if (CollectionUtil.isEmpty(dateList)) { |
|||
continue; |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
//3. 判断随访类型
|
|||
if (followupQueueVO.getFollowupType() == 0) { |
|||
//单次
|
|||
FmsFollowupTask fmsFollowupTask = new FmsFollowupTask(); |
|||
BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask); |
|||
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId()); |
|||
fmsFollowupTask.setQueueId(followupQueueVO.getId()); |
|||
fmsFollowupTask.setPatientId(followupPatient.getId()); |
|||
fmsFollowupTask.setDelFlag((byte) 0); |
|||
fmsFollowupTask.setCreateTime(new Date()); |
|||
fmsFollowupTask.setStatus((byte) 0); |
|||
fmsFollowupTask.setStartTime(dateList.get(0)); |
|||
fmsFollowupTask.setEndTime(dateList.get(0)); |
|||
fmsFollowupTask.setFollowuper(followupQueueVO.getPersonInCharge()); |
|||
fmsFollowupTask.setFollowupTime(dateList.get(0)); |
|||
changeDataSource(result); |
|||
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask); |
|||
|
|||
} else { |
|||
//周期
|
|||
//4. 根据频次和总月数添加
|
|||
for (Date date : dateList) { |
|||
//单次
|
|||
FmsFollowupTask fmsFollowupTask = new FmsFollowupTask(); |
|||
BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask); |
|||
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId()); |
|||
fmsFollowupTask.setQueueId(followupQueueVO.getId()); |
|||
fmsFollowupTask.setDelFlag((byte) 0); |
|||
fmsFollowupTask.setCreateTime(new Date()); |
|||
fmsFollowupTask.setStatus((byte) 0); |
|||
fmsFollowupTask.setStartTime(date); |
|||
fmsFollowupTask.setEndTime(date); |
|||
fmsFollowupTask.setFollowuper(followupQueueVO.getPersonInCharge()); |
|||
fmsFollowupTask.setFollowupTime(date); |
|||
changeDataSource(result); |
|||
fmsFollowupTask.setPatientId(followupPatient.getId()); |
|||
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
private static void changeDataSource(UmsDataSourceVo.Result result) { |
|||
DynamicDataSourceContextHolder.setDataSourceType(result.getDataSourceKey()); |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
package com.acupuncture.system.domain.vo; |
|||
|
|||
import com.acupuncture.system.domain.po.UmsDataSource; |
|||
import lombok.Data; |
|||
|
|||
/** |
|||
* @Author zzc |
|||
* @Package com.acupuncture.system.domain.vo |
|||
* @Date 2025/2/12 18:02 |
|||
* @description: |
|||
*/ |
|||
public class UmsDataSourceVo { |
|||
|
|||
@Data |
|||
public static class Result { |
|||
private Long id; |
|||
|
|||
private String dataSourceKey; |
|||
|
|||
private Long hospitalId; |
|||
|
|||
private String url; |
|||
|
|||
private String username; |
|||
|
|||
private String password; |
|||
|
|||
private Long tenantId; |
|||
|
|||
private String tenantName; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,20 @@ |
|||
package com.acupuncture.system.persist.dao; |
|||
|
|||
import com.acupuncture.common.annotation.DataSource; |
|||
import com.acupuncture.common.enums.DataSourceType; |
|||
import com.acupuncture.system.domain.vo.UmsDataSourceVo; |
|||
|
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Author zzc |
|||
* @Package com.acupuncture.system.persist.dao |
|||
* @Date 2025/2/12 18:02 |
|||
* @description: |
|||
*/ |
|||
public interface UmsDataSourceDao { |
|||
|
|||
@DataSource(DataSourceType.MASTER) |
|||
List<UmsDataSourceVo.Result> query(); |
|||
|
|||
} |
@ -0,0 +1,15 @@ |
|||
package com.acupuncture.system.service; |
|||
|
|||
/** |
|||
* @Author zzc |
|||
* @Package com.acupuncture.system.service |
|||
* @Date 2025/2/12 17:58 |
|||
* @description: |
|||
*/ |
|||
public interface TaskService { |
|||
|
|||
/** |
|||
* 定时任务添加随访工单 |
|||
*/ |
|||
void task(); |
|||
} |
@ -0,0 +1,120 @@ |
|||
package com.acupuncture.system.service.impl; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import cn.hutool.core.collection.CollectionUtil; |
|||
import cn.hutool.core.util.IdUtil; |
|||
import com.acupuncture.system.domain.po.FmsFollowupTask; |
|||
import com.acupuncture.system.domain.vo.FmsFollowupVo; |
|||
import com.acupuncture.system.domain.vo.UmsDataSourceVo; |
|||
import com.acupuncture.system.persist.dao.FmsFollowupDao; |
|||
import com.acupuncture.system.persist.dao.UmsDataSourceDao; |
|||
import com.acupuncture.system.persist.mapper.FmsFollowupTaskMapper; |
|||
import com.acupuncture.system.service.TaskService; |
|||
import org.quartz.TriggerUtils; |
|||
import org.quartz.impl.triggers.CronTriggerImpl; |
|||
import org.springframework.stereotype.Service; |
|||
|
|||
import javax.annotation.Resource; |
|||
import java.util.ArrayList; |
|||
import java.util.Calendar; |
|||
import java.util.Date; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Author zzc |
|||
* @Package com.acupuncture.system.service.impl |
|||
* @Date 2025/2/12 17:58 |
|||
* @description: |
|||
*/ |
|||
@Service |
|||
public class TaskServiceImpl implements TaskService { |
|||
|
|||
@Resource |
|||
private FmsFollowupDao fmsFollowupDao; |
|||
@Resource |
|||
private FmsFollowupTaskMapper fmsFollowupTaskMapper; |
|||
@Resource |
|||
private UmsDataSourceDao umsDataSourceDao; |
|||
|
|||
@Override |
|||
public void task() { |
|||
|
|||
//获取随访患者列表,根据患者出院日时间和队列添加工单
|
|||
//1. 查询队列
|
|||
List<FmsFollowupVo.FollowupQueueVO> queueList = fmsFollowupDao.selectQueueList(null); |
|||
//查询公共队列
|
|||
List<FmsFollowupVo.FollowupQueueVO> queueResults = fmsFollowupDao.queryCommonQueue(null); |
|||
if (CollectionUtil.isEmpty(queueList)) { |
|||
queueList = queueResults; |
|||
} else { |
|||
queueList.addAll(queueResults); |
|||
} |
|||
for (FmsFollowupVo.FollowupQueueVO followupQueueVO : queueList) { |
|||
//2. 查询队列随访患者列表
|
|||
List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupDao.queryPatient(followupQueueVO.getId()); |
|||
if (CollectionUtil.isEmpty(patientList)) { |
|||
continue; |
|||
} |
|||
//随访总月数
|
|||
Integer followupMethod = followupQueueVO.getFollowupMethod(); |
|||
for (FmsFollowupVo.FollowupPatient followupPatient : patientList) { |
|||
//获取随访到期时间 出院时间+随访总月数 = 到期时间
|
|||
Calendar calendar = Calendar.getInstance(); |
|||
calendar.setTime(followupPatient.getDischargeTime()); |
|||
calendar.set(Calendar.MONTH, followupMethod); |
|||
Date time = calendar.getTime(); |
|||
|
|||
//获取队列信息
|
|||
String frequency = followupQueueVO.getFrequency(); |
|||
List<Date> dateList = new ArrayList<>(); |
|||
try { |
|||
CronTriggerImpl cronTrigger = new CronTriggerImpl(); |
|||
cronTrigger.setCronExpression(frequency); |
|||
//TriggerUtils.computeFireTimesBetween(要计算触发时间的触发器对象, 用于计算触发时间的日历对象, 计算触发时间的起始时间点, 计算触发时间的结束时间点);
|
|||
dateList = TriggerUtils.computeFireTimesBetween(cronTrigger, null, followupPatient.getDischargeTime(), time); |
|||
if (CollectionUtil.isEmpty(dateList)) { |
|||
continue; |
|||
} |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
} |
|||
//3. 判断随访类型
|
|||
if (followupQueueVO.getFollowupType() == 0) { |
|||
//单次
|
|||
FmsFollowupTask fmsFollowupTask = new FmsFollowupTask(); |
|||
BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask); |
|||
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId()); |
|||
fmsFollowupTask.setQueueId(followupQueueVO.getId()); |
|||
fmsFollowupTask.setDelFlag((byte) 0); |
|||
fmsFollowupTask.setCreateTime(new Date()); |
|||
fmsFollowupTask.setStatus((byte) 0); |
|||
fmsFollowupTask.setStartTime(dateList.get(0)); |
|||
fmsFollowupTask.setEndTime(dateList.get(0)); |
|||
fmsFollowupTask.setFollowuper(followupQueueVO.getPersonInCharge()); |
|||
fmsFollowupTask.setFollowupTime(dateList.get(0)); |
|||
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask); |
|||
|
|||
} else { |
|||
//周期
|
|||
//4. 根据频次和总月数添加
|
|||
for (Date date : dateList) { |
|||
//单次
|
|||
FmsFollowupTask fmsFollowupTask = new FmsFollowupTask(); |
|||
BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask); |
|||
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId()); |
|||
fmsFollowupTask.setQueueId(followupQueueVO.getId()); |
|||
fmsFollowupTask.setDelFlag((byte) 0); |
|||
fmsFollowupTask.setCreateTime(new Date()); |
|||
fmsFollowupTask.setStatus((byte) 0); |
|||
fmsFollowupTask.setStartTime(date); |
|||
fmsFollowupTask.setEndTime(date); |
|||
fmsFollowupTask.setFollowuper(followupQueueVO.getPersonInCharge()); |
|||
fmsFollowupTask.setFollowupTime(date); |
|||
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
@ -0,0 +1,28 @@ |
|||
<?xml version="1.0" encoding="UTF-8"?> |
|||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
|||
<mapper namespace="com.acupuncture.system.persist.dao.UmsDataSourceDao"> |
|||
|
|||
<select id="query" resultType="com.acupuncture.system.domain.vo.UmsDataSourceVo$Result"> |
|||
select |
|||
t.id as tenantId, |
|||
t.name, |
|||
s.id, |
|||
s.data_source_key as dataSourceKey, |
|||
s.hospital_id as hospitalId, |
|||
s.url, |
|||
s.username, |
|||
s.password |
|||
from |
|||
dms_tenant t |
|||
left join |
|||
ums_data_source s |
|||
on t.data_source_id = s.id |
|||
where |
|||
t.del_flag = 0 |
|||
and |
|||
s.del_flag = 0 |
|||
group by |
|||
t.id |
|||
</select> |
|||
|
|||
</mapper> |
Loading…
Reference in new issue