陈昶聿
12 小时以前 38955eba4ef0e86bac6b1993b6b03abbbec2a929
ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
@@ -286,21 +286,44 @@
           其余(含 delay 为 0 或 NULL、或已超出延期窗口)          -> 3 超时完成
         finishtime 为空时用 NOW() 兜底,保证完成时一定写入状态;
         base 为空(endtime / schedule / day_offset 缺失)时保持原值不覆盖。 -->
    <!-- 2026-11-16 修复:
         MySQL 的 JOIN ... ON 只能引用当前 JOIN 里出现过的表,不能引用外层 UPDATE 的
         service_subtask;原写法 ON st.taskid = service_subtask.taskid 会报
         Unknown column 'service_subtask.taskid' in 'on clause'。
         相关标量子查询的 WHERE 可以正常引用外层 UPDATE 行,所以把三张关联表改成
         标量子查询;关联不到时返回 NULL,与原先 LEFT JOIN 未命中的结果一致。
         LIMIT 1 仅用于兼容 taskid / schedule_id 理论上不唯一的历史数据。 -->
    <sql id="finishDeadlineExpr">
        DATE_ADD(
        CASE
            WHEN COALESCE(st.appltype, '') = '5' THEN
            WHEN COALESCE((SELECT st.appltype
                           FROM service_task st
                           WHERE st.taskid = service_subtask.taskid
                           LIMIT 1), '') = '5' THEN
                DATE_ADD(
                CASE sts.schedule_benchmark
                CASE (SELECT sts.schedule_benchmark
                      FROM service_task_schedule sts
                      WHERE sts.id = service_subtask.schedule_id
                      LIMIT 1)
                    WHEN 1 THEN service_subtask.starttime
                    WHEN 2 THEN service_subtask.endtime
                    WHEN 3 THEN service_subtask.admindate
                    WHEN 4 THEN service_subtask.create_time
                    ELSE sts.schedule_benchmark_custom_time
                    ELSE (SELECT sts.schedule_benchmark_custom_time
                          FROM service_task_schedule sts
                          WHERE sts.id = service_subtask.schedule_id
                          LIMIT 1)
                END,
                INTERVAL std.day_offset DAY)
                INTERVAL (SELECT std.day_offset
                          FROM service_task_schedule_detail std
                          WHERE std.id = service_subtask.schedule_detail_id
                          LIMIT 1) DAY)
            ELSE
                DATE_ADD(service_subtask.endtime, INTERVAL COALESCE(st.send_day, 1) DAY)
                DATE_ADD(service_subtask.endtime,
                         INTERVAL COALESCE((SELECT st.send_day
                                           FROM service_task st
                                           WHERE st.taskid = service_subtask.taskid
                                           LIMIT 1), 1) DAY)
        END,
        INTERVAL (COALESCE((SELECT SUM(CAST(COALESCE(NULLIF(TRIM(pf.compensate_time), ''), '0') AS SIGNED))
        FROM service_subtask_preachform pf
@@ -308,31 +331,42 @@
    </sql>
    <sql id="finishStateExpr">
        COALESCE((
        SELECT
        COALESCE(
        CASE
        WHEN <include refid="finishDeadlineExpr"/> IS NULL THEN NULL
        WHEN COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
        &lt;= <include refid="finishDeadlineExpr"/>
        THEN 1
        WHEN COALESCE(st.delay_finish_days, 0) &gt; 0
        WHEN COALESCE((SELECT st.delay_finish_days
                       FROM service_task st
                       WHERE st.taskid = service_subtask.taskid
                       LIMIT 1), 0) &gt; 0
        AND COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
        &lt;= DATE_ADD(<include refid="finishDeadlineExpr"/>,
        INTERVAL COALESCE(st.delay_finish_days, 0) DAY)
        INTERVAL COALESCE((SELECT st.delay_finish_days
                           FROM service_task st
                           WHERE st.taskid = service_subtask.taskid
                           LIMIT 1), 0) DAY)
        THEN 2
        ELSE 3
        END
        FROM (SELECT 1) dummy
        LEFT JOIN service_task st ON st.taskid = service_subtask.taskid
        LEFT JOIN service_task_schedule sts ON sts.id = service_subtask.schedule_id
        LEFT JOIN service_task_schedule_detail std ON std.id = service_subtask.schedule_detail_id
        LIMIT 1
        ), finish_state)
        , finish_state)
    </sql>
    <!-- 当前执行方式:首次随访 current_preachform 为空时,取 preachform 的第一个执行方式兜底 -->
    <sql id="effectiveCurrentPreachformExpr">
        COALESCE(NULLIF(TRIM(current_preachform), ''), TRIM(SUBSTRING_INDEX(preachform, ',', 1)))
    </sql>
    <!-- 累计发送方式统计用:归一化 preachform 顺序(去空格、统一中文逗号) -->
    <sql id="normalizedPreachformExpr">
        REPLACE(REPLACE(IFNULL(preachform, ''), ' ', ''), ',', ',')
    </sql>
    <!-- 累计发送方式统计用:归一化后的有效 current_preachform(为空时取 preachform 第一个) -->
    <sql id="normalizedEffectiveCurrentPreachformExpr">
        COALESCE(NULLIF(TRIM(current_preachform), ''),
                 TRIM(SUBSTRING_INDEX(<include refid="normalizedPreachformExpr"/>, ',', 1)))
    </sql>
    <select id="selectServiceSubtaskBySendstate" parameterType="com.ruoyi.task.domain.entity.ServiceSubtaskEntity"
@@ -990,7 +1024,9 @@
        ), '%y%m%d' ) )
        THEN 1 ELSE 0 END ) finished,1 AS tasknumber FROM service_subtask
        WHERE del_flag=0
        <if test="starttime == null and endtime==null">
        <!-- 只有未指定任何时间范围时才默认只统计已到期任务;
             只按出院时间查询时不能再排除未来 visit_time,否则及时率分母与主表不一致。 -->
        <if test="starttime == null and endtime == null and startOutHospTime == null and endOutHospTime == null">
            AND visit_time &lt; NOW()
        </if>
        <if test="starttime != null and endtime!=null">
