陈昶聿
43 分钟以前 b1b1b15bbf4fd5a4ed21efa91279227fc915e960
ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
@@ -109,6 +109,10 @@
        <result property="isManuallyCalled" column="is_manually_called"/>
        <result property="campusid" column="campusid"/>
        <result property="diagType" column="diag_type"/>
        <!-- 2026-11:重要性标记编码(日间手术/日间诊疗/无…),建任务时取自 pat_med_inhosp -->
        <result property="importanceFlagCode" column="importance_flag_code"/>
        <!-- 2026-11:点位编码,建子任务时原样拷自 service_task.point_location -->
        <result property="pointLocation" column="point_location"/>
    </resultMap>
    <resultMap type="com.ruoyi.task.domain.ServiceSubtaskCount" id="ServiceSubtaskResult2">
@@ -244,6 +248,8 @@
               hospitaldistrictname,
               deptcode,
               deptname,
               importance_flag_code,
               point_location,
               stop_state,
               leavehospitaldistrictcode,
               leavehospitaldistrictname,
@@ -280,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
@@ -302,26 +331,26 @@
    </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 的第一个执行方式兜底 -->
@@ -384,16 +413,34 @@
    <sql id="filterLeaveDimAnd">
        /* f-v3 */
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            AND (
            leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="lvWardCodeItem" open="(" separator="," close=")">
                #{lvWardCodeItem}
            </foreach>
            <!-- 延续护理任务(service_type='99')可以在患者还没出院时生成:
                 这类记录只有「在院维度」(hospitaldistrictcode / deptcode),出院维度是 NULL。
                 NULL 不匹配任何 IN,任务会「提示生成成功、列表里却查不到」,
                 所以出院维度为空时用同一套编码体系的在院维度兜底。 -->
            OR (service_type = '99' AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode = '')
                AND hospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="lvWardCodeItem" open="(" separator="," close=")">
                    #{lvWardCodeItem}
                </foreach>)
            )
        </if>
        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
            AND leaveldeptcode IN
            AND (
            leaveldeptcode IN
            <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
                #{lvDeptCodeItem}
            </foreach>
            OR (service_type = '99' AND (leaveldeptcode IS NULL OR leaveldeptcode = '')
                AND deptcode IN
                <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
                    #{lvDeptCodeItem}
                </foreach>)
            )
        </if>
    </sql>
