陈昶聿
5 天以前 55d724ddd52b2acb6e7ee2d291e04e4b6d96ad19
smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml
@@ -23,10 +23,11 @@
        <result property="senduuid" column="senduuid"/>
        <result property="result" column="result"/>
        <result property="finishtime" column="finishtime"/>
        <result property="finishState" column="finish_state"/>
        <result property="recordid" column="recordid"/>
        <result property="exrecallcount" column="exrecallcount"/>
        <result property="pulltime" column="pulltime"/>
        <result property="deptcode" column="deptcode"/>
        <result property="leaveldeptcode" column="leaveldeptcode"/>
        <result property="labelstatus" column="labelstatus"/>
        <result property="delFlag" column="del_flag"/>
        <result property="updateBy" column="update_by"/>
@@ -53,6 +54,11 @@
        <result property="leaveicd10code" column="leaveicd10code"/>
        <result property="leavediagname" column="leavediagname"/>
        <result property="diagname" column="diagname"/>
        <result property="leaveldeptname" column="leaveldeptname"/>
        <!-- 2026-11:原 sendlimit* 四个空闲列复用为「在院维度」(hospitaldistrict* = 在院病区,deptcode/name = 在院科室) -->
        <result property="hospitaldistrictcode" column="hospitaldistrictcode"/>
        <result property="hospitaldistrictname" column="hospitaldistrictname"/>
        <result property="deptcode" column="deptcode"/>
        <result property="deptname" column="deptname"/>
        <result property="admindate" column="admindate"/>
        <result property="roomno" column="roomno"/>
@@ -72,6 +78,7 @@
        <result property="leavehospitaldistrictname" column="leavehospitaldistrictname"/>
        <result property="excep" column="excep"/>
        <result property="remark" column="remark"/>
        <result property="doctorOpinion" column="doctor_opinion"/>
        <result property="suggest" column="suggest"/>
        <result property="sendstate" column="sendstate"/>
        <result property="inhospid" column="inhospid"/>
@@ -80,6 +87,7 @@
        <result property="visitCount" column="visit_count"/>
        <result property="isVisitAgain" column="is_visit_again"/>
        <result property="upid" column="upid"/>
        <result property="visitType" column="visit_type"/>
        <result property="visitTime" column="visit_time"/>
        <result property="visitDeptName" column="visit_dept_name"/>
        <result property="visitDeptCode" column="visit_dept_code"/>
@@ -100,6 +108,7 @@
        <result property="loopCount" column="loop_count"/>
        <result property="isManuallyCalled" column="is_manually_called"/>
        <result property="campusid" column="campusid"/>
        <result property="diagType" column="diag_type"/>
    </resultMap>
    <resultMap type="com.smartor.domain.ServiceSubtaskCount" id="ServiceSubtaskResult2">
@@ -182,6 +191,7 @@
               long_send_time,
               openid,
               remark,
               doctor_opinion,
               service_type,
               libtemplateid,
               libtemplatename,
@@ -202,10 +212,11 @@
               senduuid,
               result,
               finishtime,
               finish_state,
               recordid,
               exrecallcount,
               pulltime,
               deptcode,
               leaveldeptcode,
               labelstatus,
               del_flag,
               update_by,
@@ -228,6 +239,10 @@
               sendname,
               roomno,
               admindate,
               leaveldeptname,
               hospitaldistrictcode,
               hospitaldistrictname,
               deptcode,
               deptname,
               stop_state,
               leavehospitaldistrictcode,
