Browse Source

修改定时任务

master
zzc 3 months ago
parent
commit
45946ecf85
  1. 381
      acupuncture-admin/src/main/java/com/acupuncture/web/task/TaskController.java
  2. 11
      acupuncture-framework/src/main/java/com/acupuncture/framework/aspectj/AdminGlobalDataSourceAspect.java

381
acupuncture-admin/src/main/java/com/acupuncture/web/task/TaskController.java

@ -4,6 +4,7 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil; import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.acupuncture.common.annotation.Anonymous; import com.acupuncture.common.annotation.Anonymous;
import com.acupuncture.common.core.redis.RedisCache; import com.acupuncture.common.core.redis.RedisCache;
import com.acupuncture.framework.datasource.DynamicDataSourceContextHolder; import com.acupuncture.framework.datasource.DynamicDataSourceContextHolder;
@ -35,6 +36,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* @Author zzc * @Author zzc
@ -64,196 +66,231 @@ public class TaskController {
@Resource @Resource
private FmsFollowupQueueService fmsFollowupQueueService; private FmsFollowupQueueService fmsFollowupQueueService;
@ApiOperation("定时任务添加随访工单") /**
@PostMapping("/task") * 处理业务
@Scheduled(cron = "0 0 0 * * ?") *
@Anonymous * @param tenandId 租户用户
public void task() { * @param commonQueue 公共队列
// TODO 生成工单第一次生成之后2周的,往后只生成之后一周。第一次随访时间: ( 患者的出院时间 - 7 + 轮次时间) 到 (患者的出院时间 + 7 + 轮次时间 ) */
public void doTask(Long tenandId, List<FmsFollowupVo.FollowupQueueVO> commonQueue) {
//查询租户,根据租户循环切换数据源,定时处理所有医院的随访工单 //查询从库队列
// UmsDataSourceVo.Result result1 = new UmsDataSourceVo.Result();
// result1.setDataSourceKey("MASTER");
List<UmsDataSourceVo.Result> query = umsDataSourceDao.query(null);
if (CollectionUtil.isEmpty(query)) {
return;
}
//查询公共队列
List<Object> queueResults = redisCache.getCacheList("common_followup_queue");
List<FmsFollowupVo.FollowupQueueVO> followupQueueVOS = new ArrayList<>();
if (CollectionUtil.isEmpty(queueResults)) {
followupQueueVOS = fmsFollowupQueueService.queryCommonQueue(null);
}
// List<FmsFollowupVo.FollowupQueueVO> queueResults = fmsFollowupDao.queryCommonQueue(null);
//切换数据源
for (UmsDataSourceVo.Result result : query) {
if ("MASTER".equals(result.getDataSourceKey()) || "SXYFYY".equals(result.getDataSourceKey())) {
continue;
}
changeDataSource(result);
{
//获取随访患者列表,根据患者出院日时间和队列添加工单
//1. 查询队列 //1. 查询队列
List<FmsFollowupVo.FollowupQueueVO> queueList = fmsFollowupDao.selectQueueList(null, null, null, null); List<FmsFollowupVo.FollowupQueueVO> slavelQueueList = fmsFollowupDao.selectQueueList(null, null, null, null);
if (CollectionUtil.isEmpty(queueList)) { //合并公共队列和从库的队列
if (CollectionUtil.isEmpty(queueResults)) { slavelQueueList.addAll(commonQueue);
queueList = followupQueueVOS;
}
} else {
if (CollectionUtil.isEmpty(queueResults)) {
queueList.addAll(followupQueueVOS);
}
}
for (FmsFollowupVo.FollowupQueueVO followupQueueVO : queueList) {
Integer followWindowAdys = followupQueueVO.getFollowWindowAdys();
//2. 查询队列随访患者列表 //查询每个队列的对象
// changeDataSource(result); for (FmsFollowupVo.FollowupQueueVO queue : slavelQueueList) {
FmsFollowupDto.FollowupPatientQueryDTO followupPatientQueryDTO = new FmsFollowupDto.FollowupPatientQueryDTO(); FmsFollowupDto.FollowupPatientQueryDTO followupPatientQueryDTO = new FmsFollowupDto.FollowupPatientQueryDTO();
followupPatientQueryDTO.setQueueId(followupQueueVO.getId()); followupPatientQueryDTO.setQueueId(queue.getId());
followupPatientQueryDTO.setTenantId(followupQueueVO.getTenantId()); followupPatientQueryDTO.setTenantId(queue.getTenantId());
changeDataSource(result);
List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupService.queryPatient(followupPatientQueryDTO); List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupService.queryPatient(followupPatientQueryDTO);
// List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupDao.queryPatient(followupQueueVO.getId(), null, null); log.info("查询每个队列的对象:{}", patientList);
if (CollectionUtil.isEmpty(patientList)) {
continue;
} }
//随访总月数
Integer followupMonth = followupQueueVO.getFollowupMonth();
for (FmsFollowupVo.FollowupPatient followupPatient : patientList) {
//获取随访到期时间 出院时间+随访总月数 = 到期时间
Calendar calendar = Calendar.getInstance();
calendar.setTime(followupPatient.getDischargeTime());
calendar.set(Calendar.MONTH, followupMonth);
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) {
//单次
//判断是否已有该次随访 // //循环租户
FmsFollowupTaskExample fmsFollowupTaskExample = new FmsFollowupTaskExample(); // for (UmsDataSourceVo.Result tenant : tenantList) {
fmsFollowupTaskExample.createCriteria().andTimesEqualTo(1).andPatientIdEqualTo(followupPatient.getPatientId()).andQueueIdEqualTo(followupQueueVO.getId()); // if ("MASTER".equals(tenant.getDataSourceKey())) {
List<FmsFollowupTask> fmsFollowupTasks = fmsFollowupTaskMapper.selectByExample(fmsFollowupTaskExample); // continue;
if (CollectionUtil.isNotEmpty(fmsFollowupTasks)) { // }
continue; // changeDataSource(tenant);
} // {
FmsFollowupTask fmsFollowupTask = new FmsFollowupTask(); // //获取随访患者列表,根据患者出院日时间和队列添加工单
BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask); // //1. 查询队列
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId()); // List<FmsFollowupVo.FollowupQueueVO> queueList = fmsFollowupDao.selectQueueList(null, null, null, null);
fmsFollowupTask.setName(followupPatient.getName()); //
fmsFollowupTask.setPinyinFull(followupPatient.getPinyinFull()); // if (CollectionUtil.isEmpty(queueList)) {
fmsFollowupTask.setPinyinSimple(followupPatient.getPinyinSimple()); // queueList = followupQueueVOS;
fmsFollowupTask.setGender(followupPatient.getGender()); // } else {
if (followupPatient.getBirthDate() != null) { // if (CollectionUtil.isEmpty(followupQueueVOS)) {
fmsFollowupTask.setAge(DateUtil.age(followupPatient.getBirthDate(), new Date())); // queueList.addAll(followupQueueVOS);
// }
// }
// for (FmsFollowupVo.FollowupQueueVO followupQueueVO : queueList) {
// Integer followWindowAdys = followupQueueVO.getFollowWindowAdys();
//
// //2. 查询队列随访患者列表
//// changeDataSource(result);
// FmsFollowupDto.FollowupPatientQueryDTO followupPatientQueryDTO = new FmsFollowupDto.FollowupPatientQueryDTO();
// followupPatientQueryDTO.setQueueId(followupQueueVO.getId());
// followupPatientQueryDTO.setTenantId(followupQueueVO.getTenantId());
// changeDataSource(result);
//
// List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupService.queryPatient(followupPatientQueryDTO);
//// List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupDao.queryPatient(followupQueueVO.getId(), null, null);
// if (CollectionUtil.isEmpty(patientList)) {
// continue;
// }
// //随访总月数
// Integer followupMonth = followupQueueVO.getFollowupMonth();
// for (FmsFollowupVo.FollowupPatient followupPatient : patientList) {
// //获取随访到期时间 出院时间+随访总月数 = 到期时间
// Calendar calendar = Calendar.getInstance();
// calendar.setTime(followupPatient.getDischargeTime());
// calendar.set(Calendar.MONTH, followupMonth);
// 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) {
// //单次
//
// //判断是否已有该次随访
// FmsFollowupTaskExample fmsFollowupTaskExample = new FmsFollowupTaskExample();
// fmsFollowupTaskExample.createCriteria().andTimesEqualTo(1).andPatientIdEqualTo(followupPatient.getPatientId()).andQueueIdEqualTo(followupQueueVO.getId());
// List<FmsFollowupTask> fmsFollowupTasks = fmsFollowupTaskMapper.selectByExample(fmsFollowupTaskExample);
// if (CollectionUtil.isNotEmpty(fmsFollowupTasks)) {
// continue;
// }
// FmsFollowupTask fmsFollowupTask = new FmsFollowupTask();
// BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask);
// fmsFollowupTask.setId(IdUtil.getSnowflakeNextId());
// fmsFollowupTask.setName(followupPatient.getName());
// fmsFollowupTask.setPinyinFull(followupPatient.getPinyinFull());
// fmsFollowupTask.setPinyinSimple(followupPatient.getPinyinSimple());
// fmsFollowupTask.setGender(followupPatient.getGender());
// if (followupPatient.getBirthDate() != null) {
// fmsFollowupTask.setAge(DateUtil.age(followupPatient.getBirthDate(), new Date()));
// }
// fmsFollowupTask.setEthnicity(followupPatient.getEthnicity());
// fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
// fmsFollowupTask.setPhone(followupPatient.getPhone());
// fmsFollowupTask.setTenantId(followupPatient.getTenantId());
// fmsFollowupTask.setIdCardType(followupPatient.getIdCardType());
// fmsFollowupTask.setIdCard(followupPatient.getIdCard());
// fmsFollowupTask.setTimes(1);
// fmsFollowupTask.setQueueId(followupQueueVO.getId());
// fmsFollowupTask.setPatientId(followupPatient.getId());
// fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
// fmsFollowupTask.setDelFlag((byte) 0);
// fmsFollowupTask.setCreateTime(new Date());
// fmsFollowupTask.setStatus((byte) 0);
//
// //计算第一次随访的时间
// DateComparator dateComparator = getDate(dateList);
// if (dateComparator.getDate() != null) {
// fmsFollowupTask.setStartTime(dateComparator.getDate());
// } else {
// Calendar instance = Calendar.getInstance();
// instance.setTime(dateList.get(0));
// instance.add(Calendar.DATE, -followWindowAdys / 2);
// fmsFollowupTask.setStartTime(instance.getTime());
// }
// Calendar instance1 = Calendar.getInstance();
// instance1.setTime(dateList.get(0));
// instance1.add(Calendar.DATE, followWindowAdys / 2);
// fmsFollowupTask.setEndTime(instance1.getTime());
// fmsFollowupTask.setFollowuper(followupQueueVO.getPersonInCharge());
// fmsFollowupTask.setFollowupTime(dateList.get(0));
// changeDataSource(result);
// fmsFollowupTaskMapper.insertSelective(fmsFollowupTask);
//
// } else {
// //周期
// //4. 根据频次和总月数添加
// DateComparator dateComparator = getDate(dateList);
// if (dateComparator.getDate() == null || dateComparator.getIndex() == null) {
// continue;
// }
// Date date = dateComparator.getDate();
// Integer index = dateComparator.getIndex();
//
// //判断是否已有该次随访
// FmsFollowupTaskExample fmsFollowupTaskExample = new FmsFollowupTaskExample();
// fmsFollowupTaskExample.createCriteria().andTimesEqualTo(index).andPatientIdEqualTo(followupPatient.getPatientId()).andQueueIdEqualTo(followupQueueVO.getId());
// List<FmsFollowupTask> fmsFollowupTasks = fmsFollowupTaskMapper.selectByExample(fmsFollowupTaskExample);
// if (CollectionUtil.isNotEmpty(fmsFollowupTasks)) {
// continue;
// }
//
// FmsFollowupTask fmsFollowupTask = new FmsFollowupTask();
// BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask);
// fmsFollowupTask.setId(IdUtil.getSnowflakeNextId());
// fmsFollowupTask.setName(followupPatient.getName());
// fmsFollowupTask.setPinyinFull(followupPatient.getPinyinFull());
// fmsFollowupTask.setPinyinSimple(followupPatient.getPinyinSimple());
// fmsFollowupTask.setGender(followupPatient.getGender());
// if (followupPatient.getBirthDate() != null) {
// fmsFollowupTask.setAge(DateUtil.age(followupPatient.getBirthDate(), new Date()));
// }
// fmsFollowupTask.setEthnicity(followupPatient.getEthnicity());
// fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
// fmsFollowupTask.setTimes(index);
// fmsFollowupTask.setPhone(followupPatient.getPhone());
// fmsFollowupTask.setTenantId(followupPatient.getTenantId());
// fmsFollowupTask.setIdCardType(followupPatient.getIdCardType());
// fmsFollowupTask.setIdCard(followupPatient.getIdCard());
// fmsFollowupTask.setId(IdUtil.getSnowflakeNextId());
// fmsFollowupTask.setQueueId(followupQueueVO.getId());
// fmsFollowupTask.setDelFlag((byte) 0);
// fmsFollowupTask.setCreateTime(new Date());
// fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
// fmsFollowupTask.setStatus((byte) 0);
//
// fmsFollowupTask.setStartTime(date);
// Calendar instance = Calendar.getInstance();
// instance.setTime(date);
// instance.add(Calendar.DATE, followWindowAdys / 2);
//
// fmsFollowupTask.setEndTime(instance.getTime());
// changeDataSource(result);
// fmsFollowupTask.setPatientId(followupPatient.getId());
// fmsFollowupTaskMapper.insertSelective(fmsFollowupTask);
// }
// }
// }
//
// }
// }
} }
fmsFollowupTask.setEthnicity(followupPatient.getEthnicity());
fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
fmsFollowupTask.setPhone(followupPatient.getPhone());
fmsFollowupTask.setTenantId(followupPatient.getTenantId());
fmsFollowupTask.setIdCardType(followupPatient.getIdCardType());
fmsFollowupTask.setIdCard(followupPatient.getIdCard());
fmsFollowupTask.setTimes(1);
fmsFollowupTask.setQueueId(followupQueueVO.getId());
fmsFollowupTask.setPatientId(followupPatient.getId());
fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
fmsFollowupTask.setDelFlag((byte) 0);
fmsFollowupTask.setCreateTime(new Date());
fmsFollowupTask.setStatus((byte) 0);
//计算第一次随访的时间 @ApiOperation("定时任务添加随访工单")
DateComparator dateComparator = getDate(dateList); @PostMapping("/task")
if (dateComparator.getDate() != null) { @Scheduled(cron = "0 0 0 * * ?")
fmsFollowupTask.setStartTime(dateComparator.getDate()); @Anonymous
}else { public void task() {
Calendar instance = Calendar.getInstance(); // TODO 生成工单第一次生成之后2周的,往后只生成之后一周。第一次随访时间: ( 患者的出院时间 - 7 + 轮次时间) 到 (患者的出院时间 + 7 + 轮次时间 )
instance.setTime(dateList.get(0));
instance.add(Calendar.DATE, -followWindowAdys / 2);
fmsFollowupTask.setStartTime(instance.getTime());
}
Calendar instance1 = Calendar.getInstance();
instance1.setTime(dateList.get(0));
instance1.add(Calendar.DATE, followWindowAdys / 2);
fmsFollowupTask.setEndTime(instance1.getTime());
fmsFollowupTask.setFollowuper(followupQueueVO.getPersonInCharge());
fmsFollowupTask.setFollowupTime(dateList.get(0));
changeDataSource(result);
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask);
} else { //查询租户,根据租户循环切换数据源,定时处理所有医院的随访工单
//周期 List<UmsDataSourceVo.Result> tenantList = umsDataSourceDao.query(null);
//4. 根据频次和总月数添加 if (CollectionUtil.isEmpty(tenantList)) {
DateComparator dateComparator = getDate(dateList); return;
if (dateComparator.getDate() == null || dateComparator.getIndex() == null) {
continue;
} }
Date date = dateComparator.getDate(); //过滤掉没有数据源的租户
Integer index = dateComparator.getIndex(); tenantList = tenantList.stream().filter(result -> StrUtil.isNotEmpty(result.getDataSourceKey())).collect(Collectors.toList());
//判断是否已有该次随访 //查询公共队列
FmsFollowupTaskExample fmsFollowupTaskExample = new FmsFollowupTaskExample(); List<FmsFollowupVo.FollowupQueueVO> followupQueueVOS = fmsFollowupQueueService.queryCommonQueue(null);
fmsFollowupTaskExample.createCriteria().andTimesEqualTo(index).andPatientIdEqualTo(followupPatient.getPatientId()).andQueueIdEqualTo(followupQueueVO.getId());
List<FmsFollowupTask> fmsFollowupTasks = fmsFollowupTaskMapper.selectByExample(fmsFollowupTaskExample);
if (CollectionUtil.isNotEmpty(fmsFollowupTasks)) {
continue;
}
FmsFollowupTask fmsFollowupTask = new FmsFollowupTask(); //循环租户
BeanUtil.copyProperties(followupQueueVO, fmsFollowupTask); for (UmsDataSourceVo.Result tenant : tenantList) {
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId()); //切换数据源
fmsFollowupTask.setName(followupPatient.getName()); DynamicDataSourceContextHolder.setDataSourceType(tenant.getDataSourceKey());
fmsFollowupTask.setPinyinFull(followupPatient.getPinyinFull());
fmsFollowupTask.setPinyinSimple(followupPatient.getPinyinSimple());
fmsFollowupTask.setGender(followupPatient.getGender());
if (followupPatient.getBirthDate() != null) {
fmsFollowupTask.setAge(DateUtil.age(followupPatient.getBirthDate(), new Date()));
}
fmsFollowupTask.setEthnicity(followupPatient.getEthnicity());
fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
fmsFollowupTask.setTimes(index);
fmsFollowupTask.setPhone(followupPatient.getPhone());
fmsFollowupTask.setTenantId(followupPatient.getTenantId());
fmsFollowupTask.setIdCardType(followupPatient.getIdCardType());
fmsFollowupTask.setIdCard(followupPatient.getIdCard());
fmsFollowupTask.setId(IdUtil.getSnowflakeNextId());
fmsFollowupTask.setQueueId(followupQueueVO.getId());
fmsFollowupTask.setDelFlag((byte) 0);
fmsFollowupTask.setCreateTime(new Date());
fmsFollowupTask.setEducationYears(followupPatient.getEducationYears());
fmsFollowupTask.setStatus((byte) 0);
fmsFollowupTask.setStartTime(date); //处理从库业务
Calendar instance = Calendar.getInstance(); doTask(tenant.getTenantId(), followupQueueVOS);
instance.setTime(date);
instance.add(Calendar.DATE, followWindowAdys / 2);
fmsFollowupTask.setEndTime(instance.getTime()); //销毁数据源
changeDataSource(result); DynamicDataSourceContextHolder.clearDataSourceType();
fmsFollowupTask.setPatientId(followupPatient.getId());
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask);
}
}
} }
}
}
} }
private static void changeDataSource(UmsDataSourceVo.Result result) { private static void changeDataSource(UmsDataSourceVo.Result result) {

11
acupuncture-framework/src/main/java/com/acupuncture/framework/aspectj/AdminGlobalDataSourceAspect.java

@ -70,13 +70,11 @@ public class AdminGlobalDataSourceAspect {
// 获取请求携带的令牌 // 获取请求携带的令牌
HttpServletRequest request = ((ServletRequestAttributes) HttpServletRequest request = ((ServletRequestAttributes)
RequestContextHolder.getRequestAttributes()).getRequest(); RequestContextHolder.getRequestAttributes()).getRequest();
//token为空
Long tenantId; Long tenantId;
String header = request.getHeader(UserConstants.DEPT); String header = request.getHeader(UserConstants.DEPT);
if(StrUtil.isNotEmpty(header)){ if(StrUtil.isNotEmpty(header)){
tenantId = Long.parseLong(header); tenantId = Long.parseLong(header);
}else { }else {
// String deptId = request.getHeader(WebConstant.HEADER_KEY_DEPT_ID);
String authHeader = request.getHeader(UserConstants.HEADER_KEY_TOKEN); String authHeader = request.getHeader(UserConstants.HEADER_KEY_TOKEN);
if (StrUtil.isEmpty(authHeader)) { if (StrUtil.isEmpty(authHeader)) {
return null; return null;
@ -86,15 +84,6 @@ public class AdminGlobalDataSourceAspect {
return null; return null;
} }
} }
//设置所属医院和数据源
// SecurityUtils.setCurrentHospitalId(Long.parseLong(deptId));
// // 获取当前的用户
// LoginUser loginUser = SecurityUtils.getLoginUser();
// if(ObjectUtil.isNull(loginUser) || loginUser.getUser().isAdmin()){
// return null;
// }
//根据组织ID查询数据源 //根据组织ID查询数据源
UmsDataSource dataSource = dmsLoginService.getDataSourceByTenantId(tenantId); UmsDataSource dataSource = dmsLoginService.getDataSourceByTenantId(tenantId);
if (dataSource == null) { if (dataSource == null) {

Loading…
Cancel
Save