陈昶聿
26 分钟以前 40aa3afed411c0bd101fb7a83ae84c4d0902e2e0
ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java
@@ -885,6 +885,39 @@
    }
    /**
     * 兼容「科室/病区」字段重命名前的旧入参。
     *
     * <p>2026-11 起:{@code deptcode/deptname} = 在院科室,{@code leaveldeptcode/leaveldeptname} = 离院科室。
     * 但前端/旧调用方({@code /serviceSubtask/addSubTask})仍可能把科室放在 {@code deptcode/deptname} 里。
     * 若不归位,子任务会落到「在院维度」,列表按离院科室筛选时查不到(本次线上 2654501/2663019 即此问题)。
     *
     * <p>规则:
     * <ul>
     *   <li>只对非延续护理({@code service_type != '99'})生效;99 允许使用在院维度;</li>
     *   <li>离院科室为空且 {@code deptcode} 有值时,把 {@code deptcode/deptname} 迁到离院维度;</li>
     *   <li>清空在院维度,避免同一份科室同时命中两组筛选(与 {@code toSubtaskFromTaskVO} 的口径一致)。</li>
     * </ul>
     */
    private void alignLeaveDeptFromLegacyDept(ServiceSubtask serviceSubtask) {
        if (serviceSubtask == null || "99".equals(serviceSubtask.getServiceType())) {
            return;
        }
        if (StringUtils.isEmpty(serviceSubtask.getLeaveldeptcode())
                && StringUtils.isNotEmpty(serviceSubtask.getDeptcode())) {
            serviceSubtask.setLeaveldeptcode(serviceSubtask.getDeptcode());
        }
        if (StringUtils.isEmpty(serviceSubtask.getLeaveldeptname())
                && StringUtils.isNotEmpty(serviceSubtask.getDeptname())) {
            serviceSubtask.setLeaveldeptname(serviceSubtask.getDeptname());
        }
        // 手工新增入口没有「在院维度」的可靠来源;任务配置里的科室不是患者本人的在院科室。
        serviceSubtask.setDeptcode(null);
        serviceSubtask.setDeptname(null);
        serviceSubtask.setHospitaldistrictcode(null);
        serviceSubtask.setHospitaldistrictname(null);
    }
    /**
     * 新增单一任务(随访)
     *
     * @param serviceSubtask 单一任务(随访)
@@ -892,6 +925,8 @@
     */
    @Override
    public Map<String, Object> insertServiceSubtask(ServiceSubtask serviceSubtask) {
        // 兼容 2026-11 字段重命名前的旧入参:/addSubTask 仍可能把科室放在 deptcode/deptname 里
        alignLeaveDeptFromLegacyDept(serviceSubtask);
        List<ServiceSubtask> serviceSubtasks = null;
        //先查询一下该患者是否已经创建根据serviceType,sendstate,inhospid,patid,orgid
        //这样判断主要是为了想让新华医院的外部导入患者创建任务,也能用这个接口
@@ -2565,7 +2600,10 @@
            if (serviceSubtaskCountReq.getDrcode() != null && !serviceSubtaskCountReq.getDrcode().isEmpty()) {
                groupKey = drcode;
            } else {
                groupKey = "deptcode";
                // 2026-11 字段重命名后:deptcode 是在院科室(历史数据为空),
                // 出院科室已改名为 leaveldeptcode,分组必须用后者,
                // 否则 getSfStatisticsGroupKey 会返回 NULL,拼成 IN (NULL) 导致整表查空。
                groupKey = "leaveldeptcode";
            }
        } else if (serviceSubtaskCountReq.getStatisticaltype() != null && serviceSubtaskCountReq.getStatisticaltype() == 3) {
            // 病种统计:按任务ID分组,每个任务即一个病种随访任务
@@ -2576,6 +2614,17 @@
        Map<String, List<ServiceSubtask>> collect = new HashMap<>();
        serviceSubtaskCountReq.setGroupKey(groupKey);
        List<String> groupKeyList = serviceSubtaskMapper.getSfStatisticsGroupKey(serviceSubtaskCountReq);
        /*
         * 分组键可能为 NULL(历史数据、或重命名后尚未回填的在院维度列),
         * 直接拼进 SQL 会变成 `AND IFNULL(列,'Unknown') IN (NULL)`,恒不成立,整张统计表为空。
         * 这里统一成 'Unknown',与聚合 SQL 的 IFNULL(列,'Unknown') 口径对齐。
         */
        if (groupKeyList != null) {
            groupKeyList = groupKeyList.stream()
                    .map(key -> key == null ? "Unknown" : key)
                    .distinct()
                    .collect(Collectors.toList());
        }
        serviceSubtaskCountReq.setGroupKeyList(groupKeyList);
        List<ServiceSubtask> rawData = serviceSubtaskMapper.getSfStatistics(serviceSubtaskCountReq);
        switch (groupKey) {
@@ -2590,6 +2639,7 @@
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getManagementDoctorCode()).orElse("Unknown")));
                break;
            case "deptcode":
            case "leaveldeptcode":
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getLeaveldeptcode()).orElse("Unknown")));
                break;
            case "leavehospitaldistrictcode":
