陈昶聿
7 小时以前 38955eba4ef0e86bac6b1993b6b03abbbec2a929
ruoyi-task/src/main/java/com/ruoyi/task/service/impl/ServiceSubtaskServiceImpl.java
@@ -280,6 +280,9 @@
    private static final String AGAIN_LOCK_KEY_PREFIX = "smartor:again:lock:";
    private static final long AGAIN_LOCK_SECONDS = 30L;
    /** 缙云县人民医院机构编码 */
    private static final String JINYUN_PEOPLES_HOSPITAL_ORGID = "47246116333112211A1001";
    /**
     * 查询单一任务(随访)
     *
@@ -2586,7 +2589,8 @@
        log.info("getSfStatistics的入参为:{}", serviceSubtaskCountReq);
        String drcode = "drcode";
        //缙云人民 根据经管医生分组
        if (serviceSubtaskCountReq.getOrgid().equals("47246116333112211A1001")) {
        boolean jinyunHospital = JINYUN_PEOPLES_HOSPITAL_ORGID.equals(serviceSubtaskCountReq.getOrgid());
        if (jinyunHospital) {
            drcode = "management_doctor_code";
        }
        String groupKey = drcode;
@@ -2652,18 +2656,30 @@
                collect = rawData.stream().collect(Collectors.groupingBy(subtask -> Optional.ofNullable(subtask.getDrcode()).orElse("Unknown")));
                break;
        }
        //获取及时率统计合集
        Map<String, Map<String, Object>> jsRates = getSsRateForSfStatistics(serviceSubtaskCountReq, collect);
        /*
         * 缙云县人民医院的“及时率”口径是“及时随访人次 / 应随访人次”,
         * 必须和同一批统计行里的 timelyFollowUp / followUpNeeded 保持一致。
         * 不能再走 selectTimelyRateBatch:该 SQL 在未传应随访时间范围时还会追加
         * visit_time < NOW(),只按出院时间统计时会把未到期任务排除在分母外,
         * 导致页面上的及时率和“及时随访人次/应随访人次”对不上。
         */
        Map<String, Map<String, Object>> jsRates = jinyunHospital
                ? Collections.emptyMap()
                : getSsRateForSfStatistics(serviceSubtaskCountReq, collect);
        for (String collectKey : collect.keySet()) {
            List<ServiceSubtask> serviceSubtaskList = collect.get(collectKey);
            if (CollectionUtils.isEmpty(serviceSubtaskList)) {
                continue;
            }
            ServiceSubtaskStatistic statistic = getStatistic(serviceSubtaskList, serviceSubtaskCountReq.getFollowUpCountStyle(), serviceSubtaskCountReq.getVisitCount());
            // hzszlyy:前端不改字段,直接把累计值回填到原 manual/voice/sms/weChat 及 Again 字段
            applySiteCumulativeOverride(statistic);
            if ("taskid".equals(groupKey)) {
                applyTaskGroupInfo(statistic, serviceSubtaskList);
            }
            if (ObjectUtils.isNotEmpty(jsRates)) {
            if (jinyunHospital) {
                statistic.setRate(calculateJinyunTimelyRate(statistic));
            } else if (ObjectUtils.isNotEmpty(jsRates)) {
                Map<String, Object> jsRateMap = jsRates.get(collectKey);
                BigDecimal jsRate = new BigDecimal("0");
                if (ObjectUtils.isNotEmpty(jsRateMap) && ObjectUtils.isNotEmpty(jsRateMap.get("rate"))) {
@@ -2768,6 +2784,23 @@
     */
    private long nvlLong(Long value) {
        return value == null ? 0L : value;
    }
    /**
     * 缙云县人民医院及时率:及时随访人次 / 应随访人次。
     * <p>
     * 两者都取自同一条统计结果,保证页面上的及时率与这两列可以互相验算。
     * 应随访人次为 0 时返回 0,前端按 0.00% 展示。
     */
    private double calculateJinyunTimelyRate(ServiceSubtaskStatistic statistic) {
        long followUpNeeded = nvlLong(statistic.getFollowUpNeeded());
        if (followUpNeeded <= 0) {
            return 0D;
        }
        long timelyFollowUp = nvlLong(statistic.getTimelyFollowUp());
        return BigDecimal.valueOf(timelyFollowUp)
                .divide(BigDecimal.valueOf(followUpNeeded), 4, RoundingMode.HALF_UP)
                .doubleValue();
    }
    /**
@@ -3081,6 +3114,7 @@
                }//二次出院随访
                else if (serviceSubtask.getVisitCount() != null && serviceSubtask.getVisitCount() > 1) {
                    applyAgainFollowUpExtraStatistics(serviceSubtaskStatistic, serviceSubtask);
                    applyAgainCumulativePreachformStatistics(serviceSubtaskStatistic, serviceSubtask, followUpCountStyle);
                    //再次应随访
                    if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() != 4) {
                        serviceSubtaskStatistic.setNeedFollowUpAgain(serviceSubtaskStatistic.getNeedFollowUpAgain() + 1L);
@@ -3301,6 +3335,17 @@
    /** 发送方式编码:微信 */
    private static final String PREACHFORM_WECHAT = "5";
    /** 累计位图:语音 */
    private static final int CUMULATIVE_MASK_VOICE = 1;
    /** 累计位图:微信 */
    private static final int CUMULATIVE_MASK_WECHAT = 1 << 1;
    /** 累计位图:短信 */
    private static final int CUMULATIVE_MASK_SMS = 1 << 2;
    /** 累计位图:人工 */
    private static final int CUMULATIVE_MASK_MANUAL = 1 << 3;
    /** 需要把累计值回填到原字段的站点 */
    private static final String SITE_HZSZLYY = "hzszlyy";
    /**
     * 首次随访「累计发送方式」统计。
     * <p>
@@ -3330,42 +3375,101 @@
        if (!isCumulativePreachformCountable(serviceSubtask, followUpCountStyle)) {
            return;
        }
        int mask = resolveCumulativePreachformMask(serviceSubtask);
        if ((mask & CUMULATIVE_MASK_VOICE) != 0) {
            statistic.setVoiceCumulative(statistic.getVoiceCumulative() + 1L);
        }
        if ((mask & CUMULATIVE_MASK_WECHAT) != 0) {
            statistic.setWeChatCumulative(statistic.getWeChatCumulative() + 1L);
        }
        if ((mask & CUMULATIVE_MASK_SMS) != 0) {
            statistic.setSmsCumulative(statistic.getSmsCumulative() + 1L);
        }
        if ((mask & CUMULATIVE_MASK_MANUAL) != 0) {
            statistic.setManualCumulative(statistic.getManualCumulative() + 1L);
        }
    }
    /**
     * 再次随访「累计发送方式」统计,口径与 {@link #applyFirstCumulativePreachformStatistics} 一致。
     */
    private void applyAgainCumulativePreachformStatistics(ServiceSubtaskStatistic statistic,
                                                          ServiceSubtask serviceSubtask,
                                                          String followUpCountStyle) {
        if (serviceSubtask.getVisitCount() == null || serviceSubtask.getVisitCount() <= 1) {
            return;
        }
        if (!isCumulativePreachformCountable(serviceSubtask, followUpCountStyle)) {
            return;
        }
        int mask = resolveCumulativePreachformMask(serviceSubtask);
        if ((mask & CUMULATIVE_MASK_VOICE) != 0) {
            statistic.setVoiceAgainCumulative(statistic.getVoiceAgainCumulative() + 1L);
        }
        if ((mask & CUMULATIVE_MASK_WECHAT) != 0) {
            statistic.setWeChatAgainCumulative(statistic.getWeChatAgainCumulative() + 1L);
        }
        if ((mask & CUMULATIVE_MASK_SMS) != 0) {
            statistic.setSmsAgainCumulative(statistic.getSmsAgainCumulative() + 1L);
        }
        if ((mask & CUMULATIVE_MASK_MANUAL) != 0) {
            statistic.setManualAgainCumulative(statistic.getManualAgainCumulative() + 1L);
        }
    }
    /**
     * 计算某条子任务“已进入过”的发送方式位图。
     * <p>
     * current_preachform 为空取第一个;= -1 视为全部遍历完;不在顺序里时退化为精确匹配。
     */
    private int resolveCumulativePreachformMask(ServiceSubtask serviceSubtask) {
        List<String> order = splitPreachformOrder(serviceSubtask.getPreachform());
        String current = StringUtils.isNotBlank(serviceSubtask.getCurrentPreachform())
                ? serviceSubtask.getCurrentPreachform().trim()
                : (order.isEmpty() ? null : order.get(0));
        if (order.isEmpty()) {
            // 没有顺序时退化为“当前方式精确匹配”,与 SQL 的 normalizedPreachformExpr = '' 分支一致
            if (current != null) {
                increaseExactCumulative(statistic, current);
            }
            return;
            return exactCumulativeMask(current);
        }
        int currentPosition;
        if ("-1".equals(current)) {
            // -1 表示所有发送方式都已遍历完,相当于停在最后一个位置
            currentPosition = order.size();
        } else {
            int currentIndex = order.indexOf(current);
            if (currentIndex < 0) {
                // 历史异常值不在配置顺序里:只按精确匹配兜底,避免把前后方式都算进去
                increaseExactCumulative(statistic, current);
                return;
                return exactCumulativeMask(current);
            }
            currentPosition = currentIndex + 1;
        }
        int mask = 0;
        if (reachedPreachform(order, PREACHFORM_VOICE, currentPosition)) {
            statistic.setVoiceCumulative(statistic.getVoiceCumulative() + 1L);
            mask |= CUMULATIVE_MASK_VOICE;
        }
        if (reachedPreachform(order, PREACHFORM_WECHAT, currentPosition)) {
            statistic.setWeChatCumulative(statistic.getWeChatCumulative() + 1L);
            mask |= CUMULATIVE_MASK_WECHAT;
        }
        if (reachedPreachform(order, PREACHFORM_SMS, currentPosition)) {
            statistic.setSmsCumulative(statistic.getSmsCumulative() + 1L);
            mask |= CUMULATIVE_MASK_SMS;
        }
        if (reachedPreachform(order, PREACHFORM_MANUAL, currentPosition)) {
            statistic.setManualCumulative(statistic.getManualCumulative() + 1L);
            mask |= CUMULATIVE_MASK_MANUAL;
        }
        return mask;
    }
    private int exactCumulativeMask(String current) {
        if (PREACHFORM_MANUAL.equals(current)) {
            return CUMULATIVE_MASK_MANUAL;
        }
        if (PREACHFORM_VOICE.equals(current)) {
            return CUMULATIVE_MASK_VOICE;
        }
        if (PREACHFORM_SMS.equals(current)) {
            return CUMULATIVE_MASK_SMS;
        }
        if (PREACHFORM_WECHAT.equals(current)) {
            return CUMULATIVE_MASK_WECHAT;
        }
        return 0;
    }
    private boolean isCumulativePreachformCountable(ServiceSubtask serviceSubtask, String followUpCountStyle) {
@@ -3400,15 +3504,39 @@
        return position >= 0 && currentPosition >= position + 1;
    }
    private void increaseExactCumulative(ServiceSubtaskStatistic statistic, String current) {
        if (PREACHFORM_MANUAL.equals(current)) {
            statistic.setManualCumulative(statistic.getManualCumulative() + 1L);
        } else if (PREACHFORM_VOICE.equals(current)) {
            statistic.setVoiceCumulative(statistic.getVoiceCumulative() + 1L);
        } else if (PREACHFORM_SMS.equals(current)) {
            statistic.setSmsCumulative(statistic.getSmsCumulative() + 1L);
        } else if (PREACHFORM_WECHAT.equals(current)) {
            statistic.setWeChatCumulative(statistic.getWeChatCumulative() + 1L);
    /**
     * hzszlyy 站点保持前端字段不变:把累计值回填到原 manual/voice/sms/weChat 及 Again 字段。
     * <p>
     * 其它站点不覆盖,继续使用原来的“当前方式精确匹配”口径。
     */
    private void applySiteCumulativeOverride(ServiceSubtaskStatistic statistic) {
        if (!SITE_HZSZLYY.equals(active)) {
            return;
        }
        statistic.setManual(nvlLong(statistic.getManualCumulative()));
        statistic.setVoice(nvlLong(statistic.getVoiceCumulative()));
        statistic.setSms(nvlLong(statistic.getSmsCumulative()));
        statistic.setWeChat(nvlLong(statistic.getWeChatCumulative()));
        statistic.setManualAgain(nvlLong(statistic.getManualAgainCumulative()));
        statistic.setVoiceAgain(nvlLong(statistic.getVoiceAgainCumulative()));
        statistic.setSmsAgain(nvlLong(statistic.getSmsAgainCumulative()));
        statistic.setWeChatAgain(nvlLong(statistic.getWeChatAgainCumulative()));
    }
    /**
     * 超链接下钻的发送方式条件。
     * <p>
     * hzszlyy 站点的前端不改字段,仍点原来的 manualInfo/voiceInfo/...,
     * 后端改用累计口径;其它站点保持 current_preachform 精确匹配。
     */
    private void applyPreachformInfoCondition(ServiceSubtaskCountReq serviceSubtaskCountReq,
                                              int visitCount,
                                              String preachform) {
        serviceSubtaskCountReq.setVisitCount(visitCount);
        if (SITE_HZSZLYY.equals(active)) {
            serviceSubtaskCountReq.setCumulativePreachform(preachform);
        } else {
            serviceSubtaskCountReq.setCurrentPreachform(preachform);
        }
    }
@@ -3627,6 +3755,7 @@
            //再次出院随访
            if (serviceSubtask.getVisitCount() != null && serviceSubtask.getVisitCount() > 1) {
                applyAgainCumulativePreachformStatistics(serviceSubtaskStatistic, serviceSubtask, followUpCountStyle);
                //再次应随访
                if (serviceSubtask.getSendstate() != null && serviceSubtask.getSendstate() != 4) {
                    serviceSubtaskStatistic.setNeedFollowUpAgain(serviceSubtaskStatistic.getNeedFollowUpAgain() + 1L);
@@ -4131,20 +4260,16 @@
                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Arrays.asList(5L, 7L)));
                break;
            case "manualInfo":
                serviceSubtaskCountReq.setVisitCount(1);
                serviceSubtaskCountReq.setCurrentPreachform("1");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 1, PREACHFORM_MANUAL);
                break;
            case "voiceInfo":
                serviceSubtaskCountReq.setVisitCount(1);
                serviceSubtaskCountReq.setCurrentPreachform("3");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 1, PREACHFORM_VOICE);
                break;
            case "smsInfo":
                serviceSubtaskCountReq.setVisitCount(1);
                serviceSubtaskCountReq.setCurrentPreachform("4");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 1, PREACHFORM_SMS);
                break;
            case "weChatInfo":
                serviceSubtaskCountReq.setVisitCount(1);
                serviceSubtaskCountReq.setCurrentPreachform("5");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 1, PREACHFORM_WECHAT);
                break;
            // 累计口径:按 preachform 的执行顺序,current_preachform 位于该方式及之后即计入
            case "voiceCumulativeInfo":
@@ -4204,20 +4329,16 @@
                serviceSubtaskCountReq.setSendstates(new ArrayList<>(Arrays.asList(5L, 7L)));
                break;
            case "manualAgainInfo":
                serviceSubtaskCountReq.setVisitCount(2);
                serviceSubtaskCountReq.setCurrentPreachform("1");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 2, PREACHFORM_MANUAL);
                break;
            case "voiceAgainInfo":
                serviceSubtaskCountReq.setVisitCount(2);
                serviceSubtaskCountReq.setCurrentPreachform("3");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 2, PREACHFORM_VOICE);
                break;
            case "smsAgainInfo":
                serviceSubtaskCountReq.setVisitCount(2);
                serviceSubtaskCountReq.setCurrentPreachform("4");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 2, PREACHFORM_SMS);
                break;
            case "weChatAgainInfo":
                serviceSubtaskCountReq.setVisitCount(2);
                serviceSubtaskCountReq.setCurrentPreachform("5");
                applyPreachformInfoCondition(serviceSubtaskCountReq, 2, PREACHFORM_WECHAT);
                break;
            case "manualAgainSuccessInfo":
                serviceSubtaskCountReq.setVisitCount(1);