liusheng
昨天 459aa78c84cf552ebea6ef056d978c2531d71ac8
smartor/src/main/java/com/smartor/service/impl/PatMedInhospServiceImpl.java
@@ -9,6 +9,7 @@
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.domain.*;
import com.smartor.domain.entity.ServiceSubtaskEntity;
import com.smartor.mapper.*;
import com.smartor.service.IPatMedInhospService;
import lombok.extern.slf4j.Slf4j;
@@ -389,6 +390,149 @@
        return 1;
    }
    /**
     * 多维度随访
     * @param config
     * @return
     */
    public int dealOutHospInfoByDimension(String config) {
        // 一次性查出所有需要处理的出院患者(三个维度任意一个未完成)
        List<PatMedInhosp> patList = patMedInhospMapper.selectNeedProcessList();
        log.info("【统一扫描】待处理患者数:{}", patList.size());
        for (PatMedInhosp patMedInhosp1 : patList) {
            PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patMedInhosp1.getPatid());
            if (Objects.isNull(patArchive)) {
                // 三个维度都标记为失败
                markAllFlagsAsError(patMedInhosp1, "患者基本信息为空");
                continue;
            }
            // ── 维度1:科室 ──
            if ("0".equals(patMedInhosp1.getDeptcheckFlag())) {
                processTasksByDimension(patMedInhosp1, patArchive, config, 1);
            }
            // ── 维度2:病区 ──(新华医院跳过)
            if (!active.equals("xh") && "0".equals(patMedInhosp1.getWardcheckFlag())) {
                processTasksByDimension(patMedInhosp1, patArchive, config, 2);
            }
            // ── 维度3:疾病 ──(新华医院跳过)
            if (!active.equals("xh") && "0".equals(patMedInhosp1.getDiagcheckFlag())) {
                processTasksByDimension(patMedInhosp1, patArchive, config, 3);
            }
        }
        return 1;
    }
    /**
     * 按维度找到所有匹配长期任务,批量生成 subtask
     * type: 1=科室, 2=病区, 3=疾病
     */
    private void processTasksByDimension(PatMedInhosp patMedInhosp1,
            PatArchive patArchive,
            String config, int type) {
        List<Long> matchedTaskIds = findMatchingTaskIds(patMedInhosp1, type);
        if (CollectionUtils.isEmpty(matchedTaskIds)) {
                String reason = getDimensionReason(type);
                markFlagAsNoConfig(patMedInhosp1, type, reason);
            return;
        }
        // 一条 inhosp 记录 → 多个匹配任务 → 多条 subtask
        for (Long taskId : matchedTaskIds) {
            writeInSubTask(taskId, true, patMedInhosp1, patArchive, type, config);
        }
    }
    private String getDimensionReason(int type){
        switch (type) {
            case 1: { // 科室
                return "未找到科室匹配的长期任务";
                 }
            case 2: { // 病区
                return "未找到病区匹配的长期任务";
            }
            case 3: { // 疾病
                return "未找到疾病匹配的长期任务";
            }
        }
        return "多维长期任务-匹配类型为空";
    }
    private void markFlagAsNoConfig(PatMedInhosp patMedInhosp,
                                    int type,
                                    String reason){
        switch (type) {
            case 1: { // 科室
                patMedInhosp.setDeptcheckFlag("2");
                patMedInhosp.setRemark(reason);
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
                break;
            }
            case 2: { // 病区
                patMedInhosp.setWardcheckFlag("2");
                patMedInhosp.setRemark(reason);
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
                break;
            }
            case 3: { // 疾病
                patMedInhosp.setDiagcheckFlag("2");
                patMedInhosp.setRemark(reason);
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
                break;
            }
        }
    }
    private void markAllFlagsAsError(PatMedInhosp patMedInhosp,
                                    String reason){
        patMedInhosp.setDeptcheckFlag("2");
        patMedInhosp.setWardcheckFlag("2");
        patMedInhosp.setDiagcheckFlag("2");
        patMedInhosp.setRemark(reason);
        patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
    }
    /**
     * 根据维度类型查询匹配的长期任务 ID 列表
     */
    private List<Long> findMatchingTaskIds(PatMedInhosp patMedInhosp1, int type) {
        switch (type) {
            case 1: { // 科室
                ServiceTaskdept query = new ServiceTaskdept();
                query.setLongtask(1L);
                query.setDeptCode(patMedInhosp1.getLeaveldeptcode());
                query.setDeptType("1");
                query.setOrgid(patMedInhosp1.getOrgid());
                return serviceTaskdeptMapper.selectServiceTaskdeptList(query)
                        .stream().map(ServiceTaskdept::getTaskId).collect(Collectors.toList());
            }
            case 2: { // 病区
                ServiceTaskdept query = new ServiceTaskdept();
                query.setLongtask(1L);
                query.setDeptCode(patMedInhosp1.getLeavehospitaldistrictcode());
                query.setDeptType("2");
                query.setOrgid(patMedInhosp1.getOrgid());
                return serviceTaskdeptMapper.selectServiceTaskdeptList(query)
                        .stream().map(ServiceTaskdept::getTaskId).collect(Collectors.toList());
            }
            case 3: { // 疾病
                if (StringUtils.isEmpty(patMedInhosp1.getLeaveicd10code())) return Collections.emptyList();
                ServiceTaskdiag query = new ServiceTaskdiag();
                query.setLongtask(1L);
                query.setIcd10code(patMedInhosp1.getLeaveicd10code());
                query.setOrgid(patMedInhosp1.getOrgid());
                return serviceTaskdiagMapper.selectServiceTaskdiagList(query)
                        .stream().map(ServiceTaskdiag::getTaskId).collect(Collectors.toList());
            }
        }
        return Collections.emptyList();
    }
