陈昶聿
4 天以前 96de4cd0f9d994919ae0c3d70652fdbe382949d3
smartor/src/main/resources/mapper/smartor/ServiceSubtaskMapper.xml
@@ -23,6 +23,7 @@
        <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"/>
@@ -52,6 +53,7 @@
        <result property="wechat" column="wechat"/>
        <result property="leaveicd10code" column="leaveicd10code"/>
        <result property="leavediagname" column="leavediagname"/>
        <result property="diagname" column="diagname"/>
        <result property="deptname" column="deptname"/>
        <result property="admindate" column="admindate"/>
        <result property="roomno" column="roomno"/>
@@ -92,6 +94,13 @@
        <result property="continueContent" column="continue_content"/>
        <result property="continueCount" column="continue_count"/>
        <result property="continueTimeNext" column="continue_time_next"/>
        <result property="operationItemId" column="operation_item_id"/>
        <result property="scheduleId" column="schedule_id"/>
        <result property="scheduleDetailId" column="schedule_detail_id"/>
        <result property="scheduleSeq" column="schedule_seq"/>
        <result property="loopCount" column="loop_count"/>
        <result property="isManuallyCalled" column="is_manually_called"/>
        <result property="campusid" column="campusid"/>
    </resultMap>
    <resultMap type="com.smartor.domain.ServiceSubtaskCount" id="ServiceSubtaskResult2">
@@ -120,10 +129,34 @@
        <result property="options" column="options"/>
    </resultMap>
    <resultMap type="com.smartor.domain.ServiceSubtaskStatistic" id="ServiceSubtaskStatisticResult">
        <result property="taskName" column="task_name"/>
        <result property="drcode" column="drcode"/>
        <result property="drname" column="drname"/>
        <result property="dischargeCount" column="discharge_count"/>
        <result property="nonFollowUp" column="non_follow_up"/>
        <result property="filterCount" column="filter_count"/>
        <result property="followUpNeeded" column="follow_up_needed"/>
        <result property="needFollowUp" column="need_follow_up"/>
        <result property="pendingFollowUp" column="pending_follow_up"/>
        <result property="followUpFail" column="follow_up_fail"/>
        <result property="manual" column="manual"/>
        <result property="voice" column="voice"/>
        <result property="sms" column="sms"/>
        <result property="weChat" column="we_chat"/>
        <result property="taskSituation1" column="task_situation1"/>
        <result property="taskSituation2" column="task_situation2"/>
        <result property="taskSituation3" column="task_situation3"/>
        <result property="taskSituation4" column="task_situation4"/>
        <result property="taskSituation5" column="task_situation5"/>
        <result property="taskSituation6" column="task_situation6"/>
    </resultMap>
    <sql id="selectServiceSubtaskVo">
        select id,
               hosp_type,
               campusid,
               task_guid,
               diagname,
               patfrom,
               management_doctor,
               management_doctor_code,
@@ -170,6 +203,7 @@
               senduuid,
               result,
               finishtime,
               finish_state,
               recordid,
               exrecallcount,
               pulltime,
@@ -182,7 +216,6 @@
               create_time,
               isupload,
               upload_time,
               orgid,
               pid,
               guid,
               text_param,