@@ -1082,7 +1118,9 @@
        WHERE ss.del_flag = 0
        AND ss.visit_time IS NOT NULL
        AND ss.sendstate != 4
        <if test="starttime == null and endtime==null">
        <!-- 只有未指定任何时间范围时才默认只统计已到期任务;
             只按出院时间查询时不能再排除未来 visit_time,否则及时率分母与主表不一致。 -->
        <if test="starttime == null and endtime == null and startOutHospTime == null and endOutHospTime == null">
            AND DATE(ss.visit_time) &lt; DATE(NOW())
        </if>
        <if test="starttime != null and endtime!=null">
@@ -2107,10 +2145,28 @@
        where 1=1
        and a.del_flag = '0'
        and b.del_flag = '0'
        and a.appltype = '3'
        <!-- 专病统计:单病种任务(appltype=3) + 专病任务组(appltype=5) -->
        and (
            a.appltype = '3'
            or (
                a.appltype = '5'
                and (
                    a.hosp_type = '3'
                    or (a.diag_type is not null and a.diag_type &lt;&gt; '')
                    or exists (
                        select 1
                        from service_task_schedule s
                        where s.taskid = a.taskid
                          and s.del_flag = '0'
                          and s.icd10_codes is not null
                          and s.icd10_codes &lt;&gt; ''
                    )
                )
            )
        )
        <if test="orgid != null and orgid != ''">and a.orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and b.campusid = #{campusid}</if>
        <if test="taskId != null">and a.task_id = #{taskId}</if>
        <if test="taskId != null">and a.taskid = #{taskId}</if>
        <if test="taskName != null and taskName != ''">
            and a.task_name like concat('%', #{taskName}, '%')
        </if>
@@ -2118,7 +2174,7 @@
            and a.diag_type = #{diagType}
        </if>
        <if test="taskIds != null and taskIds.size() > 0">
            AND a.task_id IN
            AND a.taskid IN
            <foreach collection="taskIds" item="taskIdItem" open="(" separator=","
                     close=")">
                #{taskIdItem}
@@ -2850,9 +2906,52 @@
        </if>
        <if test="currentPreachform != null and currentPreachform != '' ">
            AND <include refid="effectiveCurrentPreachformExpr"/> = #{currentPreachform}
            <if test="followUpCountStyle != null and followUpCountStyle = '1'">
            <!-- 注意:OGNL 里单引号 '1' 是 Character,String 与之用 = 比较会得到非 0 数字(恒真),必须用 =="1" -->
            <if test='followUpCountStyle != null and followUpCountStyle == "1"'>
                AND sendstate != 4
            </if>
            <if test='followUpCountStyle != null and followUpCountStyle == "3"'>
                AND sendstate = 6
            </if>
        </if>
        <!--
          累计发送方式下钻:
          preachform 是执行顺序(如 3,5,4,1),current_preachform 是当前执行到的位置。
          若要在“语音”下钻,则 current 位于语音及之后的所有行都要返回。
          以下条件与 ServiceSubtaskServiceImpl#applyFirstCumulativePreachformStatistics 口径一致:
            current 为空   -> 按 preachform 第一个方式
            current = -1   -> 按最后一个位置(全部方式都进入过)
            current 不在顺序中 -> 只做精确匹配兜底
        -->
        <if test="cumulativePreachform != null and cumulativePreachform != ''">
            AND (
                CASE
                    WHEN <include refid="normalizedPreachformExpr"/> = '' THEN
                        <include refid="normalizedEffectiveCurrentPreachformExpr"/> = #{cumulativePreachform}
                    WHEN <include refid="normalizedEffectiveCurrentPreachformExpr"/> = '-1' THEN
                        FIND_IN_SET(#{cumulativePreachform},
                            <include refid="normalizedPreachformExpr"/>) &gt; 0
                    WHEN FIND_IN_SET(<include refid="normalizedEffectiveCurrentPreachformExpr"/>,
                            <include refid="normalizedPreachformExpr"/>) = 0 THEN
                        <include refid="normalizedEffectiveCurrentPreachformExpr"/> = #{cumulativePreachform}
                    ELSE
                        FIND_IN_SET(#{cumulativePreachform},
                            <include refid="normalizedPreachformExpr"/>) &gt; 0
                        AND FIND_IN_SET(#{cumulativePreachform},
                            <include refid="normalizedPreachformExpr"/>)
                            &lt;= FIND_IN_SET(<include refid="normalizedEffectiveCurrentPreachformExpr"/>,
                            <include refid="normalizedPreachformExpr"/>)
                END
            )
            <if test='followUpCountStyle != null and followUpCountStyle == "1"'>
                AND sendstate != 4
            </if>
            <if test='followUpCountStyle != null and followUpCountStyle == "3"'>
                AND sendstate = 6
            </if>
        </if>
        <if test="excep != null and excep != ''">
            AND excep = #{excep}