From 599beddc80e7e50b2099bacdbf7df2d89df00ef2 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期六, 07 十一月 2026 14:42:31 +0800
Subject: [PATCH] fix: 完成时状态与超时判断区分任务组

---
 ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml |  820 ++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 709 insertions(+), 111 deletions(-)

diff --git a/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml b/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
index dc7c0b4..789a25d 100644
--- a/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
+++ b/ruoyi-task/src/main/resources/mapper/task/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"/>
@@ -77,6 +78,7 @@
         <result property="leavehospitaldistrictname" column="leavehospitaldistrictname"/>
         <result property="excep" column="excep"/>
         <result property="remark" column="remark"/>
+        <result property="doctorOpinion" column="doctor_opinion"/>
         <result property="suggest" column="suggest"/>
         <result property="sendstate" column="sendstate"/>
         <result property="inhospid" column="inhospid"/>
@@ -85,6 +87,7 @@
         <result property="visitCount" column="visit_count"/>
         <result property="isVisitAgain" column="is_visit_again"/>
         <result property="upid" column="upid"/>
+        <result property="visitType" column="visit_type"/>
         <result property="visitTime" column="visit_time"/>
         <result property="visitDeptName" column="visit_dept_name"/>
         <result property="visitDeptCode" column="visit_dept_code"/>
@@ -105,6 +108,7 @@
         <result property="loopCount" column="loop_count"/>
         <result property="isManuallyCalled" column="is_manually_called"/>
         <result property="campusid" column="campusid"/>
+        <result property="diagType" column="diag_type"/>
     </resultMap>
 
     <resultMap type="com.ruoyi.task.domain.ServiceSubtaskCount" id="ServiceSubtaskResult2">
@@ -187,6 +191,7 @@
                long_send_time,
                openid,
                remark,
+               doctor_opinion,
                service_type,
                libtemplateid,
                libtemplatename,
@@ -207,6 +212,7 @@
                senduuid,
                result,
                finishtime,
+               finish_state,
                recordid,
                exrecallcount,
                pulltime,
@@ -258,6 +264,69 @@
                task_situation,
                is_manually_called
         from service_subtask