@@ -434,20 +481,38 @@
                  or (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
            AND (
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                (
                leavehospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="lvWardCodeItem" open="(" separator="," close=")">
                    #{lvWardCodeItem}
                </foreach>
                <!-- 延续护理任务(service_type='99')可以在患者还没出院时生成:
                     这类记录只有「在院维度」(hospitaldistrictcode / deptcode),出院维度是 NULL。
                     NULL 不匹配任何 IN,任务会「提示生成成功、列表里却查不到」,
                     所以出院维度为空时用同一套编码体系的在院维度兜底。 -->
                OR (service_type = '99' AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode = '')
                    AND hospitaldistrictcode IN
                    <foreach collection="leavehospitaldistrictcodes" item="lvWardCodeItem" open="(" separator="," close=")">
                        #{lvWardCodeItem}
                    </foreach>)
                )
            </if>
            <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
                      and (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
                OR
            </if>
            <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
                (
                leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
                    #{lvDeptCodeItem}
                </foreach>
                OR (service_type = '99' AND (leaveldeptcode IS NULL OR leaveldeptcode = '')
                    AND deptcode IN
                    <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
                        #{lvDeptCodeItem}
                    </foreach>)
                )
            </if>
            )
        </if>
@@ -684,6 +749,41 @@
        <if test="operatorNo != null  and operatorNo != ''">and operator_no = #{operatorNo}</if>
        <if test="wechat != null  and wechat != ''">and wechat = #{wechat}</if>
        <if test="hospType != null  and hospType != ''">and hosp_type = #{hospType}</if>
        <!--
          2026-11:重要性标记筛选(对应 service_subtask.importance_flag_code,建任务时取自
          pat_med_inhosp.importance_flag_code)。
            ① 模糊(/patItem 用这个)  importanceFlagCode    → LIKE '%值%'
               传「日间」即可命中「日间手术」「日间诊疗」「无,日间手术」多值行;
               传完整值(「日间手术」/「FH0165.01」)退化为精确匹配。
            ② 精确多值                 importanceFlagCodes → IN
          该列采集时就被拼成**描述串**(ImportFlagEnum 的 desc),多值用英文逗号拼接,
          所以「日间」这类跨取值查询只能靠模糊;描述与 FH0165.xx 编码两种口径都按原值比,
          不做值域归一。
          ⚠️ 同一条件必须同步加到 countByCondition(列表 total / 各状态统计走那条 SQL),
             否则会出现「列表筛出 N 行、总数还是全量」的错配。
        -->
        <if test="importanceFlagCode != null  and importanceFlagCode != ''">
            and importance_flag_code like concat('%', #{importanceFlagCode}, '%')
        </if>
        <if test="importanceFlagCodes != null and importanceFlagCodes.size() > 0">
            AND importance_flag_code IN
            <foreach collection="importanceFlagCodes" item="impFlagItem" open="(" separator="," close=")">
                #{impFlagItem}
            </foreach>
        </if>
        <!--
          2026-11:点位筛选(对应 service_subtask.point_location,建子任务时从 service_task 快照过来)。
          只有一条 IN 条件:单选/多选都在 ServiceSubtaskEntity#getPointLocations() 里归一
          (只传 pointLocation 会自动包成单元素集合),所以不再写等值分支。
          ⚠️ 同一条件必须同步加到 countByCondition(列表 total / 各状态统计走那条 SQL),
             否则会出现「列表筛出 N 行、总数还是全量」的错配。
        -->
        <if test="pointLocations != null and pointLocations.size() > 0">
            AND point_location IN
            <foreach collection="pointLocations" item="pointLocItem" open="(" separator="," close=")">
                #{pointLocItem}
            </foreach>
        </if>
        <if test="libtemplateid != null ">and libtemplateid = #{libtemplateid}</if>
        <if test="libtemplatename != null ">and libtemplatename = #{libtemplatename}</if>
        <if test="openid != null ">and openid = #{openid}</if>
@@ -1155,6 +1255,10 @@
            <if test="hospitaldistrictname != null  and hospitaldistrictname != ''">hospitaldistrictname,</if>
            <if test="deptcode != null  and deptcode != ''">deptcode,</if>
            <if test="deptname != null  and deptname != ''">deptname,</if>
            <!-- 2026-11:重要性标记编码(日间手术/日间诊疗…),建任务时从 pat_med_inhosp 同步 -->
            <if test="importanceFlagCode != null  and importanceFlagCode != ''">importance_flag_code,</if>
            <!-- 2026-11:点位编码,建子任务时从 service_task.point_location 同步 -->
            <if test="pointLocation != null  and pointLocation != ''">point_location,</if>
            <if test="labelstatus != null">labelstatus,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
@@ -1264,6 +1368,8 @@
            <if test="hospitaldistrictname != null  and hospitaldistrictname != ''">#{hospitaldistrictname},</if>
            <if test="deptcode != null  and deptcode != ''">#{deptcode},</if>
            <if test="deptname != null  and deptname != ''">#{deptname},</if>
            <if test="importanceFlagCode != null  and importanceFlagCode != ''">#{importanceFlagCode},</if>
            <if test="pointLocation != null  and pointLocation != ''">#{pointLocation},</if>
            <if test="labelstatus != null">#{labelstatus},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
@@ -1411,6 +1517,11 @@
            <if test="openid != null ">openid = #{openid},</if>
            <if test="longSendTime != null ">long_send_time = #{longSendTime},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</if>
            <!-- 完成时间不变量(2026-11):状态一旦不是"完成(6)",就显式清空完成时间与完成时状态。
                 上面 finishtime 那一段是 if(finishtime != null),传 null 会被整段跳过,
                 所以"清空"必须在这里用 SQL 的 NULL 写出来;否则 3 已发送 / 5 发送失败 / 7 超时
                 的行会一直带着历史完成时间("超时却有完成时间、及时率虚高"就是这么来的)。 -->
            <if test="sendstate != null and sendstate != 6">finishtime = NULL, finish_state = NULL,</if>
            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
            <if test="serviceType != null ">service_type = #{serviceType},</if>
            <if test="score != null ">score = #{score},</if>
@@ -1457,6 +1568,8 @@
            <if test="scheduleSeq != null ">schedule_seq = #{scheduleSeq},</if>
            <if test="loopCount != null ">loop_count = #{loopCount},</if>
            <if test="isManuallyCalled != null ">is_manually_called = #{isManuallyCalled},</if>
            <!-- 2026-11:点位编码(建子任务时的快照;任务上改点位不会自动回写) -->
            <if test="pointLocation != null  and pointLocation != ''">point_location = #{pointLocation},</if>
        </trim>
        where id = #{id}
    </update>
@@ -1536,6 +1649,11 @@
            <if test="openid != null ">openid = #{openid},</if>
            <if test="longSendTime != null ">long_send_time = #{longSendTime},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</if>
            <!-- 完成时间不变量(2026-11):状态一旦不是"完成(6)",就显式清空完成时间与完成时状态。
                 上面 finishtime 那一段是 if(finishtime != null),传 null 会被整段跳过,
                 所以"清空"必须在这里用 SQL 的 NULL 写出来;否则 3 已发送 / 5 发送失败 / 7 超时
                 的行会一直带着历史完成时间("超时却有完成时间、及时率虚高"就是这么来的)。 -->
            <if test="sendstate != null and sendstate != 6">finishtime = NULL, finish_state = NULL,</if>
            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
            <if test="serviceType != null ">service_type = #{serviceType},</if>
            <if test="score != null ">score = #{score},</if>
@@ -1594,6 +1712,11 @@
            <if test="sex != null">sex = #{sex},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</if>
            <!-- 完成时间不变量(2026-11):状态一旦不是"完成(6)",就显式清空完成时间与完成时状态。
                 上面 finishtime 那一段是 if(finishtime != null),传 null 会被整段跳过,
                 所以"清空"必须在这里用 SQL 的 NULL 写出来;否则 3 已发送 / 5 发送失败 / 7 超时
                 的行会一直带着历史完成时间("超时却有完成时间、及时率虚高"就是这么来的)。 -->
            <if test="sendstate != null and sendstate != 6">finishtime = NULL, finish_state = NULL,</if>
            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
            <if test="age != null">age = #{age},</if>
            <if test="sfzh != null">sfzh = #{sfzh},</if>
@@ -1707,6 +1830,11 @@
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</if>
            <!-- 完成时间不变量(2026-11):状态一旦不是"完成(6)",就显式清空完成时间与完成时状态。
                 上面 finishtime 那一段是 if(finishtime != null),传 null 会被整段跳过,
                 所以"清空"必须在这里用 SQL 的 NULL 写出来;否则 3 已发送 / 5 发送失败 / 7 超时
                 的行会一直带着历史完成时间("超时却有完成时间、及时率虚高"就是这么来的)。 -->
            <if test="sendstate != null and sendstate != 6">finishtime = NULL, finish_state = NULL,</if>
            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
            <if test="age != null">age = #{age},</if>
            <if test="sfzh != null">sfzh = #{sfzh},</if>
@@ -3649,6 +3777,25 @@
        <if test="sendname != null and sendname != ''">
            AND sendname LIKE CONCAT('%', #{sendname}, '%')
        </if>
        <!--
          2026-11:重要性标记模糊筛选 —— 必须与 serviceSubtaskWhere 里的同名条件【完全一致】,
          否则 /patItem 的列表与 total/各状态统计会错配(列表筛了、总数没筛)。
          口径见 serviceSubtaskWhere 处的注释。
        -->
        <if test="importanceFlagCode != null  and importanceFlagCode != ''">
            AND importance_flag_code LIKE CONCAT('%', #{importanceFlagCode}, '%')
        </if>
        <!--
          2026-11:点位筛选 —— 必须与 serviceSubtaskWhere 里的同名条件【完全一致】,
          否则 /patItem 的列表与 total/各状态统计会错配(列表筛了、总数没筛)。
          单选/多选归一在 ServiceSubtaskEntity#getPointLocations(),这里只有一条 IN。
        -->
        <if test="pointLocations != null and pointLocations.size() > 0">
            AND point_location IN
            <foreach collection="pointLocations" item="pointLocItem" open="(" separator="," close=")">
                #{pointLocItem}
            </foreach>
        </if>
        <if test="phone != null and phone != ''">
            AND phone = #{phone}
        </if>