Browse Source

修改定时任务

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

385
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.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.acupuncture.common.annotation.Anonymous;
import com.acupuncture.common.core.redis.RedisCache;
import com.acupuncture.framework.datasource.DynamicDataSourceContextHolder;
@ -35,6 +36,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
/**
* @Author zzc
@ -64,6 +66,200 @@ public class TaskController {
@Resource
private FmsFollowupQueueService fmsFollowupQueueService;
/**
* 处理业务
*
* @param tenandId 租户用户
* @param commonQueue 公共队列
*/
public void doTask(Long tenandId, List<FmsFollowupVo.FollowupQueueVO> commonQueue) {
//查询从库队列
//1. 查询队列
List<FmsFollowupVo.FollowupQueueVO> slavelQueueList = fmsFollowupDao.selectQueueList(null, null, null, null);
//合并公共队列和从库的队列
slavelQueueList.addAll(commonQueue);
//查询每个队列的对象
for (FmsFollowupVo.FollowupQueueVO queue : slavelQueueList) {
FmsFollowupDto.FollowupPatientQueryDTO followupPatientQueryDTO = new FmsFollowupDto.FollowupPatientQueryDTO();
followupPatientQueryDTO.setQueueId(queue.getId());
followupPatientQueryDTO.setTenantId(queue.getTenantId());
List<FmsFollowupVo.FollowupPatient> patientList = fmsFollowupService.queryPatient(followupPatientQueryDTO);
log.info("查询每个队列的对象:{}", patientList);
}
//定时任务
// //循环租户
// for (UmsDataSourceVo.Result tenant : tenantList) {
// if ("MASTER".equals(tenant.getDataSourceKey())) {
// continue;
// }
// changeDataSource(tenant);
// {
// //获取随访患者列表,根据患者出院日时间和队列添加工单
// //1. 查询队列
// List<FmsFollowupVo.FollowupQueueVO> queueList = fmsFollowupDao.selectQueueList(null, null, null, null);
//
// if (CollectionUtil.isEmpty(queueList)) {
// queueList = followupQueueVOS;
// } else {
// if (CollectionUtil.isEmpty(followupQueueVOS)) {
// 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);
// }
// }
// }
//
// }
// }
}
@ApiOperation("定时任务添加随访工单")
@PostMapping("/task")
@Scheduled(cron = "0 0 0 * * ?")
@ -72,188 +268,29 @@ public class TaskController {
// TODO 生成工单第一次生成之后2周的,往后只生成之后一周。第一次随访时间: ( 患者的出院时间 - 7 + 轮次时间) 到 (患者的出院时间 + 7 + 轮次时间 )
//查询租户,根据租户循环切换数据源,定时处理所有医院的随访工单
// UmsDataSourceVo.Result result1 = new UmsDataSourceVo.Result();
// result1.setDataSourceKey("MASTER");
List<UmsDataSourceVo.Result> query = umsDataSourceDao.query(null);
if (CollectionUtil.isEmpty(query)) {
List<UmsDataSourceVo.Result> tenantList = umsDataSourceDao.query(null);
if (CollectionUtil.isEmpty(tenantList)) {
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. 查询队列
List<FmsFollowupVo.FollowupQueueVO> queueList = fmsFollowupDao.selectQueueList(null, null, null, null);
//过滤掉没有数据源的租户
tenantList = tenantList.stream().filter(result -> StrUtil.isNotEmpty(result.getDataSourceKey())).collect(Collectors.toList());
if (CollectionUtil.isEmpty(queueList)) {
if (CollectionUtil.isEmpty(queueResults)) {
queueList = followupQueueVOS;
}
} else {
if (CollectionUtil.isEmpty(queueResults)) {
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();
//查询公共队列
List<FmsFollowupVo.FollowupQueueVO> followupQueueVOS = fmsFollowupQueueService.queryCommonQueue(null);
//判断是否已有该次随访
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;
}
//循环租户
for (UmsDataSourceVo.Result tenant : tenantList) {
//切换数据源
DynamicDataSourceContextHolder.setDataSourceType(tenant.getDataSourceKey());
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);
//处理从库业务
doTask(tenant.getTenantId(), followupQueueVOS);
fmsFollowupTask.setStartTime(date);
Calendar instance = Calendar.getInstance();
instance.setTime(date);
instance.add(Calendar.DATE, followWindowAdys / 2);
//销毁数据源
DynamicDataSourceContextHolder.clearDataSourceType();
}
fmsFollowupTask.setEndTime(instance.getTime());
changeDataSource(result);
fmsFollowupTask.setPatientId(followupPatient.getId());
fmsFollowupTaskMapper.insertSelective(fmsFollowupTask);
}
}
}
}
}
}
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)
RequestContextHolder.getRequestAttributes()).getRequest();
//token为空
Long tenantId;
String header = request.getHeader(UserConstants.DEPT);
if(StrUtil.isNotEmpty(header)){
tenantId = Long.parseLong(header);
}else {
// String deptId = request.getHeader(WebConstant.HEADER_KEY_DEPT_ID);
String authHeader = request.getHeader(UserConstants.HEADER_KEY_TOKEN);
if (StrUtil.isEmpty(authHeader)) {
return null;
@ -86,15 +84,6 @@ public class AdminGlobalDataSourceAspect {
return null;
}
}
//设置所属医院和数据源
// SecurityUtils.setCurrentHospitalId(Long.parseLong(deptId));
// // 获取当前的用户
// LoginUser loginUser = SecurityUtils.getLoginUser();
// if(ObjectUtil.isNull(loginUser) || loginUser.getUser().isAdmin()){
// return null;
// }
//根据组织ID查询数据源
UmsDataSource dataSource = dmsLoginService.getDataSourceByTenantId(tenantId);
if (dataSource == null) {

Loading…
Cancel
Save