+    </sql>
+
+    <!-- 瓒呮椂瀹屾垚銆佸欢鏈熷畬鎴愰�昏緫 瀹屾垚鏃剁姸鎬�(finish_state)璁$畻锛氫粎褰撴湰娆℃洿鏂版妸 sendstate 缃负 6锛堥殢璁垮畬鎴愶級鏃朵娇鐢ㄣ��
+         鍩哄噯鎴鏃堕棿 base锛�
+           闈炰换鍔$粍(appltype != 5)锛歟ndtime + send_day(缂虹渷1) + (危 service_subtask_preachform.compensate_time + 1) 澶╋紱
+           浠诲姟缁�(appltype = 5)锛氭寜 schedule_benchmark 瑙f瀽鍩哄噯鏃堕棿
+                                   (1-鍏ラ櫌 2-鍑洪櫌 3-灏辫瘖 4-鍏ョ粍 0/鍏跺畠-鑷畾涔夋椂闂�)
+                                   + day_offset + (危 compensate_time + 1) 澶╋紙涓嶅彔鍔� send_day锛夛紱
+         涓� RyTask/RedisMqReceiver 鐨� isSubtaskDeadline銆乻electTimelyRateBatch 鍙e緞淇濇寔涓�鑷达紱
+         delay = COALESCE(service_task.delay_finish_days, 0)锛�
+         finish_time = COALESCE(鍏ュ弬 finishtime, 搴撲腑 finishtime, NOW())锛�
+           finish_time <= base                                    -> 1 姝e父瀹屾垚
+           delay > 0 涓� finish_time <= base + delay               -> 2 寤舵湡瀹屾垚
+           鍏朵綑锛堝惈 delay 涓� 0 鎴� NULL銆佹垨宸茶秴鍑哄欢鏈熺獥鍙o級          -> 3 瓒呮椂瀹屾垚
+         finishtime 涓虹┖鏃剁敤 NOW() 鍏滃簳锛屼繚璇佸畬鎴愭椂涓�瀹氬啓鍏ョ姸鎬侊紱
+         base 涓虹┖锛坋ndtime / schedule / day_offset 缂哄け锛夋椂淇濇寔鍘熷�间笉瑕嗙洊銆� -->
+    <sql id="finishDeadlineExpr">
+        DATE_ADD(
+        CASE
+            WHEN COALESCE(st.appltype, '') = '5' THEN
+                DATE_ADD(
+                CASE sts.schedule_benchmark
+                    WHEN 1 THEN service_subtask.starttime
+                    WHEN 2 THEN service_subtask.endtime
+                    WHEN 3 THEN service_subtask.admindate
+                    WHEN 4 THEN service_subtask.create_time
+                    ELSE sts.schedule_benchmark_custom_time
+                END,
+                INTERVAL std.day_offset DAY)
+            ELSE
+                DATE_ADD(service_subtask.endtime, INTERVAL COALESCE(st.send_day, 1) DAY)
+        END,
+        INTERVAL (COALESCE((SELECT SUM(CAST(COALESCE(NULLIF(TRIM(pf.compensate_time), ''), '0') AS SIGNED))
+        FROM service_subtask_preachform pf
+        WHERE pf.subid = service_subtask.id AND pf.del_flag = 0), 0) + 1) DAY)
+    </sql>
+
+    <sql id="finishStateExpr">
+        COALESCE((
+        SELECT
+        CASE
+        WHEN <include refid="finishDeadlineExpr"/> IS NULL THEN NULL
+        WHEN COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
+        &lt;= <include refid="finishDeadlineExpr"/>
+        THEN 1
+        WHEN COALESCE(st.delay_finish_days, 0) &gt; 0
+        AND COALESCE(#{finishtime,jdbcType=TIMESTAMP}, service_subtask.finishtime, NOW())
+        &lt;= DATE_ADD(<include refid="finishDeadlineExpr"/>,
+        INTERVAL COALESCE(st.delay_finish_days, 0) DAY)
+        THEN 2
+        ELSE 3
+        END
+        FROM (SELECT 1) dummy
+        LEFT JOIN service_task st ON st.taskid = service_subtask.taskid
+        LEFT JOIN service_task_schedule sts ON sts.id = service_subtask.schedule_id
+        LEFT JOIN service_task_schedule_detail std ON std.id = service_subtask.schedule_detail_id
+        LIMIT 1
+        ), finish_state)
+    </sql>
+
+    <!-- 褰撳墠鎵ц鏂瑰紡锛氶娆¢殢璁� current_preachform 涓虹┖鏃讹紝鍙� preachform 鐨勭涓�涓墽琛屾柟寮忓厹搴� -->
+    <sql id="effectiveCurrentPreachformExpr">
+        COALESCE(NULLIF(TRIM(current_preachform), ''), TRIM(SUBSTRING_INDEX(preachform, ',', 1)))
     </sql>
 
     <select id="selectServiceSubtaskBySendstate" parameterType="com.ruoyi.task.domain.entity.ServiceSubtaskEntity"
@@ -325,6 +394,37 @@
             <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
                 #{lvDeptCodeItem}
             </foreach>
+        </if>
+    </sql>
+
+    <!-- 鍗冲皢瓒呮椂杩囨护锛氭槸鍚﹀嵆灏嗚秴鏃�(soonTimeout=1) + 鍗冲皢瓒呮椂鏃堕棿(soonTimeoutHours锛屽崟浣嶅皬鏃�)銆�
+         鎴鏃堕棿鍙e緞涓� 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>
 
@@ -572,7 +672,10 @@
         <if test="campusid != null  and campusid != ''">and campusid = #{campusid}</if>
         <if test="pid != null ">and pid = #{pid}</if>
         <if test="preachform != null ">and preachform = #{preachform}</if>
-        <if test="currentPreachform != null ">and current_preachform = #{currentPreachform}</if>
+        <if test="currentPreachform != null and currentPreachform != '' ">and current_preachform = #{currentPreachform}</if>
+        <if test="currentPreachforms != null and currentPreachforms != ''">
+            and FIND_IN_SET(<include refid="effectiveCurrentPreachformExpr"/>, #{currentPreachforms})
+        </if>
         <if test="guid != null  and guid != ''">and guid = #{guid}</if>
         <if test="textParam != null  and textParam != ''">and text_param = #{textParam}</if>
         <if test="bedNo != null">and bed_no = #{bedNo}</if>
@@ -602,6 +705,7 @@
             AND visit_count > 1
         </if>
         <if test="remark != null">and remark = #{remark}</if>
+        <if test="doctorOpinion != null and doctorOpinion != ''">and doctor_opinion like concat('%', #{doctorOpinion}, '%')</if>
         <if test="suggest != null">and suggest = #{suggest}</if>
         <if test="createBy != null">and create_by = #{createBy}</if>
         <if test="taskGuid != null">and task_guid = #{taskGuid}</if>
@@ -616,6 +720,7 @@
                 #{visitDeptCode}
             </foreach>
         </if>
+        <include refid="soonTimeoutFilter"/>
     </sql>
 
     <!-- 鎺掑簭鐗囨锛氫繚鎸佸師璇箟锛屽彧鍦ㄨ皟鐢ㄦ柟鏄惧紡浼� sort 鏃舵墠鎷� order by
@@ -716,7 +821,7 @@
         <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
         <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
         <if test="managementDoctor != null">and management_doctor = #{managementDoctor}</if>
-        <if test="currentPreachform != null">and current_preachform = #{currentPreachform}</if>
+        <if test="currentPreachform != null and currentPreachform != '' ">and current_preachform = #{currentPreachform}</if>
         <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
         <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
         <if test="continueTimeNow != null ">and continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
@@ -775,7 +880,7 @@
         <if test="visitTime != null ">and visit_time = #{visitTime}</if>
         <if test="visitDeptCode != null">and visit_dept_code = #{visitDeptCode}</if>
         <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
-        <if test="currentPreachform != null">and current_preachform = #{currentPreachform}</if>
+        <if test="currentPreachform != null and currentPreachform != '' ">and current_preachform = #{currentPreachform}</if>
         <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
         <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
         <if test="managementDoctor != null">and management_doctor like concat('%',#{managementDoctor}, '%')</if>
@@ -783,6 +888,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.ruoyi.task.domain.ServiceSubtask" resultType="Integer">
@@ -845,26 +951,57 @@
         </if>
         SUM(CASE WHEN ss.finishtime IS NOT NULL AND DATE(ss.visit_time) &lt;= DATE(ss.finishtime) THEN 1 ELSE 0 END) /
         COUNT(*) AS finishRate,
-        <if test="rateDay != null">
-            SUM(CASE WHEN ss.finishtime IS NOT NULL
-            AND DATEDIFF(ss.finishtime, ss.endtime) &lt;= #{rateDay} + COALESCE(cp.compensate_days, 0)
-            THEN 1 ELSE 0 END) / COUNT(*) AS rate
-        </if>
-        <if test="rateDay == null">
-            NULL AS rate
-        </if>
+        <!-- 鍙婃椂鐜囧熀鍑嗭細
+             1) 闈炰换鍔$粍(appltype != 5)锛歟ndtime + send_day(缂虹渷1) + 危琛ュ伩澶╂暟 + 1 澶╋紱
+             2) 浠诲姟缁�(appltype = 5)锛氭寜 schedule_benchmark 瑙f瀽鍩哄噯鏃堕棿
+                (1-鍏ラ櫌鏃堕棿 2-鍑洪櫌鏃堕棿 3-灏辫瘖鏃堕棿 4-鍏ョ粍鏃堕棿 0/鍏跺畠-鑷畾涔夋椂闂�)
+                + day_offset + 危琛ュ伩澶╂暟 + 1 澶╋紙涓嶅啀鍙犲姞 send_day锛夛紱
+             瀹屾垚鏃堕棿 finishtime <= 鍩哄噯鏃堕棿璁′负鍙婃椂锛屽垎姣嶄粛涓哄簲闅忚鏁般��
+             琛ュ伩澶╂暟鍙彇 service_subtask_preachform.compensate_time 鐨勬眹鎬诲�间竴娆★紝
+             閬垮厤涓庤ˉ鍋挎槑缁嗙浉鍔犻�犳垚閲嶅琛ュ伩鎴栦竴瀵瑰鏀惧ぇ鍒嗘瘝銆� -->
+        SUM(CASE
+            WHEN ss.finishtime IS NOT NULL
+             AND COALESCE(st.appltype, '') != '5'
+             AND ss.endtime IS NOT NULL
+             AND ss.finishtime &lt;= DATE_ADD(
+                   DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
+                   INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY)
+            THEN 1
+            WHEN ss.finishtime IS NOT NULL
+             AND st.appltype = '5'
+             AND std.day_offset IS NOT NULL
+             AND ss.finishtime &lt;= DATE_ADD(
+                   DATE_ADD(
+                       CASE sts.schedule_benchmark
+                         WHEN 1 THEN ss.starttime
+                         WHEN 2 THEN ss.endtime
+                         WHEN 3 THEN ss.admindate
+                         WHEN 4 THEN ss.create_time
+                         ELSE sts.schedule_benchmark_custom_time
+                       END, INTERVAL std.day_offset DAY),
+                   INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY)
+            THEN 1
+            ELSE 0
+        END) / COUNT(*) AS rate
         FROM service_subtask ss
-        <if test="rateDay != null">
-            <!-- 琛ュ伩鏃堕棿锛氬厛鎸夊瓙浠诲姟鑱氬悎鎴愪竴琛屽啀鍏宠仈锛岄伩鍏嶄竴瀵瑰鏀惧ぇ COUNT(*) 鍒嗘瘝銆�
-                 compensate_time 鏄� varchar 涓斿彲鑳藉瓨鎴愮┖涓诧紝闇�鍏堢疆绌哄啀杞暟瀛� -->
-            LEFT JOIN (
-            SELECT subid,
-            SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
-            FROM service_subtask_preachform
-            WHERE del_flag = 0
-            GROUP BY subid
-            ) cp ON cp.subid = ss.id
-        </if>
+        <!-- 杩欓噷鎸� taskid / 鍘嗗彶 schedule 蹇収鍏宠仈锛屼笉鎸� del_flag 杩囨护锛�
+             浠诲姟鎴栬鍒掕缂栬緫鍚庢棫 schedule/detail 浼氱疆涓� del_flag=1锛�
+             浣嗗巻鍙� subtask 浠嶅紩鐢ㄦ棫 ID锛岃繃婊や細涓㈠け鍘嗗彶鍙婃椂鐜囧熀鍑嗐�� -->
+        LEFT JOIN service_task st
+               ON st.taskid = ss.taskid
+        LEFT JOIN service_task_schedule sts
+               ON sts.id = ss.schedule_id
+        LEFT JOIN service_task_schedule_detail std
+               ON std.id = ss.schedule_detail_id
+        <!-- 琛ュ伩鏃堕棿锛氬厛鎸夊瓙浠诲姟鑱氬悎鎴愪竴琛屽啀鍏宠仈锛岄伩鍏嶄竴瀵瑰鏀惧ぇ COUNT(*) 鍒嗘瘝銆�
+             compensate_time 鏄� varchar 涓斿彲鑳藉瓨鎴愮┖涓诧紝闇�鍏堢疆绌哄啀杞暟瀛� -->
+        LEFT JOIN (
+        SELECT subid,
+        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
+        FROM service_subtask_preachform
+        WHERE del_flag = 0
+        GROUP BY subid
+        ) cp ON cp.subid = ss.id
         WHERE ss.del_flag = 0
         AND ss.visit_time IS NOT NULL
         AND ss.sendstate != 4
@@ -949,6 +1086,12 @@
         </if>
         <if test="managementDoctorCode != null">and management_doctor_code = #{managementDoctorCode}</if>
         <if test="managementDoctor != null">and management_doctor like concat('%',#{managementDoctor}, '%')</if>
+        <if test="startOutHospTime != null">
+            AND date_format(endtime,'%y%m%d') &gt;= date_format(#{startOutHospTime},'%y%m%d')
+        </if>
+        <if test="endOutHospTime != null">
+            AND date_format(endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
+        </if>
         ) a where a.finished =1)
     </select>
 
@@ -1002,6 +1145,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>
@@ -1064,6 +1208,7 @@
             <if test="nurseName != null ">nurse_name,</if>
             <if test="nurseId != null ">nurse_id,</if>
             <if test="remark != null ">remark,</if>
+            <if test="doctorOpinion != null ">doctor_opinion,</if>
             <if test="suggest != null ">suggest,</if>
             <if test="sendstate != null ">sendstate,</if>
             <if test="inhospid != null ">inhospid,</if>
@@ -1078,7 +1223,7 @@
             <if test="visitDeptName != null">visit_dept_name,</if>
             <if test="managementDoctorCode != null">management_doctor_code,</if>
             <if test="managementDoctor != null">management_doctor,</if>
-            <if test="currentPreachform != null">current_preachform,</if>
+            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform,</if>
             <if test="taskSituation != null">task_situation,</if>
             <if test="isabnormal != null">isabnormal,</if>
             <if test="continueFlag != null ">continue_flag,</if>
@@ -1109,6 +1254,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>
@@ -1168,6 +1314,7 @@
             <if test="nurseName != null ">#{nurseName},</if>
             <if test="nurseId != null ">#{nurseId},</if>
             <if test="remark != null ">#{remark},</if>
+            <if test="doctorOpinion != null ">#{doctorOpinion},</if>
             <if test="suggest != null ">#{suggest},</if>
             <if test="sendstate != null ">#{sendstate},</if>
             <if test="inhospid != null ">#{inhospid},</if>
@@ -1182,7 +1329,7 @@
             <if test="visitDeptName != null">#{visitDeptName},</if>
             <if test="managementDoctorCode != null">#{managementDoctorCode},</if>
             <if test="managementDoctor != null">#{managementDoctor},</if>
-            <if test="currentPreachform != null">#{currentPreachform},</if>
+            <if test="currentPreachform != null and currentPreachform != '' ">#{currentPreachform},</if>
             <if test="taskSituation != null">#{taskSituation},</if>
             <if test="isabnormal != null">#{isabnormal},</if>
             <if test="continueFlag != null ">#{continueFlag},</if>
@@ -1198,6 +1345,15 @@
             <if test="isManuallyCalled != null ">#{isManuallyCalled},</if>
         </trim>
     </insert>
+
+    <!-- 浜屾闅忚骞傜瓑锛氫粎褰� is_visit_again 涓嶆槸鐩爣鍊兼椂鏇存柊锛岃繑鍥炲彈褰卞搷琛屾暟 -->
+    <update id="updateIsVisitAgainById">
+        update service_subtask
+        set is_visit_again = #{isVisitAgain},
+            update_time = now()
+        where id = #{id}
+          and (is_visit_again is null or is_visit_again &lt;&gt; #{isVisitAgain})
+    </update>
 
     <update id="updateServiceSubtask" parameterType="com.ruoyi.task.domain.ServiceSubtask">
         update service_subtask
@@ -1255,6 +1411,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>
@@ -1272,6 +1429,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1285,7 +1443,7 @@
             <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
             <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
             <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
-            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
+            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
             <if test="taskSituation != null">task_situation=#{taskSituation},</if>
             <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
             <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1301,6 +1459,25 @@
             <if test="isManuallyCalled != null ">is_manually_called = #{isManuallyCalled},</if>
         </trim>
         where id = #{id}
+    </update>
+
+    <!-- 寰俊鍏紬鍙峰凡棰嗗彇鏈~鍐欐寜榛樿閫夐」瀹屾垚锛氬彧鏇存柊褰撳墠瀛愪换鍔°��
+         鏉′欢 sendstate=1 + 鏈彁浜わ紝淇濊瘉鍙鐞嗗綋鍓嶈棰嗗彇涓旀湭濉啓鐨勫瓙浠诲姟锛屼笉娉㈠強鍏朵粬浠诲姟銆� -->
+    <update id="updateWechatDefaultComplete" parameterType="com.ruoyi.task.domain.ServiceSubtask">
+        update service_subtask
+        set sendstate = 6,
+            finishtime = #{finishtime},
+            result = #{result},
+            remark = case
+                         when remark is null or remark = '' then #{remark}
+                         else concat(remark, ',', #{remark})
+                     end,
+            update_time = now(),
+            finish_state = <include refid="finishStateExpr"/>
+        where id = #{id}
+          and orgid = #{orgid}
+          and sendstate = 1
+          and (submit is null or submit != 1)
     </update>
 
     <update id="updateServiceSubtaskByPatId" parameterType="com.ruoyi.task.domain.ServiceSubtask">
@@ -1359,6 +1536,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>
@@ -1377,6 +1555,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1389,7 +1568,7 @@
             <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
             <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
             <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
-            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
+            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
             <if test="taskSituation != null">task_situation=#{taskSituation},</if>
             <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
             <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1415,6 +1594,7 @@
             <if test="sex != null">sex = #{sex},</if>
             <if test="delFlag != null">del_flag = #{delFlag},</if>
             <if test="sendstate != null ">sendstate = #{sendstate},</if>
+            <if test="sendstate != null and sendstate == 6">finish_state = <include refid="finishStateExpr"/>,</if>
             <if test="age != null">age = #{age},</if>
             <if test="sfzh != null">sfzh = #{sfzh},</if>
             <if test="addr != null">addr = #{addr},</if>
@@ -1482,6 +1662,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1493,7 +1674,7 @@
             <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
             <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
             <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
-            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
+            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
             <if test="taskSituation != null">task_situation=#{taskSituation},</if>
             <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
             <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1526,6 +1707,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>
@@ -1593,6 +1775,7 @@
             <if test="nurseName != null ">nurse_name=#{nurseName},</if>
             <if test="nurseId != null ">nurse_id=#{nurseId},</if>
             <if test="remark != null ">remark=#{remark},</if>
+            <if test="doctorOpinion != null ">doctor_opinion=#{doctorOpinion},</if>
             <if test="suggest != null ">suggest=#{suggest},</if>
             <if test="inhospid != null ">inhospid=#{inhospid},</if>
             <if test="patfrom != null ">patfrom=#{patfrom},</if>
@@ -1604,7 +1787,7 @@
             <if test="visitDeptName != null">visit_dept_name = #{visitDeptName},</if>
             <if test="managementDoctorCode != null">management_doctor_code = #{managementDoctorCode},</if>
             <if test="managementDoctor != null">management_doctor=#{managementDoctor},</if>
-            <if test="currentPreachform != null">current_preachform=#{currentPreachform},</if>
+            <if test="currentPreachform != null and currentPreachform != '' ">current_preachform=#{currentPreachform},</if>
             <if test="taskSituation != null">task_situation=#{taskSituation},</if>
             <if test="isabnormal != null">isabnormal=#{isabnormal},</if>
             <if test="continueFlag != null ">continue_flag = #{continueFlag},</if>
@@ -1633,6 +1816,35 @@
         where patid = #{patid} and taskid = #{taskid}
     </update>
 
+    <!-- 浜哄伐闅忚锛氳蒋鍒犻櫎鎮h�呭嚭闄㈣褰曞搴旂殑鏈畬鎴愰殢璁夸换鍔� -->
+    <update id="deleteUnfinishedServiceSubtaskByInhospids">
+        update service_subtask
+        set del_flag = 1
+        where del_flag = 0
+          and patid = #{patid}
+          and sendstate in
+          <foreach collection="sendstates" item="sendstate" open="(" separator="," close=")">
+              #{sendstate}
+          </foreach>
+          and inhospid in
+          <foreach collection="inhospids" item="inhospid" open="(" separator="," close=")">
+              #{inhospid}
+          </foreach>
+    </update>
+
+    <!-- 浜哄伐闅忚閲嶅鏍¢獙锛氬悓涓�鎮h�呭悓涓�浠诲姟涓嬫槸鍚﹀凡鏈夋湭瀹屾垚闅忚浠诲姟 -->
+    <select id="countUnfinishedServiceSubtaskByPatidAndTaskid" resultType="int">
+        select count(1)
+        from service_subtask
+        where del_flag = 0
+          and patid = #{patid}
+          and taskid = #{taskid}
+          and sendstate in
+          <foreach collection="sendstates" item="sendstate" open="(" separator="," close=")">
+              #{sendstate}
+          </foreach>
+    </select>
+
     <select id="getSfStatistics" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
             resultMap="ServiceSubtaskResult">
         select id,
@@ -1650,13 +1862,17 @@
         leaveldeptname,
         sendstate,
         preachform,
-        current_preachform,
+        <include refid="effectiveCurrentPreachformExpr"/> AS current_preachform,
         excep,
         nurse_id,
         nurse_name,
         taskid,
         task_name,
+        <if test="groupKey != null and groupKey == 'taskid'">
+            (SELECT st.diag_type FROM service_task st WHERE st.taskid = service_subtask.taskid LIMIT 1) AS diag_type,
+        </if>
         visit_time,
+        visit_dept_code,
         finishtime,
         endtime,
         senddate,
@@ -1674,7 +1890,9 @@
         continue_content,
         continue_count,
         continue_time_next,
-        task_situation
+        task_situation,
+        finish_state,
+        is_manually_called
         from service_subtask
         where 1=1
         and del_flag = 0
@@ -1701,6 +1919,13 @@
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
+            </foreach>
+        </if>
+        <if test="taskIds != null and taskIds.size() > 0">
+            AND taskid IN
+            <foreach collection="taskIds" item="taskId" open="(" separator=","
+                     close=")">
+                #{taskId}
             </foreach>
         </if>
         <if test="serviceType != null and serviceType.size() > 0">
@@ -1861,9 +2086,17 @@
     <select id="getSfStatisticsGroupKey" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
             resultType="String">
         select
-        <if test="groupKey != null and groupKey != ''">
-            ${groupKey} as groupKey
-        </if>
+        <choose>
+            <when test="groupColumn != null and groupColumn != ''">
+                ${groupColumn} as groupKey
+            </when>
+            <when test="groupKey != null and groupKey != ''">
+                ${groupKey} as groupKey
+            </when>
+            <otherwise>
+                NULL as groupKey
+            </otherwise>
+        </choose>
         from service_subtask
         where 1=1
         and del_flag = 0
@@ -1877,14 +2110,39 @@
              鍗楀崕婊℃剰搴﹀瓙浠诲姟鐨� leavehospitaldistrictcode 鏈� 83%锛�3236/3811锛変负 NULL锛�
              鍘熸潵銆岀瀹� AND 鐥呭尯銆嶄袱涓潯浠舵案杩滀笉鍙兘鍚屾椂鎴愮珛 鈫� 鏁村紶缁熻琛ㄧ┖銆�
              鍒ょ┖鏀捐鍙奖鍝嶇梾鍖轰负绌虹殑璁板綍锛屾湁鐥呭尯鐨勬暟鎹涓轰笉鍙樸�� -->
-        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
-            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
-                     close=")">
-                #{leavehospitaldistrictcode}
-            </foreach>
-            )
-        </if>
+        <choose>
+            <!-- 婊℃剰搴︾粺璁★紙getSfStatisticsScript锛夛細鏀圭敤鈥滄湁鏁堢梾鍖衡�濆垪锛�
+                 鍑洪櫌鐥呭尯涓虹┖鍒欏洖閫�鍒板湪闄㈢梾鍖恒�傜瓫閫夊洜姝ょ湡姝g敓鏁堬紝
+                 绌虹梾鍖虹殑璁板綍涔熶細褰掑埌瀹冪湡瀹炵殑鍦ㄩ櫌鐥呭尯锛岃�屼笉鏄贩杩涚敤鎴烽�変腑鐨勭梾鍖恒�� -->
+            <when test="districtColumn != null and districtColumn != ''">
+                <!-- 鍑洪櫌鐥呭尯涓虹┖鍥為��鍒板湪闄㈢梾鍖恒�傝繖閲屽埢鎰忓啓鎴� OR 灞曞紑鑰屼笉鏄� IFNULL(a,b) IN (...)锛�
+                     鍒楄鍑芥暟鍖呬綇灏辩敤涓嶄簡绱㈠紩锛�56 涓囪鏄庣粏琛ㄤ笂鏄叏琛ㄦ壂锛堝疄娴� 5.1s 鈫� 0.7s锛夈��
+                     涓や釜鍒嗘敮鍒嗗埆鍛戒腑 idx_ss_district_pair 鐨� (leave..., hospital...) 鍓嶇紑銆� -->
+                <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                    AND (leavehospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    OR (leavehospitaldistrictcode IS NULL AND hospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    ))
+                </if>
+            </when>
+            <otherwise>
+                <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                    AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    )
+                </if>
+            </otherwise>
+        </choose>
         <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
             AND leaveldeptcode IN
             <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
@@ -1912,6 +2170,13 @@
             <foreach collection="hospitaldeptcodes" item="hospitaldeptcode" open="(" separator=","
                      close=")">
                 #{hospitaldeptcode}
+            </foreach>
+        </if>
+        <if test="taskIds != null and taskIds.size() > 0">
+            AND taskid IN
+            <foreach collection="taskIds" item="taskId" open="(" separator=","
+                     close=")">
+                #{taskId}
             </foreach>
         </if>
         <if test="serviceType != null and serviceType.size() > 0">
@@ -1947,11 +2212,20 @@
         <if test="visitCount != null and visitCount > 1">
             AND visit_count > 1
         </if>
-        <if test="groupKey != null and groupKey != ''">
-            group by ${groupKey}
-        </if>
+        <choose>
+            <when test="groupColumn != null and groupColumn != ''">
+                group by ${groupColumn}
+                order by ${groupColumn}
+            </when>
+            <when test="groupKey != null and groupKey != ''">
+                group by ${groupKey}
+                order by ${groupKey}
+            </when>
+        </choose>
+        <!-- pageNum 鏄� 1 璧峰鐨勶紝OFFSET 蹇呴』鐢� (pageNum-1)*pageSize锛�
+             浠ュ墠鐩存帴鍐� OFFSET ${pageNum}锛岀涓�椤典細璺虫帀绗竴鏉″垎缁勩�� -->
         <if test="pageSize != null  and pageNum != null">
-            limit ${pageSize} OFFSET ${pageNum}
+            limit ${pageSize} OFFSET ${pageOffset}
         </if>
     </select>
 
@@ -1977,6 +2251,10 @@
     -->
     <resultMap type="com.ruoyi.task.domain.ServiceSubtaskStatistic" id="ServiceSubtaskScriptStatisticResult">
         <result property="groupCode" column="group_code"/>
+        <result property="groupName" column="group_name"/>
+        <result property="taskId" column="taskid"/>
+        <result property="taskName" column="task_name"/>
+        <result property="dischargeCount" column="discharge_count"/>
         <result property="firstVisitCount" column="first_visit_count"/>
         <result property="againVisitCount" column="again_visit_count"/>
         <result property="drcode" column="drcode"/>
@@ -2025,6 +2303,19 @@
             resultMap="ServiceSubtaskScriptStatisticResult">
         select
         IFNULL(${groupColumn}, 'Unknown') as group_code,
+        <!-- 鍒嗙粍閿搴旂殑鏄剧ず鍚嶏細鐥呭尯缁熻鏃跺氨鏄繖涓�缁勭殑鐥呭尯鍚嶃��
+             浠ュ墠杩欎竴鍒楁槸 MAX(hospitaldistrictname)锛屼竴涓粍娣蜂簡澶氫釜鐥呭尯鏃朵細闅忔満鏄剧ず鍏朵腑涓�涓紝
+             浜庢槸鍑虹幇鈥滈�夎娑插唴绉戠梾鍖哄嵈鏄剧ず楠ㄧ涓績涓夌梾鍖衡�濄�� -->
+        <choose>
+            <when test="groupKey != null and groupKey == 'taskid'">
+                MAX(task_name) as group_name,
+            </when>
+            <otherwise>
+                MAX(IFNULL(leavehospitaldistrictname, hospitaldistrictname)) as group_name,
+            </otherwise>
+        </choose>
+        MAX(taskid) as taskid,
+        MAX(task_name) as task_name,
         MAX(drcode) as drcode,
         MAX(drname) as drname,
         MAX(leaveldeptcode) as deptcode,
@@ -2056,7 +2347,10 @@
         SUM(CASE WHEN visit_count > 1 and sendstate is not null and current_preachform = '3' THEN 1 ELSE 0 END) as voice_again,
         SUM(CASE WHEN visit_count > 1 and sendstate is not null and current_preachform = '4' THEN 1 ELSE 0 END) as sms_again,
         SUM(CASE WHEN visit_count > 1 and sendstate is not null and current_preachform = '5' THEN 1 ELSE 0 END) as we_chat_again,
-        SUM(CASE WHEN visit_count > 1 and sendstate is not null and excep = '1' THEN 1 ELSE 0 END) as abnormal_again
+        SUM(CASE WHEN visit_count > 1 and sendstate is not null and excep = '1' THEN 1 ELSE 0 END) as abnormal_again,
+        <!-- 鍑洪櫌浜烘锛氬師鏉ョ殑鍐呭瓨瀹炵幇鏄� serviceSubtaskList.size()锛屽嵆鍒嗙粍鍐呭懡涓殑瀛愪换鍔℃潯鏁般��
+             鏀规垚鏈鍙ュ悗涓�鐩存病鏈夎仛鍚堣繖涓�鍒楋紝椤甸潰涓娾�滃嚭闄汉娆♀�濇亽涓� 0锛屼笌搴旈殢璁夸汉娆¤嚜鐩哥煕鐩俱�� -->
+        COUNT(*) as discharge_count
         from service_subtask
         where 1=1
         and del_flag = 0
@@ -2066,14 +2360,37 @@
         <if test="campusid != null and campusid != ''">
             and campusid = #{campusid}
         </if>
-        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
-            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
-                     close=")">
-                #{leavehospitaldistrictcode}
-            </foreach>
-            )
-        </if>
+        <choose>
+            <!-- 婊℃剰搴︾粺璁★細鏀圭敤鈥滄湁鏁堢梾鍖衡�濆垪锛堝嚭闄㈢梾鍖轰负绌哄洖閫�鍦ㄩ櫌鐥呭尯锛夛紝绛涢�夋墠鐪熸鐢熸晥 -->
+            <when test="districtColumn != null and districtColumn != ''">
+                <!-- 鍑洪櫌鐥呭尯涓虹┖鍥為��鍒板湪闄㈢梾鍖恒�傝繖閲屽埢鎰忓啓鎴� OR 灞曞紑鑰屼笉鏄� IFNULL(a,b) IN (...)锛�
+                     鍒楄鍑芥暟鍖呬綇灏辩敤涓嶄簡绱㈠紩锛�56 涓囪鏄庣粏琛ㄤ笂鏄叏琛ㄦ壂锛堝疄娴� 5.1s 鈫� 0.7s锛夈��
+                     涓や釜鍒嗘敮鍒嗗埆鍛戒腑 idx_ss_district_pair 鐨� (leave..., hospital...) 鍓嶇紑銆� -->
+                <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                    AND (leavehospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    OR (leavehospitaldistrictcode IS NULL AND hospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    ))
+                </if>
+            </when>
+            <otherwise>
+                <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                    AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    )
+                </if>
+            </otherwise>
+        </choose>
         <if test="deptcodes != null and deptcodes.size() > 0">
             AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
@@ -2134,8 +2451,10 @@
         <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 IFNULL(${groupKey}, 'Unknown') IN
+        <!-- 杩囨护鍒楀繀椤讳笌 GROUP BY 鐨勫垪涓�鑷达細閮界敤 groupColumn锛堟弧鎰忓害鐥呭尯缁熻鏃跺畠鏄�
+             IFNULL(鍑洪櫌鐥呭尯, 鍦ㄩ櫌鐥呭尯) 杩欐牱鐨勮〃杈惧紡锛夛紝鍚﹀垯鑱氬悎鍑烘潵鐨勫垎缁勯敭杩囨护涓嶄笂锛岄鐩噺浼氬叏鍙� 0銆� -->
+        <if test="groupColumn != null and groupColumn != '' and groupKeyList != null and groupKeyList.size>0">
+            AND IFNULL(${groupColumn}, 'Unknown') IN
             <foreach collection="groupKeyList" item="key" open="(" separator=","
                      close=")">
                 #{key}
@@ -2153,7 +2472,14 @@
     <select id="getSfStatisticsScriptJoySum" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
             resultType="map">
         select
-        IFNULL(t.${groupColumn}, 'Unknown') as groupCode,
+        <choose>
+            <when test="districtColumn != null and districtColumn != ''">
+                IFNULL(${districtColumn}, 'Unknown') as groupCode,
+            </when>
+            <otherwise>
+                IFNULL(t.${groupColumn}, 'Unknown') as groupCode,
+            </otherwise>
+        </choose>
         SUM(CASE WHEN a.categoryid IN(${configValue})
         THEN 1 ELSE 0 END) as joyAllCount,
         SUM(CASE WHEN a.categoryid IN(${configValue})
@@ -2171,14 +2497,38 @@
         <if test="campusid != null and campusid != ''">
             and t.campusid = #{campusid}
         </if>
-        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND (t.leavehospitaldistrictcode IS NULL OR t.leavehospitaldistrictcode IN
-            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
-                     close=")">
-                #{leavehospitaldistrictcode}
-            </foreach>
-            )
-        </if>
+        <choose>
+            <!-- 婊℃剰搴︾粺璁★細涓庤仛鍚堜富鏌ヨ涓�鑷达紝鐢ㄢ�滄湁鏁堢梾鍖衡�濆垪杩囨护锛�
+                 鍚﹀垯棰樼洰閲忎細鍜屼富鏌ヨ鐨勮瀵逛笉涓婏紙鍒嗙粍閿笉涓�鏍凤紝杩囨护涓嶄笂灏卞叏鍙� 0锛夈�� -->
+            <when test="districtColumn != null and districtColumn != ''">
+                <!-- 鍑洪櫌鐥呭尯涓虹┖鍥為��鍒板湪闄㈢梾鍖恒�傝繖閲屽埢鎰忓啓鎴� OR 灞曞紑鑰屼笉鏄� IFNULL(a,b) IN (...)锛�
+                     鍒楄鍑芥暟鍖呬綇灏辩敤涓嶄簡绱㈠紩锛�56 涓囪鏄庣粏琛ㄤ笂鏄叏琛ㄦ壂锛堝疄娴� 5.1s 鈫� 0.7s锛夈��
+                     涓や釜鍒嗘敮鍒嗗埆鍛戒腑 idx_ss_district_pair 鐨� (leave..., hospital...) 鍓嶇紑銆� -->
+                <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                    AND (leavehospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    OR (leavehospitaldistrictcode IS NULL AND hospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    ))
+                </if>
+            </when>
+            <otherwise>
+                <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                    AND (t.leavehospitaldistrictcode IS NULL OR t.leavehospitaldistrictcode IN
+                    <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                             close=")">
+                        #{leavehospitaldistrictcode}
+                    </foreach>
+                    )
+                </if>
+            </otherwise>
+        </choose>
         <if test="deptcodes != null and deptcodes.size() > 0">
             AND t.leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
@@ -2239,14 +2589,28 @@
         <if test="visitCount != null and visitCount > 1">
             AND t.visit_count > 1
         </if>
-        <if test="groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
+        <if test="districtColumn != null and districtColumn != '' and groupKeyList != null and groupKeyList.size>0">
+            AND IFNULL(${districtColumn}, 'Unknown') IN
+            <foreach collection="groupKeyList" item="key" open="(" separator=","
+                     close=")">
+                #{key}
+            </foreach>
+        </if>
+        <if test="(districtColumn == null or districtColumn == '') and groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
             AND IFNULL(t.${groupKey}, 'Unknown') IN
             <foreach collection="groupKeyList" item="key" open="(" separator=","
                      close=")">
                 #{key}
             </foreach>
         </if>
-        group by IFNULL(t.${groupColumn}, 'Unknown')
+        <choose>
+            <when test="districtColumn != null and districtColumn != ''">
+                group by IFNULL(${districtColumn}, 'Unknown')
+            </when>
+            <otherwise>
+                group by IFNULL(t.${groupColumn}, 'Unknown')
+            </otherwise>
+        </choose>
     </select>
 
     <select id="getSfStatisticsHyperlink" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
@@ -2264,13 +2628,15 @@
         leaveldeptname,
         sendstate,
         preachform,
-        current_preachform,
+        <include refid="effectiveCurrentPreachformExpr"/> AS current_preachform,
         excep,
         nurse_id,
         nurse_name,
         taskid,
         task_name,
         visit_time,
+        visit_dept_code,
+        visit_dept_name,
         finishtime,
         endtime,
         senddate,
@@ -2289,7 +2655,10 @@
         continue_count,
         continue_time_next,
         task_situation,
-        remark
+        remark,
+        doctor_opinion,
+        finish_state,
+        is_manually_called
         from service_subtask
         where 1=1
         and del_flag = 0
@@ -2300,13 +2669,20 @@
             and campusid = #{campusid}
         </if>
         <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"锛堝崡鍗庢弧鎰忓害 83% 瀛愪换鍔℃棤鐥呭尯锛夛紝鏈夌梾鍖虹殑鏁版嵁琛屼负涓嶅彉 -->
+        <!-- 涓� getSfStatisticsScript 鐨勬眹鎬昏淇濇寔涓�鑷达細鍑洪櫌鐥呭尯涓虹┖鍒欏洖閫�鍒板湪闄㈢梾鍖猴紝
+             鍚﹀垯鐐光�滄煡鐪嬭鎯呪�濇煡鍑烘潵鐨勬潯鏁颁細鍜屾眹鎬昏鐨勬暟瀛楀涓嶄笂銆� -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
+            AND (leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
-            )
+            OR (leavehospitaldistrictcode IS NULL AND hospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+            ))
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
             AND leaveldeptcode IN
@@ -2327,6 +2703,10 @@
             AND IFNULL(visit_time, IFNULL(finishtime, create_time)) >= DATE(#{startTime})
             AND IFNULL(visit_time, IFNULL(finishtime, create_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>
@@ -2338,10 +2718,15 @@
         <if test="type != null">
             and type = #{type}
         </if>
-        <if test="visitCount != null and visitCount == 1">
+        <!-- 棣栦釜鍐嶆闅忚锛氱簿纭煡 visit_count = 2锛屼笌缁熻鍙e緞涓�鑷� -->
+        <if test="hyperLinkInfoType == 'againFollowUpPushCountInfo'">
+            AND visit_count = 2
+        </if>
+        <!-- 鍏朵粬绫诲瀷淇濇寔鍘熻涔夛細1 棣栨锛�>1 鍐嶆 -->
+        <if test="hyperLinkInfoType != 'againFollowUpPushCountInfo' and visitCount != null and visitCount == 1">
             AND visit_count = 1
         </if>
-        <if test="visitCount != null and visitCount > 1">
+        <if test="hyperLinkInfoType != 'againFollowUpPushCountInfo' and visitCount != null and visitCount > 1">
             AND visit_count > 1
         </if>
         <if test="groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
@@ -2358,8 +2743,8 @@
                 #{state}
             </foreach>
         </if>
-        <if test="currentPreachform != null">
-            AND current_preachform = #{currentPreachform}
+        <if test="currentPreachform != null and currentPreachform != '' ">
+            AND <include refid="effectiveCurrentPreachformExpr"/> = #{currentPreachform}
             <if test="followUpCountStyle != null and followUpCountStyle = '1'">
                 AND sendstate != 4
             </if>
@@ -2370,6 +2755,9 @@
         <if test="taskSituation != null">
             AND task_situation = #{taskSituation}
         </if>
+        <if test="finishState != null">
+            AND finish_state = #{finishState}
+        </if>
         <if test="drcode != null and drcode != ''">
             AND drcode = #{drcode}
         </if>
@@ -2378,6 +2766,12 @@
         </if>
         <if test="deptcode != null and deptcode != ''">
             AND leaveldeptcode = #{deptcode}
+        </if>
+        <if test="isManuallyCalled != null">
+            AND is_manually_called = #{isManuallyCalled}
+        </if>
+        <if test="suggest != null and suggest != ''">
+            AND TRIM(suggest) = #{suggest}
         </if>
         order by endtime desc
         <if test="pageSize != null  and pageNum != null">
@@ -2601,14 +2995,14 @@
     <select id="getSfStatisticsScriptDetails" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
             resultMap="ServiceSubtaskScriptCountResult">
         SELECT
-        a.id as scriptid,
-        a.templateid as templateid,
-        a.script_type,
+        MIN(a.id) as scriptid,
+        MIN(a.templateid) as templateid,
+        MIN(a.script_type) as script_type,
         a.script_content,
         b.subtask_type,
-        b.completed_quantity,
-        b.all_quantity,
-        ROUND(b.completed_quantity/b.all_quantity,2) as completed_percentage
+        SUM(b.completed_quantity) as completed_quantity,
+        SUM(b.all_quantity) as all_quantity,
+        ROUND(SUM(b.completed_quantity)/SUM(b.all_quantity),2) as completed_percentage
         FROM
         <if test="type == 1">
             ivr_task_template_script a
@@ -2637,11 +3031,17 @@
             AND ss.campusid = #{campusid}
         </if>
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND ss.leavehospitaldistrictcode IN
+            AND (ss.leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            OR (ss.leavehospitaldistrictcode IS NULL AND ss.hospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+            ))
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
             AND ss.leaveldeptcode IN
@@ -2693,25 +3093,37 @@
         group by ssd.scriptid) b
         ON a.id = b.scriptid
         <!-- 閫傜敤鍗曟潯鏌ヨ -->
-        <if test="scriptids != null">
+        <if test="scriptids != null and scriptids != ''">
             AND a.scriptid in
             (SELECT lib_script.scriptid FROM
             svy_lib_template_script lib_script
             WHERE lib_script.scriptid in (${scriptids}))
         </if>
+        <!-- 鍚屼竴閬撻浼氳澶嶅埗鍒板涓换鍔℃ā鏉匡紝鎸夐鐩唴瀹瑰悎骞讹紝淇濊瘉涓�閬撻鍙繑鍥炰竴鏉� -->
+        GROUP BY a.script_content, b.subtask_type
+        ORDER BY MIN(a.id)
     </select>
 
     <select id="getSfStatisticsScriptItemDetails" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
             resultMap="ServiceSubtaskScriptCountResult">
         SELECT
-        ssd.scriptid,
+        a.script_content,
+        ss.type as subtask_type,
         IFNULL(ssd.matchedtext,ssd.asrtext) AS option_text,
         COUNT(*) AS chosen_quantity
         FROM
         service_subtask_detail ssd
         INNER JOIN service_subtask ss ON ssd.sub_id = ss.id
+        INNER JOIN
+        <if test="type == 1">
+            ivr_task_template_script a ON a.id = ssd.scriptid
+        </if>
+        <if test="type == 2">
+            svy_task_template_script a ON a.id = ssd.scriptid
+        </if>
         where 1=1
         and ss.del_flag=0
+        and ssd.del_flag=0
         <if test="orgid != null and orgid != ''">
             and ss.orgid = #{orgid}
         </if>
@@ -2719,11 +3131,17 @@
             and ss.campusid = #{campusid}
         </if>
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND ss.leavehospitaldistrictcode IN
+            AND (ss.leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            OR (ss.leavehospitaldistrictcode IS NULL AND ss.hospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+            ))
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
             AND ss.leaveldeptcode IN
@@ -2772,7 +3190,137 @@
         <if test="configValue != null and configValue != ''">
             AND ssd.categoryid IN (${configValue})
         </if>
-        group by ssd.scriptid, IFNULL(ssd.matchedtext,ssd.asrtext)
+        group by a.script_content, ss.type, IFNULL(ssd.matchedtext,ssd.asrtext)
+    </select>
+
+    <!--
+      闂閫夐」鏁板瓧涓嬮捇锛氳繑鍥為�夋嫨浜嗘寚瀹氶�夐」鐨� service_subtask 鍒楄〃銆�
+      杩囨护鍙e緞涓� getSfStatisticsScriptDetails / getSfStatisticsScriptItemDetails 淇濇寔涓�鑷达紝
+      棰濆鏀寔 statisticaltype锛�1-鐥呭尯銆�2-绉戝銆�3-鐥呯銆�
+    -->
+    <select id="getSfStatisticsScriptDetailsHyperlink" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
+            resultMap="ServiceSubtaskResult">
+        SELECT ss.*
+        FROM service_subtask ss
+        WHERE ss.del_flag = 0
+        <if test="orgid != null and orgid != ''">
+            AND ss.orgid = #{orgid}
+        </if>
+        <if test="campusid != null and campusid != ''">
+            AND ss.campusid = #{campusid}
+        </if>
+        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+            AND ss.leavehospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+        </if>
+        <if test="deptcodes != null and deptcodes.size() > 0">
+            AND ss.deptcode IN
+            <foreach collection="deptcodes" item="deptcode" open="(" separator="," close=")">
+                #{deptcode}
+            </foreach>
+        </if>
+        <if test="serviceType != null and serviceType.size() > 0">
+            AND ss.service_type IN
+            <foreach collection="serviceType" item="serviceType" open="(" separator="," close=")">
+                #{serviceType}
+            </foreach>
+        </if>
+        <if test="startTime != null and endTime != null">
+            AND ss.visit_time >= DATE(#{startTime})
+            AND ss.visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
+        </if>
+        <if test="type != null and type != ''">
+            AND ss.type = #{type}
+        </if>
+
+        <!-- 缁熻缁村害锛�1-鎸夌梾鍖� 2-鎸夌瀹� 3-鎸塼askid -->
+        <if test="statisticaltype != null and statisticaltype == 1">
+            <choose>
+                <when test="deptcode != null and deptcode != ''">
+                    AND ss.leavehospitaldistrictcode = #{deptcode}
+                </when>
+                <when test="leavehospitaldistrictcode != null and leavehospitaldistrictcode != ''">
+                    AND ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
+                </when>
+            </choose>
+        </if>
+        <if test="statisticaltype != null and statisticaltype == 2 and deptcode != null and deptcode != ''">
+            AND ss.deptcode = #{deptcode}
+        </if>
+        <if test="statisticaltype != null and statisticaltype == 3 and taskId != null">
+            AND ss.taskid = #{taskId}
+        </if>
+        <!-- 鍏煎涓嶄紶 statisticaltype 鏃舵寜鍗曞�兼潯浠惰繃婊� -->
+        <if test="statisticaltype == null">
+            <if test="leavehospitaldistrictcode != null and leavehospitaldistrictcode != ''">
+                AND ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
+            </if>
+            <if test="deptcode != null and deptcode != ''">
+                AND ss.deptcode = #{deptcode}
+            </if>
+            <if test="taskId != null">
+                AND ss.taskid = #{taskId}
+            </if>
+        </if>
+
+        <!-- 棰樼洰 + 閫夐」鍖归厤锛氬彧淇濈暀鍥炵瓟涓�夋嫨浜嗚閫夐」鐨勫瓙浠诲姟 -->
+        AND EXISTS (
+            SELECT 1
+            FROM service_subtask_detail ssd
+            WHERE ssd.sub_id = ss.id
+              AND ssd.del_flag = 0
+            <if test="configValue != null and configValue != ''">
+                AND ssd.categoryid IN (${configValue})
+            </if>
+            <choose>
+                <when test="optionText != null">
+                    AND IFNULL(ssd.matchedtext, ssd.asrtext) = #{optionText}
+                </when>
+                <otherwise>
+                    AND 1 = 0
+                </otherwise>
+            </choose>
+            <choose>
+                <!-- 浼樺厛鎸夐鐩唴瀹瑰尮閰嶏細鍚屼竴閬撻澶嶅埗鍒板涓换鍔℃ā鏉挎椂 scriptid 涓嶅悓锛屼絾 script_content 鐩稿悓 -->
+                <when test="scriptContent != null and scriptContent != ''">
+                    AND ssd.scriptid IN (
+                        <choose>
+                            <when test="type == '1'">
+                                SELECT its.id FROM ivr_task_template_script its
+                                WHERE its.script_content = #{scriptContent}
+                            </when>
+                            <when test="type == '2'">
+                                SELECT sts.id FROM svy_task_template_script sts
+                                WHERE sts.script_content = #{scriptContent}
+                            </when>
+                            <otherwise>
+                                SELECT its.id FROM ivr_task_template_script its
+                                WHERE its.script_content = #{scriptContent}
+                                UNION
+                                SELECT sts.id FROM svy_task_template_script sts
+                                WHERE sts.script_content = #{scriptContent}
+                            </otherwise>
+                        </choose>
+                    )
+                </when>
+                <when test="scriptid != null">
+                    AND ssd.scriptid = #{scriptid}
+                </when>
+                <otherwise>
+                    AND 1 = 0
+                </otherwise>
+            </choose>
+        )
+        ORDER BY ss.endtime DESC, ss.id DESC
+        <if test="pageSize != null and pageSize > 0">
+            LIMIT #{pageSize}
+            <if test="pageNum != null and pageNum >= 0">
+                OFFSET #{pageNum}
+            </if>
+        </if>
     </select>
 
     <select id="getDataByTime" resultMap="ServiceSubtaskResult">
@@ -2843,6 +3391,7 @@
                nurse_id,
                nurse_name,
                remark,
+               doctor_opinion,
                inhospid,
                patfrom,
                task_guid,
@@ -3085,11 +3634,12 @@
         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
+        SUM(CASE WHEN (sendstate = 1 OR sendstate = 2 OR sendstate = 3 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
@@ -3222,6 +3772,9 @@
                 #{state}
             </foreach>
         </if>
+        <if test="currentPreachforms != null and currentPreachforms != ''">
+            AND FIND_IN_SET(<include refid="effectiveCurrentPreachformExpr"/>, #{currentPreachforms})
+        </if>
         <if test="orgid != null and orgid != ''">
             AND orgid = #{orgid}
         </if>
@@ -3282,7 +3835,8 @@
         褰撳墠鐧诲綍浜恒�屽緟澶勭悊銆嶉殢璁夸换鍔★紝鎸夊嚭闄㈠勾鏈堬紙endtime锛夊垎缁勭粺璁★紝鍑洪櫌闅忚 / 闂ㄨ瘖闅忚鍒嗗紑璁℃暟銆�
         鍙e緞锛堜笌闅忚鍒楄〃銆屽緟闅忚銆嶉〉绛句竴鑷达級锛�
           1) 鍑洪櫌闅忚 service_type = '2'锛岄棬璇婇殢璁� service_type = '3'
-          2) 寰呴殢璁匡紙鏈畬鎴愶級鈫� sendstate IN (1,2,3,5,7)锛屾帓闄� 4 涓嶆墽琛� / 6 瀹屾垚
+          2) 缁熻鐘舵�� 鈫� sendstate IN (1,2,3,7)锛�1 琚鍙� / 2 寰呭彂閫� / 3 宸插彂閫� / 7 瓒呮椂
+             锛堜笉鍚� 4 涓嶆墽琛屻��5 鍙戦�佸け璐ャ��6 宸插畬鎴愶級
           3) 鍑洪櫌婊� 7 澶� 鈫� endtime < (浠婂ぉ - 7 澶�) + 1 澶╋紝
              鍗炽��7 澶╁墠閭d竴澶┿�嶅嚭闄㈢殑涔熻鍏ワ紙鐢� <= DATE_SUB(...) 鍙細鎴埌褰撳ぉ 0 鐐癸紝浼氭紡鎺� 9 鏉★級
           4) 鍙湅鏈�杩� 4 涓嚭闄㈡湀浠斤紙鍚綋鏈堬級鈫� endtime >= 褰撴湀寰�鍓嶆帹 3 涓湀鐨� 1 鍙�
@@ -3302,7 +3856,8 @@
         FROM service_subtask
         WHERE del_flag = '0'
         AND service_type IN ('2', '3')
-        AND sendstate IN (1, 2, 3, 5, 7)
+        <!-- 娴獥銆屽緟澶勭悊闅忚浠诲姟銆嶅彛寰勶細1 琚鍙� / 2 寰呭彂閫� / 3 宸插彂閫� / 7 瓒呮椂 -->
+        AND sendstate IN (1, 2, 3, 7)
         AND endtime IS NOT NULL
         AND endtime &lt; DATE_ADD(DATE_SUB(CURDATE(), INTERVAL 7 DAY), INTERVAL 1 DAY)
         AND endtime &gt;= DATE_FORMAT(DATE_SUB(CURDATE(), INTERVAL 3 MONTH), '%Y-%m-01')
@@ -3326,32 +3881,74 @@
         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 鍙e緞淇濇寔涓�鑷达紱
+             鏃犳湁鏁堣ˉ鍋胯褰�(cp.subid 涓虹┖)鏃舵埅姝㈡椂闂翠负 NULL锛屼笉鍙備笌鍗冲皢瓒呮椂缁熻 -->
+        CASE
+        WHEN cp.subid IS NULL THEN NULL
+        ELSE DATE_ADD(
+        DATE_ADD(ss.endtime, INTERVAL COALESCE(st.send_day, 1) DAY),
+        INTERVAL (COALESCE(cp.compensate_days, 0) + 1) DAY
+        )
+        END AS soon_deadline
+        FROM service_subtask ss
+        LEFT JOIN service_task st ON st.taskid = ss.taskid AND st.del_flag = '0'
+        <!-- 琛ュ伩鏃堕棿锛氬厛鎸夊瓙浠诲姟鑱氬悎鎴愪竴琛屽啀鍏宠仈锛岄伩鍏嶄竴瀵瑰鏀惧ぇ璁℃暟锛�
+             compensate_time 鏄� varchar 涓斿彲鑳藉瓨鎴愮┖涓诧紝闇�鍏堢疆绌哄啀杞暟瀛� -->
+        LEFT JOIN (
+        SELECT subid,
+        SUM(CAST(COALESCE(NULLIF(TRIM(compensate_time), ''), '0') AS SIGNED)) AS compensate_days
+        FROM service_subtask_preachform
+        WHERE del_flag = 0
+        GROUP BY subid
+        ) cp ON cp.subid = ss.id
+        WHERE ss.del_flag = '0'
+        AND ss.service_type = '2'
+        AND ss.visit_count = 1
         <if test="orgid != null and orgid != ''">
-            AND orgid = #{orgid}
+            AND ss.orgid = #{orgid}
         </if>
         <if test="campusid != null and campusid != ''">
-            and campusid = #{campusid}
+            and ss.campusid = #{campusid}
         </if>
-        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND leavehospitaldistrictcode IN
-            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
-                     separator=","
-                     close=")">
-                #{leavehospitaldistrictcode}
-            </foreach>
+        <!-- 绂婚櫌绉戝/鐥呭尯锛氭湁绉戝鎸夌瀹ゃ�佹湁鐥呭尯鎸夌梾鍖猴紝涓よ�呴兘鏈夋椂鍙� OR锛堝師鍐欐硶 1=1 浼氳绛涢�夊け鏁堬級 -->
+        <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
+                or (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
+            AND (
+            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                ss.leavehospitaldistrictcode IN
+                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
+                         separator=","
+                         close=")">
+                    #{leavehospitaldistrictcode}
+                </foreach>
+            </if>
+            <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
+                and (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
+                OR
+            </if>
+            <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
+                ss.leaveldeptcode IN
+                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                         close=")">
+                    #{leaveldeptcode}
+                </foreach>
+            </if>)
         </if>
-        <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
-            AND leaveldeptcode IN
-            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
-                     close=")">
-                #{leaveldeptcode}
-            </foreach>
-        </if>
+        ) t
     </select>
 
             <!--
@@ -3535,7 +4132,8 @@
         continue_count,
         continue_time_next,
         task_situation,
-        remark
+        remark,
+        doctor_opinion
         from service_subtask
         where 1=1
         and del_flag = 0

--
Gitblit v1.9.3