package com.smartor.service.impl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.outpatientvisit.api.OuthospDataFacade;
import com.ruoyi.outpatientvisit.domain.PatMedOuthosp;
import com.ruoyi.patarchive.mapper.PatArchiveMapper;
import com.smartor.service.IPatMedOuthospService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
import com.ruoyi.common.core.domain.entity.PatArchive;
import com.ruoyi.task.domain.entity.ServiceSubtaskEntity;
import com.ruoyi.task.domain.ServiceSubtask;
import com.ruoyi.task.domain.ServiceTask;
import com.ruoyi.task.domain.ServiceTaskdept;
import com.ruoyi.task.mapper.ServiceSubtaskMapper;
import com.ruoyi.task.mapper.ServiceTaskdeptMapper;
import com.ruoyi.task.mapper.ServiceTaskdiagMapper;
import com.ruoyi.task.mapper.ServiceTaskMapper;
/**
* {@link IPatMedOuthospService} 的委托实现 —— 门急诊随访模块化的过渡件。
*
*
它做两件事
*
* - 已迁的 4 个方法 → 委托模块:
* {@code insert} / {@code update} / {@code selectExistingSerialnums} / {@code dealOutpatientInfo}
* 转发到 {@link OuthospDataFacade}(SQL 唯一收敛在模块里);
* - 未迁的 3 个方法 → 保留原生:{@code dealPatMedOuthosp} /
* {@code followUpByPatMedOuthosp} / {@code followUpAgainByPatMedOuthosp}
* 及它们的私有依赖({@code addDays} / {@code writeInSubTask} / {@code boxedServiceSubtask})。
* 这条是"单患者按需随访"链路(依赖 {@code serviceSubtaskMapper}、
* 景宁站点白名单等),与已迁的"扫全量"是两条不同的入口。
*
*
* ⚠️ 未迁部分逐字来自基线 {@code b25c3b51}
* 由 {@code sql/_probe/gen_outhosp_delegating_impl.py} 从基线自动提取,
* 不是手抄 —— 这类代码手抄出错不会编译报错,只会静默改变业务行为。
* 改动未迁部分时必须同步更新该探针的期望。
*
* 将来动作
* 调用方逐个改为直接依赖模块门面后,本类与 {@link IPatMedOuthospService} 一起删除。
*/
@Slf4j
@Service
public class PatMedOuthospServiceDelegatingImpl implements IPatMedOuthospService {
/** 模块门面:已迁方法的唯一出口 */
@Autowired
private OuthospDataFacade outhospDataFacade;
// ==== 以下依赖仅供【未迁】的单患者按需随访链路使用 ====
@Autowired
private ServiceSubtaskMapper serviceSubtaskMapper;
@Autowired
private ServiceTaskMapper serviceTaskMapper;
@Autowired
private ServiceTaskdeptMapper serviceTaskdeptMapper;
@Autowired
private ServiceTaskdiagMapper serviceTaskdiagMapper;
@Autowired
private PatArchiveMapper patArchiveMapper;
/**
* 列表查询 —— 供【未迁】的单患者按需随访链路内部使用。
*
* 原实现里它是本类的 {@code selectPatMedOuthospList};现已迁入模块的
* {@code OuthospDataService#selectList},故这里只做内部转发。
*
* ⚠️ 注意它与 Controller 用的是同一个方法 —— 后者会做展示富化(仅当 pageNum != null)。
* 本链路不设 pageNum,故不会富化,与原实现一致。
*/
private List selectPatMedOuthospList(PatMedOuthosp query) {
return outhospDataFacade.selectList(query);
}
// ======================================================================
// 已迁:委托模块
// ======================================================================
@Override
public int insertPatMedOuthosp(PatMedOuthosp patMedOuthosp) {
return outhospDataFacade.insert(patMedOuthosp);
}
@Override
public int updatePatMedOuthosp(PatMedOuthosp patMedOuthosp) {
return outhospDataFacade.update(patMedOuthosp);
}
@Override
public Set selectExistingSerialnums(String admitDateStr, Collection serialnums) {
return outhospDataFacade.selectExistingSerialnums(admitDateStr, serialnums);
}
// ======================================================================
// 未迁:单患者按需随访(逐字来自基线 b25c3b51)
// ======================================================================
public Integer dealPatMedOuthosp(PatMedOuthosp patMedOuthosp, Integer visitTimeNum){
Integer num = 0;
//景宁 存在相同诊断的门诊记录则新增再次随访,否则新增随访
if(ObjectUtils.isNotEmpty(patMedOuthosp.getOrgid()) &&
patMedOuthosp.getOrgid().equals("47255004333112711A1001")){
PatMedOuthosp patMedOuthospVo = new PatMedOuthosp();
List patMedOuthosps = new ArrayList<>();
patMedOuthospVo.setOrgid(patMedOuthosp.getOrgid());
patMedOuthospVo.setPatid(patMedOuthosp.getPatid());
patMedOuthospVo.setIcd10code(patMedOuthosp.getIcd10code());
// 同一个患者 + 同一个诊断,历史上已经有门诊随访记录(排除本次就诊,本次在调用前已落库)
patMedOuthosps = selectPatMedOuthospList(patMedOuthospVo);
Long currentId = patMedOuthosp.getId();
List ids = patMedOuthosps.stream().map(PatMedOuthosp::getId).filter(ObjectUtils::isNotEmpty)
.filter(id -> !id.equals(currentId))
.distinct()
.collect(Collectors.toList());
ServiceSubtaskEntity serviceSubtask = new ServiceSubtaskEntity();
//景宁 只要存在相同诊断、患者的门诊记录则当再次随访处理
serviceSubtask.setInhospids(ids);
serviceSubtask.setServiceType("3");
List serviceSubtasks = CollectionUtils.isEmpty(ids)
? new ArrayList<>()
: serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtask);
if(CollectionUtils.isEmpty(serviceSubtasks)){
num = followUpByPatMedOuthosp(patMedOuthosp, visitTimeNum);
}else {
num = followUpAgainByPatMedOuthosp(patMedOuthosp, visitTimeNum, ids);
}
}else {
ServiceSubtaskEntity serviceSubtask = new ServiceSubtaskEntity();
serviceSubtask.setInhospid(patMedOuthosp.getId());
serviceSubtask.setServiceType("3");
serviceSubtask.setVisitCount(1);
List serviceSubtasks = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtask);
if(CollectionUtils.isEmpty(serviceSubtasks)){
num = followUpByPatMedOuthosp(patMedOuthosp, visitTimeNum);
}else {
num = followUpAgainByPatMedOuthosp(patMedOuthosp, visitTimeNum, null);
}
}
return num;
}
public Integer followUpByPatMedOuthosp(PatMedOuthosp patMedOuthosp, Integer visitTimeNum) {
// if(ObjectUtils.isEmpty(patMedOuthosp.getFudate())){
Date adminDate = patMedOuthosp.getAdmitdate();
Date fuDate = addDays(adminDate, visitTimeNum);
patMedOuthosp.setFudate(fuDate);
// }
PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patMedOuthosp.getPatid());
//根据患者科室,获取该疾病的长期任务
ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
serviceTaskdept.setLongtask(1L);
serviceTaskdept.setDeptCode(patMedOuthosp.getDeptcode());
serviceTaskdept.setServiceType("3");
List serviceTaskdeptList = serviceTaskdeptMapper.selectServiceTaskdeptList(serviceTaskdept);
if (CollectionUtils.isEmpty(serviceTaskdeptList) || serviceTaskdeptList.size() == 0) {
patMedOuthosp.setDeptcheckFlag("2");
patMedOuthosp.setRemark("通过部门,没有找到门诊随访任务ID");
updatePatMedOuthosp(patMedOuthosp);
} else {
for (ServiceTaskdept serviceTaskdept1 : serviceTaskdeptList) {
writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedOuthosp, patArchive, 1);
}
}
// 根据患者的疾病,获取该疾病的长期任务
// ServiceTaskdiag serviceTaskdiag = new ServiceTaskdiag();
// serviceTaskdiag.setLongtask(1L);
// serviceTaskdiag.setIcd10code(patMedOuthosp.getIcd10code());
// List serviceTaskdiags = serviceTaskdiagMapper.selectServiceTaskdiagList(serviceTaskdiag);
// //如果部门模板为空(将deptIsNull设置为true)
// if (org.apache.commons.collections4.CollectionUtils.isEmpty(serviceTaskdiags) || serviceTaskdiags.size() == 0) {
// patMedOuthosp.setDiagcheckFlag("2");
// patMedOuthosp.setRemark("通过icd10,没有找到门诊随访任务ID");
// updatePatMedOuthosp(patMedOuthosp);
// } else {
// for (ServiceTaskdiag serviceTaskdept1 : serviceTaskdiags) {
// writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedOuthosp, patArchive, 2);
// }
// }
return 1;
}
public Integer followUpAgainByPatMedOuthosp(PatMedOuthosp patMedOuthosp, Integer visitTimeNum, List inhospids) {
Date adminDate = patMedOuthosp.getAdmitdate();
Date newDate = addDays(adminDate, visitTimeNum);
Integer i = 0;
String deptcode = patMedOuthosp.getDeptcode();
Long patid = patMedOuthosp.getPatid();
//查看有无进行中的任务
ServiceSubtaskEntity serviceSubtaskEntity0 = new ServiceSubtaskEntity();
serviceSubtaskEntity0.setOrgid(patMedOuthosp.getOrgid());
serviceSubtaskEntity0.setSendstates(new ArrayList<>(Arrays.asList(1L, 2L, 3L, 7L)));
serviceSubtaskEntity0.setPatid(patid);
//景宁根据诊断再次随访
if(ObjectUtils.isNotEmpty(patMedOuthosp.getOrgid()) &&
patMedOuthosp.getOrgid().equals("47255004333112711A1001") && !CollectionUtils.isEmpty(inhospids)){
serviceSubtaskEntity0.setInhospids(inhospids);
}else {
serviceSubtaskEntity0.setLeaveldeptcode(deptcode);
}
serviceSubtaskEntity0.setSort(0);
List serviceSubtaskList0 = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskEntity0);
if (!CollectionUtils.isEmpty(serviceSubtaskList0)) {
for (ServiceSubtask serviceSubtask0 : serviceSubtaskList0) {
serviceSubtask0.setSendstate(6L);
serviceSubtask0.setUpdateTime(new Date());
serviceSubtask0.setFinishtime(adminDate);
serviceSubtaskMapper.updateServiceSubtask(serviceSubtask0);
ServiceSubtask serviceSubtask1 = DtoConversionUtils.sourceToTarget(serviceSubtask0, ServiceSubtask.class);
serviceSubtask1.setId(null);
serviceSubtask1.setSendstate(2L);
serviceSubtask1.setUpid(serviceSubtask0.getId().intValue());
//老数据的visit_count可能为空,默认按第1次算
Integer visitCount = ObjectUtils.isEmpty(serviceSubtask0.getVisitCount()) ? 1 : serviceSubtask0.getVisitCount();
serviceSubtask1.setVisitCount(visitCount + 1);
serviceSubtask1.setVisitType(serviceSubtask0.getVisitType());
serviceSubtask1.setVisitDeptCode(serviceSubtask0.getVisitDeptCode());
serviceSubtask1.setVisitDeptName(serviceSubtask0.getVisitDeptName());
//新任务科室是当前出院科室
serviceSubtask1.setLeaveldeptcode(patMedOuthosp.getDeptcode());
serviceSubtask1.setCreateTime(new Date());
serviceSubtask1.setUpdateTime(new Date());
serviceSubtask1.setFinishtime(null);
serviceSubtask1.setAdmindate(adminDate);
serviceSubtask1.setVisitTime(newDate);
serviceSubtask1.setLongSendTime(newDate);
i = i + serviceSubtaskMapper.insertServiceSubtask(serviceSubtask1);
}
} else {
//查看已完成的任务
// ServiceSubtaskEntity serviceSubtaskEntity = new ServiceSubtaskEntity();
// serviceSubtaskEntity0.setOrgid(patMedOuthosp.getOrgid());
// serviceSubtaskEntity.setSendstates(new ArrayList<>(Arrays.asList(6L)));
// serviceSubtaskEntity.setPatid(patid);
// serviceSubtaskEntity.setDeptcode(deptcode);
// serviceSubtaskEntity.setSort(0);
// List serviceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(serviceSubtaskEntity);
// if(!CollectionUtils.isEmpty(serviceSubtaskList)){
// ServiceSubtask serviceSubtask = serviceSubtaskList.get(0);
// serviceSubtask.setSendstate(2L);
// serviceSubtask.setCreateTime(new Date());
// serviceSubtask.setUpdateTime(new Date());
//
// serviceSubtask.setVisitTime(newDate);
// serviceSubtask.setLongSendTime(newDate);
// i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
// }
}
return i;
}
private void writeInSubTask(Long taskid, Boolean check, PatMedOuthosp patMedOuthosp, PatArchive patArchive, Integer type) {
ServiceTask st = new ServiceTask();
st.setTaskid(taskid);
st.setSendState(2L);
List serviceTasks = serviceTaskMapper.selectServiceTaskList(st);
if (org.apache.commons.collections4.CollectionUtils.isEmpty(serviceTasks)) {
log.info("该患者疾病随访长期任务不存在,任务ID为:{}", taskid);
if (type == 1) patMedOuthosp.setDiagcheckFlag("2");
if (type == 2) patMedOuthosp.setDeptcheckFlag("2");
patMedOuthosp.setRemark("该患者门诊随访长期任务不存在,任务ID为:" + taskid);
updatePatMedOuthosp(patMedOuthosp);
return;
}
ServiceTask serviceTask = serviceTasks.get(0);
//封装serviceSubtask
ServiceSubtask serviceSubtask = boxedServiceSubtask(serviceTask, patMedOuthosp, patArchive);
Integer i = 0;
//先判断一下,是否需要校验
if (check) {
//在新增之前,先通过患者ID,sendstate=2查询一下,在所有长期任务中,是不是还有该患者待执行的任务,有的话,比较之前的endtime是否小于当前的endtaime,如果之前的小于现在的,则直接将之前的停掉(原因再入院)
ServiceSubtaskEntity subtask = new ServiceSubtaskEntity();
subtask.setPatid(patArchive.getId());
subtask.setSendstate(2L);
subtask.setTaskid(taskid);
List selectServiceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(subtask);
log.info("该患者待执行的任务:{}", CollectionUtils.isEmpty(selectServiceSubtaskList) ? null : selectServiceSubtaskList.size());
if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(selectServiceSubtaskList) && selectServiceSubtaskList.size() > 0) {
for (ServiceSubtask serviceSubtask1 : selectServiceSubtaskList) {
if (Objects.isNull(serviceSubtask1.getLongSendTime())) {
//不是长期任务,不处理
continue;
}
//将之前的停掉
serviceSubtask1.setSendstate(6L);
serviceSubtask1.setRemark("患者已经回院复诊");
serviceSubtask1.setResult("success");
serviceSubtask1.setFinishtime(new Date());
serviceSubtask1.setUpdateBy(serviceTask.getUpdateBy());
serviceSubtaskMapper.updateServiceSubtask(serviceSubtask1);
//重新新增子任务
i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
}
} else {
if (StringUtils.isEmpty(serviceSubtask.getPhone())) {
serviceSubtask.setRemark("手机号为空");
serviceSubtask.setSendstate(4L);
serviceSubtask.setResult("error");
// serviceSubtask.setFinishtime(new Date());
}
serviceSubtask.setCreateBy(serviceTask.getCreateBy());
serviceSubtask.setCreateTime(new Date());
i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
}
} else {
if (StringUtils.isEmpty(serviceSubtask.getPhone())) {
serviceSubtask.setRemark("手机号为空");
serviceSubtask.setSendstate(4L);
serviceSubtask.setResult("error");
// serviceSubtask.setFinishtime(new Date());
}
serviceSubtask.setCreateBy(serviceTask.getCreateBy());
serviceSubtask.setCreateTime(new Date());
i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
}
if (i == 1) {
//将check_flag改成1(已处理)
PatMedOuthosp patMedOuthosp1 = new PatMedOuthosp();
patMedOuthosp1.setId(patMedOuthosp.getId());
if (type == 1) patMedOuthosp1.setDiagcheckFlag("2");
if (type == 2) patMedOuthosp1.setDeptcheckFlag("2");
updatePatMedOuthosp(patMedOuthosp1);
} else {
//生成子任务失败,
log.info("生成子任务失败serviceSubtask的taskid为:{},patid为:{}", serviceSubtask.getTaskid(), serviceSubtask.getPatid());
PatMedOuthosp pmo = new PatMedOuthosp();
pmo.setGuid(patMedOuthosp.getGuid());
pmo.setSerialnum(patMedOuthosp.getSerialnum());
if (type == 1) pmo.setDiagcheckFlag("2");
if (type == 2) pmo.setDeptcheckFlag("2");
pmo.setRemark("生成子任务失败");
updatePatMedOuthosp(pmo);
}
}
public ServiceSubtask boxedServiceSubtask(ServiceTask serviceTask, PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
ServiceSubtask serviceSubtask = DtoConversionUtils.sourceToTarget(serviceTask, ServiceSubtask.class);
serviceSubtask.setTaskid(serviceTask.getTaskid());
serviceSubtask.setOrgid(serviceTask.getOrgid());
serviceSubtask.setCampusid(serviceTask.getCampusid());
if (StringUtils.isNotEmpty(serviceTask.getLibtemplateid()))
serviceSubtask.setLibtemplateid(Long.valueOf(serviceTask.getLibtemplateid()));
serviceSubtask.setDrcode(patMedOuthosp.getDrcode());
serviceSubtask.setDrname(patMedOuthosp.getDrname());
serviceSubtask.setLeaveldeptcode(patMedOuthosp.getDeptcode());
serviceSubtask.setInhospid(patMedOuthosp.getId());
serviceSubtask.setHospno(patMedOuthosp.getOuthospno());
serviceSubtask.setLeaveldeptname(patMedOuthosp.getDeptname());
serviceSubtask.setTemplateid(serviceTask.getTemplateid());
serviceSubtask.setTemplatename(serviceTask.getTemplatename());
serviceSubtask.setPatid(patArchive.getId());
serviceSubtask.setSendname(patArchive.getName());
serviceSubtask.setSfzh(patArchive.getIdcardno());
serviceSubtask.setLeavediagname(patMedOuthosp.getDiagname());
serviceSubtask.setLeaveicd10code(patMedOuthosp.getIcd10code());
serviceSubtask.setSfzh(patArchive.getIdcardno());
serviceSubtask.setPhone(patArchive.getTelcode());
if (StringUtils.isBlank(patArchive.getTelcode())) serviceSubtask.setPhone(patArchive.getRelativetelcode());
serviceSubtask.setSex(patArchive.getSex());
serviceSubtask.setAge(patArchive.getAge());
serviceSubtask.setSendstate(2L);
serviceSubtask.setServiceType(serviceTask.getServiceType());
serviceSubtask.setPreachform(serviceTask.getPreachform());
serviceSubtask.setHospType("1");
serviceSubtask.setCreateTime(new Date());
serviceSubtask.setUpdateTime(new Date());
serviceSubtask.setUpdateBy(serviceTask.getUpdateBy());
serviceSubtask.setUpdateTime(new Date());
serviceSubtask.setVisitDeptCode(patMedOuthosp.getDeptcode());
serviceSubtask.setVisitDeptName(patMedOuthosp.getDeptname());
serviceSubtask.setUpdateTime(new Date());
serviceSubtask.setAdmindate(patMedOuthosp.getAdmitdate());
//设置发送时间
if (serviceTask.getSendDay() == null) serviceTask.setSendDay(1L);
Date newDate = addDays(patMedOuthosp.getAdmitdate(), serviceTask.getSendDay().intValue());
if (patMedOuthosp.getFudate() != null) {
//如果门诊表有指定随访时间,那就用指定的
newDate = patMedOuthosp.getFudate();
}
serviceSubtask.setLongSendTime(newDate);
serviceSubtask.setVisitTime(newDate);
//患者发送时间
if (StringUtils.isNotEmpty(patArchive.getNotrequiredFlag()) && patArchive.getNotrequiredFlag().equals("1")) {
String remark = patArchive.getNotrequiredreason();
serviceSubtask.setRemark(remark);
serviceSubtask.setResult("error");
// serviceSubtask.setFinishtime(new Date());
//不执行
serviceSubtask.setSendstate(4L);
}
return serviceSubtask;
}
private Date addDays(Date date, Integer days) {
if (days == null) {
days = 1;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_MONTH, days);
return calendar.getTime();
}
}