陈昶聿
8 小时以前 6cb6d9ed6df1c50557f6b4b9f2c888261244df05
smartor/src/main/java/com/smartor/service/impl/PatMedInhospServiceImpl.java
@@ -57,6 +57,8 @@
    private PatMedOperationMapper patMedOperationMapper;
    @Autowired
    private PatMedOperationItemMapper patMedOperationItemMapper;
    @Autowired
    private ServiceTaskRuleMapper serviceTaskRuleMapper;
    @Value("${spring.profiles.active}")
    private String active;
@@ -307,31 +309,16 @@
            }
        }
        //2.出院随访
        String generationRule = iSysConfigService.selectConfigByKey("multitask.generation.rule");
        //generationRule    0:所有服务都生成;    1:专病任务优先
        if ("0".equals(generationRule)) {
            //科室任务新增
            addDeptSubTask(config, generationRule);
            //根据患者所在病区,获取该病区的长期任务,新华医院可以先不用根据病区和疾病来做
            if (!active.equals("xh")) {
                addWardSubTask(config, generationRule);
                addIcd10SubTask(config, generationRule);
                addOperationSubTask(config, generationRule);
            }
        //2.出院随访 - 基于规则的任务生成
        addSubTaskByRule(config);
        //根据患者所在病区,获取该病区的长期任务,新华医院可以先不用根据病区和疾病来做
        if (!active.equals("xh")) {
            addOperationSubTask(config);
        }
        //丽水的特殊要求(需要先执行疾病,如果疾病为空,则执行科室和病区)
        if ("1".equals(generationRule)) {
            addIcd10SubTask(config, generationRule);
            //科室任务新增
            addDeptSubTask(config, generationRule);
            addWardSubTask(config, generationRule);
        }
        //获取未处理在院的数据(如果刚刚出院的患者数据的出院时间,在下面查询出的入院时间之前,那之前的出院患者的数据,也得停掉,因为又入院了)
        PatMedInhosp patMedInhosp = new PatMedInhosp();
        //获取需要出院部门随访,未处理的数据
        patMedInhosp.setDeptcheckFlag("0");
        patMedInhosp.setInhospstate("0");
        List<PatMedInhosp> patMedInhospList = patMedInhospMapper.selectPatMedInhospList(patMedInhosp);
@@ -711,7 +698,7 @@
                    i = serviceSubtaskMapper.insertServiceSubtask(serviceSubtask);
                    addServiceSubtaskPreachform(serviceSubtask, null);
                }
            } else if (serviceSubtask.getTaskSituation() == 6) {
            } else if (serviceSubtask.getTaskSituation() != null && serviceSubtask.getTaskSituation() == 6) {
                //说明该患者是被过滤的,不用再执行了
                serviceSubtask.setCreateBy(serviceTask.getCreateBy());
                serviceSubtask.setCreateTime(new Date());
@@ -1178,7 +1165,7 @@
        }
    }
    private void addOperationSubTask(String config, String generationRule) {
    private void addOperationSubTask(String config) {
        //            手术随访
        PatMedOperationItem pmoi = new PatMedOperationItem();
        //获取需要出院疾病随访,未处理的数据
@@ -1223,7 +1210,7 @@
        }
    }
    private void addIcd10SubTask(String config, String generationRule) {
    private void addIcd10SubTask(String config) {
        PatMedInhosp pmjb = new PatMedInhosp();
        //获取需要出院疾病随访,未处理的数据
        pmjb.setDiagcheckFlag("0");
@@ -1314,7 +1301,7 @@
        }
    }
    private void addWardSubTask(String config, String generationRule) {
    private void addWardSubTask(String config) {
        PatMedInhosp pmbq = new PatMedInhosp();
        //获取需要出院部门随访,未处理的数据
        pmbq.setWardcheckFlag("0");
@@ -1383,7 +1370,7 @@
        }
    }
    private void addDeptSubTask(String config, String generationRule) {
    private void addDeptSubTask(String config) {
        //获取需要出院部门随访,未处理的数据
        PatMedInhosp pmks = new PatMedInhosp();
        pmks.setDeptcheckFlag("0");
@@ -1443,5 +1430,363 @@
        }
    }
    /**
     * 统一更新pat_med_inhosp的check_flag
     *
     * @param patMedInhosp 患者住院记录
     * @param type         1=deptcheck_flag, 2=wardcheck_flag, 3=diagcheck_flag, 4=inDeptcheck_flag, 5=inWardcheck_flag
     * @param flagValue    要设置的值
     * @param remark       备注信息
     */
    private void updateCheckFlag(PatMedInhosp patMedInhosp, Integer type, String flagValue, String remark) {
        PatMedInhosp updateObj = new PatMedInhosp();
        updateObj.setInhospid(patMedInhosp.getInhospid());
        switch (type) {
            case 1:
                updateObj.setDeptcheckFlag(flagValue);
                break;
            case 2:
                updateObj.setWardcheckFlag(flagValue);
                break;
            case 3:
                updateObj.setDiagcheckFlag(flagValue);
                break;
            case 4:
                updateObj.setInDeptcheckFlag(flagValue);
                break;
            case 5:
                updateObj.setInWardcheckFlag(flagValue);
                break;
        }
        if (StringUtils.isNotEmpty(remark)) {
            updateObj.setRemark(remark + " (type=" + type + ")");
        }
        patMedInhospMapper.updatePatMedInhosp(updateObj);
    }
    /**
     * 基于规则的任务生成方法
     * 根据患者离院科室或病区查询service_task_rule,获取deptRule、wradRule、diagRule
     * 根据规则值决定调用哪个方法生成随访任务
     */
    private void addSubTaskByRule(String config) {
        // 获取所有需要处理的出院患者(科室维度未处理)
        PatMedInhosp pmks = new PatMedInhosp();
        pmks.setDeptcheckFlag("0");
        pmks.setInhospstate("1");
        pmks.setFuflag("1");
        List<PatMedInhosp> patMedInhosps = patMedInhospMapper.selectPatMedInhospList(pmks);
        log.info("【基于规则】需要处理的出院患者信息:{}", CollectionUtils.isNotEmpty(patMedInhosps) ? patMedInhosps.size() : null);
        for (PatMedInhosp patMedInhosp1 : patMedInhosps) {
            // 获取患者基本信息
            PatArchive patArchive = patArchiveMapper.selectPatArchiveByPatid(patMedInhosp1.getPatid());
            if (Objects.isNull(patArchive)) {
                patMedInhosp1.setDeptcheckFlag("2");
                patMedInhosp1.setLongTaskReason("患者基本信息为空");
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
                continue;
            }
            try {
                // 查询规则:根据离院科室或病区
                ServiceTaskRule ruleQuery = new ServiceTaskRule();
                ruleQuery.setDeptcode(patMedInhosp1.getLeaveldeptcode());
                ruleQuery.setWardcode(patMedInhosp1.getLeavehospitaldistrictcode());
                ServiceTaskRule rule = serviceTaskRuleMapper.selectServiceTaskRuleByDeptOrWard(ruleQuery);
                if (rule == null) {
                    // 规则为空,全部生成
                    log.info("【基于规则】患者{}未配置规则,执行全部生成逻辑", patMedInhosp1.getInhospid());
                    addDeptSubTaskForPatient(patMedInhosp1, patArchive, config);
                    if (!active.equals("xh")) {
                        addWardSubTaskForPatient(patMedInhosp1, patArchive, config);
                        addIcd10SubTaskForPatient(patMedInhosp1, patArchive, config);
                    }
                } else {
                    // 有规则,按规则执行
                    log.info("【基于规则】患者{}查询到规则:deptRule={}, wradRule={}, diagRule={}", patMedInhosp1.getInhospid(), rule.getDeptRule(), rule.getWradRule(), rule.getDiagRule());
                    boolean deptIsPrimary = "1".equals(rule.getDeptRule());
                    boolean wardIsPrimary = "1".equals(rule.getWradRule());
                    boolean diagIsPrimary = "1".equals(rule.getDiagRule());
                    boolean deptIsSecondary = "2".equals(rule.getDeptRule());
                    boolean wardIsSecondary = "2".equals(rule.getWradRule());
                    boolean diagIsSecondary = "2".equals(rule.getDiagRule());
                    boolean hasPrimaryRule = deptIsPrimary || wardIsPrimary || diagIsPrimary;
                    if (hasPrimaryRule) {
                        // 第一步:执行所有规则=1的方法,每个独立记录成功/失败
                        int deptGenCount = 0;
                        int wardGenCount = 0;
                        int diagGenCount = 0;
                        if (deptIsPrimary) {
                            log.info("【基于规则】执行科室首要规则");
                            deptGenCount = addDeptSubTaskForPatient(patMedInhosp1, patArchive, config);
                        }
                        if (!active.equals("xh")) {
                            if (wardIsPrimary) {
                                log.info("【基于规则】执行病区首要规则");
                                wardGenCount = addWardSubTaskForPatient(patMedInhosp1, patArchive, config);
                            }
                            if (diagIsPrimary) {
                                log.info("【基于规则】执行疾病首要规则");
                                diagGenCount = addIcd10SubTaskForPatient(patMedInhosp1, patArchive, config);
                            }
                        }
                        int primaryGeneratedCount = deptGenCount + wardGenCount + diagGenCount;
                        log.info("【基于规则】首要规则执行结果: deptGen={}, wardGen={}, diagGen={}, total={}", deptGenCount, wardGenCount, diagGenCount, primaryGeneratedCount);
                        // 第二步:判断首要规则是否有任意一个生成成功
                        if (primaryGeneratedCount > 0) {
                            // 至少有一个首要规则生成成功
                            // —— 将=2的次要规则对应flag设为"2"(跳过)
                            log.info("【基于规则】首要规则有成功(count={}),将次要规则对应flag设为2", primaryGeneratedCount);
                            if (deptIsSecondary) {
                                updateCheckFlag(patMedInhosp1, 1, "2", "首要规则已生成,次要规则跳过");
                            }
                            if (!active.equals("xh")) {
                                if (wardIsSecondary) {
                                    updateCheckFlag(patMedInhosp1, 2, "2", "首要规则已生成,次要规则跳过");
                                }
                                if (diagIsSecondary) {
                                    updateCheckFlag(patMedInhosp1, 3, "2", "首要规则已生成,次要规则跳过");
                                }
                            }
                            // —— 对于=1且生成成功的,将其对应flag设为"1"
                            if (deptIsPrimary && deptGenCount > 0) {
                                updateCheckFlag(patMedInhosp1, 1, "1", "科室首要规则生成成功");
                            }
                            if (!active.equals("xh")) {
                                if (wardIsPrimary && wardGenCount > 0) {
                                    updateCheckFlag(patMedInhosp1, 2, "1", "病区首要规则生成成功");
                                }
                                if (diagIsPrimary && diagGenCount > 0) {
                                    updateCheckFlag(patMedInhosp1, 3, "1", "疾病首要规则生成成功");
                                }
                            }
                            // —— 对于=1但生成失败的,将其对应flag设为"2"
                            if (deptIsPrimary && deptGenCount == 0) {
                                updateCheckFlag(patMedInhosp1, 1, "2", "科室首要规则生成失败");
                            }
                            if (!active.equals("xh")) {
                                if (wardIsPrimary && wardGenCount == 0) {
                                    updateCheckFlag(patMedInhosp1, 2, "2", "病区首要规则生成失败");
                                }
                                if (diagIsPrimary && diagGenCount == 0) {
                                    updateCheckFlag(patMedInhosp1, 3, "2", "疾病首要规则生成失败");
                                }
                            }
                        } else {
                            // 所有首要规则全部失败,将=1的flag设为"2",然后执行=2的方法
                            log.info("【基于规则】首要规则全部失败,将=1的flag设为2,开始执行次要规则");
                            if (deptIsPrimary) {
                                updateCheckFlag(patMedInhosp1, 1, "2", "科室首要规则全部失败");
                            }
                            if (!active.equals("xh")) {
                                if (wardIsPrimary) {
                                    updateCheckFlag(patMedInhosp1, 2, "2", "病区首要规则全部失败");
                                }
                                if (diagIsPrimary) {
                                    updateCheckFlag(patMedInhosp1, 3, "2", "疾病首要规则全部失败");
                                }
                            }
                            // 执行次要规则(=2)的方法
                            if (deptIsSecondary) {
                                log.info("【基于规则】执行科室次要规则");
                                addDeptSubTaskForPatient(patMedInhosp1, patArchive, config);
                            }
                            if (!active.equals("xh")) {
                                if (wardIsSecondary) {
                                    log.info("【基于规则】执行病区次要规则");
                                    addWardSubTaskForPatient(patMedInhosp1, patArchive, config);
                                }
                                if (diagIsSecondary) {
                                    log.info("【基于规则】执行疾病次要规则");
                                    addIcd10SubTaskForPatient(patMedInhosp1, patArchive, config);
                                }
                            }
                        }
                    } else {
                        // 没有首要规则(都是"2"或其他),全部生成
                        log.info("【基于规则】无首要规则,执行全部生成逻辑");
                        addDeptSubTaskForPatient(patMedInhosp1, patArchive, config);
                        if (!active.equals("xh")) {
                            addWardSubTaskForPatient(patMedInhosp1, patArchive, config);
                            addIcd10SubTaskForPatient(patMedInhosp1, patArchive, config);
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error("【基于规则】患者{}处理异常:{}", patMedInhosp1.getInhospid(), e.getMessage());
                patMedInhosp1.setDeptcheckFlag("2");
                patMedInhosp1.setLongTaskReason("基于规则处理异常:" + e.getMessage());
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
            }
        }
    }
    /**
     * 为单个患者生成科室维度的子任务
     *
     * @return 生成的子任务数量
     */
    private int addDeptSubTaskForPatient(PatMedInhosp patMedInhosp1, PatArchive patArchive, String config) {
        int generatedCount = 0;
        try {
            //根据患者所在科室,获取该科室的长期任务
            ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
            serviceTaskdept.setLongtask(1L);
            serviceTaskdept.setDeptCode(patMedInhosp1.getLeaveldeptcode());
            serviceTaskdept.setDeptType("1");
            serviceTaskdept.setServiceType("2");
            serviceTaskdept.setOrgid(patMedInhosp1.getOrgid());
            List<ServiceTaskdept> serviceTaskdepts = serviceTaskdeptMapper.selectServiceTaskdeptList(serviceTaskdept);
            if (CollectionUtils.isEmpty(serviceTaskdepts)) {
                PatMedInhosp patMedInhosp = patMedInhospMapper.selectPatMedInhospByInhospid(patMedInhosp1.getInhospid());
                patMedInhosp1.setDeptcheckFlag("2");
                patMedInhosp1.setWardcheckFlag(patMedInhosp.getWardcheckFlag());
                patMedInhosp1.setDiagcheckFlag(patMedInhosp.getDiagcheckFlag());
                patMedInhosp1.setLongTaskReason(patMedInhosp.getLongTaskReason() + "该患者所在科室未配置离院长期任务;");
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
            } else {
                for (ServiceTaskdept serviceTaskdept1 : serviceTaskdepts) {
                    writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedInhosp1, patArchive, 1, config);
                    generatedCount++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【科室任务】患者{}处理异常:{}", patMedInhosp1.getInhospid(), e.getMessage());
            patMedInhosp1.setDeptcheckFlag("2");
            patMedInhosp1.setLongTaskReason("科室任务处理异常:" + e.getMessage());
            patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
        }
        return generatedCount;
    }
    /**
     * 为单个患者生成病区维度的子任务
     *
     * @return 生成的子任务数量
     */
    private int addWardSubTaskForPatient(PatMedInhosp patMedInhosp1, PatArchive patArchive, String config) {
        int generatedCount = 0;
        try {
            String errorreason = null;
            List<ServiceTaskdept> serviceTaskdeptList = null;
            if (!StringUtils.isEmpty(patMedInhosp1.getLeavehospitaldistrictcode())) {
                ServiceTaskdept serviceTaskdept = new ServiceTaskdept();
                serviceTaskdept.setLongtask(1L);
                serviceTaskdept.setDeptCode(patMedInhosp1.getLeavehospitaldistrictcode());
                serviceTaskdept.setDeptType("2");
                serviceTaskdept.setServiceType("2");
                serviceTaskdept.setOrgid(patMedInhosp1.getOrgid());
                serviceTaskdeptList = serviceTaskdeptMapper.selectServiceTaskdeptList(serviceTaskdept);
                if (CollectionUtils.isEmpty(serviceTaskdeptList)) {
                    errorreason = "该患者所在病区未配置长期任务;";
                }
            } else {
                errorreason = "离院病区为空;";
            }
            if (StringUtils.isNotEmpty(errorreason)) {
                PatMedInhosp patMedInhosp = patMedInhospMapper.selectPatMedInhospByInhospid(patMedInhosp1.getInhospid());
                patMedInhosp1.setWardcheckFlag("2");
                patMedInhosp1.setDeptcheckFlag(patMedInhosp.getDeptcheckFlag());
                patMedInhosp1.setDiagcheckFlag(patMedInhosp.getDiagcheckFlag());
                patMedInhosp1.setLongTaskReason(StringUtils.isNotEmpty(patMedInhosp.getLongTaskReason()) ? patMedInhosp.getLongTaskReason() + errorreason : errorreason);
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
            } else {
                for (ServiceTaskdept serviceTaskdept1 : serviceTaskdeptList) {
                    writeInSubTask(serviceTaskdept1.getTaskId(), true, patMedInhosp1, patArchive, 2, config);
                    generatedCount++;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【病区任务】患者{}处理异常:{}", patMedInhosp1.getInhospid(), e.getMessage());
            patMedInhosp1.setWardcheckFlag("2");
            patMedInhosp1.setLongTaskReason("病区任务处理异常:" + e.getMessage());
            patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
        }
        return generatedCount;
    }
    /**
     * 为单个患者生成疾病维度的子任务
     *
     * @return 生成的子任务数量
     */
    private int addIcd10SubTaskForPatient(PatMedInhosp patMedInhosp1, PatArchive patArchive, String config) {
        int generatedCount = 0;
        try {
            if (StringUtils.isEmpty(patMedInhosp1.getLeaveicd10code())) {
                return 0;
            }
            ServiceTaskdiag serviceTaskdiag = new ServiceTaskdiag();
            serviceTaskdiag.setLongtask(1L);
            serviceTaskdiag.setServiceType("2");
            if (active.equals("nhfy")) {
                serviceTaskdiag.setIcd10name(patMedInhosp1.getLeavediagname());
                if (StringUtils.isEmpty(patMedInhosp1.getLeavediagname())) {
                    PatMedInhosp patMedInhosp = patMedInhospMapper.selectPatMedInhospByInhospid(patMedInhosp1.getInhospid());
                    patMedInhosp1.setDiagcheckFlag("2");
                    patMedInhosp1.setLongTaskReason("该患者主诊断名称为空;");
                    patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
                    return 0;
                }
            } else {
                serviceTaskdiag.setIcd10code(patMedInhosp1.getLeaveicd10code());
            }
            serviceTaskdiag.setOrgid(patMedInhosp1.getOrgid());
            List<ServiceTaskdiag> serviceTaskdiags = serviceTaskdiagMapper.selectServiceTaskdiagListByIcdName(serviceTaskdiag);
            if (CollectionUtils.isEmpty(serviceTaskdiags)) {
                PatMedInhosp patMedInhosp = patMedInhospMapper.selectPatMedInhospByInhospid(patMedInhosp1.getInhospid());
                patMedInhosp.setDiagcheckFlag("2");
                patMedInhosp.setLongTaskReason("该患者所患疾病未配置长期任务;");
                patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
            } else {
                int flag = 0;
                for (ServiceTaskdiag serviceTaskdiag1 : serviceTaskdiags) {
                    if (StringUtils.isNotEmpty(serviceTaskdiag1.getDeptCode()) && serviceTaskdiag1.getDeptCode().equals(patMedInhosp1.getLeaveldeptcode())) {
                        writeInSubTask(serviceTaskdiag1.getTaskId(), true, patMedInhosp1, patArchive, 3, config);
                        generatedCount++;
                        flag = 1;
                    } else if (StringUtils.isNotEmpty(serviceTaskdiag1.getWardCode()) && serviceTaskdiag1.getWardCode().equals(patMedInhosp1.getLeavehospitaldistrictcode())) {
                        writeInSubTask(serviceTaskdiag1.getTaskId(), true, patMedInhosp1, patArchive, 3, config);
                        generatedCount++;
                        flag = 1;
                    }
                }
                if (flag == 0) {
                    log.error("该科室或病区对于患者所患疾病未配置长期任务,患者:{}", patMedInhosp1.getInhospid());
                    PatMedInhosp patMedInhosp = patMedInhospMapper.selectPatMedInhospByInhospid(patMedInhosp1.getInhospid());
                    patMedInhosp.setDiagcheckFlag("2");
                    patMedInhosp.setLongTaskReason("该科室或病区对于患者所患疾病未配置长期任务;");
                    patMedInhospMapper.updatePatMedInhosp(patMedInhosp);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【疾病任务】患者{}处理异常:{}", patMedInhosp1.getInhospid(), e.getMessage());
            patMedInhosp1.setDiagcheckFlag("2");
            patMedInhosp1.setLongTaskReason("疾病任务处理异常:" + e.getMessage());
            patMedInhospMapper.updatePatMedInhosp(patMedInhosp1);
        }
        return generatedCount;
    }
}