@@ -3206,7 +3256,7 @@
    }
    /**
     * 再次随访附加统计:待随访、已发送、人工电话超时及回院复诊复查(suggest=6)。
     * 再次随访附加统计:待随访、已发送、人工电话超时及回院复诊复查(suggest=7)。
     */
    private void applyAgainFollowUpExtraStatistics(ServiceSubtaskStatistic statistic, ServiceSubtask serviceSubtask) {
        if (serviceSubtask.getVisitCount() == null || serviceSubtask.getVisitCount() <= 1) {
@@ -3216,9 +3266,9 @@
            return;
        }
        Long sendstate = serviceSubtask.getSendstate();
        // 回院复诊复查:再次随访中按处理意见 suggest=6 统计
        if (StringUtils.equals("6", StringUtils.trim(serviceSubtask.getSuggest()))) {
            statistic.setSuggest6CountAgain(statistic.getSuggest6CountAgain() + 1L);
        // 回院复诊复查:再次随访中按处理意见 suggest=7 统计
        if (StringUtils.equals("7", StringUtils.trim(serviceSubtask.getSuggest()))) {
            statistic.setSuggest7CountAgain(statistic.getSuggest7CountAgain() + 1L);
        }
        if (sendstate.equals(3L)) {
            statistic.setSentAgain(statistic.getSentAgain() + 1L);
@@ -3864,7 +3914,7 @@
     * pendingFollowUpSentAgainInfo - visitCount > 1 && sendstate in (3,7)
     * sentInfo - visitCount = 1 && sendstate = 3
     * sentAgainInfo - visitCount > 1 && sendstate = 3
     * suggest6CountAgainInfo - visitCount > 1 && suggest = 6
     * suggest7CountAgainInfo - visitCount > 1 && suggest = 7
     * filterCountList - taskSituation = 6 && sendstate = 4
     *
     */
@@ -4049,10 +4099,10 @@
                serviceSubtaskCountReq.setVisitCount(2);
                serviceSubtaskCountReq.setExcep("1");
                break;
            case "suggest6CountAgainInfo":
                // 再次随访回院复诊复查:visitCount > 1 且 suggest = 6
            case "suggest7CountAgainInfo":
                // 再次随访回院复诊复查:visitCount > 1 且 suggest = 7
                serviceSubtaskCountReq.setVisitCount(2);
                serviceSubtaskCountReq.setSuggest("6");
                serviceSubtaskCountReq.setSuggest("7");
                break;
            case "taskSituation1Info":
                serviceSubtaskCountReq.setTaskSituation(1);
@@ -4611,7 +4661,7 @@
        total.setSentAgain(0L);
        total.setFollowUpSuccessAgain(0L);
        total.setFollowUpFailAgain(0L);
        total.setSuggest6CountAgain(0L);
        total.setSuggest7CountAgain(0L);
        total.setManualAgain(0L);
        total.setSmsAgain(0L);
        total.setWeChatAgain(0L);
@@ -4656,7 +4706,7 @@
            total.setSentAgain(total.getSentAgain() + (stat.getSentAgain() != null ? stat.getSentAgain() : 0L));
            total.setFollowUpSuccessAgain(total.getFollowUpSuccessAgain() + (stat.getFollowUpSuccessAgain() != null ? stat.getFollowUpSuccessAgain() : 0L));
            total.setFollowUpFailAgain(total.getFollowUpFailAgain() + (stat.getFollowUpFailAgain() != null ? stat.getFollowUpFailAgain() : 0L));
            total.setSuggest6CountAgain(total.getSuggest6CountAgain() + (stat.getSuggest6CountAgain() != null ? stat.getSuggest6CountAgain() : 0L));
            total.setSuggest7CountAgain(total.getSuggest7CountAgain() + (stat.getSuggest7CountAgain() != null ? stat.getSuggest7CountAgain() : 0L));
            total.setManualAgain(total.getManualAgain() + (stat.getManualAgain() != null ? stat.getManualAgain() : 0L));
            total.setSmsAgain(total.getSmsAgain() + (stat.getSmsAgain() != null ? stat.getSmsAgain() : 0L));
            total.setWeChatAgain(total.getWeChatAgain() + (stat.getWeChatAgain() != null ? stat.getWeChatAgain() : 0L));