//
//    @Override
//    public int dealOutHospInfo() {
@@ -547,6 +691,15 @@
        return calendar.getTime();
    }
    /**
     *
     * @param taskid 任务id
     * @param check 是否需要校验
     * @param patMedInhosp1 病人出入院信息
     * @param patArchive 病人信息
     * @param type 随访类型(1-科室,2-病区,3-疾病)
     * @param config 配置信息 visit.early.day
     */
    //将患者放到subtask中
    private void writeInSubTask(Long taskid, Boolean check, PatMedInhosp patMedInhosp1, PatArchive patArchive, Integer type, String config) {
        String longTaskReason = patMedInhosp1.getLongTaskReason();
@@ -600,7 +753,7 @@
            //先判断一下,是否需要校验
            if (check && (Objects.isNull(serviceSubtask.getTaskSituation()) || serviceSubtask.getTaskSituation() != 6)) {
                //在新增之前,先通过患者ID,sendstate=2查询一下,在所有长期任务中,是不是还有该患者待执行的任务,有的话,比较之前的endtime是否小于当前的endtaime,如果之前的小于现在的,则直接将之前的停掉(原因再入院)
                ServiceSubtaskVO subtask = new ServiceSubtaskVO();
                ServiceSubtaskEntity subtask = new ServiceSubtaskEntity();
                subtask.setPatid(patArchive.getId());
                subtask.setSendstate(2L);
                subtask.setTaskid(taskid);
@@ -897,7 +1050,7 @@
        for (PatMedInhosp patMedInhosp1 : patMedInhospList) {
            try {
                //获取当前入院患者的待执行的出院子任务随访信息
                ServiceSubtaskVO subtask = new ServiceSubtaskVO();
                ServiceSubtaskEntity subtask = new ServiceSubtaskEntity();
                subtask.setPatid(patMedInhosp1.getPatid());
                subtask.setSendstate(2L);
                List<ServiceSubtask> selectServiceSubtaskList = serviceSubtaskMapper.selectServiceSubtaskList(subtask);