@@ -249,6 +264,69 @@
               task_situation,
               is_manually_called
        from service_subtask
    </sql>
    <!-- 超时完成、延期完成逻辑 完成时状态(finish_state)计算:仅当本次更新把 sendstate 置为 6(随访完成)时使用。
         基准截止时间 base:
           非任务组(appltype != 5):endtime + send_day(缺省1) + (Σ service_subtask_preachform.compensate_time + 1) 天;
           任务组(appltype = 5):按 schedule_benchmark 解析基准时间
                                   (1-入院 2-出院 3-就诊 4-入组 0/其它-自定义时间)
                                   + day_offset + (Σ compensate_time + 1) 天(不叠加 send_day);
         与 RyTask/RedisMqReceiver 的 isSubtaskDeadline、selectTimelyRateBatch 口径保持一致;
         delay = COALESCE(service_task.delay_finish_days, 0);
         finish_time = COALESCE(入参 finishtime, 库中 finishtime, NOW()):
           finish_time <= base                                    -> 1 正常完成
           delay > 0 且 finish_time <= base + delay               -> 2 延期完成
           其余(含 delay 为 0 或 NULL、或已超出延期窗口)          -> 3 超时完成
         finishtime 为空时用 NOW() 兜底,保证完成时一定写入状态;
         base 为空(endtime / schedule / day_offset 缺失)时保持原值不覆盖。 -->
    <sql id="finishDeadlineExpr">
        DATE_ADD(
        CASE
            WHEN COALESCE(st.appltype, '') = '5' THEN
                DATE_ADD(
                CASE sts.schedule_benchmark
                    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
                END,
                INTERVAL std.day_offset DAY)
            ELSE
                DATE_ADD(service_subtask.endtime, INTERVAL COALESCE(st.send_day, 1) DAY)
        END,
        INTERVAL (COALESCE((SELECT SUM(CAST(COALESCE(NULLIF(TRIM(pf.compensate_time), ''), '0') AS SIGNED))
        FROM service_subtask_preachform pf
        WHERE pf.subid = service_subtask.id AND pf.del_flag = 0), 0) + 1) DAY)
    </sql>
    <sql id="finishStateExpr">
        COALESCE((
        SELECT
        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
        AND COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
        &lt;= DATE_ADD(<include refid="finishDeadlineExpr"/>,
        INTERVAL COALESCE(st.delay_finish_days, 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)
    </sql>
    <!-- 当前执行方式:首次随访 current_preachform 为空时,取 preachform 的第一个执行方式兜底 -->
    <sql id="effectiveCurrentPreachformExpr">
        COALESCE(NULLIF(TRIM(current_preachform), ''), TRIM(SUBSTRING_INDEX(preachform, ',', 1)))
    </sql>
    <select id="selectServiceSubtaskBySendstate" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity"
@@ -291,6 +369,36 @@
        <if test="continueTimeNext != null ">and continue_time_next = #{continueTimeNext}</if>
    </select>
    <!-- 即将超时过滤:是否即将超时(soonTimeout=1) + 即将超时时间(soonTimeoutHours,单位小时)。
         截止时间口径与 RyTask/RedisMqReceiver 的 isSubtaskDeadline 保持一致:
         service_subtask.endtime + service_task.send_day(缺省1) + (Σ service_subtask_preachform.compensate_time + 1) 天;
         仅当存在有效补偿记录时才计算截止时间(无补偿记录不参与即将超时);
         仅统计“尚未超时、且在 N 小时内将要超时”的数据。 -->
    <sql id="soonTimeoutDeadlineExpr">
        (
        SELECT DATE_ADD(
        DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
        INTERVAL (COALESCE(pf.compensate_days, 0) + 1) DAY
        )
        FROM service_subtask ss
        LEFT JOIN service_task st ON st.taskid = ss.taskid AND st.del_flag = '0'
        INNER JOIN (
        SELECT subid,
        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
        FROM service_subtask_preachform
        WHERE del_flag = 0
        GROUP BY subid
        ) pf ON pf.subid = ss.id
        WHERE ss.id = service_subtask.id
        )
    </sql>
    <sql id="soonTimeoutFilter">
        <if test="soonTimeout != null and soonTimeout == 1">
            AND <include refid="soonTimeoutDeadlineExpr"/> &gt; NOW()
            AND <include refid="soonTimeoutDeadlineExpr"/> &lt;= DATE_ADD(NOW(), INTERVAL COALESCE(#{soonTimeoutHours}, 0) HOUR)
        </if>
    </sql>
    <select id="selectServiceSubtaskList" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity"
            resultMap="ServiceSubtaskResult">
@@ -398,7 +506,7 @@
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                AND deptcode IN
                AND leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
@@ -406,17 +514,21 @@
            </if>
        </if>
        <if test="deptOrDistrict==2">
            AND (1=1
            <if test="leavehospitaldistrictcodes != null">
                AND leavehospitaldistrictcode IN
            AND (
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                leavehospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                         separator=","
                         close=")">
                    #{leavehospitaldistrictcode}
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null">
                OR deptcode IN
            <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="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
@@ -472,8 +584,8 @@
        <if test="recordid != null ">and recordid = #{recordid}</if>
        <if test="exrecallcount != null ">and exrecallcount = #{exrecallcount}</if>
        <if test="pulltime != null ">and pulltime = #{pulltime}</if>
        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
        <if test="labelstatus != null ">and labelstatus = #{labelstatus}</if>
        <if test="isupload != null ">and isupload = #{isupload}</if>
        <if test="uploadTime != null ">and upload_time = #{uploadTime}</if>
@@ -481,7 +593,10 @@
        <if test="campusid != null  and campusid != ''">and campusid = #{campusid}</if>
        <if test="pid != null ">and pid = #{pid}</if>
        <if test="preachform != null ">and preachform = #{preachform}</if>
        <if test="currentPreachform != null ">and current_preachform = #{currentPreachform}</if>
        <if test="currentPreachform != null and currentPreachform != '' ">and current_preachform = #{currentPreachform}</if>
        <if test="currentPreachforms != null and currentPreachforms != ''">
            and FIND_IN_SET(<include refid="effectiveCurrentPreachformExpr"/>, #{currentPreachforms})
        </if>
        <if test="guid != null  and guid != ''">and guid = #{guid}</if>
        <if test="textParam != null  and textParam != ''">and text_param = #{textParam}</if>
        <if test="bedNo != null">and bed_no = #{bedNo}</if>
@@ -511,6 +626,7 @@
            AND visit_count > 1
        </if>
        <if test="remark != null">and remark = #{remark}</if>
        <if test="doctorOpinion != null and doctorOpinion != ''">and doctor_opinion like concat('%', #{doctorOpinion}, '%')</if>
        <if test="suggest != null">and suggest = #{suggest}</if>
        <if test="createBy != null">and create_by = #{createBy}</if>
        <if test="taskGuid != null">and task_guid = #{taskGuid}</if>
@@ -525,6 +641,7 @@
                #{visitDeptCode}
            </foreach>
        </if>
        <include refid="soonTimeoutFilter"/>
        <if test="sort != null  and sort==0">order by endtime asc</if>
        <if test="sort != null  and sort==1">order by endtime desc</if>
        <if test="sort != null  and sort==2">order by visit_time asc</if>
@@ -534,7 +651,9 @@
        <if test="sort != null  and sort==7">order by visit_time asc</if>
        <if test="sort != null  and sort==8">order by visit_time desc</if>
        <if test="sort != null  and sort==9">order by sendname asc</if>
        <if test="pageSize != null  and pageNum != null">limit ${pageSize} OFFSET ${pageNum}</if>
        <if test="pageSize != null  and pageNum != null">
            LIMIT ${pageSize} OFFSET ${pageNum}
        </if>
    </select>
    <select id="selectSatisfactionSubtaskList" parameterType="com.smartor.domain.VO.PatSatisfactionReqVO"
@@ -552,7 +671,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -624,7 +743,7 @@
        <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
        <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
        <if test="managementDoctor != null">and management_doctor = #{managementDoctor}</if>
        <if test="currentPreachform != null">and current_preachform = #{currentPreachform}</if>
        <if test="currentPreachform != null and currentPreachform != '' ">and current_preachform = #{currentPreachform}</if>
        <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
@@ -651,8 +770,8 @@
        <if test="recordid != null ">and recordid = #{recordid}</if>
        <if test="exrecallcount != null ">and exrecallcount = #{exrecallcount}</if>
        <if test="pulltime != null ">and pulltime = #{pulltime}</if>
        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
        <if test="labelstatus != null ">and labelstatus = #{labelstatus}</if>
        <if test="isupload != null ">and isupload = #{isupload}</if>
        <if test="uploadTime != null ">and upload_time = #{uploadTime}</if>
@@ -684,7 +803,7 @@
        <if test="visitTime != null ">and visit_time = #{visitTime}</if>
        <if test="visitDeptCode != null">and visit_dept_code = #{visitDeptCode}</if>
        <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
        <if test="currentPreachform != null">and current_preachform = #{currentPreachform}</if>
        <if test="currentPreachform != null and currentPreachform != '' ">and current_preachform = #{currentPreachform}</if>
        <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
        <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
        <if test="managementDoctor != null">and management_doctor like concat('%',#{managementDoctor}, '%')</if>
@@ -692,6 +811,7 @@
        <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
        <if test="continueCount != null ">and continue_count = #{continueCount}</if>
        <if test="continueTimeNext != null ">and continue_time_next = #{continueTimeNext}</if>
        <include refid="soonTimeoutFilter"/>
    </select>
    <select id="selectSendstateByCondition" parameterType="com.smartor.domain.ServiceSubtask" resultType="Integer">
@@ -727,8 +847,8 @@
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="serviceType != null and serviceType != ''">and service_type = #{serviceType}</if>
        <if test="serviceTypes != null and serviceTypes != ''">and service_type IN (#{serviceTypes})</if>
        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
        <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
        <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
            leavehospitaldistrictcode = #{leavehospitaldistrictcode}
@@ -738,6 +858,12 @@
        </if>
        <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
        <if test="managementDoctor != null">and management_doctor like concat('%',#{managementDoctor}, '%')</if>
        <if test="startOutHospTime != null">
            AND date_format(endtime,'%y%m%d') &gt;= date_format(#{startOutHospTime},'%y%m%d')
        </if>
        <if test="endOutHospTime != null">
            AND date_format(endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
        </if>
        ) a
    </select>
@@ -748,28 +874,60 @@
        </if>
        SUM(CASE WHEN ss.finishtime IS NOT NULL AND DATE(ss.visit_time) &lt;= DATE(ss.finishtime) THEN 1 ELSE 0 END) /
        COUNT(*) AS finishRate,
        <if test="rateDay != null">
            SUM(CASE WHEN ss.finishtime IS NOT NULL
            AND DATEDIFF(ss.finishtime, ss.endtime) &lt;= #{rateDay} + COALESCE(cp.compensate_days, 0)
            THEN 1 ELSE 0 END) / COUNT(*) AS rate
        </if>
        <if test="rateDay == null">
            NULL AS rate
        </if>
        <!-- 及时率基准:
             1) 非任务组(appltype != 5):endtime + send_day(缺省1) + Σ补偿天数 + 1 天;
             2) 任务组(appltype = 5):按 schedule_benchmark 解析基准时间
                (1-入院时间 2-出院时间 3-就诊时间 4-入组时间 0/其它-自定义时间)
                + day_offset + Σ补偿天数 + 1 天(不再叠加 send_day);
             完成时间 finishtime <= 基准时间计为及时,分母仍为应随访数。
             补偿天数只取 service_subtask_preachform.compensate_time 的汇总值一次,
             避免与补偿明细相加造成重复补偿或一对多放大分母。 -->
        SUM(CASE
            WHEN ss.finishtime IS NOT NULL
             AND COALESCE(st.appltype, '') != '5'
             AND ss.endtime IS NOT NULL
             AND ss.finishtime &lt;= DATE_ADD(
                   DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
                   INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY)
            THEN 1
            WHEN ss.finishtime IS NOT NULL
             AND st.appltype = '5'
             AND std.day_offset IS NOT NULL
             AND ss.finishtime &lt;= DATE_ADD(
                   DATE_ADD(
                       CASE sts.schedule_benchmark
                         WHEN 1 THEN ss.starttime
                         WHEN 2 THEN ss.endtime
                         WHEN 3 THEN ss.admindate
                         WHEN 4 THEN ss.create_time
                         ELSE sts.schedule_benchmark_custom_time
                       END, INTERVAL std.day_offset DAY),
                   INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY)
            THEN 1
            ELSE 0
        END) / COUNT(*) AS rate
        FROM service_subtask ss
        <if test="rateDay != null">
            <!-- 补偿时间:先按子任务聚合成一行再关联,避免一对多放大 COUNT(*) 分母。
                 compensate_time 是 varchar 且可能存成空串,需先置空再转数字 -->
            LEFT JOIN (
            SELECT subid,
            SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
            FROM service_subtask_preachform
            WHERE del_flag = 0
            GROUP BY subid
            ) cp ON cp.subid = ss.id
        </if>
        <!-- 这里按 taskid / 历史 schedule 快照关联,不按 del_flag 过滤:
             任务或计划被编辑后旧 schedule/detail 会置为 del_flag=1,
             但历史 subtask 仍引用旧 ID,过滤会丢失历史及时率基准。 -->
        LEFT JOIN service_task st
               ON st.taskid = ss.taskid
        LEFT JOIN service_task_schedule sts
               ON sts.id = ss.schedule_id
        LEFT JOIN service_task_schedule_detail std
               ON std.id = ss.schedule_detail_id
        <!-- 补偿时间:先按子任务聚合成一行再关联,避免一对多放大 COUNT(*) 分母。
             compensate_time 是 varchar 且可能存成空串,需先置空再转数字 -->
        LEFT JOIN (
        SELECT subid,
        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
        FROM service_subtask_preachform
        WHERE del_flag = 0
        GROUP BY subid
        ) cp ON cp.subid = ss.id
        WHERE ss.del_flag = 0
        AND ss.visit_time IS NOT NULL
        AND ss.sendstate != 4
        <if test="starttime == null and endtime==null">
            AND DATE(ss.visit_time) &lt; DATE(NOW())
        </if>
@@ -779,8 +937,8 @@
        </if>
        <if test="orgid != null and orgid != ''">AND ss.orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and ss.campusid = #{campusid}</if>
        <if test="deptcode != null  and deptcode != ''">and ss.deptcode = #{deptcode}</if>
        <if test="deptname != null  and deptname != ''">and ss.deptname = #{deptname}</if>
        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and ss.leaveldeptcode = #{leaveldeptcode}</if>
        <if test="leaveldeptname != null  and leaveldeptname != ''">and ss.leaveldeptname = #{leaveldeptname}</if>
        <if test="isabnormal != null">and ss.isabnormal = #{isabnormal}</if>
        <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
            ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
@@ -802,6 +960,12 @@
                     close=")">
                #{serviceType}
            </foreach>
        </if>
        <if test="startOutHospTime != null">
            AND date_format(ss.endtime,'%y%m%d') &gt;= date_format(#{startOutHospTime},'%y%m%d')
        </if>
        <if test="endOutHospTime != null">
            AND date_format(ss.endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
        </if>
        <if test="groupKey != null and groupKey != ''">
            AND ss.${groupKey} IN
@@ -836,8 +1000,8 @@
        <if test="serviceType != null and serviceType != ''">and service_type = #{serviceType}</if>
        <if test="serviceTypes != null and serviceTypes != ''">and service_type IN (#{serviceTypes})</if>
        <if test="isabnormal != null ">and isabnormal = #{isabnormal}</if>
        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
        <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
            leavehospitaldistrictcode = #{leavehospitaldistrictcode}
        </if>
@@ -846,6 +1010,12 @@
        </if>
        <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
        <if test="managementDoctor != null">and management_doctor like concat('%',#{managementDoctor}, '%')</if>
        <if test="startOutHospTime != null">
            AND date_format(endtime,'%y%m%d') &gt;= date_format(#{startOutHospTime},'%y%m%d')
        </if>
        <if test="endOutHospTime != null">
            AND date_format(endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
        </if>
        ) a where a.finished =1)
    </select>
@@ -899,10 +1069,15 @@
            <if test="senduuid != null">senduuid,</if>
            <if test="result != null">result,</if>
            <if test="finishtime != null">finishtime,</if>
            <if test="finishState != null">finish_state,</if>
            <if test="recordid != null">recordid,</if>
            <if test="exrecallcount != null">exrecallcount,</if>
            <if test="pulltime != null">pulltime,</if>
            <if test="deptcode != null">deptcode,</if>
            <if test="leaveldeptcode != null">leaveldeptcode,</if>
            <if test="hospitaldistrictcode != null  and hospitaldistrictcode != ''">hospitaldistrictcode,</if>
            <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="labelstatus != null">labelstatus,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
@@ -927,7 +1102,7 @@
            <if test="wechat != null  and wechat != ''">wechat,</if>
            <if test="roomno != null  and roomno != ''">roomno,</if>
            <if test="admindate != null">admindate,</if>
            <if test="deptname != null  and deptname != ''">deptname,</if>
            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname,</if>
            <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code,</if>
            <if test="leavediagname != null  and leavediagname != ''">leavediagname,</if>
            <if test="libtemplateid != null ">libtemplateid,</if>
@@ -952,6 +1127,7 @@
            <if test="nurseName != null ">nurse_name,</if>
            <if test="nurseId != null ">nurse_id,</if>
            <if test="remark != null ">remark,</if>
            <if test="doctorOpinion != null ">doctor_opinion,</if>
            <if test="suggest != null ">suggest,</if>
            <if test="sendstate != null ">sendstate,</if>
            <if test="inhospid != null ">inhospid,</if>
@@ -966,7 +1142,7 @@
            <if test="visitDeptName != null">visit_dept_name,</if>
            <if test="managementDoctorCode != null">management_doctor_code,</if>
            <if test="managementDoctor != null">management_doctor,</if>
            <if test="currentPreachform != null">current_preachform,</if>
            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform,</if>
            <if test="taskSituation != null">task_situation,</if>
            <if test="isabnormal != null">isabnormal,</if>
            <if test="continueFlag != null ">continue_flag,</if>
@@ -997,10 +1173,15 @@
            <if test="senduuid != null">#{senduuid},</if>
            <if test="result != null">#{result},</if>
            <if test="finishtime != null">#{finishtime},</if>
            <if test="finishState != null">#{finishState},</if>
            <if test="recordid != null">#{recordid},</if>
            <if test="exrecallcount != null">#{exrecallcount},</if>
            <if test="pulltime != null">#{pulltime},</if>
            <if test="deptcode != null">#{deptcode},</if>
            <if test="leaveldeptcode != null">#{leaveldeptcode},</if>
            <if test="hospitaldistrictcode != null  and hospitaldistrictcode != ''">#{hospitaldistrictcode},</if>
            <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="labelstatus != null">#{labelstatus},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
@@ -1025,7 +1206,7 @@
            <if test="wechat != null  and wechat != ''">#{wechat},</if>
            <if test="roomno != null  and roomno != ''">#{roomno},</if>
            <if test="admindate != null ">#{admindate},</if>
            <if test="deptname != null  and deptname != ''">#{deptname},</if>
            <if test="leaveldeptname != null  and leaveldeptname != ''">#{leaveldeptname},</if>
            <if test="leaveicd10code != null  and leaveicd10code != ''">#{leaveicd10code},</if>
            <if test="leavediagname != null  and leavediagname != ''">#{leavediagname},</if>
            <if test="libtemplateid != null ">#{libtemplateid},</if>
@@ -1050,6 +1231,7 @@
            <if test="nurseName != null ">#{nurseName},</if>
            <if test="nurseId != null ">#{nurseId},</if>
            <if test="remark != null ">#{remark},</if>
            <if test="doctorOpinion != null ">#{doctorOpinion},</if>
            <if test="suggest != null ">#{suggest},</if>
            <if test="sendstate != null ">#{sendstate},</if>
            <if test="inhospid != null ">#{inhospid},</if>
@@ -1064,7 +1246,7 @@
            <if test="visitDeptName != null">#{visitDeptName},</if>
            <if test="managementDoctorCode != null">#{managementDoctorCode},</if>
            <if test="managementDoctor != null">#{managementDoctor},</if>
            <if test="currentPreachform != null">#{currentPreachform},</if>
            <if test="currentPreachform != null and currentPreachform != '' ">#{currentPreachform},</if>
            <if test="taskSituation != null">#{taskSituation},</if>
            <if test="isabnormal != null">#{isabnormal},</if>
            <if test="continueFlag != null ">#{continueFlag},</if>
@@ -1080,6 +1262,15 @@
            <if test="isManuallyCalled != null ">#{isManuallyCalled},</if>
        </trim>
    </insert>
    <!-- 二次随访幂等:仅当 is_visit_again 不是目标值时更新,返回受影响行数 -->
    <update id="updateIsVisitAgainById">
        update service_subtask
        set is_visit_again = #{isVisitAgain},
            update_time = now()
        where id = #{id}
          and (is_visit_again is null or is_visit_again &lt;&gt; #{isVisitAgain})
    </update>
    <update id="updateServiceSubtask" parameterType="com.smartor.domain.ServiceSubtask">
        update service_subtask
@@ -1103,7 +1294,7 @@
            <if test="recordid != null">recordid = #{recordid},</if>
            <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
            <if test="pulltime != null">pulltime = #{pulltime},</if>
            <if test="deptcode != null">deptcode = #{deptcode},</if>
            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
            <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1128,7 +1319,7 @@
            <if test="wechat != null  and wechat != ''">wechat = #{wechat},</if>
            <if test="roomno != null  and roomno != ''">roomno = #{roomno},</if>
            <if test="admindate != null ">admindate = #{admindate},</if>
            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
            <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
            <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
            <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1137,6 +1328,7 @@
            <if test="openid != null ">openid = #{openid},</if>
            <if test="longSendTime != null ">long_send_time = #{longSendTime},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</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>
            <if test="submit != null ">submit = #{submit},</if>
@@ -1154,6 +1346,7 @@
            <if test="nurseName != null ">nurse_name=#{nurseName},</if>
            <if test="nurseId != null ">nurse_id=#{nurseId},</if>
            <if test="remark != null ">remark=#{remark},</if>
            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
            <if test="suggest != null ">suggest=#{suggest},</if>
            <if test="inhospid != null ">inhospid=#{inhospid},</if>
            <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1167,7 +1360,7 @@
            <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
            <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
            <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
            <if test="taskSituation != null">task_situation=#{taskSituation},</if>
            <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
            <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1183,6 +1376,25 @@
            <if test="isManuallyCalled != null ">is_manually_called = #{isManuallyCalled},</if>
        </trim>
        where id = #{id}
    </update>
    <!-- 微信公众号已领取未填写按默认选项完成:只更新当前子任务。
         条件 sendstate=1 + 未提交,保证只处理当前被领取且未填写的子任务,不波及其他任务。 -->
    <update id="updateWechatDefaultComplete" parameterType="com.smartor.domain.ServiceSubtask">
        update service_subtask
        set sendstate = 6,
            finishtime = #{finishtime},
            result = #{result},
            remark = case
                         when remark is null or remark = '' then #{remark}
                         else concat(remark, ',', #{remark})
                     end,
            update_time = now(),
            finish_state = <include refid="finishStateExpr"/>
        where id = #{id}
          and orgid = #{orgid}
          and sendstate = 1
          and (submit is null or submit != 1)
    </update>
    <update id="updateServiceSubtaskByPatId" parameterType="com.smartor.domain.ServiceSubtask">
@@ -1207,7 +1419,7 @@
            <if test="recordid != null">recordid = #{recordid},</if>
            <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
            <if test="pulltime != null">pulltime = #{pulltime},</if>
            <if test="deptcode != null">deptcode = #{deptcode},</if>
            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
            <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1232,7 +1444,7 @@
            <if test="wechat != null  and wechat != ''">wechat = #{wechat},</if>
            <if test="roomno != null  and roomno != ''">roomno = #{roomno},</if>
            <if test="admindate != null">admindate = #{admindate},</if>
            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
            <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
            <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
            <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1241,6 +1453,7 @@
            <if test="openid != null ">openid = #{openid},</if>
            <if test="longSendTime != null ">long_send_time = #{longSendTime},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</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>
            <if test="visitCount != null ">visit_count = #{visitCount},</if>
@@ -1259,6 +1472,7 @@
            <if test="nurseName != null ">nurse_name=#{nurseName},</if>
            <if test="nurseId != null ">nurse_id=#{nurseId},</if>
            <if test="remark != null ">remark=#{remark},</if>
            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
            <if test="suggest != null ">suggest=#{suggest},</if>
            <if test="inhospid != null ">inhospid=#{inhospid},</if>
            <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1271,7 +1485,7 @@
            <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
            <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
            <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
            <if test="taskSituation != null">task_situation=#{taskSituation},</if>
            <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
            <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1296,6 +1510,7 @@
            <if test="sex != null">sex = #{sex},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</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>
            <if test="addr != null">addr = #{addr},</if>
@@ -1311,7 +1526,7 @@
            <if test="recordid != null">recordid = #{recordid},</if>
            <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
            <if test="pulltime != null">pulltime = #{pulltime},</if>
            <if test="deptcode != null">deptcode = #{deptcode},</if>
            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
            <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1338,7 +1553,7 @@
            <if test="wechat != null  and wechat != ''">wechat = #{wechat},</if>
            <if test="roomno != null  and roomno != ''">roomno = #{roomno},</if>
            <if test="admindate != null">admindate = #{admindate},</if>
            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
            <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
            <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
            <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1363,6 +1578,7 @@
            <if test="nurseName != null ">nurse_name=#{nurseName},</if>
            <if test="nurseId != null ">nurse_id=#{nurseId},</if>
            <if test="remark != null ">remark=#{remark},</if>
            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
            <if test="suggest != null ">suggest=#{suggest},</if>
            <if test="inhospid != null ">inhospid=#{inhospid},</if>
            <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1374,7 +1590,7 @@
            <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
            <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
            <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
            <if test="taskSituation != null">task_situation=#{taskSituation},</if>
            <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
            <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1407,6 +1623,7 @@
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="sendstate != null ">sendstate = #{sendstate},</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>
            <if test="addr != null">addr = #{addr},</if>
@@ -1422,7 +1639,7 @@
            <if test="recordid != null">recordid = #{recordid},</if>
            <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
            <if test="pulltime != null">pulltime = #{pulltime},</if>
            <if test="deptcode != null">deptcode = #{deptcode},</if>
            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
            <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1449,7 +1666,7 @@
            <if test="admindate != null ">admindate = #{admindate},</if>
            <if test="visitCount != null ">visit_count = #{visitCount},</if>
            <if test="isVisitAgain != null ">is_visit_again = #{isVisitAgain},</if>
            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
            <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
            <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
            <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1474,6 +1691,7 @@
            <if test="nurseName != null ">nurse_name=#{nurseName},</if>
            <if test="nurseId != null ">nurse_id=#{nurseId},</if>
            <if test="remark != null ">remark=#{remark},</if>
            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
            <if test="suggest != null ">suggest=#{suggest},</if>
            <if test="inhospid != null ">inhospid=#{inhospid},</if>
            <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1485,7 +1703,7 @@
            <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
            <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
            <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
            <if test="taskSituation != null">task_situation=#{taskSituation},</if>
            <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
            <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1515,6 +1733,35 @@
        where patid = #{patid} and taskid = #{taskid}
    </update>
    <!-- 人工随访:软删除患者出院记录对应的未完成随访任务 -->
    <update id="deleteUnfinishedServiceSubtaskByInhospids">
        update service_subtask
        set del_flag = 1
        where del_flag = 0
          and patid = #{patid}
          and sendstate in
          <foreach collection="sendstates" item="sendstate" open="(" separator="," close=")">
              #{sendstate}
          </foreach>
          and inhospid in
          <foreach collection="inhospids" item="inhospid" open="(" separator="," close=")">
              #{inhospid}
          </foreach>
    </update>
    <!-- 人工随访重复校验:同一患者同一任务下是否已有未完成随访任务 -->
    <select id="countUnfinishedServiceSubtaskByPatidAndTaskid" resultType="int">
        select count(1)
        from service_subtask
        where del_flag = 0
          and patid = #{patid}
          and taskid = #{taskid}
          and sendstate in
          <foreach collection="sendstates" item="sendstate" open="(" separator="," close=")">
              #{sendstate}
          </foreach>
    </select>
    <select id="getSfStatistics" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskResult">
        select id,
@@ -1528,17 +1775,21 @@
        management_doctor,
        leavehospitaldistrictcode,
        leavehospitaldistrictname,
        deptcode,
        deptname,
        leaveldeptcode,
        leaveldeptname,
        sendstate,
        preachform,
        current_preachform,
        <include refid="effectiveCurrentPreachformExpr"/> AS current_preachform,
        excep,
        nurse_id,
        nurse_name,
        taskid,
        task_name,
        <if test="groupKey != null and groupKey == 'taskid'">
            (SELECT st.diag_type FROM service_task st WHERE st.taskid = service_subtask.taskid LIMIT 1) AS diag_type,
        </if>
        visit_time,
        visit_dept_code,
        finishtime,
        endtime,
        senddate,
@@ -1556,7 +1807,9 @@
        continue_content,
        continue_count,
        continue_time_next,
        task_situation
        task_situation,
        finish_state,
        is_manually_called
        from service_subtask
        where 1=1
        and del_flag = 0
@@ -1574,10 +1827,17 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
            </foreach>
        </if>
        <if test="taskIds != null and taskIds.size() > 0">
            AND taskid IN
            <foreach collection="taskIds" item="taskId" open="(" separator=","
                     close=")">
                #{taskId}
            </foreach>
        </if>
        <if test="serviceType != null and serviceType.size() > 0">
@@ -1692,7 +1952,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND b.deptcode IN
            AND b.leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -1757,10 +2017,17 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
            </foreach>
        </if>
        <if test="taskIds != null and taskIds.size() > 0">
            AND taskid IN
            <foreach collection="taskIds" item="taskId" open="(" separator=","
                     close=")">
                #{taskId}
            </foreach>
        </if>
        <if test="serviceType != null and serviceType.size() > 0">
@@ -1798,9 +2065,6 @@
        <if test="groupKey != null and groupKey != ''">
            group by ${groupKey}
        </if>
        <if test="pageSize != null  and pageNum != null">
            limit ${pageSize} OFFSET ${pageNum}
        </if>
    </select>
    <select id="getSfStatisticsHyperlink" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
@@ -1814,17 +2078,19 @@
        drname,
        leavehospitaldistrictcode,
        leavehospitaldistrictname,
        deptcode,
        deptname,
        leaveldeptcode,
        leaveldeptname,
        sendstate,
        preachform,
        current_preachform,
        <include refid="effectiveCurrentPreachformExpr"/> AS current_preachform,
        excep,
        nurse_id,
        nurse_name,
        taskid,
        task_name,
        visit_time,
        visit_dept_code,
        visit_dept_name,
        finishtime,
        endtime,
        senddate,
@@ -1843,7 +2109,10 @@
        continue_count,
        continue_time_next,
        task_situation,
        remark
        remark,
        doctor_opinion,
        finish_state,
        is_manually_called
        from service_subtask
        where 1=1
        and del_flag = 0
@@ -1861,7 +2130,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -1878,6 +2147,10 @@
            AND visit_time >= DATE(#{startTime})
            AND visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
        </if>
        <if test="hospitalStartTime != null and hospitalEndTime!=null">
            AND endtime >= DATE(#{hospitalStartTime})
            AND endtime &lt; DATE_ADD(DATE(#{hospitalEndTime}), INTERVAL 1 DAY)
        </if>
        <if test="visitDeptCode != null">and visit_dept_code = #{visitDeptCode}</if>
        <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
        <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
@@ -1889,10 +2162,15 @@
        <if test="type != null">
            and type = #{type}
        </if>
        <if test="visitCount != null and visitCount == 1">
        <!-- 首个再次随访:精确查 visit_count = 2,与统计口径一致 -->
        <if test="hyperLinkInfoType == 'againFollowUpPushCountInfo'">
            AND visit_count = 2
        </if>
        <!-- 其他类型保持原语义:1 首次,>1 再次 -->
        <if test="hyperLinkInfoType != 'againFollowUpPushCountInfo' and visitCount != null and visitCount == 1">
            AND visit_count = 1
        </if>
        <if test="visitCount != null and visitCount > 1">
        <if test="hyperLinkInfoType != 'againFollowUpPushCountInfo' and visitCount != null and visitCount > 1">
            AND visit_count > 1
        </if>
        <if test="groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
@@ -1909,8 +2187,8 @@
                #{state}
            </foreach>
        </if>
        <if test="currentPreachform != null">
            AND current_preachform = #{currentPreachform}
        <if test="currentPreachform != null and currentPreachform != '' ">
            AND <include refid="effectiveCurrentPreachformExpr"/> = #{currentPreachform}
            <if test="followUpCountStyle != null and followUpCountStyle = '1'">
                AND sendstate != 4
            </if>
@@ -1921,6 +2199,9 @@
        <if test="taskSituation != null">
            AND task_situation = #{taskSituation}
        </if>
        <if test="finishState != null">
            AND finish_state = #{finishState}
        </if>
        <if test="drcode != null and drcode != ''">
            AND drcode = #{drcode}
        </if>
@@ -1928,7 +2209,13 @@
            AND leavehospitaldistrictcode = #{leavehospitaldistrictcode}
        </if>
        <if test="deptcode != null and deptcode != ''">
            AND deptcode = #{deptcode}
            AND leaveldeptcode = #{deptcode}
        </if>
        <if test="isManuallyCalled != null">
            AND is_manually_called = #{isManuallyCalled}
        </if>
        <if test="suggest != null and suggest != ''">
            AND TRIM(suggest) = #{suggest}
        </if>
        order by endtime desc
        <if test="pageSize != null  and pageNum != null">
@@ -1959,7 +2246,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -2006,7 +2293,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -2042,7 +2329,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -2099,7 +2386,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND t.deptcode IN
            AND t.leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -2142,14 +2429,14 @@
    <select id="getSfStatisticsScriptDetails" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskScriptCountResult">
        SELECT
        a.id as scriptid,
        a.templateid as templateid,
        a.script_type,
        MIN(a.id) as scriptid,
        MIN(a.templateid) as templateid,
        MIN(a.script_type) as script_type,
        a.script_content,
        b.subtask_type,
        b.completed_quantity,
        b.all_quantity,
        ROUND(b.completed_quantity/b.all_quantity,2) as completed_percentage
        SUM(b.completed_quantity) as completed_quantity,
        SUM(b.all_quantity) as all_quantity,
        ROUND(SUM(b.completed_quantity)/SUM(b.all_quantity),2) as completed_percentage
        FROM
        <if test="type == 1">
            ivr_task_template_script a
@@ -2185,7 +2472,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND ss.deptcode IN
            AND ss.leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -2212,25 +2499,37 @@
        group by ssd.scriptid) b
        ON a.id = b.scriptid
        <!-- 适用单条查询 -->
        <if test="scriptids != null">
        <if test="scriptids != null and scriptids != ''">
            AND a.scriptid in
            (SELECT lib_script.scriptid FROM
            svy_lib_template_script lib_script
            WHERE lib_script.scriptid in (${scriptids}))
        </if>
        <!-- 同一道题会被复制到多个任务模板,按题目内容合并,保证一道题只返回一条 -->
        GROUP BY a.script_content, b.subtask_type
        ORDER BY MIN(a.id)
    </select>
    <select id="getSfStatisticsScriptItemDetails" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskScriptCountResult">
        SELECT
        ssd.scriptid,
        a.script_content,
        ss.type as subtask_type,
        IFNULL(ssd.matchedtext,ssd.asrtext) AS option_text,
        COUNT(*) AS chosen_quantity
        FROM
        service_subtask_detail ssd
        INNER JOIN service_subtask ss ON ssd.sub_id = ss.id
        INNER JOIN
        <if test="type == 1">
            ivr_task_template_script a ON a.id = ssd.scriptid
        </if>
        <if test="type == 2">
            svy_task_template_script a ON a.id = ssd.scriptid
        </if>
        where 1=1
        and ss.del_flag=0
        and ssd.del_flag=0
        <if test="orgid != null and orgid != ''">
            and ss.orgid = #{orgid}
        </if>
@@ -2245,7 +2544,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND ss.deptcode IN
            AND ss.leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -2269,7 +2568,137 @@
        <if test="configValue != null and configValue != ''">
            AND ssd.categoryid IN (${configValue})
        </if>
        group by ssd.scriptid, IFNULL(ssd.matchedtext,ssd.asrtext)
        group by a.script_content, ss.type, IFNULL(ssd.matchedtext,ssd.asrtext)
    </select>
    <!--
      问题选项数字下钻:返回选择了指定选项的 service_subtask 列表。
      过滤口径与 getSfStatisticsScriptDetails / getSfStatisticsScriptItemDetails 保持一致,
      额外支持 statisticaltype:1-病区、2-科室、3-病种。
    -->
    <select id="getSfStatisticsScriptDetailsHyperlink" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskResult">
        SELECT ss.*
        FROM service_subtask ss
        WHERE ss.del_flag = 0
        <if test="orgid != null and orgid != ''">
            AND ss.orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND ss.campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND ss.leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                     close=")">
                #{leavehospitaldistrictcode}
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND ss.leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator="," close=")">
                #{deptcode}
            </foreach>
        </if>
        <if test="serviceType != null and serviceType.size() > 0">
            AND ss.service_type IN
            <foreach collection="serviceType" item="serviceType" open="(" separator="," close=")">
                #{serviceType}
            </foreach>
        </if>
        <if test="startTime != null and endTime != null">
            AND ss.visit_time >= DATE(#{startTime})
            AND ss.visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
        </if>
        <if test="type != null and type != ''">
            AND ss.type = #{type}
        </if>
        <!-- 统计维度:1-按病区 2-按科室 3-按taskid -->
        <if test="statisticaltype != null and statisticaltype == 1">
            <choose>
                <when test="deptcode != null and deptcode != ''">
                    AND ss.leavehospitaldistrictcode = #{deptcode}
                </when>
                <when test="leavehospitaldistrictcode != null and leavehospitaldistrictcode != ''">
                    AND ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
                </when>
            </choose>
        </if>
        <if test="statisticaltype != null and statisticaltype == 2 and deptcode != null and deptcode != ''">
            AND ss.leaveldeptcode = #{deptcode}
        </if>
        <if test="statisticaltype != null and statisticaltype == 3 and taskId != null">
            AND ss.taskid = #{taskId}
        </if>
        <!-- 兼容不传 statisticaltype 时按单值条件过滤 -->
        <if test="statisticaltype == null">
            <if test="leavehospitaldistrictcode != null and leavehospitaldistrictcode != ''">
                AND ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
            </if>
            <if test="deptcode != null and deptcode != ''">
                AND ss.leaveldeptcode = #{deptcode}
            </if>
            <if test="taskId != null">
                AND ss.taskid = #{taskId}
            </if>
        </if>
        <!-- 题目 + 选项匹配:只保留回答中选择了该选项的子任务 -->
        AND EXISTS (
            SELECT 1
            FROM service_subtask_detail ssd
            WHERE ssd.sub_id = ss.id
              AND ssd.del_flag = 0
            <if test="configValue != null and configValue != ''">
                AND ssd.categoryid IN (${configValue})
            </if>
            <choose>
                <when test="optionText != null">
                    AND IFNULL(ssd.matchedtext, ssd.asrtext) = #{optionText}
                </when>
                <otherwise>
                    AND 1 = 0
                </otherwise>
            </choose>
            <choose>
                <!-- 优先按题目内容匹配:同一道题复制到多个任务模板时 scriptid 不同,但 script_content 相同 -->
                <when test="scriptContent != null and scriptContent != ''">
                    AND ssd.scriptid IN (
                        <choose>
                            <when test="type == '1'">
                                SELECT its.id FROM ivr_task_template_script its
                                WHERE its.script_content = #{scriptContent}
                            </when>
                            <when test="type == '2'">
                                SELECT sts.id FROM svy_task_template_script sts
                                WHERE sts.script_content = #{scriptContent}
                            </when>
                            <otherwise>
                                SELECT its.id FROM ivr_task_template_script its
                                WHERE its.script_content = #{scriptContent}
                                UNION
                                SELECT sts.id FROM svy_task_template_script sts
                                WHERE sts.script_content = #{scriptContent}
                            </otherwise>
                        </choose>
                    )
                </when>
                <when test="scriptid != null">
                    AND ssd.scriptid = #{scriptid}
                </when>
                <otherwise>
                    AND 1 = 0
                </otherwise>
            </choose>
        )
        ORDER BY ss.endtime DESC, ss.id DESC
        <if test="pageSize != null and pageSize > 0">
            LIMIT #{pageSize}
            <if test="pageNum != null and pageNum >= 0">
                OFFSET #{pageNum}
            </if>
        </if>
    </select>
    <select id="getDataByTime" resultMap="ServiceSubtaskResult">
@@ -2298,7 +2727,7 @@
               recordid,
               exrecallcount,
               pulltime,
               deptcode,
               leaveldeptcode,
               labelstatus,
               del_flag,
               upid,
@@ -2328,7 +2757,7 @@
               admindate,
               patid,
               service_type,
               deptname,
               leaveldeptname,
               score,
               drname,
               drcode,
@@ -2340,6 +2769,7 @@
               nurse_id,
               nurse_name,
               remark,
               doctor_opinion,
               inhospid,
               patfrom,
               task_guid,
@@ -2387,7 +2817,7 @@
    </select>
    <select id="getDeptRanking" parameterType="com.smartor.domain.PatMedReq" resultType="com.smartor.domain.PatMedRes">
        SELECT deptname,
        SELECT leaveldeptname AS deptname,
        count(1) AS rc
        FROM service_subtask
        WHERE DATE_FORMAT(visit_time, '%Y-%m-%d') >= #{startDate}
@@ -2400,7 +2830,7 @@
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        GROUP BY deptname
        GROUP BY leaveldeptname
    </select>
    <!-- 服务统计查询 - 按时间维度统计出院/门诊随访数据 -->
@@ -2451,7 +2881,7 @@
            AND campusid = #{campusid}
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator="," close=")">
                #{deptcode}
            </foreach>
@@ -2584,7 +3014,10 @@
        SUM(CASE WHEN sendstate != 4 THEN 1 ELSE 0 END) AS xsf,
        SUM(CASE WHEN (sendstate = 1 OR sendstate = 2 OR sendstate = 3 OR sendstate = 5 OR sendstate = 7 ) THEN 1 ELSE 0
        END) AS dsf,
        SUM(CASE WHEN sendstate != 2 THEN 1 ELSE 0 END) AS yfs,
        SUM(CASE WHEN sendstate = 2 THEN 1 ELSE 0 END) AS wfs,
        SUM(CASE WHEN sendstate = 6 THEN 1 ELSE 0 END) AS ywc,
        SUM(CASE WHEN sendstate = 7 THEN 1 ELSE 0 END) AS cs,
        SUM(CASE WHEN excep IS NOT NULL AND excep = '1' THEN 1 ELSE 0 END) AS yc,
        SUM(CASE WHEN excep IS NOT NULL AND excep = '2' THEN 1 ELSE 0 END) AS jg,
        COUNT(1) as total
@@ -2613,7 +3046,7 @@
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                AND deptcode IN
                AND leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
@@ -2621,17 +3054,21 @@
            </if>
        </if>
        <if test="deptOrDistrict==2">
            AND (1=1
            AND (
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                AND leavehospitaldistrictcode IN
                leavehospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                         separator=","
                         close=")">
                    #{leavehospitaldistrictcode}
                </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">
                OR deptcode IN
                leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
@@ -2734,14 +3171,14 @@
                </foreach>
            </if>
            <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
                AND deptcode IN
                AND leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator="," close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>
        </if>
        <if test="deptcode != null and deptcode != ''">
            AND deptcode = #{deptcode}
        <if test="leaveldeptcode != null and leaveldeptcode != ''">
            AND leaveldeptcode = #{leaveldeptcode}
        </if>
        <if test="sendstate != null">
            AND sendstate = #{sendstate}
@@ -2751,6 +3188,9 @@
            <foreach collection="sendstates" item="state" open="(" separator="," close=")">
                #{state}
            </foreach>
        </if>
        <if test="currentPreachforms != null and currentPreachforms != ''">
            AND FIND_IN_SET(<include refid="effectiveCurrentPreachformExpr"/>, #{currentPreachforms})
        </if>
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
@@ -2802,7 +3242,7 @@
            AND date_format( visit_time, '%y%m%d' ) &lt;= date_format(#{endDate},'%y%m%d')
        </if>
        <if test="deptcodeList != null and deptcodeList.size()>0">
            and deptcode in
            and leaveldeptcode in
            <foreach collection="deptcodeList" item="deptcode" open="(" separator="," close=")">
                #{deptcode}
            </foreach>
@@ -2817,32 +3257,73 @@
        COUNT(CASE WHEN sendstate = 5 THEN 1 END) AS failedVisitCount,
        SUM(CASE WHEN excep IS NOT NULL AND excep = '1' THEN 1 ELSE 0 END) AS abnormalVisitCount,
        SUM(CASE WHEN excep IS NOT NULL AND excep = '2' THEN 1 ELSE 0 END) AS warnningVisitCount,
        COUNT(*) AS allVisitCount
        FROM service_subtask
        WHERE del_flag = '0'
        AND service_type = '2'
        AND visit_count = 1
        COUNT(*) AS allVisitCount,
        <choose>
            <when test="soonTimeoutHours != null and soonTimeoutHours > 0">
                COUNT(CASE WHEN t.soon_deadline &gt; NOW()
                AND t.soon_deadline &lt;= DATE_ADD(NOW(), INTERVAL #{soonTimeoutHours} HOUR)
                THEN 1 END) AS soonTimeoutCount
            </when>
            <otherwise>
                0 AS soonTimeoutCount
            </otherwise>
        </choose>
        FROM (
        SELECT ss.*,
        <!-- 即将超时截止时间 = 出院时间(endtime) + 发送日(send_day, 缺省1) + 补偿天数(compensate_time 求和后再 +1 天),
             与 RyTask/RedisMqReceiver 的 isSubtaskDeadline 口径保持一致;
             无有效补偿记录(cp.subid 为空)时截止时间为 NULL,不参与即将超时统计 -->
        CASE
        WHEN cp.subid IS NULL THEN NULL
        ELSE DATE_ADD(
        DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
        INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY
        )
        END AS soon_deadline
        FROM service_subtask ss
        LEFT JOIN service_task st ON st.taskid = ss.taskid AND st.del_flag = '0'
        <!-- 补偿时间:先按子任务聚合成一行再关联,避免一对多放大计数;
             compensate_time 是 varchar 且可能存成空串,需先置空再转数字 -->
        LEFT JOIN (
        SELECT subid,
        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
        FROM service_subtask_preachform
        WHERE del_flag = 0
        GROUP BY subid
        ) cp ON cp.subid = ss.id
        WHERE ss.del_flag = '0'
        AND ss.service_type = '2'
        AND ss.visit_count = 1
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
            AND ss.orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
            and ss.campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                     separator=","
                     close=")">
                #{leavehospitaldistrictcode}
            </foreach>
        <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
                or (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
            AND (
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                ss.leavehospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                         separator=","
                         close=")">
                    #{leavehospitaldistrictcode}
                </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">
                ss.leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>)
        </if>
        <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
            AND deptcode IN
            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                     close=")">
                #{leaveldeptcode}
            </foreach>
        </if>
        ) t
    </select>
    <select id="querySendCount" parameterType="com.smartor.domain.entity.ServiceSubtaskSatisfactionEntity"
@@ -2883,7 +3364,7 @@
                OR
            </if>
            <if test="deptcodes != null and deptcodes.size() > 0">
                deptcode IN
                leaveldeptcode IN
                <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                         close=")">
                    #{deptcode}
@@ -2933,14 +3414,14 @@
            parameterType="com.smartor.domain.VO.ServiceSubtaskCotinueCountVO"
            resultType="java.util.Map">
        SELECT
        deptcode AS groupCode,
        deptname AS groupName,
        leaveldeptcode AS groupCode,
        leaveldeptname AS groupName,
        SUM(CASE WHEN continue_flag = 1 THEN 1 ELSE 0 END) AS noContinueCnt,
        SUM(CASE WHEN continue_flag = 2 THEN 1 ELSE 0 END) AS continueCnt
        FROM service_subtask
        WHERE del_flag = 0
        AND continue_flag IN (1, 2)
        AND deptcode IS NOT NULL AND deptcode != ''
        AND leaveldeptcode IS NOT NULL AND leaveldeptcode != ''
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
@@ -2948,12 +3429,12 @@
            AND campusid = #{campusid}
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="code" open="(" separator="," close=")">
                #{code}
            </foreach>
        </if>
        GROUP BY deptcode, deptname
        GROUP BY leaveldeptcode, leaveldeptname
        ORDER BY groupCode
    </select>
@@ -2966,8 +3447,8 @@
                leavehospitaldistrictname AS groupName,
            </when>
            <otherwise>
                deptcode AS groupCode,
                deptname AS groupName,
                leaveldeptcode AS groupCode,
                leaveldeptname AS groupName,
            </otherwise>
        </choose>
        COUNT(*) AS totalCount,
@@ -3004,12 +3485,12 @@
            </when>
            <otherwise>
                <if test="deptcodes != null and deptcodes.size() > 0">
                    AND deptcode IN
                    AND leaveldeptcode IN
                    <foreach collection="deptcodes" item="code" open="(" separator="," close=")">
                        #{code}
                    </foreach>
                </if>
                GROUP BY deptcode, deptname
                GROUP BY leaveldeptcode, leaveldeptname
                ORDER BY groupCode
            </otherwise>
        </choose>
@@ -3156,8 +3637,8 @@
        drname,
        leavehospitaldistrictcode,
        leavehospitaldistrictname,
        deptcode,
        deptname,
        leaveldeptcode,
        leaveldeptname,
        sendstate,
        preachform,
        current_preachform,
@@ -3185,7 +3666,8 @@
        continue_count,
        continue_time_next,
        task_situation,
        remark
        remark,
        doctor_opinion
        from service_subtask
        where 1=1
        and del_flag = 0
@@ -3210,7 +3692,7 @@
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            AND leaveldeptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
@@ -3258,6 +3740,13 @@
                #{taskId}
            </foreach>
        </if>
        <!-- 传了 subtaskIds 只统计这批子任务(本次导出的患者) -->
        <if test="subtaskIds != null and subtaskIds.size() > 0">
            AND s.id IN
            <foreach collection="subtaskIds" item="subtaskId" open="(" separator="," close=")">
                #{subtaskId}
            </foreach>
        </if>
        <if test="valueType != null">AND d.value_type = #{valueType}</if>
        <if test="scoreStart != null">AND d.score &gt;= #{scoreStart}</if>
        <if test="scoreEnd != null">AND d.score &lt;= #{scoreEnd}</if>
@@ -3266,6 +3755,19 @@
        </if>
        and s.sendstate=6
        and d.del_flag=0
        <!-- 分母必须和明细用同一套时间条件,否则选了时间范围后比例会按全量历史被稀释 -->
        <if test="startOutHospTime != null">
            AND date_format(s.endtime,'%y%m%d') &gt;= date_format(#{startOutHospTime},'%y%m%d')
        </if>
        <if test="endOutHospTime != null">
            AND date_format(s.endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
        </if>
        <if test="startFinishTime != null">
            AND date_format(s.finishtime,'%y%m%d') &gt;= date_format(#{startFinishTime},'%y%m%d')
        </if>
        <if test="endFinishTime != null">
            AND date_format(s.finishtime,'%y%m%d') &lt;= date_format(#{endFinishTime},'%y%m%d')
        </if>
        GROUP BY
        t.taskid,
        d.questiontext
@@ -3275,6 +3777,12 @@
            AND t.taskid IN
            <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
                #{taskId}
            </foreach>
        </if>
        <if test="subtaskIds != null and subtaskIds.size() > 0">
            AND s.id IN
            <foreach collection="subtaskIds" item="subtaskId" open="(" separator="," close=")">
                #{subtaskId}
            </foreach>
        </if>
        <if test="valueType != null">AND d.value_type = #{valueType}</if>
@@ -3309,6 +3817,64 @@
        t.taskid
    </select>
    <!--
      导出「问题详情占比」专用统计(只统计本次导出的这批患者)
      数量 = 选择该选项的患者数(COUNT(DISTINCT s.id),并用 MAX(id) 保证同一患者同一题只算最新一条)
      分数 = 该选项明细的 score 之和
      比例 = 数量 / 该题作答患者数,在 Java 侧统一计算,因此「单题各选项数量之和 = 该题作答患者数」
    -->
    <select id="statMydQuestionOptionForExport" resultType="com.smartor.domain.DTO.ServiceSubtaskOptionStatDTO">
        SELECT
        t.taskid AS taskid,
        t.task_name AS taskName,
        d.questiontext AS questiontext,
        d.asrtext AS optionresult,
        d.targetvalue AS targetvalue,
        d.value_type AS valueType,
        COUNT(DISTINCT s.id) AS patientCount,
        SUM(COALESCE(CAST(d.score AS DECIMAL(18,2)), 0)) AS scoreSum
        FROM
        service_task t
        INNER JOIN service_subtask s ON s.taskid = t.taskid
        INNER JOIN service_subtask_detail d ON d.sub_id = s.id
        WHERE 1=1
        AND s.del_flag = '0'
        AND d.del_flag = 0
        <if test="subtaskIds != null and subtaskIds.size() > 0">
            AND s.id IN
            <foreach collection="subtaskIds" item="subtaskId" open="(" separator="," close=")">
                #{subtaskId}
            </foreach>
        </if>
        <!-- 不传子任务时直接查不到数据,避免误统计全量 -->
        <if test="subtaskIds == null or subtaskIds.size() == 0">
            AND 1 = 0
        </if>
        <if test="questiontext != null and questiontext != ''">
            AND d.questiontext LIKE CONCAT('%', #{questiontext}, '%')
        </if>
        <!-- 同一患者同一题目只保留最新一条,重答不重复计入数量 -->
        AND d.id = (
        SELECT MAX(d2.id)
        FROM service_subtask_detail d2
        WHERE d2.sub_id = d.sub_id
        AND d2.questiontext &lt;=&gt; d.questiontext
        AND d2.del_flag = '0'
        )
        GROUP BY
        t.taskid,
        t.task_name,
        d.questiontext,
        d.asrtext,
        d.targetvalue,
        d.value_type
        <!-- 按任务名称分组展示:先任务名称、再任务编号,同一任务的题目/选项连续排列 -->
        ORDER BY
        t.task_name,
        t.taskid,
        d.questiontext
    </select>
    <!-- 新方法:根据子任务 ID 统计问题选项百分比 (用于满意度统计 serviceType=6) -->
    <select id="statQuestionOptionBySubtaskIds" resultType="com.smartor.domain.ServiceSubtaskDetailRatioExport">
        SELECT
@@ -3317,8 +3883,8 @@
        t.task_name AS taskName,
        d.asrtext AS optionresult,
        d.score AS score,
        s.deptcode AS deptcode,
        s.deptname AS deptname,
        s.leaveldeptcode AS deptcode,
        s.leaveldeptname AS deptname,
        s.leavehospitaldistrictcode AS leavehospitaldistrictcode,
        s.leavehospitaldistrictname AS leavehospitaldistrictname,
        d.targetvalue AS targetvalue,
@@ -3425,8 +3991,8 @@
        d.targetvalue,
        d.asrtext,
        t.task_name,
        s.deptcode,
        s.deptname,
        s.leaveldeptcode,
        s.leaveldeptname,
        s.leavehospitaldistrictcode,
        s.leavehospitaldistrictname,
        d.value_type
@@ -3438,7 +4004,7 @@
    <select id="statQuestionOptionByKsOrBq" resultType="com.smartor.domain.DTO.QuestionCountKsOrBqDTO"
            parameterType="com.smartor.domain.VO.QuestionCountKsOrBqVO">
        SELECT
        s.deptname AS deptname,
        IFNULL(s.leaveldeptname, s.deptname) AS deptname,
        s.leavehospitaldistrictname AS leavehospitaldistrictname,
        d.questiontext AS questionText,
        d.targetvalue AS targetvalue, -- 所有选项拼接
@@ -3468,7 +4034,7 @@
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                AND deptcode IN
                AND leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
@@ -3485,7 +4051,7 @@
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                OR deptcode IN
                OR leaveldeptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
@@ -3520,8 +4086,8 @@
                s.leavehospitaldistrictname AS name,
            </when>
            <otherwise>
                s.deptcode AS code,
                s.deptname AS name,
                s.leaveldeptcode AS code,
                s.leaveldeptname AS name,
            </otherwise>
        </choose>
        CAST(d.score AS DECIMAL(10,4)) AS w
@@ -3574,10 +4140,10 @@
                </if>
            </when>
            <otherwise>
                AND s.deptcode IS NOT NULL AND s.deptcode &lt;&gt; ''
                AND s.deptname IS NOT NULL AND s.deptname &lt;&gt; ''
                AND s.leaveldeptcode IS NOT NULL AND s.leaveldeptcode &lt;&gt; ''
                AND s.leaveldeptname IS NOT NULL AND s.leaveldeptname &lt;&gt; ''
                <if test="deptCodes != null and deptCodes.size() > 0">
                    AND s.deptcode IN
                    AND s.leaveldeptcode IN
                    <foreach collection="deptCodes" item="deptCode" open="(" separator="," close=")">
                        #{deptCode}
                    </foreach>
@@ -3593,5 +4159,6 @@
    </select>
</mapper>