@@ -205,15 +238,66 @@
               leaveicd10code,
               orgid,
               visit_type,
               management_doctor,
               management_doctor_code,
               continue_flag,
               continue_time_now,
               continue_content,
               continue_count,
               continue_time_next,
               task_situation
               operation_item_id,
               schedule_id,
               schedule_detail_id,
               schedule_seq,
               loop_count,
               task_situation,
               is_manually_called
        from service_subtask
    </sql>
    <!-- 完成时状态(finish_state)计算:仅当本次更新把 sendstate 置为 6(随访完成)时使用。
         基准截止时间 base = endtime + send_day(缺省1) + (Σ service_subtask_preachform.compensate_time + 1) 天,
         与 RyTask/RedisMqReceiver 的 isSubtaskDeadline 口径保持一致;
         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() 兜底,保证完成时一定写入状态;
         仅当 endtime 为空导致无法计算时才保持原值不覆盖。 -->
    <sql id="finishStateExpr">
        CASE
        WHEN service_subtask.endtime IS NULL THEN finish_state
        WHEN COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
        &lt;= DATE_ADD(
        DATE_ADD(
        service_subtask.endtime,
        INTERVAL COALESCE((SELECT st.send_day FROM service_task st
        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 1) DAY
        ),
        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
        )
        THEN 1
        WHEN COALESCE((SELECT st.delay_finish_days FROM service_task st
        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 0) &gt; 0
        AND COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
        &lt;= DATE_ADD(
        DATE_ADD(
        DATE_ADD(
        service_subtask.endtime,
        INTERVAL COALESCE((SELECT st.send_day FROM service_task st
        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 1) DAY
        ),
        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
        ),
        INTERVAL COALESCE((SELECT st.delay_finish_days FROM service_task st
        WHERE st.taskid = service_subtask.taskid AND st.del_flag = '0' LIMIT 1), 0) DAY
        )
        THEN 2
        ELSE 3
        END
    </sql>
    <select id="selectServiceSubtaskBySendstate" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity"
@@ -221,9 +305,8 @@
        <include refid="selectServiceSubtaskVo"/>
        where 1=1
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="taskid != null ">and taskid = #{taskid}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
@@ -245,13 +328,11 @@
        <include refid="selectServiceSubtaskVo"/>
        where 1=1
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="subId != null">and id = #{subId}</if>
        <if test="taskid != null ">and taskid = #{taskid}</if>
        <if test="visitTime != null">
            AND date_format(visit_time,'%y%m%d') &lt;= date_format(#{visitTime},'%y%m%d')
        </if>
        <if test="visitTime != null">and date_format(visit_time,'%y%m%d') &lt;= date_format(#{visitTime},'%y%m%d')</if>
        <if test="sendstate != null ">and sendstate = #{sendstate}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
@@ -259,11 +340,42 @@
        <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">
        <include refid="selectServiceSubtaskVo"/>
        where del_flag = 0
        <if test="subId != null ">and id = #{subId}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
        <if test="continueCount != null ">and continue_count = #{continueCount}</if>
@@ -298,12 +410,36 @@
        <if test="senderdetail != null  and senderdetail != ''">and senderdetail = #{senderdetail}</if>
        <if test="type != null  and type != ''">and type = #{type}</if>
        <if test="taskid != null ">and taskid = #{taskid}</if>
        <if test="sendstates != null and sendstates.size() > 0">
            AND sendstate IN
            <foreach collection="sendstates" item="state" open="(" separator="," close=")">
                #{state}
            </foreach>
        </if>
        <if test="subIdList != null  and subIdList.size() > 0">
            AND id IN
            <foreach collection="subIdList" item="subId" open="(" separator="," close=")">
                #{subId}
            </foreach>
        </if>
        <!-- taskIds筛选 -->
        <if test="taskIds != null and taskIds.size() > 0">
            AND taskid IN
            <foreach collection="taskIds" item="itemTaskId" open="(" separator=","
                     close=")">
                #{itemTaskId}
            </foreach>
        </if>
        <if test="operationItemIds != null and operationItemIds.size() > 0">
            AND operation_item_id IN
            <foreach collection="operationItemIds" item="operationItemIdItem" open="(" separator="," close=")">
                #{operationItemIdItem}
            </foreach>
        </if>
        <if test="inhospids != null and inhospids.size() > 0">
            AND inhospid IN
            <foreach collection="inhospids" item="inhospidItem" open="(" separator="," close=")">
                #{inhospidItem}
            </foreach>
        </if>
        <if test="templateid != null  and templateid != ''">and templateid = #{templateid}</if>
@@ -322,7 +458,12 @@
        <if test="endSendDateTime != null">
            AND date_format(visit_time,'%y%m%d') &lt;= date_format(#{endSendDateTime},'%y%m%d')
        </if>
        <if test="finishtimeStart != null">
            AND date_format(finishtime,'%y%m%d') &gt;= date_format(#{finishtimeStart},'%y%m%d')
        </if>
        <if test="finishtimeEnd != null">
            AND date_format(finishtime,'%y%m%d') &lt;= date_format(#{finishtimeEnd},'%y%m%d')
        </if>
        <if test="longSendTime != null">
            AND date_format(long_send_time,'%y%m%d') &lt;= date_format(#{longSendTime},'%y%m%d')
        </if>
@@ -344,22 +485,27 @@
            </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=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                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">
                deptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>)
            </if>
            )
        </if>
        <if test="deptOrDistrict==3">
            AND (1=1
@@ -380,7 +526,6 @@
            </if>
            )
        </if>
        <if test="deptOrDistrict==4">
            AND (1=1
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
@@ -400,7 +545,6 @@
            </if>
            )
        </if>
        <if test="visitTime != null">
            AND date_format(visit_time,'%y%m%d') = date_format(#{visitTime},'%y%m%d')
        </if>
@@ -417,6 +561,7 @@
        <if test="isupload != null ">and isupload = #{isupload}</if>
        <if test="uploadTime != null ">and upload_time = #{uploadTime}</if>
        <if test="orgid != null  and orgid != ''">and orgid = #{orgid}</if>
        <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>
@@ -431,8 +576,8 @@
        <if test="libtemplateid != null ">and libtemplateid = #{libtemplateid}</if>
        <if test="libtemplatename != null ">and libtemplatename = #{libtemplatename}</if>
        <if test="openid != null ">and openid = #{openid}</if>
        <if test="longSendTime != null ">and long_send_time = #{longSendTime}</if>
        <if test="sendstate != null ">and sendstate = #{sendstate}</if>
        <if test="operationItemId != null ">and operation_item_id = #{operationItemId}</if>
        <if test="drname != null  and drname != ''">and drname like concat('%', #{drname}, '%')</if>
        <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
        <if test="drcode != null and drcode != ''">and (drcode = #{drcode}</if>
@@ -453,15 +598,17 @@
        <if test="createBy != null">and create_by = #{createBy}</if>
        <if test="taskGuid != null">and task_guid = #{taskGuid}</if>
        <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
        <if test="isManuallyCalled != null">and is_manually_called = #{isManuallyCalled}</if>
        <if test="isVisitAgain != null">and is_visit_again = #{isVisitAgain}</if>
        <if test="visitCount != null and visitCount > 1 and visitDeptCodes != null and visitDeptCodes.size() > 0">
            AND visit_dept_code IN
            <foreach collection="visitDeptCodes" item="visitDeptCodes" open="("
            <foreach collection="visitDeptCodes" item="visitDeptCode" open="("
                     separator=","
                     close=")">
                #{visitDeptCodes}
                #{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>
@@ -471,7 +618,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"
@@ -512,11 +661,27 @@
            resultMap="ServiceSubtaskResult">
        <include refid="selectServiceSubtaskVo"/>
        where del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        and sendstate != 5
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="visitTime != null">and date_format(visit_time,'%y%m%d') &lt;= date_format(#{visitTime},'%y%m%d') </if>
        <if test="subId != null">and id = #{subId} </if>
        <if test="sendstate != null ">and sendstate = #{sendstate}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <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>
        UNION ALL
        <include refid="selectServiceSubtaskVo"/>
        where del_flag = 0
        and sendstate = 5
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="visitTime != null">
            AND date_format(visit_time,'%y%m%d') &lt;= date_format(#{visitTime},'%y%m%d')
            AND date_format(visit_time,'%y%m%d') &lt;= date_format(DATE_ADD(#{visitTime}, INTERVAL 1 DAY),'%y%m%d')
        </if>
        <if test="subId != null">
            AND id = #{subId}
        </if>
        <if test="sendstate != null ">and sendstate = #{sendstate}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
@@ -529,9 +694,8 @@
            resultMap="ServiceSubtaskResult">
        <include refid="selectServiceSubtaskVo"/>
        where del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="createStartTime != null  and createEndTime != null">and create_time BETWEEN #{createStartTime} AND
            #{createEndTime}
        </if>
@@ -578,7 +742,8 @@
        <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>
        <if test="orgid != null  and orgid != ''">and orgid = #{orgid}</if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <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="guid != null  and guid != ''">and guid = #{guid}</if>
@@ -613,6 +778,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">
@@ -626,9 +792,8 @@
            DATE_FORMAT(b.create_time, '%Y-%m-%d') LIKE CONCAT(#{createTimeNew}, '%')
        </if>
        <if test="sendstate != null ">and b.sendstate = #{sendstate}</if>
        <if test="orgid != null and orgid != ''">
            and b.orgid = #{orgid}
        </if>
        <if test="orgid != null and orgid != ''">and b.orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and b.campusid = #{campusid}</if>
    </select>
    <select id="selectTimelyRate" parameterType="com.smartor.domain.ServiceSubtask" resultType="double">
@@ -645,7 +810,8 @@
            AND date_format(visit_time,'%y%m%d') >= date_format(#{starttime},'%y%m%d')
            AND date_format(visit_time,'%y%m%d') &lt;= date_format(#{endtime},'%y%m%d')
        </if>
        <if test="orgid != null  and orgid != ''">and orgid = #{orgid}</if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <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>
@@ -659,63 +825,91 @@
        </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>
    <select id="selectTimelyRateBatch" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity" resultType="map">
        SELECT
        <if test="groupKey != null and groupKey != ''">
            ${groupKey} as groupKey,
            ss.${groupKey} as groupKey,
        </if>
        SUM(CASE WHEN finishtime IS NOT NULL AND DATE(visit_time) &lt;= DATE(finishtime) THEN 1 ELSE 0 END) / COUNT(*) AS finishRate,
        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 finishtime IS NOT NULL AND DATEDIFF(finishtime, endtime) &lt;= #{rateDay} THEN 1 ELSE 0 END) / COUNT(*) AS rate
            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>
        FROM service_subtask
        WHERE del_flag = 0
        AND visit_time IS NOT NULL
        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>
        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(visit_time) &lt; DATE(NOW())
            AND DATE(ss.visit_time) &lt; DATE(NOW())
        </if>
        <if test="starttime != null and endtime!=null">
            AND visit_time >= #{starttime}
            AND visit_time &lt; DATE_ADD(#{endtime}, INTERVAL 1 DAY)
            AND ss.visit_time >= #{starttime}
            AND ss.visit_time &lt; DATE_ADD(#{endtime}, INTERVAL 1 DAY)
        </if>
        <if test="orgid != null and orgid != ''">AND orgid = #{orgid}</if>
        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
        <if test="isabnormal != null">and isabnormal = #{isabnormal}</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="isabnormal != null">and ss.isabnormal = #{isabnormal}</if>
        <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
            leavehospitaldistrictcode = #{leavehospitaldistrictcode}
            ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
        </if>
        <if test="leavehospitaldistrictname != null  and leavehospitaldistrictname != ''">and
            leavehospitaldistrictname = #{leavehospitaldistrictname}
            ss.leavehospitaldistrictname = #{leavehospitaldistrictname}
        </if>
        <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
        <if test="managementDoctor != null">and management_doctor like concat('%',#{managementDoctor}, '%')</if>
        <if test="managementDoctorCode != null">and ss.management_doctor_code = #{managementDoctorCode}</if>
        <if test="managementDoctor != null">and ss.management_doctor like concat('%',#{managementDoctor}, '%')</if>
        <if test="visitCount != null and visitCount == 1">
            AND visit_count = 1
            AND ss.visit_count = 1
        </if>
        <if test="visitCount != null and visitCount > 1">
            AND visit_count > 1
            AND ss.visit_count > 1
        </if>
        <if test="serviceTypeList != null">
            AND service_type IN
            AND ss.service_type IN
            <foreach collection="serviceTypeList" item="serviceType" open="(" separator=","
                     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 ${groupKey} IN
            AND ss.${groupKey} IN
            <foreach collection="groupKeyList" item="key" open="(" separator=","
                     close=")">
                #{key}
            </foreach>
            GROUP BY ${groupKey}
            GROUP BY ss.${groupKey}
        </if>
    </select>
@@ -738,6 +932,7 @@
            AND date_format(visit_time,'%y%m%d') &lt;= date_format(#{endtime},'%y%m%d')
        </if>
        <if test="orgid != null  and orgid != ''">and orgid = #{orgid}</if>
        <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="isabnormal != null ">and isabnormal = #{isabnormal}</if>
@@ -768,11 +963,18 @@
        <if test="sendname != null">and sendname like concat('%',#{sendname}, '%')</if>
        <if test="taskName != null">and task_name like concat('%',#{taskName}, '%')</if>
        <if test="sendstate != null">and sendstate = #{sendstate}</if>
        <if test="orgid != null">and orgid =#{orgid}</if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid} </if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <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>
        <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>
        GROUP BY sendname,sendstate,starttime,endtime,nurse_name,phone,sex,task_name,leavediagname,visit_time
        ORDER BY sendname,visit_time
        <if test="pageSize != null  and pageNum != null">limit ${pageSize} OFFSET ${pageNum}</if>
@@ -797,6 +999,7 @@
            <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>
@@ -808,7 +1011,8 @@
            <if test="createTime != null">create_time,</if>
            <if test="isupload != null">isupload,</if>
            <if test="uploadTime != null">upload_time,</if>
            <if test="orgid != null">orgid,</if>
            <if test="orgid != null and orgid != ''">orgid,</if>
            <if test="campusid != null and campusid != ''">campusid,</if>
            <if test="pid != null">pid,</if>
            <if test="guid != null">guid,</if>
            <if test="textParam != null">text_param,</if>
@@ -871,6 +1075,12 @@
            <if test="continueCount != null ">continue_count,</if>
            <if test="continueTimeNext != null ">continue_time_next,</if>
            <if test="continueContent != null ">continue_content,</if>
            <if test="operationItemId != null ">operation_item_id,</if>
            <if test="scheduleId != null ">schedule_id,</if>
            <if test="scheduleDetailId != null ">schedule_detail_id,</if>
            <if test="scheduleSeq != null ">schedule_seq,</if>
            <if test="loopCount != null ">loop_count,</if>
            <if test="isManuallyCalled != null ">is_manually_called,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="sendname != null">#{sendname},</if>
@@ -888,6 +1098,7 @@
            <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>
@@ -899,7 +1110,8 @@
            <if test="createTime != null">#{createTime},</if>
            <if test="isupload != null">#{isupload},</if>
            <if test="uploadTime != null">#{uploadTime},</if>
            <if test="orgid != null">#{orgid},</if>
            <if test="orgid != null and orgid != ''">#{orgid},</if>
            <if test="campusid != null and campusid != ''">#{campusid},</if>
            <if test="pid != null">#{pid},</if>
            <if test="guid != null">#{guid},</if>
            <if test="textParam != null">#{textParam},</if>
@@ -962,8 +1174,23 @@
            <if test="continueCount != null ">#{continueCount},</if>
            <if test="continueTimeNext != null ">#{continueTimeNext,jdbcType=TIMESTAMP},</if>
            <if test="continueContent != null ">#{continueContent},</if>
            <if test="operationItemId != null ">#{operationItemId},</if>
            <if test="scheduleId != null ">#{scheduleId},</if>
            <if test="scheduleDetailId != null ">#{scheduleDetailId},</if>
            <if test="scheduleSeq != null ">#{scheduleSeq},</if>
            <if test="loopCount != null ">#{loopCount},</if>
            <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
@@ -995,7 +1222,8 @@
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isupload != null">isupload = #{isupload},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="orgid != null">orgid = #{orgid},</if>
            <if test="orgid != null and orgid != ''">orgid = #{orgid},</if>
            <if test="campusid != null and campusid != ''">campusid = #{campusid},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="guid != null">guid = #{guid},</if>
            <if test="textParam != null">text_param = #{textParam},</if>
@@ -1020,6 +1248,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>
@@ -1058,6 +1287,12 @@
            <if test="continueCount != null ">continue_count = #{continueCount},</if>
            <if test="continueTimeNext != null ">continue_time_next = #{continueTimeNext,jdbcType=TIMESTAMP},</if>
            <if test="continueContent != null ">continue_content = #{continueContent},</if>
            <if test="operationItemId != null ">operation_item_id = #{operationItemId},</if>
            <if test="scheduleId != null ">schedule_id = #{scheduleId},</if>
            <if test="scheduleDetailId != null ">schedule_detail_id = #{scheduleDetailId},</if>
            <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>
        </trim>
        where id = #{id}
    </update>
@@ -1092,7 +1327,8 @@
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isupload != null">isupload = #{isupload},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="orgid != null">orgid = #{orgid},</if>
            <if test="orgid != null and orgid != ''">orgid = #{orgid},</if>
            <if test="campusid != null and campusid != ''">campusid = #{campusid},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="guid != null">guid = #{guid},</if>
            <if test="textParam != null">text_param = #{textParam},</if>
@@ -1117,6 +1353,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>
@@ -1155,13 +1392,13 @@
            <if test="continueCount != null ">continue_count = #{continueCount},</if>
            <if test="continueTimeNext != null ">continue_time_next = #{continueTimeNext,jdbcType=TIMESTAMP},</if>
            <if test="continueContent != null ">continue_content = #{continueContent},</if>
            <if test="isManuallyCalled != null ">is_manually_called = #{isManuallyCalled},</if>
        </trim>
        where 1=1
        <if test="patid != null ">and patid = #{patid}</if>
        <if test="taskid != null ">and taskid = #{taskid}</if>
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="orgid != null and orgid != ''">and orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
    </update>
    <update id="updateServiceSubtaskByCondition" parameterType="com.smartor.domain.ServiceSubtask">
@@ -1170,7 +1407,9 @@
            <if test="sendname != null">sendname = #{sendname},</if>
            <if test="phone != null">phone = #{phone},</if>
            <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>
@@ -1194,7 +1433,8 @@
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isupload != null">isupload = #{isupload},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="orgid != null">orgid = #{orgid},</if>
            <if test="orgid != null and orgid != ''">orgid = #{orgid},</if>
            <if test="campusid != null and campusid != ''">campusid = #{campusid},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="guid != null">guid = #{guid},</if>
            <if test="visitCount != null">visit_count = #{visitCount},</if>
@@ -1256,11 +1496,22 @@
            <if test="continueCount != null ">continue_count = #{continueCount},</if>
            <if test="continueTimeNext != null ">continue_time_next = #{continueTimeNext,jdbcType=TIMESTAMP},</if>
            <if test="continueContent != null ">continue_content = #{continueContent},</if>
            <if test="isManuallyCalled != null ">is_manually_called = #{isManuallyCalled},</if>
        </trim>
        where patid = #{patid} and taskid = #{taskid}
        <if test="id != null ">
            and id = #{id}
        </if>
        <where>
            <if test="patid != null ">
                patid = #{patid}
            </if>
            <if test="taskid != null ">
                and taskid = #{taskid}
            </if>
            <if test="id != null ">
                and id = #{id}
            </if>
            <if test="inhospid != null ">
                and inhospid = #{inhospid}
            </if>
        </where>
    </update>
    <update id="updateServiceSubtaskByTaskGuid" parameterType="com.smartor.domain.ServiceSubtask">
@@ -1270,6 +1521,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>
@@ -1293,7 +1545,8 @@
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isupload != null">isupload = #{isupload},</if>
            <if test="uploadTime != null">upload_time = #{uploadTime},</if>
            <if test="orgid != null">orgid = #{orgid},</if>
            <if test="orgid != null and orgid != ''">orgid = #{orgid},</if>
            <if test="campusid != null and campusid != ''">campusid = #{campusid},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="guid != null">guid = #{guid},</if>
            <if test="textParam != null">text_param = #{textParam},</if>
@@ -1355,6 +1608,7 @@
            <if test="continueCount != null ">continue_count = #{continueCount},</if>
            <if test="continueTimeNext != null ">continue_time_next = #{continueTimeNext,jdbcType=TIMESTAMP},</if>
            <if test="continueContent != null ">continue_content = #{continueContent},</if>
            <if test="isManuallyCalled != null ">is_manually_called = #{isManuallyCalled},</if>
        </trim>
        where task_guid = #{taskGuid} and task_name = #{taskName}
    </update>
@@ -1385,12 +1639,15 @@
        orgid,
        drcode,
        drname,
        management_doctor_code,
        management_doctor,
        leavehospitaldistrictcode,
        leavehospitaldistrictname,
        deptcode,
        deptname,
        sendstate,
        preachform,
        current_preachform,
        excep,
        nurse_id,
        nurse_name,
@@ -1414,12 +1671,16 @@
        continue_content,
        continue_count,
        continue_time_next,
        task_situation
        task_situation,
        is_manually_called
        from service_subtask
        where 1=1
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
@@ -1445,6 +1706,10 @@
        <if test="startTime != null and endTime!=null">
            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>
@@ -1471,6 +1736,120 @@
            </foreach>
        </if>
    </select>
    <select id="getSpecialSfStatistics" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskStatisticResult">
        select
        <if test="groupKey != null and groupKey == 'task_name'">
            a.task_name,
        </if>
        <if test="groupKey != null and groupKey == 'drcode'">
            b.drcode,
            b.drname,
        </if>
        COUNT(1) AS discharge_count,
        SUM(CASE WHEN b.sendstate = 4 THEN 1 ELSE 0 END) AS non_follow_up,
        SUM(CASE WHEN b.sendstate = 4 and b.task_situation = 6 THEN 1 ELSE 0 END) AS filter_count,
        SUM(CASE WHEN b.sendstate != 4 THEN 1 ELSE 0 END) AS follow_up_needed,
        SUM(CASE WHEN b.sendstate != 4 THEN 1 ELSE 0 END) AS need_follow_up,
        SUM(CASE WHEN b.sendstate = 2 THEN 1 ELSE 0 END) AS pending_follow_up,
        SUM(CASE WHEN b.sendstate = 5 or b.sendstate = 7 THEN 1 ELSE 0 END) AS follow_up_fail,
        SUM(CASE WHEN b.current_preachform = '1' THEN 1 ELSE 0 END) AS manual,
        SUM(CASE WHEN b.current_preachform = '3' THEN 1 ELSE 0 END) AS voice,
        SUM(CASE WHEN b.current_preachform = '4' THEN 1 ELSE 0 END) AS sms,
        SUM(CASE WHEN b.current_preachform = '5' THEN 1 ELSE 0 END) AS we_chat,
        SUM(CASE WHEN b.task_situation = 1 THEN 1 ELSE 0 END) AS task_situation1,
        SUM(CASE WHEN b.task_situation = 2 THEN 1 ELSE 0 END) AS task_situation2,
        SUM(CASE WHEN b.task_situation = 3 THEN 1 ELSE 0 END) AS task_situation3,
        SUM(CASE WHEN b.task_situation = 4 THEN 1 ELSE 0 END) AS task_situation4,
        SUM(CASE WHEN b.task_situation = 5 THEN 1 ELSE 0 END) AS task_situation5,
        SUM(CASE WHEN b.task_situation = 7 THEN 1 ELSE 0 END) AS task_situation7
        from service_task a
        JOIN service_subtask b on a.taskid = b.taskid
        where 1=1
        and a.del_flag = '0'
        and b.del_flag = '0'
        and a.appltype = '3'
        <if test="orgid != null and orgid != ''">and a.orgid = #{orgid}</if>
        <if test="campusid != null and campusid != ''">and b.campusid = #{campusid}</if>
        <if test="taskId != null">and a.task_id = #{taskId}</if>
        <if test="taskName != null and taskName != ''">
            and a.task_name like concat('%', #{taskName}, '%')
        </if>
        <if test="diagType != null and diagType != ''">
            and a.diag_type = #{diagType}
        </if>
        <if test="taskIds != null and taskIds.size() > 0">
            AND a.task_id IN
            <foreach collection="taskIds" item="taskIdItem" open="(" separator=","
                     close=")">
                #{taskIdItem}
            </foreach>
        </if>
        <if test="taskNames != null and taskNames.size() > 0">
            AND a.task_name IN
            <foreach collection="taskNames" item="taskNameItem" open="(" separator=","
                     close=")">
                #{taskNameItem}
            </foreach>
        </if>
        <if test="diagTypes != null and diagTypes.size() > 0">
            AND a.diag_type IN
            <foreach collection="diagTypes" item="diagTypeItem" open="(" separator=","
                     close=")">
                #{diagTypeItem}
            </foreach>
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND b.leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                     close=")">
                #{leavehospitaldistrictcode}
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND b.deptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
            </foreach>
        </if>
        <if test="serviceType != null and serviceType.size() > 0">
            AND b.service_type IN
            <foreach collection="serviceType" item="serviceType" open="(" separator=","
                     close=")">
                #{serviceType}
            </foreach>
        </if>
        <if test="startTime != null and endTime!=null">
            AND b.visit_time >= DATE(#{startTime})
            AND b.visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
        </if>
        <if test="visitDeptCode != null">and b.visit_dept_code = #{visitDeptCode}</if>
        <if test="visitDeptName != null">and b.visit_dept_name = #{visitDeptName}</if>
        <if test="isabnormal != null">and b.isabnormal = #{isabnormal}</if>
        <if test="continueFlag != null ">and b.continue_flag = #{continueFlag}</if>
        <if test="continueTimeNow != null ">and b.continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
        <if test="continueCount != null ">and b.continue_count = #{continueCount}</if>
        <if test="continueTimeNext != null ">and b.continue_time_next = #{continueTimeNext,jdbcType=TIMESTAMP}</if>
        <!-- 目前只统计语音和问卷 -->
        <if test="type != null">
            and b.type = #{type}
        </if>
        <if test="visitCount != null and visitCount == 1">
            AND b.visit_count = 1
        </if>
        <if test="visitCount != null and visitCount > 1">
            AND b.visit_count > 1
        </if>
        <if test="groupKey != null and groupKey == 'task_name'">
            GROUP BY a.task_name
        </if>
        <if test="groupKey != null and groupKey == 'drcode'">
            GROUP BY b.drcode, b.drname
        </if>
    </select>
    <select id="getSfStatisticsGroupKey" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultType="String">
        select
@@ -1482,6 +1861,9 @@
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
@@ -1508,6 +1890,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>
@@ -1533,6 +1919,147 @@
        </if>
    </select>
    <select id="getSfStatisticsHyperlink" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskResult">
        select id,
        visit_count,
        is_visit_again,
        type,
        orgid,
        drcode,
        drname,
        leavehospitaldistrictcode,
        leavehospitaldistrictname,
        deptcode,
        deptname,
        sendstate,
        preachform,
        current_preachform,
        excep,
        nurse_id,
        nurse_name,
        taskid,
        task_name,
        visit_time,
        finishtime,
        endtime,
        senddate,
        suggest,
        result,
        templateid,
        templatename,
        patid,
        sendname,
        send_type,
        recordid,
        exrecallcount,
        continue_flag,
        continue_time_now,
        continue_content,
        continue_count,
        continue_time_next,
        task_situation,
        remark,
        is_manually_called
        from service_subtask
        where 1=1
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and 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>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
            </foreach>
        </if>
        <if test="serviceType != null and serviceType.size() > 0">
            AND service_type IN
            <foreach collection="serviceType" item="serviceType" open="(" separator=","
                     close=")">
                #{serviceType}
            </foreach>
        </if>
        <if test="startTime != null and endTime!=null">
            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>
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <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,jdbcType=TIMESTAMP}</if>
        <!-- 目前只统计语音和问卷 -->
        <if test="type != null">
            and type = #{type}
        </if>
        <if test="visitCount != null and visitCount == 1">
            AND visit_count = 1
        </if>
        <if test="visitCount != null and visitCount > 1">
            AND visit_count > 1
        </if>
        <if test="groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
            AND ${groupKey} IN
            <foreach collection="groupKeyList" item="key" open="(" separator=","
                     close=")">
                #{key}
            </foreach>
        </if>
        AND sendstate IS NOT NULL
        <if test="sendstates != null and sendstates.size() > 0">
            AND sendstate IN
            <foreach collection="sendstates" item="state" open="(" separator="," close=")">
                #{state}
            </foreach>
        </if>
        <if test="currentPreachform != null">
            AND current_preachform = #{currentPreachform}
            <if test="followUpCountStyle != null and followUpCountStyle = '1'">
                AND sendstate != 4
            </if>
        </if>
        <if test="excep != null and excep != ''">
            AND excep = #{excep}
        </if>
        <if test="taskSituation != null">
            AND task_situation = #{taskSituation}
        </if>
        <if test="drcode != null and drcode != ''">
            AND drcode = #{drcode}
        </if>
        <if test="leavehospitaldistrictcode != null and leavehospitaldistrictcode != ''">
            AND leavehospitaldistrictcode = #{leavehospitaldistrictcode}
        </if>
        <if test="deptcode != null and deptcode != ''">
            AND deptcode = #{deptcode}
        </if>
        <if test="isManuallyCalled != null">
            AND is_manually_called = #{isManuallyCalled}
        </if>
        order by endtime desc
        <if test="pageSize != null  and pageNum != null">
            limit ${pageSize} OFFSET ${pageNum}
        </if>
    </select>
    <select id="getSfStatisticsJoy" parameterType="com.smartor.domain.ServiceSubtaskCountReq"
            resultMap="ServiceSubtaskResult2">
        SELECT sub_id as subTaskId,COUNT(sub_id) joyCount,
@@ -1544,6 +2071,9 @@
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
@@ -1589,6 +2119,9 @@
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
@@ -1620,6 +2153,9 @@
        and del_flag = 0
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">AND
            leavehospitaldistrictcode IN
@@ -1675,6 +2211,9 @@
        AND t.del_flag=0
        <if test="orgid != null and orgid != ''">
            and t.orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and t.campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND t.leavehospitaldistrictcode IN
@@ -1759,6 +2298,9 @@
        <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=","
@@ -1815,6 +2357,9 @@
        and 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
@@ -1969,10 +2514,15 @@
        SELECT deptname,
        count(1) AS rc
        FROM service_subtask
        WHERE DATE_FORMAT(finishtime, '%Y-%m-%d') >= #{startDate}
        AND DATE_FORMAT(finishtime, '%Y-%m-%d') &lt;= #{endDate}
        <if test="orgid != null">
        WHERE DATE_FORMAT(visit_time, '%Y-%m-%d') >= #{startDate}
        AND DATE_FORMAT(visit_time, '%Y-%m-%d') &lt;= #{endDate}
        AND service_type IN (2, 13)
        AND del_flag = '0'
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        GROUP BY deptname
    </select>
@@ -2012,15 +2562,18 @@
        FROM service_subtask
        WHERE
        del_flag = '0'
        AND service_type IN (2, 13)
        <if test="startDate != null and endDate != null">
            AND visit_time >= #{startDate}
            AND visit_time &lt;= DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        </if>
        <if test="orgid != null">
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator="," close=")">
@@ -2073,12 +2626,13 @@
            AND endtime >= #{startDate}
            AND endtime &lt;= DATE_ADD(#{endDate}, INTERVAL 1 DAY)
        </if>
        AND inhospstate=1
        <if test="orgid != null">
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="districtcode" open="(" separator="," close=")">
@@ -2125,9 +2679,11 @@
        <!--            AND admitdate &lt;= DATE_ADD(#{endDate}, INTERVAL 1 DAY)-->
        <!--        </if>-->
        <!--        <if test="orgid != null">-->
        <!--            AND orgid = #{orgid}-->
        <!--             AND orgid = #{orgid}-->
        <!--        </if>-->
        <!--        <if test="campusid != null">-->
        <!--             AND campusid = #{campusid}-->
        <!--        </if>-->
        <!--        <if test="deptcodes != null and deptcodes.size() > 0">-->
        <!--            AND deptcode IN-->
        <!--            <foreach collection="deptcodes" item="deptcode" open="(" separator="," close=")">-->
@@ -2147,19 +2703,32 @@
        SUM(CASE WHEN sendstate != 4 AND sendstate != 2 THEN 1 ELSE 0 END) AS ysf,
        SUM(CASE WHEN sendstate = 5 THEN 1 ELSE 0 END) AS fssb,
        SUM(CASE WHEN sendstate >= 3 OR sendstate = 1 THEN 1 ELSE 0 END) AS yfs,
        SUM(CASE WHEN sendstate = 2 THEN 1 ELSE 0 END) AS dsf,
        SUM(CASE WHEN sendstate = 1 THEN 1 ELSE 0 END) AS blq,
        SUM(CASE WHEN sendstate = 4 THEN 1 ELSE 0 END) AS wxsf,
        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
        FROM service_subtask
        where 1=1
        and del_flag = 0
        where del_flag = 0
        <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
        <if test="sendname != null and sendname != ''">
            AND sendname LIKE CONCAT('%', #{sendname}, '%')
        </if>
        <if test="phone != null and phone != ''">
            AND phone = #{phone}
        </if>
        <if test="finishtimeStart != null">
            AND date_format(finishtime,'%y%m%d') &gt;= date_format(#{finishtimeStart},'%y%m%d')
        </if>
        <if test="finishtimeEnd != null">
            AND date_format(finishtime,'%y%m%d') &lt;= date_format(#{finishtimeEnd},'%y%m%d')
        </if>
        <if test="deptOrDistrict==1">
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
@@ -2179,28 +2748,34 @@
            </if>
        </if>
        <if test="deptOrDistrict==2">
            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
                deptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close="))">
                         close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>
            )
        </if>
        <if test="visitCount != null and visitCount > 1 and visitDeptCodes != null and visitDeptCodes.size() > 0">
            AND visit_dept_code IN
            <foreach collection="visitDeptCodes" item="visitDeptCodes" open="("
            <foreach collection="visitDeptCodes" item="visitDeptCode" open="("
                     separator=","
                     close=")">
                #{visitDeptCodes}
                #{visitDeptCode}
            </foreach>
        </if>
        <if test="leavehospitaldistrictname != null and leavehospitaldistrictname != ''">
@@ -2311,6 +2886,9 @@
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        <if test="guid != null and guid != ''">
            AND guid = #{guid}
        </if>
@@ -2326,6 +2904,9 @@
        <if test="visitCount != null and visitCount > 1">
            AND visit_count > 1
        </if>
        <if test="drname != null  and drname != ''">
            AND drname like concat('%', #{drname}, '%')
        </if>
    </select>
@@ -2339,8 +2920,11 @@
        where 1=1
        and del_flag = 0
        and service_type=3
        <if test="orgid != null">
            and orgid = #{orgid}
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        <if test="startDate != null">
            AND date_format( visit_time, '%y%m%d' ) &gt;= date_format( #{startDate}, '%y%m%d' )
@@ -2364,29 +2948,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="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                     separator=","
                     close=")">
                #{leavehospitaldistrictcode}
            </foreach>
        <if test="campusid != null and campusid != ''">
            and ss.campusid = #{campusid}
        </if>
        <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
            AND deptcode IN
            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                     close=")">
                #{leaveldeptcode}
            </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.deptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>)
        </if>
        ) t
    </select>
    <select id="querySendCount" parameterType="com.smartor.domain.entity.ServiceSubtaskSatisfactionEntity"
@@ -2459,6 +3087,9 @@
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            <foreach collection="leavehospitaldistrictcodes" item="code" open="(" separator="," close=")">
@@ -2485,6 +3116,9 @@
        <if test="orgid != null and orgid != ''">
            AND orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND campusid = #{campusid}
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            <foreach collection="deptcodes" item="code" open="(" separator="," close=")">
@@ -2495,4 +3129,726 @@
        ORDER BY groupCode
    </select>
    <!-- 宣教统计:按病区或科室分组,统计发送总量、发送成功量(sendstate=6)、已读量(finishtime不为空) -->
    <select id="getHeLibraryCount" parameterType="com.smartor.domain.VO.HeLibraryCountVO" resultType="map">
        SELECT
        <choose>
            <when test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
                leavehospitaldistrictcode AS groupCode,
                leavehospitaldistrictname AS groupName,
            </when>
            <otherwise>
                deptcode AS groupCode,
                deptname AS groupName,
            </otherwise>
        </choose>
        COUNT(*) AS totalCount,
        SUM(CASE WHEN sendstate = 6 THEN 1 ELSE 0 END) AS sendSuccessCount,
        SUM(CASE WHEN finishtime IS NOT NULL THEN 1 ELSE 0 END) AS readCount
        FROM service_subtask
        WHERE del_flag = 0
        AND service_type = 4
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="sendstate != null">
            AND sendstate = #{sendstate}
        </if>
        <if test="isFinished != null and isFinished != ''">
            and finishtime is not null
        </if>
        <if test="starttime != null">AND starttime &gt;= #{starttime}</if>
        <if test="endtime != null">AND endtime &lt;= #{endtime}</if>
        <if test="visittime != null">AND DATE(visit_time) = DATE(#{visittime})</if>
        <if test="finishtime != null">AND DATE(finishtime) = DATE(#{finishtime})</if>
        <if test="hospType != null and hospType != ''">AND hosp_type = #{hospType}</if>
        <choose>
            <when test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
                AND leavehospitaldistrictcode IN
                <foreach collection="hospitaldistrictcodes" item="code" open="(" separator="," close=")">
                    #{code}
                </foreach>
                GROUP BY leavehospitaldistrictcode, leavehospitaldistrictname
                ORDER BY groupCode
            </when>
            <otherwise>
                <if test="deptcodes != null and deptcodes.size() > 0">
                    AND deptcode IN
                    <foreach collection="deptcodes" item="code" open="(" separator="," close=")">
                        #{code}
                    </foreach>
                </if>
                GROUP BY deptcode, deptname
                ORDER BY groupCode
            </otherwise>
        </choose>
    </select>
    <!-- 任务组(appltype=5)滚动调度用:本轮仍在跑的数量 -->
    <select id="countActiveByScheduleIdAndLoop" resultType="int">
        select count(1)
        from service_subtask
        where schedule_id = #{scheduleId}
          and loop_count = #{loopCount}
          and (del_flag is null or del_flag = '0')
          and sendstate in (1, 2, 3)
    </select>
    <select id="mydTrend" resultType="com.smartor.domain.DTO.MydTrendReqDTO">
        SELECT CASE #{type}
                   WHEN 'year' THEN DATE_FORMAT(d.create_time, '%Y')
                   WHEN 'quarter' THEN CONCAT(YEAR(d.create_time), '-Q', QUARTER(d.create_time))
                   WHEN 'month' THEN DATE_FORMAT(d.create_time, '%Y-%m')
                   WHEN 'week' THEN FLOOR(DATEDIFF(d.create_time, #{startFinishTime}) / 7) + 1
                   ELSE DATE_FORMAT(d.create_time, '%Y-%m-%d')
                   END                AS period,
               ROUND(avg(d.score), 2) AS totalScore
        FROM service_subtask_detail d
                 JOIN service_task t ON d.taskid = t.taskid
        WHERE t.service_type IN (14)
          AND t.del_flag = 0
          AND d.del_flag = 0
          AND d.create_time >= #{startFinishTime}
          AND d.create_time &lt; DATE_ADD(#{endFinishTime}, INTERVAL 1 DAY)
        GROUP BY period
        ORDER BY period;
    </select>
    <select id="sltdMydTotalByDimension" resultType="com.smartor.domain.DTO.MydTrendReqDTO">
        SELECT
        p.period,
        dim.dimension,
        IFNULL(ROUND(SUM(d.score), 2), 0) AS totalScore
        FROM (
        <!-- 生成查询范围内实际出现过的时间段 -->
        SELECT DISTINCT
        CASE #{type}
        WHEN 'year' THEN DATE_FORMAT(d.create_time, '%Y')
        WHEN 'quarter' THEN CONCAT(YEAR(d.create_time), '-Q', QUARTER(d.create_time))
        WHEN 'month' THEN DATE_FORMAT(d.create_time, '%Y-%m')
        WHEN 'week' THEN FLOOR(DATEDIFF(d.create_time, #{startFinishTime}) / 7) + 1
        ELSE DATE_FORMAT(d.create_time, '%Y-%m-%d')
        END AS period
        FROM service_subtask_detail d
        WHERE d.del_flag = 0
        AND d.create_time >= #{startFinishTime}
        AND d.create_time &lt; #{endFinishTime}
        ) p
        CROSS JOIN (
        <!-- 所有需要展示的 dimension -->
        SELECT DISTINCT dimension
        FROM service_subtask_detail
        WHERE del_flag = 0
        AND dimension IS NOT NULL
        ) dim
        LEFT JOIN service_task t
        ON t.service_type IN (6, 14)
        AND t.del_flag = 0
        LEFT JOIN service_subtask_detail d
        ON d.taskid = t.taskid
        AND d.del_flag = 0
        AND d.dimension = dim.dimension
        AND CASE #{type}
        WHEN 'year' THEN DATE_FORMAT(d.create_time, '%Y')
        WHEN 'quarter' THEN CONCAT(YEAR(d.create_time), '-Q', QUARTER(d.create_time))
        WHEN 'month' THEN DATE_FORMAT(d.create_time, '%Y-%m')
        WHEN 'week' THEN FLOOR(DATEDIFF(d.create_time, #{startFinishTime}) / 7) + 1
        ELSE DATE_FORMAT(d.create_time, '%Y-%m-%d')
        END = p.period
        AND d.create_time >= #{startFinishTime}
        AND d.create_time &lt; #{endFinishTime}
        GROUP BY p.period, dim.dimension
        ORDER BY p.period, dim.dimension
    </select>
    <select id="sltdMydTotalByYQ" resultType="com.smartor.domain.DTO.MydTrendReqDTO">
        SELECT p.period,
               c.campusid,
               IFNULL(ROUND(AVG(d.score), 2), 0) AS totalScore
        FROM (
                 -- 生成查询范围内实际出现过的时间段
                 SELECT DISTINCT CASE #{type}
                                     WHEN 'year' THEN DATE_FORMAT(d.create_time, '%Y')
                                     WHEN 'quarter' THEN CONCAT(YEAR(d.create_time), '-Q', QUARTER(d.create_time))
                                     WHEN 'month' THEN DATE_FORMAT(d.create_time, '%Y-%m')
                                     WHEN 'week' THEN FLOOR(DATEDIFF(d.create_time, #{startFinishTime}) / 7) + 1
                                     ELSE DATE_FORMAT(d.create_time, '%Y-%m-%d')
                                     END AS period
                 FROM service_subtask_detail d
                 WHERE d.del_flag = 0
                   AND d.create_time >= #{startFinishTime}
                   AND d.create_time &lt; #{endFinishTime}) p
                 CROSS JOIN (
            -- 所有需要展示的院区
            SELECT DISTINCT campusid
            FROM service_task
            WHERE service_type IN (6, 14)
              AND del_flag = 0
              AND campusid IS NOT NULL) c
                 LEFT JOIN service_task t
                           ON t.campusid = c.campusid
                               AND t.service_type IN (6, 14)
                               AND t.del_flag = 0
                 LEFT JOIN service_subtask_detail d
                           ON d.taskid = t.taskid
                               AND d.del_flag = 0
                               AND CASE #{type}
                                       WHEN 'year' THEN DATE_FORMAT(d.create_time, '%Y')
                                       WHEN 'quarter' THEN CONCAT(YEAR(d.create_time), '-Q', QUARTER(d.create_time))
                                       WHEN 'month' THEN DATE_FORMAT(d.create_time, '%Y-%m')
                                       WHEN 'week' THEN FLOOR(DATEDIFF(d.create_time, #{startFinishTime}) / 7) + 1
                                       ELSE DATE_FORMAT(d.create_time, '%Y-%m-%d')
                                       END = p.period
                               AND d.create_time >= #{startFinishTime}
                               AND d.create_time &lt; #{endFinishTime}
        GROUP BY p.period, c.campusid
        ORDER BY p.period, c.campusid;
    </select>
    <!-- 任务组(appltype=5)滚动调度用:拉本轮全量 subtask,作为下一轮的克隆模板 -->
    <select id="selectByScheduleIdAndLoop" resultMap="ServiceSubtaskResult">
        <include refid="selectServiceSubtaskVo"/>
        where schedule_id = #{scheduleId}
        and loop_count = #{loopCount}
        and (del_flag is null or del_flag = '0')
        order by schedule_seq asc, id asc
    </select>
    <select id="gethelibraryCountHyperlink" parameterType="com.smartor.domain.VO.HeLibraryCountVO"
            resultMap="ServiceSubtaskResult">
        select id,
        visit_count,
        is_visit_again,
        type,
        orgid,
        drcode,
        drname,
        leavehospitaldistrictcode,
        leavehospitaldistrictname,
        deptcode,
        deptname,
        sendstate,
        preachform,
        current_preachform,
        excep,
        nurse_id,
        nurse_name,
        taskid,
        task_name,
        visit_time,
        finishtime,
        endtime,
        senddate,
        suggest,
        result,
        templateid,
        templatename,
        patid,
        sendname,
        send_type,
        recordid,
        exrecallcount,
        continue_flag,
        continue_time_now,
        continue_content,
        continue_count,
        continue_time_next,
        task_situation,
        remark
        from service_subtask
        where 1=1
        and del_flag = 0
        AND service_type = 4
        <if test="orgid != null and orgid != ''">
            and orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            and campusid = #{campusid}
        </if>
        <if test="sendstate != null">
            AND sendstate = #{sendstate}
        </if>
        <if test="isFinished != null and isFinished != ''">
            and finishtime is not null
        </if>
        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
            AND leavehospitaldistrictcode IN
            <foreach collection="hospitaldistrictcodes" item="hospitaldistrictcode" open="(" separator=","
                     close=")">
                #{hospitaldistrictcode}
            </foreach>
        </if>
        <if test="deptcodes != null and deptcodes.size() > 0">
            AND deptcode IN
            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                     close=")">
                #{deptcode}
            </foreach>
        </if>
        <if test="starttime != null">AND starttime &gt;= #{starttime}</if>
        <if test="endtime != null">AND endtime &lt;= #{endtime}</if>
        <if test="visittime != null">AND DATE(visit_time) = DATE(#{visittime})</if>
        <if test="finishtime != null">AND DATE(finishtime) = DATE(#{finishtime})</if>
        <if test="hospType != null and hospType != ''">AND hosp_type = #{hospType}</if>
        order by endtime desc
        <if test="pageSize != null  and pageNum != null">
            limit ${pageSize} OFFSET ${pageNum}
        </if>
    </select>
    <select id="statQuestionOption" resultType="com.smartor.domain.ServiceSubtaskDetailRatioExport">
        SELECT
        d.questiontext AS questiontext,
        t.taskid AS taskid,
        t.task_name AS taskName,
        d.asrtext AS optionresult,
        d.score AS score,
        d.targetvalue AS targetvalue,
        d.value_type AS valueType,
        COUNT(*) AS count,
        CONCAT(ROUND(COUNT(*) * 100.0 / t2.total_count, 2), '%') AS ratio
        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
        INNER JOIN (
        SELECT
        t.taskid,
        d.questiontext,
        COUNT(*) AS total_count
        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
        <if test="taskIds != null and taskIds.size() > 0">
            AND t.taskid IN
            <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
                #{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>
        <if test="questiontext != null  and questiontext != ''">and d.questiontext like concat('%', #{questiontext},
            '%')
        </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
        ) t2 ON t2.taskid = t.taskid AND t2.questiontext = d.questiontext
        WHERE 1=1
        <if test="taskIds != null and taskIds.size() > 0">
            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>
        <if test="scoreStart != null">AND d.score &gt;= #{scoreStart}</if>
        <if test="scoreEnd != null">AND d.score &lt;= #{scoreEnd}</if>
        <if test="questiontext != null  and questiontext != ''">and d.questiontext like concat('%', #{questiontext},
            '%')
        </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,
        d.score,
        d.targetvalue,
        d.asrtext,
        d.value_type
        ORDER BY
        d.questiontext,
        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
        d.questiontext AS questiontext,
        t.taskid AS taskid,
        t.task_name AS taskName,
        d.asrtext AS optionresult,
        d.score AS score,
        s.deptcode AS deptcode,
        s.deptname AS deptname,
        s.leavehospitaldistrictcode AS leavehospitaldistrictcode,
        s.leavehospitaldistrictname AS leavehospitaldistrictname,
        d.targetvalue AS targetvalue,
        d.value_type AS valueType,
        COUNT(*) AS count,
        CONCAT(ROUND(COUNT(*) * 100.0 / t2.total_count, 2), '%') AS ratio
        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
        <!-- 去重:同一子任务同一题目只保留最新一条,避免重答/重复回调把同一道题重复计入项数(与 mzMydScoreRank 口径一致) -->
        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'
        )
        INNER JOIN (
        SELECT
        d.questiontext,
        COUNT(*) AS total_count
        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
        <!-- 去重:分母(该题有效答卷总数)与明细口径保持一致,保证各选项比例合计 100% -->
        AND d.id = (
        SELECT MAX(d4.id)
        FROM service_subtask_detail d4
        WHERE d4.sub_id = d.sub_id
        AND d4.questiontext &lt;=&gt; d.questiontext
        AND d4.del_flag = '0'
        )
        WHERE 1=1
        <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 &gt;= #{valueType}</if>
        <if test="scoreStart != null">AND d.score &gt;= #{scoreStart}</if>
        <if test="scoreEnd != null">AND d.score &lt;= #{scoreEnd}</if>
        <if test="questiontext != null  and questiontext != ''">and d.questiontext like concat('%', #{questiontext},
            '%')
        </if>
        AND s.sendstate = 6
        AND d.del_flag=0
        <!-- 分母与明细用同一套时间条件,否则选了日期区间后各选项比例合计不到 100% -->
        <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 d.questiontext
        ) t2 ON t2.questiontext = d.questiontext
        WHERE 1=1
        <!-- 去重:明细行同一子任务同一题目只保留最新一条,重答不重复计入项数 -->
        AND d.id = (
        SELECT MAX(d3.id)
        FROM service_subtask_detail d3
        WHERE d3.sub_id = d.sub_id
        AND d3.questiontext &lt;=&gt; d.questiontext
        AND d3.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>
        AND s.sendstate = 6
        AND d.del_flag=0
        <if test="valueType != null">AND d.value_type &gt;= #{valueType}</if>
        <if test="scoreStart != null">AND d.score &gt;= #{scoreStart}</if>
        <if test="scoreEnd != null">AND d.score &lt;= #{scoreEnd}</if>
        <if test="questiontext != null  and questiontext != ''">and d.questiontext like concat('%', #{questiontext},
            '%')
        </if>
        <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,
        d.score,
        d.targetvalue,
        d.asrtext,
        t.task_name,
        s.deptcode,
        s.deptname,
        s.leavehospitaldistrictcode,
        s.leavehospitaldistrictname,
        d.value_type
        ORDER BY
        d.questiontext,
        t.taskid
    </select>
    <select id="statQuestionOptionByKsOrBq" resultType="com.smartor.domain.DTO.QuestionCountKsOrBqDTO"
            parameterType="com.smartor.domain.VO.QuestionCountKsOrBqVO">
        SELECT
        s.deptname AS deptname,
        s.leavehospitaldistrictname AS leavehospitaldistrictname,
        d.questiontext AS questionText,
        d.targetvalue AS targetvalue, -- 所有选项拼接
        d.asrtext AS asrtext, -- 用户实际选中的选项
        d.score AS singleScore -- 单条记录的分数
        FROM service_subtask_detail d
        JOIN service_subtask s ON d.sub_id = s.id
        WHERE s.del_flag = 0
        AND d.del_flag = 0
        AND s.service_type IN ('6', '14')
        <if test="taskTemplateid != null">
            AND s.templateid = #{taskTemplateid}
        </if>
        <if test="endFinishTime != null">
            AND d.create_time &lt; #{endFinishTime}
        </if>
        <if test="startFinishTime != null">
            AND d.create_time &gt;= #{startFinishTime}
        </if>
        <if test="deptOrDistrict ==1">
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                AND leavehospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                         separator=","
                         close=")">
                    #{leavehospitaldistrictcode}
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                AND deptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>
        </if>
        <if test="deptOrDistrict ==2">
            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                AND leavehospitaldistrictcode IN
                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
                         separator=","
                         close=")">
                    #{leavehospitaldistrictcode}
                </foreach>
            </if>
            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
                OR deptcode IN
                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                         close=")">
                    #{leaveldeptcode}
                </foreach>
            </if>
        </if>
    </select>
    <select id="selectServiceSubtaskListDeadLine" parameterType="com.smartor.domain.entity.ServiceSubtaskEntity"
            resultMap="ServiceSubtaskResult">
        <include refid="selectServiceSubtaskVo"/>
        where 1=1
        and del_flag = 0
        and visit_count = 1
        and endtime
    </select>
    <!-- 门诊满意度综合得分排名(按科室或病区) -->
    <!-- 综合满意度 = Σ(评分明细权重) / 评分明细条数 × 100%,权重只取 1、0.8、0.6、0.4、0.2 五档 -->
    <select id="selectMzMydScoreRank" parameterType="com.smartor.domain.VO.MzMydScoreRankVO"
            resultType="com.smartor.domain.DTO.MzMydScoreRankDTO">
        SELECT
        t.code                   AS code,
        t.name                   AS name,
        ROUND(AVG(t.w) * 100, 2) AS score,
        COUNT(*)                 AS itemCount
        FROM (
        SELECT
        <choose>
            <when test='dimensionType == "2"'>
                s.leavehospitaldistrictcode AS code,
                s.leavehospitaldistrictname AS name,
            </when>
            <otherwise>
                s.deptcode AS code,
                s.deptname AS name,
            </otherwise>
        </choose>
        CAST(d.score AS DECIMAL(10,4)) AS w
        FROM service_subtask s
        INNER JOIN service_subtask_detail d ON d.sub_id = s.id
        WHERE s.del_flag = '0'
        AND d.del_flag = '0'
        AND s.sendstate = 6
        <if test="serviceTypes != null and serviceTypes.size() > 0">
            AND service_type IN
            <foreach collection="serviceTypes" item="serviceType" open="(" separator=","
                     close=")">
                #{serviceType}
            </foreach>
        </if>
<!--        &lt;!&ndash; 只取公式里的五档权重,空值/0/未答等既不进分子也不进分母 &ndash;&gt;-->
<!--        AND CAST(d.score AS DECIMAL(10,4)) IN (1, 0.8, 0.6, 0.4, 0.2)-->
        <!-- 同一子任务同一题目只保留最新一条,避免重答被重复计入项数 -->
        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'
        )
        <if test="orgid != null and orgid != ''">
            AND s.orgid = #{orgid}
        </if>
        <if test="campusid != null and campusid != ''">
            AND s.campusid = #{campusid}
        </if>
        <if test="startFinishTime != null">
            AND s.finishtime &gt;= #{startFinishTime}
        </if>
        <if test="endFinishTime != null">
            AND s.finishtime &lt; DATE_ADD(#{endFinishTime}, INTERVAL 1 DAY)
        </if>
        <if test="questionText != null  and questionText != ''">and d.questiontext like concat('%', #{questionText},
            '%')
        </if>
        <choose>
            <when test='dimensionType == "2"'>
                AND s.leavehospitaldistrictcode IS NOT NULL AND s.leavehospitaldistrictcode &lt;&gt; ''
                AND s.leavehospitaldistrictname IS NOT NULL AND s.leavehospitaldistrictname &lt;&gt; ''
                <if test="districtCodes != null and districtCodes.size() > 0">
                    AND s.leavehospitaldistrictcode IN
                    <foreach collection="districtCodes" item="districtCode" open="(" separator="," close=")">
                        #{districtCode}
                    </foreach>
                </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; ''
                <if test="deptCodes != null and deptCodes.size() > 0">
                    AND s.deptcode IN
                    <foreach collection="deptCodes" item="deptCode" open="(" separator="," close=")">
                        #{deptCode}
                    </foreach>
                </if>
            </otherwise>
        </choose>
        ) t
        GROUP BY t.code, t.name
        ORDER BY AVG(t.w) DESC, COUNT(*) DESC
        <if test="limit != null and limit > 0">
            LIMIT #{limit}
        </if>
    </select>
</mapper>