From c24d16a37523a49080392610357872ce580cbc38 Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期一, 26 十月 2026 14:03:33 +0800
Subject: [PATCH] 代码提交

---
 ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml |  875 +++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 681 insertions(+), 194 deletions(-)

diff --git a/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml b/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
index c478704..da3b3a7 100644
--- a/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
+++ b/ruoyi-task/src/main/resources/mapper/task/ServiceSubtaskMapper.xml
@@ -26,7 +26,7 @@
         <result property="recordid" column="recordid"/>
         <result property="exrecallcount" column="exrecallcount"/>
         <result property="pulltime" column="pulltime"/>
-        <result property="deptcode" column="deptcode"/>
+        <result property="leaveldeptcode" column="leaveldeptcode"/>
         <result property="labelstatus" column="labelstatus"/>
         <result property="delFlag" column="del_flag"/>
         <result property="updateBy" column="update_by"/>
@@ -53,6 +53,11 @@
         <result property="leaveicd10code" column="leaveicd10code"/>
         <result property="leavediagname" column="leavediagname"/>
         <result property="diagname" column="diagname"/>
+        <result property="leaveldeptname" column="leaveldeptname"/>
+        <!-- 2026-11锛氬師 sendlimit* 鍥涗釜绌洪棽鍒楀鐢ㄤ负銆屽湪闄㈢淮搴︺�嶏紙hospitaldistrict* = 鍦ㄩ櫌鐥呭尯锛宒eptcode/name = 鍦ㄩ櫌绉戝锛� -->
+        <result property="hospitaldistrictcode" column="hospitaldistrictcode"/>
+        <result property="hospitaldistrictname" column="hospitaldistrictname"/>
+        <result property="deptcode" column="deptcode"/>
         <result property="deptname" column="deptname"/>
         <result property="admindate" column="admindate"/>
         <result property="roomno" column="roomno"/>
@@ -205,7 +210,7 @@
                recordid,
                exrecallcount,
                pulltime,
-               deptcode,
+               leaveldeptcode,
                labelstatus,
                del_flag,
                update_by,
@@ -228,6 +233,10 @@
                sendname,
                roomno,
                admindate,
+               leaveldeptname,
+               hospitaldistrictcode,
+               hospitaldistrictname,
+               deptcode,
                deptname,
                stop_state,
                leavehospitaldistrictcode,
@@ -291,9 +300,111 @@
         <if test="continueTimeNext != null ">and continue_time_next = #{continueTimeNext}</if>
     </select>
 
-    <select id="selectServiceSubtaskList" parameterType="com.ruoyi.task.domain.entity.ServiceSubtaskEntity"
-            resultMap="ServiceSubtaskResult">
-        <include refid="selectServiceSubtaskVo"/>
+    <!-- ======================================================================
+         绉戝 / 鐥呭尯绛涢�夛紙2026-11 淇鐗堬級
+         涓ょ粍鏉′欢銆愬悇鑷嫭绔嬨�戯紝浜掍笉鐗佃繛锛堝湪闄㈢梾鍖轰笌绂婚櫌鐥呭尯涓嶄細鍚屾椂瀛樺湪锛夛細
+           绂婚櫌缁勶細leavehospitaldistrictcodes 鈫� leavehospitaldistrictcode
+                   leaveldeptcodes            鈫� leaveldeptcode
+           鍦ㄩ櫌缁勶細hospitaldistrictcodes      鈫� hospitaldistrictcode锛堝湪闄㈢梾鍖猴級
+                   deptcodes                  鈫� deptcode锛堝湪闄㈢瀹わ級
+         缁勫唴缁勫悎鏂瑰紡鐢� deptOrDistrict 鍐冲畾锛�'1' = AND锛�'2' = OR銆�
+         锛�'3'/'4' 鏄�岀闄㈢梾鍖� + 闅忚绉戝 visit_dept_code銆嶉偅濂楋紝鍗曠嫭鍒嗘敮锛屼笉璧拌繖閲岋級
+         涓ょ粍涔嬮棿鏄� AND锛氬彧浼犱竴缁勬椂鍙︿竴缁勮嚜鍔ㄤ笉鎷笺��
+         绌烘暟缁� [] 涓�寰嬭涓�"娌′紶"锛屼笉浼氭嫾鍑� IN ()銆�
+         ====================================================================== -->
+    <sql id="filterLeaveDimAnd">
+        /* f-v3 */
+        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+            AND leavehospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="lvWardCodeItem" open="(" separator="," close=")">
+                #{lvWardCodeItem}
+            </foreach>
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+            AND leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
+                #{lvDeptCodeItem}
+            </foreach>
+        </if>
+    </sql>
+
+    <sql id="filterLeaveDimOr">
+        /* f-v3 */
+        <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
+                  or (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
+            AND (
+            <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
+                leavehospitaldistrictcode IN
+                <foreach collection="leavehospitaldistrictcodes" item="lvWardCodeItem" open="(" separator="," close=")">
+                    #{lvWardCodeItem}
+                </foreach>
+            </if>
+            <if test="(leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0)
+                      and (leaveldeptcodes != null and leaveldeptcodes.size() > 0)">
+                OR
+            </if>
+            <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+                leaveldeptcode IN
+                <foreach collection="leaveldeptcodes" item="lvDeptCodeItem" open="(" separator="," close=")">
+                    #{lvDeptCodeItem}
+                </foreach>
+            </if>
+            )
+        </if>
+    </sql>
+
+    <sql id="filterInhospDimAnd">
+        /* f-v3 */
+        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+            AND hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodes" item="inWardCodeItem" open="(" separator="," close=")">
+                #{inWardCodeItem}
+            </foreach>
+        </if>
+        <if test="deptcodes != null and deptcodes.size() > 0">
+            AND deptcode IN
+            <foreach collection="deptcodes" item="inDeptCodeItem" open="(" separator="," close=")">
+                #{inDeptCodeItem}
+            </foreach>
+        </if>
+    </sql>
+
+    <sql id="filterInhospDimOr">
+        /* f-v3 */
+        <if test="(hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0)
+                  or (deptcodes != null and deptcodes.size() > 0)">
+            AND (
+            <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+                hospitaldistrictcode IN
+                <foreach collection="hospitaldistrictcodes" item="inWardCodeItem" open="(" separator="," close=")">
+                    #{inWardCodeItem}
+                </foreach>
+            </if>
+            <if test="(hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0)
+                      and (deptcodes != null and deptcodes.size() > 0)">
+                OR
+            </if>
+            <if test="deptcodes != null and deptcodes.size() > 0">
+                deptcode IN
+                <foreach collection="deptcodes" item="inDeptCodeItem" open="(" separator="," close=")">
+                    #{inDeptCodeItem}
+                </foreach>
+            </if>
+            )
+        </if>
+    </sql>
+
+    <!-- ==================================================================
+         service_subtask 鍒楄〃鏌ヨ鐨勫叕鍏� where 鐗囨
+         ==================================================================
+         patItem 璧般�屼袱闃舵鍒嗛〉銆嶏細
+           鈶� selectServiceSubtaskIdsPage 鈥斺�� 鍙煡 id锛岃蛋瑕嗙洊绱㈠紩锛屼笉涓鸿 offset
+              璺宠繃鐨勬瘡涓�琛屽洖琛ㄨ 100 鍒楋紱
+           鈶� selectServiceSubtaskList + subIdList 鈥斺�� 鎸� id 鍥炶〃锛屽彧璇讳竴椤电殑閲忋��
+         涓ゆ潯璇彞鐨� where 蹇呴』銆愰�愬瓧涓�鑷淬�戯紝鍚﹀垯绗憽姝ヤ細鎶婄鈶犳閫夊嚭鐨勮杩囨护鎺夛紝
+         瀵艰嚧涓�椤佃繑鍥炰笉瓒� pageSize 鏉°�傛墍浠ユ敹鎴愬叕鍏辩墖娈碉紝鏉滅粷涓や唤鍙e緞婕傜Щ銆�
+         ================================================================== -->
+    <sql id="serviceSubtaskWhere">
         where del_flag = 0
         <if test="subId != null ">and id = #{subId}</if>
         <if test="continueFlag != null ">and continue_flag = #{continueFlag}</if>
@@ -367,63 +478,43 @@
             '%')
         </if>
         <if test="startOutHospTime != null">
-            AND date_format(endtime,'%y%m%d') &gt;= date_format(#{startOutHospTime},'%y%m%d')
+            AND endtime &gt;= DATE(#{startOutHospTime})
         </if>
         <if test="endOutHospTime != null">
-            AND date_format(endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
+            AND endtime &lt; DATE_ADD(DATE(#{endOutHospTime}), INTERVAL 1 DAY)
         </if>
         <if test="startSendDateTime != null">
-            AND date_format(visit_time,'%y%m%d') &gt;= date_format(#{startSendDateTime},'%y%m%d')
+            AND visit_time &gt;= DATE(#{startSendDateTime})
         </if>
         <if test="endSendDateTime != null">
-            AND date_format(visit_time,'%y%m%d') &lt;= date_format(#{endSendDateTime},'%y%m%d')
+            AND visit_time &lt; DATE_ADD(DATE(#{endSendDateTime}), INTERVAL 1 DAY)
         </if>
         <if test="finishtimeStart != null">
-            AND date_format(finishtime,'%y%m%d') &gt;= date_format(#{finishtimeStart},'%y%m%d')
+            AND finishtime &gt;= DATE(#{finishtimeStart})
         </if>
         <if test="finishtimeEnd != null">
-            AND date_format(finishtime,'%y%m%d') &lt;= date_format(#{finishtimeEnd},'%y%m%d')
+            AND finishtime &lt; DATE_ADD(DATE(#{finishtimeEnd}), INTERVAL 1 DAY)
         </if>
         <if test="longSendTime != null">
-            AND date_format(long_send_time,'%y%m%d') &lt;= date_format(#{longSendTime},'%y%m%d')
+            AND long_send_time &lt; DATE_ADD(DATE(#{longSendTime}), INTERVAL 1 DAY)
         </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>
+        <!--
+            绉戝/鐥呭尯绛涢�夛紙2026-11 淇锛夛細
+              绂婚櫌缁勶紙leaveldeptcodes / leavehospitaldistrictcodes锛変繚鎸佸師閫昏緫锛�
+              鍦ㄩ櫌缁勶紙deptcodes / hospitaldistrictcodes 鈫� deptcode / hospitaldistrictcode锛夋槸鏂板鐨勪竴缁勶紱
+              缁勫唴鎸� deptOrDistrict 鍙� AND('1') / OR('2')锛屼袱缁勪箣闂� AND锛堝彧浼犱竴缁勬椂鍙︿竴缁勪笉鎷硷級銆�
+        -->
+        <if test='deptOrDistrict == "1"'>
+            <include refid="filterLeaveDimAnd"/>
+            <include refid="filterInhospDimAnd"/>
         </if>
-        <if test="deptOrDistrict==2">
-            AND (1=1
-            <if test="leavehospitaldistrictcodes != null">
-                AND leavehospitaldistrictcode IN
-                <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="("
-                         separator=","
-                         close=")">
-                    #{leavehospitaldistrictcode}
-                </foreach>
-            </if>
-            <if test=" leaveldeptcodes != null">
-                OR deptcode IN
-                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
-                         close=")">
-                    #{leaveldeptcode}
-                </foreach>
-            </if>
-            )
+        <if test='deptOrDistrict == "2"'>
+            <include refid="filterLeaveDimOr"/>
+            <include refid="filterInhospDimOr"/>
         </if>
-        <if test="deptOrDistrict==3">
+        <!-- 鈿狅笍 deptOrDistrict 鏄� String锛歄GNL 閲� '1'锛堝崟寮曞彿鍗曞瓧绗︼級鏄� char锛孲tring != char 鎭� false锛�
+             蹇呴』鍐欐垚鍙屽紩鍙峰瓧绗︿覆锛圶ML 灞炴�х敤鍗曞紩鍙峰寘浣忥級锛屾垨鐩存帴鍜屾暟瀛楁瘮锛圤GNL 浼氬仛 String鈫扤umber 杞崲锛夈�� -->
+        <if test='deptOrDistrict == "3"'>
             AND (1=1
             <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                 AND leavehospitaldistrictcode IN
@@ -442,7 +533,7 @@
             </if>
             )
         </if>
-        <if test="deptOrDistrict==4">
+        <if test='deptOrDistrict == "4"'>
             AND (1=1
             <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
                 AND leavehospitaldistrictcode IN
@@ -462,7 +553,8 @@
             )
         </if>
         <if test="visitTime != null">
-            AND date_format(visit_time,'%y%m%d') = date_format(#{visitTime},'%y%m%d')
+            AND visit_time &gt;= DATE(#{visitTime})
+            AND visit_time &lt; DATE_ADD(DATE(#{visitTime}), INTERVAL 1 DAY)
         </if>
         <if test="senddate != null ">and senddate = #{senddate}</if>
         <if test="senduuid != null  and senduuid != ''">and senduuid = #{senduuid}</if>
@@ -471,8 +563,8 @@
         <if test="recordid != null ">and recordid = #{recordid}</if>
         <if test="exrecallcount != null ">and exrecallcount = #{exrecallcount}</if>
         <if test="pulltime != null ">and pulltime = #{pulltime}</if>
-        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
-        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
+        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
+        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
         <if test="labelstatus != null ">and labelstatus = #{labelstatus}</if>
         <if test="isupload != null ">and isupload = #{isupload}</if>
         <if test="uploadTime != null ">and upload_time = #{uploadTime}</if>
@@ -524,6 +616,11 @@
                 #{visitDeptCode}
             </foreach>
         </if>
+    </sql>
+
+    <!-- 鎺掑簭鐗囨锛氫繚鎸佸師璇箟锛屽彧鍦ㄨ皟鐢ㄦ柟鏄惧紡浼� sort 鏃舵墠鎷� order by
+         锛堝鍑虹瓑涓嶅垎椤电殑璋冪敤鏂硅涓哄畬鍏ㄤ笉鍙橈級 -->
+    <sql id="serviceSubtaskOrderBy">
         <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>
@@ -533,7 +630,37 @@
         <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>
+    </sql>
+
+    <!-- 鍒嗛〉鍦烘櫙涓撶敤鎺掑簭锛歴ort 涓虹┖鏃跺厹搴� order by id desc銆�
+         鍘熸潵 sort 涓虹┖鏃舵暣鏉� SQL 娌℃湁 order by锛宭imit/offset 鍙栧埌鐨勬槸銆屼笉纭畾椤哄簭銆嶏紝
+         缈婚〉浼氶噸澶嶆垨婕忚锛涜繖閲岀粰涓�涓ǔ瀹氶『搴忥紙鎯虫敼鎴� id asc 鍙敼杩欎竴琛岋級銆� -->
+    <sql id="serviceSubtaskOrderByPaged">
+        <include refid="serviceSubtaskOrderBy"/>
+        <if test="sort == null">order by id desc</if>
+    </sql>
+
+    <select id="selectServiceSubtaskList" parameterType="com.ruoyi.task.domain.entity.ServiceSubtaskEntity"
+            resultMap="ServiceSubtaskResult">
+        <include refid="selectServiceSubtaskVo"/>
+        <include refid="serviceSubtaskWhere"/>
+        <include refid="serviceSubtaskOrderBy"/>
         <if test="pageSize != null  and pageNum != null">limit ${pageSize} OFFSET ${pageNum}</if>
+    </select>
+
+    <!--
+        涓ら樁娈靛垎椤电 1 姝ワ細鍙彇 id銆�
+        select 鍒楄〃鍙湁 id锛寃here/order 娑夊強鐨勫垪閮藉湪 idx_ss_org_cam_del_* 涓婏紝
+        鍥犳鑳借蛋銆岃鐩栫储寮� + 鎻愬墠鎴柇銆嶏紱琚� offset 璺宠繃鐨勯偅鍑犲崈琛屼笉鍐嶅洖琛ㄨ 100 鍒椼��
+        绗� 2 姝ョ敱璋冪敤鏂圭敤 subIdList 鍥炶〃鍙栨暣琛岋紙瑙� ServiceSubtaskServiceImpl#selectServiceSubtaskPage锛夈��
+    -->
+    <select id="selectServiceSubtaskIdsPage" parameterType="com.ruoyi.task.domain.entity.ServiceSubtaskEntity"
+            resultType="java.lang.Long">
+        select id
+        from service_subtask
+        <include refid="serviceSubtaskWhere"/>
+        <include refid="serviceSubtaskOrderByPaged"/>
+        limit ${pageSize} OFFSET ${pageNum}
     </select>
 
             <select id="getCompensateServiceSubtaskList" parameterType="com.ruoyi.task.domain.entity.ServiceSubtaskEntity"
@@ -615,8 +742,8 @@
         <if test="recordid != null ">and recordid = #{recordid}</if>
         <if test="exrecallcount != null ">and exrecallcount = #{exrecallcount}</if>
         <if test="pulltime != null ">and pulltime = #{pulltime}</if>
-        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
-        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
+        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
+        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
         <if test="labelstatus != null ">and labelstatus = #{labelstatus}</if>
         <if test="isupload != null ">and isupload = #{isupload}</if>
         <if test="uploadTime != null ">and upload_time = #{uploadTime}</if>
@@ -691,8 +818,8 @@
         <if test="campusid != null and campusid != ''">and campusid = #{campusid}</if>
         <if test="serviceType != null and serviceType != ''">and service_type = #{serviceType}</if>
         <if test="serviceTypes != null and serviceTypes != ''">and service_type IN (#{serviceTypes})</if>
-        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
-        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
+        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
+        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
         <if test="isabnormal != null">and isabnormal = #{isabnormal}</if>
         <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
             leavehospitaldistrictcode = #{leavehospitaldistrictcode}
@@ -703,10 +830,10 @@
         <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')
+            AND endtime &gt;= DATE(#{startOutHospTime})
         </if>
         <if test="endOutHospTime != null">
-            AND date_format(endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
+            AND endtime &lt; DATE_ADD(DATE(#{endOutHospTime}), INTERVAL 1 DAY)
         </if>
         ) a
     </select>
@@ -750,8 +877,8 @@
         </if>
         <if test="orgid != null and orgid != ''">AND ss.orgid = #{orgid}</if>
         <if test="campusid != null and campusid != ''">and ss.campusid = #{campusid}</if>
-        <if test="deptcode != null  and deptcode != ''">and ss.deptcode = #{deptcode}</if>
-        <if test="deptname != null  and deptname != ''">and ss.deptname = #{deptname}</if>
+        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and ss.leaveldeptcode = #{leaveldeptcode}</if>
+        <if test="leaveldeptname != null  and leaveldeptname != ''">and ss.leaveldeptname = #{leaveldeptname}</if>
         <if test="isabnormal != null">and ss.isabnormal = #{isabnormal}</if>
         <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
             ss.leavehospitaldistrictcode = #{leavehospitaldistrictcode}
@@ -781,7 +908,7 @@
             AND date_format(ss.endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
         </if>
         <if test="groupKey != null and groupKey != ''">
-            AND ss.${groupKey} IN
+            AND IFNULL(ss.${groupKey}, 'Unknown') IN
             <foreach collection="groupKeyList" item="key" open="(" separator=","
                      close=")">
                 #{key}
@@ -812,8 +939,8 @@
         <if test="serviceType != null and serviceType != ''">and service_type = #{serviceType}</if>
         <if test="serviceTypes != null and serviceTypes != ''">and service_type IN (#{serviceTypes})</if>
         <if test="isabnormal != null ">and isabnormal = #{isabnormal}</if>
-        <if test="deptcode != null  and deptcode != ''">and deptcode = #{deptcode}</if>
-        <if test="deptname != null  and deptname != ''">and deptname = #{deptname}</if>
+        <if test="leaveldeptcode != null  and leaveldeptcode != ''">and leaveldeptcode = #{leaveldeptcode}</if>
+        <if test="leaveldeptname != null  and leaveldeptname != ''">and leaveldeptname = #{leaveldeptname}</if>
         <if test="leavehospitaldistrictcode != null  and leavehospitaldistrictcode != ''">and
             leavehospitaldistrictcode = #{leavehospitaldistrictcode}
         </if>
@@ -846,10 +973,10 @@
         <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')
+            AND endtime &gt;= DATE(#{startOutHospTime})
         </if>
         <if test="endOutHospTime != null">
-            AND date_format(endtime,'%y%m%d') &lt;= date_format(#{endOutHospTime},'%y%m%d')
+            AND endtime &lt; DATE_ADD(DATE(#{endOutHospTime}), INTERVAL 1 DAY)
         </if>
         GROUP BY sendname,sendstate,starttime,endtime,nurse_name,phone,sex,task_name,leavediagname,visit_time
         ORDER BY sendname,visit_time
@@ -878,7 +1005,12 @@
             <if test="recordid != null">recordid,</if>
             <if test="exrecallcount != null">exrecallcount,</if>
             <if test="pulltime != null">pulltime,</if>
-            <if test="deptcode != null">deptcode,</if>
+            <if test="leaveldeptcode != null">leaveldeptcode,</if>
+            <!-- 2026-11锛氬師 sendlimit* 澶嶇敤鍒楋紙鍦ㄩ櫌缁村害锛氬湪闄㈢梾鍖�/鍦ㄩ櫌绉戝锛夛紝浠呭嚭闄㈤殢璁夸細濉�� -->
+            <if test="hospitaldistrictcode != null  and hospitaldistrictcode != ''">hospitaldistrictcode,</if>
+            <if test="hospitaldistrictname != null  and hospitaldistrictname != ''">hospitaldistrictname,</if>
+            <if test="deptcode != null  and deptcode != ''">deptcode,</if>
+            <if test="deptname != null  and deptname != ''">deptname,</if>
             <if test="labelstatus != null">labelstatus,</if>
             <if test="updateBy != null">update_by,</if>
             <if test="updateTime != null">update_time,</if>
@@ -903,7 +1035,7 @@
             <if test="wechat != null  and wechat != ''">wechat,</if>
             <if test="roomno != null  and roomno != ''">roomno,</if>
             <if test="admindate != null">admindate,</if>
-            <if test="deptname != null  and deptname != ''">deptname,</if>
+            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname,</if>
             <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code,</if>
             <if test="leavediagname != null  and leavediagname != ''">leavediagname,</if>
             <!-- diagname锛氬叆闄㈣瘖鏂悕绉般�傚師 insert 婕忎簡杩欎竴鍒楋紝鑰屽嚭闄㈤摼璺殑
@@ -980,7 +1112,12 @@
             <if test="recordid != null">#{recordid},</if>
             <if test="exrecallcount != null">#{exrecallcount},</if>
             <if test="pulltime != null">#{pulltime},</if>
-            <if test="deptcode != null">#{deptcode},</if>
+            <if test="leaveldeptcode != null">#{leaveldeptcode},</if>
+            <!-- 2026-11锛氬師 sendlimit* 澶嶇敤鍒楋紙鍏ラ櫌/鍦ㄩ櫌缁村害锛夛紝浠呭嚭闄㈤殢璁夸細濉�� -->
+            <if test="hospitaldistrictcode != null  and hospitaldistrictcode != ''">#{hospitaldistrictcode},</if>
+            <if test="hospitaldistrictname != null  and hospitaldistrictname != ''">#{hospitaldistrictname},</if>
+            <if test="deptcode != null  and deptcode != ''">#{deptcode},</if>
+            <if test="deptname != null  and deptname != ''">#{deptname},</if>
             <if test="labelstatus != null">#{labelstatus},</if>
             <if test="updateBy != null">#{updateBy},</if>
             <if test="updateTime != null">#{updateTime},</if>
@@ -1005,7 +1142,7 @@
             <if test="wechat != null  and wechat != ''">#{wechat},</if>
             <if test="roomno != null  and roomno != ''">#{roomno},</if>
             <if test="admindate != null ">#{admindate},</if>
-            <if test="deptname != null  and deptname != ''">#{deptname},</if>
+            <if test="leaveldeptname != null  and leaveldeptname != ''">#{leaveldeptname},</if>
             <if test="leaveicd10code != null  and leaveicd10code != ''">#{leaveicd10code},</if>
             <if test="leavediagname != null  and leavediagname != ''">#{leavediagname},</if>
             <if test="diagname != null  and diagname != ''">#{diagname},</if>
@@ -1084,7 +1221,7 @@
             <if test="recordid != null">recordid = #{recordid},</if>
             <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
             <if test="pulltime != null">pulltime = #{pulltime},</if>
-            <if test="deptcode != null">deptcode = #{deptcode},</if>
+            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
             <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1109,7 +1246,7 @@
             <if test="wechat != null  and wechat != ''">wechat = #{wechat},</if>
             <if test="roomno != null  and roomno != ''">roomno = #{roomno},</if>
             <if test="admindate != null ">admindate = #{admindate},</if>
-            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
+            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
             <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
             <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
             <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1188,7 +1325,7 @@
             <if test="recordid != null">recordid = #{recordid},</if>
             <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
             <if test="pulltime != null">pulltime = #{pulltime},</if>
-            <if test="deptcode != null">deptcode = #{deptcode},</if>
+            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
             <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1213,7 +1350,7 @@
             <if test="wechat != null  and wechat != ''">wechat = #{wechat},</if>
             <if test="roomno != null  and roomno != ''">roomno = #{roomno},</if>
             <if test="admindate != null">admindate = #{admindate},</if>
-            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
+            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
             <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
             <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
             <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1293,7 +1430,7 @@
             <if test="recordid != null">recordid = #{recordid},</if>
             <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
             <if test="pulltime != null">pulltime = #{pulltime},</if>
-            <if test="deptcode != null">deptcode = #{deptcode},</if>
+            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
             <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1320,7 +1457,7 @@
             <if test="wechat != null  and wechat != ''">wechat = #{wechat},</if>
             <if test="roomno != null  and roomno != ''">roomno = #{roomno},</if>
             <if test="admindate != null">admindate = #{admindate},</if>
-            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
+            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
             <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
             <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
             <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1404,7 +1541,7 @@
             <if test="recordid != null">recordid = #{recordid},</if>
             <if test="exrecallcount != null">exrecallcount = #{exrecallcount},</if>
             <if test="pulltime != null">pulltime = #{pulltime},</if>
-            <if test="deptcode != null">deptcode = #{deptcode},</if>
+            <if test="leaveldeptcode != null">leaveldeptcode = #{leaveldeptcode},</if>
             <if test="labelstatus != null">labelstatus = #{labelstatus},</if>
             <if test="updateBy != null">update_by = #{updateBy},</if>
             <if test="updateTime != null">update_time = #{updateTime},</if>
@@ -1431,7 +1568,7 @@
             <if test="admindate != null ">admindate = #{admindate},</if>
             <if test="visitCount != null ">visit_count = #{visitCount},</if>
             <if test="isVisitAgain != null ">is_visit_again = #{isVisitAgain},</if>
-            <if test="deptname != null  and deptname != ''">deptname = #{deptname},</if>
+            <if test="leaveldeptname != null  and leaveldeptname != ''">leaveldeptname = #{leaveldeptname},</if>
             <if test="leaveicd10code != null  and leaveicd10code != ''">leaveicd10code = #{leaveicd10code},</if>
             <if test="leavediagname != null  and leavediagname != ''">leavediagname = #{leavediagname},</if>
             <if test="libtemplateid != null ">libtemplateid = #{libtemplateid},</if>
@@ -1509,8 +1646,8 @@
         management_doctor,
         leavehospitaldistrictcode,
         leavehospitaldistrictname,
-        deptcode,
-        deptname,
+        leaveldeptcode,
+        leaveldeptname,
         sendstate,
         preachform,
         current_preachform,
@@ -1547,15 +1684,20 @@
         <if test="campusid != null and campusid != ''">
             and campusid = #{campusid}
         </if>
+        <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"
+             鍗楀崕婊℃剰搴﹀瓙浠诲姟鐨� leavehospitaldistrictcode 鏈� 83%锛�3236/3811锛変负 NULL锛�
+             鍘熸潵銆岀瀹� AND 鐥呭尯銆嶄袱涓潯浠舵案杩滀笉鍙兘鍚屾椂鎴愮珛 鈫� 鏁村紶缁熻琛ㄧ┖銆�
+             鍒ょ┖鏀捐鍙奖鍝嶇梾鍖轰负绌虹殑璁板綍锛屾湁鐥呭尯鐨勬暟鎹涓轰笉鍙樸�� -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND leavehospitaldistrictcode IN
+            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            )
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -1568,9 +1710,10 @@
                 #{serviceType}
             </foreach>
         </if>
+        <!-- 2026-10-21锛歷isit_time 涓虹┖锛堝崡鍗庢弧鎰忓害 3811/3811 鍏ㄤ负 NULL锛夋椂鎸夊畬鎴�/鍒涘缓鏃堕棿鍏滃簳 -->
         <if test="startTime != null and endTime!=null">
-            AND visit_time >= DATE(#{startTime})
-            AND visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
+            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})
@@ -1594,7 +1737,7 @@
             AND visit_count > 1
         </if>
         <if test="groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
-            AND ${groupKey} IN
+            AND IFNULL(${groupKey}, 'Unknown') IN
             <foreach collection="groupKeyList" item="key" open="(" separator=","
                      close=")">
                 #{key}
@@ -1673,7 +1816,7 @@
             </foreach>
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND b.deptcode IN
+            AND b.leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -1730,18 +1873,45 @@
         <if test="campusid != null and campusid != ''">
             and campusid = #{campusid}
         </if>
+        <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"
+             鍗楀崕婊℃剰搴﹀瓙浠诲姟鐨� leavehospitaldistrictcode 鏈� 83%锛�3236/3811锛変负 NULL锛�
+             鍘熸潵銆岀瀹� AND 鐥呭尯銆嶄袱涓潯浠舵案杩滀笉鍙兘鍚屾椂鎴愮珛 鈫� 鏁村紶缁熻琛ㄧ┖銆�
+             鍒ょ┖鏀捐鍙奖鍝嶇梾鍖轰负绌虹殑璁板綍锛屾湁鐥呭尯鐨勬暟鎹涓轰笉鍙樸�� -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND leavehospitaldistrictcode IN
+            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            )
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+            AND leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                     close=")">
+                #{leaveldeptcode}
+            </foreach>
+        </if>
+        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+            AND (hospitaldistrictcode IS NULL OR hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodes" item="hospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{hospitaldistrictcode}
+            </foreach>
+            )
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
+            </foreach>
+        </if>
+        <if test="hospitaldeptcodes != null and hospitaldeptcodes.size() > 0">
+            AND deptcode IN
+            <foreach collection="hospitaldeptcodes" item="hospitaldeptcode" open="(" separator=","
+                     close=")">
+                #{hospitaldeptcode}
             </foreach>
         </if>
         <if test="serviceType != null and serviceType.size() > 0">
@@ -1751,9 +1921,10 @@
                 #{serviceType}
             </foreach>
         </if>
+        <!-- 2026-10-21锛歷isit_time 涓虹┖锛堝崡鍗庢弧鎰忓害 3811/3811 鍏ㄤ负 NULL锛夋椂鎸夊畬鎴�/鍒涘缓鏃堕棿鍏滃簳 -->
         <if test="startTime != null and endTime!=null">
-            AND visit_time >= DATE(#{startTime})
-            AND visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
+            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})
@@ -1784,6 +1955,300 @@
         </if>
     </select>
 
+    <!--
+        ==================================================================
+        闅忚鍒嗙被缁熻锛�/smartor/serviceSubtask/getSfStatisticsCount锛夋�ц兘浼樺寲
+        ==================================================================
+        鏀归�犲墠锛歡etSfStatisticsScript 鍏堟妸 service_subtask 鍛戒腑鐨�**鍏ㄩ儴鏄庣粏琛�**
+        锛坓etSfStatistics 鍏� select 39 涓瓧娈碉紝鍚� continue_content / result / suggest 绛夊ぇ瀛楁锛�
+        鎹炲洖 JVM锛屽啀鍦ㄥ唴瀛橀噷鎸� drcode/绉戝/鐥呭尯 閫愯 if 绱姞锛涙澶栬繕瑕佹妸杩欐鍛戒腑鐨�
+        **鍏ㄩ儴瀛愪换鍔� id** 鐏岃繘 getSfStatisticsCategory 鐨� IN 鍒楄〃鍐嶆煡涓�閬嶃��
+        鍛戒腑鍑犲崈琛屾椂锛岀綉缁滀紶杈擄紙杩滅鐨勫簱灏ゅ叾鏄庢樉锛�+ 鍐呭瓨閬嶅巻 + 瓒呭ぇ IN 鍒楄〃灏辨槸鎺ュ彛鎱㈢殑涓诲洜銆�
+
+        鏀归�犲悗锛氱疮鍔犱笅鎺ㄥ埌鏁版嵁搴擄紝涓�娆� GROUP BY 鍙繑鍥炩�滃垎缁勬暟鈥濓紙閫氬父鍑犲崄琛岋級鐨勭粺璁$粨鏋滐紱
+        婊℃剰搴﹂鐩噺涔熷悓鏍锋寜鍒嗙粍鍦ㄥ簱閲岃仛鍚堛�傜粺璁″彛寰勪笌鍘熸潵鐨� getStatisticCategory 閫愭潯瀵归綈銆�
+        杩欎袱涓鍙ョ殑 where 鏉′欢涓� getSfStatistics 淇濇寔涓�鑷达紝淇濊瘉缁撴灉闆嗕笉鍙樸��
+
+        鍒嗙粍鍒楋紙2026-10 璧凤級锛�
+          绂婚櫌缁村害 鈥斺�� groupKey = leaveldeptcode / leavehospitaldistrictcode锛�
+          鍦ㄩ櫌缁村害 鈥斺�� groupKey = deptcode / hospitaldistrictcode锛�
+          杩囨护涓庡垎缁勪娇鐢ㄥ悓涓�鍒楋紙groupColumn = groupKey锛夛紝閬垮厤鍒嗙粍閿笌鑱氬悎琛屽彛寰勪笉涓�鑷淬��
+        ==================================================================
+    -->
+    <resultMap type="com.ruoyi.task.domain.ServiceSubtaskStatistic" id="ServiceSubtaskScriptStatisticResult">
+        <result property="groupCode" column="group_code"/>
+        <result property="firstVisitCount" column="first_visit_count"/>
+        <result property="againVisitCount" column="again_visit_count"/>
+        <result property="drcode" column="drcode"/>
+        <result property="drname" column="drname"/>
+        <result property="deptcode" column="deptcode"/>
+        <result property="deptname" column="deptname"/>
+        <result property="leavehospitaldistrictcode" column="leavehospitaldistrictcode"/>
+        <result property="leavehospitaldistrictname" column="leavehospitaldistrictname"/>
+        <result property="hospitaldistrictcode" column="hospitaldistrictcode"/>
+        <result property="hospitaldistrictname" column="hospitaldistrictname"/>
+        <result property="hospitaldeptcode" column="hospitaldeptcode"/>
+        <result property="hospitaldeptname" column="hospitaldeptname"/>
+        <result property="nonFollowUp" column="non_follow_up"/>
+        <result property="followUpNeeded" column="follow_up_needed"/>
+        <result property="needFollowUp" column="need_follow_up"/>
+        <result property="pendingFollowUp" column="pending_follow_up"/>
+        <result property="followUpSuccess" column="follow_up_success"/>
+        <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="abnormal" column="abnormal"/>
+        <result property="needFollowUpAgain" column="need_follow_up_again"/>
+        <result property="pendingFollowUpAgain" column="pending_follow_up_again"/>
+        <result property="followUpSuccessAgain" column="follow_up_success_again"/>
+        <result property="followUpFailAgain" column="follow_up_fail_again"/>
+        <result property="manualAgain" column="manual_again"/>
+        <result property="voiceAgain" column="voice_again"/>
+        <result property="smsAgain" column="sms_again"/>
+        <result property="weChatAgain" column="we_chat_again"/>
+        <result property="abnormalAgain" column="abnormal_again"/>
+    </resultMap>
+
+    <!--
+        鎸夊垎缁勮仛鍚堥殢璁跨粺璁★紙鏇夸唬鈥滃叏閲忔槑缁嗗洖鍐呭瓨鍐嶇疮鍔犫�濓級銆�
+        鍙e緞璇存槑锛堜笌鍘� ServiceSubtaskServiceImpl.getStatisticCategory 瀹屽叏涓�鑷达級锛�
+          * 鏃犻渶闅忚锛歴endstate = 4
+          * 搴旈殢璁匡細sendstate 闈炵┖涓� != 4
+          * 棣栨(visit_count = 1) / 鍐嶆(visit_count > 1) 涓や釜鍒嗘敮浜掓枼锛屼笌鍘熸潵鐨� if / else if 涓�鑷�
+          * 浜哄伐/璇煶/鐭俊/寰俊锛氶娆″垎鏀�**涓�**鎺掗櫎 sendstate = 4锛堝師浠g爜濡傛锛夛紝鍐嶆鍒嗘敮鍚屾牱
+          * 寮傚父锛歟xcep = '1' 涓� sendstate 闈炵┖
+          * 棣栨闅忚鐜� / 鍐嶆闅忚鐜囧湪 Java 閲屾寜鍘熷叕寮忥紙鍚師鏉ョ殑鍒嗘瘝鍒ゆ柇锛夎绠�
+    -->
+    <select id="getSfStatisticsScriptSum" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
+            resultMap="ServiceSubtaskScriptStatisticResult">
+        select
+        IFNULL(${groupColumn}, 'Unknown') as group_code,
+        MAX(drcode) as drcode,
+        MAX(drname) as drname,
+        MAX(leaveldeptcode) as deptcode,
+        MAX(leaveldeptname) as deptname,
+        MAX(leavehospitaldistrictcode) as leavehospitaldistrictcode,
+        MAX(leavehospitaldistrictname) as leavehospitaldistrictname,
+        MAX(hospitaldistrictcode) as hospitaldistrictcode,
+        MAX(hospitaldistrictname) as hospitaldistrictname,
+        MAX(deptcode) as hospitaldeptcode,
+        MAX(deptname) as hospitaldeptname,
+        SUM(CASE WHEN visit_count = 1 THEN 1 ELSE 0 END) as first_visit_count,
+        SUM(CASE WHEN visit_count > 1 THEN 1 ELSE 0 END) as again_visit_count,
+        SUM(CASE WHEN sendstate = 4 THEN 1 ELSE 0 END) as non_follow_up,
+        SUM(CASE WHEN sendstate is not null and sendstate != 4 THEN 1 ELSE 0 END) as follow_up_needed,
+        SUM(CASE WHEN visit_count = 1 and sendstate is not null and sendstate != 4 THEN 1 ELSE 0 END) as need_follow_up,
+        SUM(CASE WHEN visit_count = 1 and sendstate = 2 THEN 1 ELSE 0 END) as pending_follow_up,
+        SUM(CASE WHEN visit_count = 1 and sendstate = 6 THEN 1 ELSE 0 END) as follow_up_success,
+        SUM(CASE WHEN visit_count = 1 and (sendstate = 5 or sendstate = 7) THEN 1 ELSE 0 END) as follow_up_fail,
+        SUM(CASE WHEN visit_count = 1 and sendstate is not null and current_preachform = '1' THEN 1 ELSE 0 END) as manual,
+        SUM(CASE WHEN visit_count = 1 and sendstate is not null and current_preachform = '3' THEN 1 ELSE 0 END) as voice,
+        SUM(CASE WHEN visit_count = 1 and sendstate is not null and current_preachform = '4' THEN 1 ELSE 0 END) as sms,
+        SUM(CASE WHEN visit_count = 1 and sendstate is not null and current_preachform = '5' THEN 1 ELSE 0 END) as we_chat,
+        SUM(CASE WHEN visit_count = 1 and sendstate is not null and excep = '1' THEN 1 ELSE 0 END) as abnormal,
+        SUM(CASE WHEN visit_count > 1 and sendstate is not null and sendstate != 4 THEN 1 ELSE 0 END) as need_follow_up_again,
+        SUM(CASE WHEN visit_count > 1 and sendstate = 2 THEN 1 ELSE 0 END) as pending_follow_up_again,
+        SUM(CASE WHEN visit_count > 1 and (sendstate = 1 or sendstate = 3 or sendstate = 6) THEN 1 ELSE 0 END) as follow_up_success_again,
+        SUM(CASE WHEN visit_count > 1 and sendstate = 5 THEN 1 ELSE 0 END) as follow_up_fail_again,
+        SUM(CASE WHEN visit_count > 1 and sendstate is not null and current_preachform = '1' THEN 1 ELSE 0 END) as manual_again,
+        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
+        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 IS NULL OR leavehospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+            )
+        </if>
+        <if test="deptcodes != null and deptcodes.size() > 0">
+            AND leaveldeptcode IN
+            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
+                     close=")">
+                #{deptcode}
+            </foreach>
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+            AND leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                     close=")">
+                #{leaveldeptcode}
+            </foreach>
+        </if>
+        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+            AND (hospitaldistrictcode IS NULL OR hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodes" item="hospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{hospitaldistrictcode}
+            </foreach>
+            )
+        </if>
+        <if test="hospitaldeptcodes != null and hospitaldeptcodes.size() > 0">
+            AND deptcode IN
+            <foreach collection="hospitaldeptcodes" item="hospitaldeptcode" open="(" separator=","
+                     close=")">
+                #{hospitaldeptcode}
+            </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 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>
+        <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 IFNULL(${groupKey}, 'Unknown') IN
+            <foreach collection="groupKeyList" item="key" open="(" separator=","
+                     close=")">
+                #{key}
+            </foreach>
+        </if>
+        group by IFNULL(${groupColumn}, 'Unknown')
+    </select>
+
+    <!--
+        鎸夊垎缁勮仛鍚堟弧鎰忓害棰樼洰閲忥紙鏇夸唬 getSfStatisticsCategory 鐨勨�滈�愬瓙浠诲姟 IN 鏌ヨ + 鍐呭瓨 N*M 杩囨护鈥濓級銆�
+        where 鏉′欢 = 涓婇潰鑱氬悎鏌ヨ鐨勬潯浠� + 鍘� getSfStatisticsCategory 鐨勬槑缁嗘潯浠讹紝
+        鍥犳鍛戒腑鐨勬槑缁嗛泦鍚堜笌鏀归�犲墠锛坓etSfStatisticsCategory 缁撴灉鍐嶆寜瀛愪换鍔′笌鍒嗙粍姹備氦锛夊畬鍏ㄤ竴鑷淬��
+        configValue 涓虹┖鏃朵笉璋冪敤鏈鍙ワ紙鍘熷疄鐜板湪杩欑鍦烘櫙涓嬮鐩噺涓�寰嬩负 0锛夈��
+    -->
+    <select id="getSfStatisticsScriptJoySum" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
+            resultType="map">
+        select
+        IFNULL(t.${groupColumn}, 'Unknown') as groupCode,
+        SUM(CASE WHEN a.categoryid IN(${configValue})
+        THEN 1 ELSE 0 END) as joyAllCount,
+        SUM(CASE WHEN a.categoryid IN(${configValue})
+        AND IFNULL(a.matchedtext,a.asrtext) is not null
+        AND IFNULL(a.matchedtext,a.asrtext) != ''
+        THEN 1 ELSE 0 END) as joyCount
+        from service_subtask_detail a
+        INNER JOIN service_subtask t on a.sub_id = t.id
+        where 1=1
+        and a.del_flag = 0
+        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 IS NULL OR t.leavehospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+            )
+        </if>
+        <if test="deptcodes != null and deptcodes.size() > 0">
+            AND t.leaveldeptcode IN
+            <foreach collection="deptcodes" item="deptcode" open="(" separator=","
+                     close=")">
+                #{deptcode}
+            </foreach>
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+            AND t.leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                     close=")">
+                #{leaveldeptcode}
+            </foreach>
+        </if>
+        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+            AND (t.hospitaldistrictcode IS NULL OR t.hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodes" item="hospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{hospitaldistrictcode}
+            </foreach>
+            )
+        </if>
+        <if test="hospitaldeptcodes != null and hospitaldeptcodes.size() > 0">
+            AND t.deptcode IN
+            <foreach collection="hospitaldeptcodes" item="hospitaldeptcode" open="(" separator=","
+                     close=")">
+                #{hospitaldeptcode}
+            </foreach>
+        </if>
+        <if test="serviceType != null and serviceType.size() > 0">
+            AND t.service_type IN
+            <foreach collection="serviceType" item="serviceType" open="(" separator=","
+                     close=")">
+                #{serviceType}
+            </foreach>
+        </if>
+        <if test="startTime != null and endTime!=null">
+            AND IFNULL(t.visit_time, IFNULL(t.finishtime, t.create_time)) >= DATE(#{startTime})
+            AND IFNULL(t.visit_time, IFNULL(t.finishtime, t.create_time)) &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
+        </if>
+        <if test="hospitalStartTime != null and hospitalEndTime!=null">
+            AND t.endtime >= DATE(#{hospitalStartTime})
+            AND t.endtime &lt; DATE_ADD(DATE(#{hospitalEndTime}), INTERVAL 1 DAY)
+        </if>
+        <if test="visitDeptCode != null">and t.visit_dept_code = #{visitDeptCode}</if>
+        <if test="visitDeptName != null">and t.visit_dept_name = #{visitDeptName}</if>
+        <if test="isabnormal != null">and t.isabnormal = #{isabnormal}</if>
+        <if test="continueFlag != null ">and t.continue_flag = #{continueFlag}</if>
+        <if test="continueTimeNow != null ">and t.continue_time_now = #{continueTimeNow,jdbcType=TIMESTAMP}</if>
+        <if test="continueCount != null ">and t.continue_count = #{continueCount}</if>
+        <if test="continueTimeNext != null ">and t.continue_time_next = #{continueTimeNext,jdbcType=TIMESTAMP}</if>
+        <if test="type != null">
+            and t.type = #{type}
+        </if>
+        <if test="visitCount != null and visitCount == 1">
+            AND t.visit_count = 1
+        </if>
+        <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">
+            AND IFNULL(t.${groupKey}, 'Unknown') IN
+            <foreach collection="groupKeyList" item="key" open="(" separator=","
+                     close=")">
+                #{key}
+            </foreach>
+        </if>
+        group by IFNULL(t.${groupColumn}, 'Unknown')
+    </select>
+
     <select id="getSfStatisticsHyperlink" parameterType="com.ruoyi.task.domain.ServiceSubtaskCountReq"
             resultMap="ServiceSubtaskResult">
         select id,
@@ -1795,8 +2260,8 @@
         drname,
         leavehospitaldistrictcode,
         leavehospitaldistrictname,
-        deptcode,
-        deptname,
+        leaveldeptcode,
+        leaveldeptname,
         sendstate,
         preachform,
         current_preachform,
@@ -1834,15 +2299,17 @@
         <if test="campusid != null and campusid != ''">
             and campusid = #{campusid}
         </if>
+        <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"锛堝崡鍗庢弧鎰忓害 83% 瀛愪换鍔℃棤鐥呭尯锛夛紝鏈夌梾鍖虹殑鏁版嵁琛屼负涓嶅彉 -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND leavehospitaldistrictcode IN
+            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            )
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -1855,9 +2322,10 @@
                 #{serviceType}
             </foreach>
         </if>
+        <!-- 2026-10-21锛歷isit_time 涓虹┖鏃舵寜瀹屾垚/鍒涘缓鏃堕棿鍏滃簳 -->
         <if test="startTime != null and endTime!=null">
-            AND visit_time >= DATE(#{startTime})
-            AND visit_time &lt; DATE_ADD(DATE(#{endTime}), INTERVAL 1 DAY)
+            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="visitDeptCode != null">and visit_dept_code = #{visitDeptCode}</if>
         <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
@@ -1877,7 +2345,7 @@
             AND visit_count > 1
         </if>
         <if test="groupKey != null and groupKey != '' and groupKeyList != null and groupKeyList.size>0">
-            AND ${groupKey} IN
+            AND IFNULL(${groupKey}, 'Unknown') IN
             <foreach collection="groupKeyList" item="key" open="(" separator=","
                      close=")">
                 #{key}
@@ -1909,7 +2377,7 @@
             AND leavehospitaldistrictcode = #{leavehospitaldistrictcode}
         </if>
         <if test="deptcode != null and deptcode != ''">
-            AND deptcode = #{deptcode}
+            AND leaveldeptcode = #{deptcode}
         </if>
         order by endtime desc
         <if test="pageSize != null  and pageNum != null">
@@ -1932,15 +2400,17 @@
         <if test="campusid != null and campusid != ''">
             and campusid = #{campusid}
         </if>
+        <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"锛堝崡鍗庢弧鎰忓害 83% 瀛愪换鍔℃棤鐥呭尯锛夛紝鏈夌梾鍖虹殑鏁版嵁琛屼负涓嶅彉 -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND leavehospitaldistrictcode IN
+            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            )
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -1954,8 +2424,8 @@
             </foreach>
         </if>
         <if test="startTime != null and endTime!=null">
-            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')
+            AND date_format(IFNULL(visit_time, IFNULL(finishtime, create_time)),'%y%m%d') >= date_format(#{startTime},'%y%m%d')
+            AND date_format(IFNULL(visit_time, IFNULL(finishtime, create_time)),'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
         </if>
         <if test="visitDeptCode != null">and visit_dept_code = #{visitDeptCode}</if>
         <if test="visitDeptName != null">and visit_dept_name = #{visitDeptName}</if>
@@ -1979,15 +2449,17 @@
         <if test="campusid != null and campusid != ''">
             and campusid = #{campusid}
         </if>
+        <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"锛堝崡鍗庢弧鎰忓害 83% 瀛愪换鍔℃棤鐥呭尯锛夛紝鏈夌梾鍖虹殑鏁版嵁琛屼负涓嶅彉 -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND leavehospitaldistrictcode IN
+            AND (leavehospitaldistrictcode IS NULL OR leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            )
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -2001,8 +2473,8 @@
             </foreach>
         </if>
         <if test="startTime != null and endTime!=null">
-            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')
+            AND date_format(IFNULL(visit_time, IFNULL(finishtime, create_time)),'%y%m%d') >= date_format(#{startTime},'%y%m%d')
+            AND date_format(IFNULL(visit_time, IFNULL(finishtime, create_time)),'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
         </if>
         ) AND targetid=a.id) joyCount , (SELECT COUNT(*) from (select sub_id FROM service_subtask_detail
         WHERE sub_id IN(SELECT id FROM service_subtask t
@@ -2023,7 +2495,7 @@
             </foreach>
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -2037,8 +2509,8 @@
             </foreach>
         </if>
         <if test="startTime != null and endTime!=null">
-            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')
+            AND date_format(IFNULL(visit_time, IFNULL(finishtime, create_time)),'%y%m%d') >= date_format(#{startTime},'%y%m%d')
+            AND date_format(IFNULL(visit_time, IFNULL(finishtime, create_time)),'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
         </if>
         GROUP BY sub_id ) ) ta ) joyAllCount
         FROM ivr_liba_target a
@@ -2072,15 +2544,20 @@
         <if test="campusid != null and campusid != ''">
             and t.campusid = #{campusid}
         </if>
+        <!-- 2026-10-21锛氱梾鍖轰负绌烘椂瑙嗕负"涓嶉檺"
+             鍗楀崕婊℃剰搴﹀瓙浠诲姟鐨� leavehospitaldistrictcode 鏈� 83%锛�3236/3811锛変负 NULL锛�
+             鍘熸潵銆岀瀹� AND 鐥呭尯銆嶄袱涓潯浠舵案杩滀笉鍙兘鍚屾椂鎴愮珛 鈫� 鏁村紶缁熻琛ㄧ┖銆�
+             鍒ょ┖鏀捐鍙奖鍝嶇梾鍖轰负绌虹殑璁板綍锛屾湁鐥呭尯鐨勬暟鎹涓轰笉鍙樸�� -->
         <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size() > 0">
-            AND t.leavehospitaldistrictcode IN
+            AND (t.leavehospitaldistrictcode IS NULL OR t.leavehospitaldistrictcode IN
             <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
                      close=")">
                 #{leavehospitaldistrictcode}
             </foreach>
+            )
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND t.deptcode IN
+            AND t.leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
@@ -2100,9 +2577,10 @@
                 #{serviceType}
             </foreach>
         </if>
+        <!-- 2026-10-21锛歷isit_time 涓虹┖鏃舵寜瀹屾垚/鍒涘缓鏃堕棿鍏滃簳 -->
         <if test="startTime != null and endTime!=null">
-            AND date_format(t.visit_time,'%y%m%d') >= date_format(#{startTime},'%y%m%d')
-            AND date_format(t.visit_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
+            AND date_format(IFNULL(t.visit_time, IFNULL(t.finishtime, t.create_time)),'%y%m%d') >= date_format(#{startTime},'%y%m%d')
+            AND date_format(IFNULL(t.visit_time, IFNULL(t.finishtime, t.create_time)),'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
         </if>
         <if test="visitDeptCode != null">
             and t.visit_dept_code = #{visitDeptCode}
@@ -2166,10 +2644,31 @@
             </foreach>
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND ss.deptcode IN
+            AND ss.leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
+            </foreach>
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+            AND ss.leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                     close=")">
+                #{leaveldeptcode}
+            </foreach>
+        </if>
+        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+            AND ss.hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodes" item="hospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{hospitaldistrictcode}
+            </foreach>
+        </if>
+        <if test="hospitaldeptcodes != null and hospitaldeptcodes.size() > 0">
+            AND ss.deptcode IN
+            <foreach collection="hospitaldeptcodes" item="hospitaldeptcode" open="(" separator=","
+                     close=")">
+                #{hospitaldeptcode}
             </foreach>
         </if>
         <if test="serviceType != null and serviceType.size() > 0">
@@ -2179,9 +2678,10 @@
                 #{serviceType}
             </foreach>
         </if>
+        <!-- 2026-10-21锛歷isit_time 涓虹┖锛堝崡鍗庢弧鎰忓害 3811/3811 鍏ㄤ负 NULL锛夋椂鎸夊畬鎴�/鍒涘缓鏃堕棿鍏滃簳 -->
         <if test="startTime != null and endTime!=null">
-            AND date_format(ss.visit_time,'%y%m%d') >= date_format(#{startTime},'%y%m%d')
-            AND date_format(ss.visit_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
+            AND date_format(IFNULL(ss.visit_time, IFNULL(ss.finishtime, ss.create_time)),'%y%m%d') >= date_format(#{startTime},'%y%m%d')
+            AND date_format(IFNULL(ss.visit_time, IFNULL(ss.finishtime, ss.create_time)),'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
         </if>
         <!-- 鐩墠鍙粺璁¤闊冲拰闂嵎 -->
         <if test="type != null">
@@ -2226,10 +2726,31 @@
             </foreach>
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND ss.deptcode IN
+            AND ss.leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}
+            </foreach>
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size() > 0">
+            AND ss.leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                     close=")">
+                #{leaveldeptcode}
+            </foreach>
+        </if>
+        <if test="hospitaldistrictcodes != null and hospitaldistrictcodes.size() > 0">
+            AND ss.hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodes" item="hospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{hospitaldistrictcode}
+            </foreach>
+        </if>
+        <if test="hospitaldeptcodes != null and hospitaldeptcodes.size() > 0">
+            AND ss.deptcode IN
+            <foreach collection="hospitaldeptcodes" item="hospitaldeptcode" open="(" separator=","
+                     close=")">
+                #{hospitaldeptcode}
             </foreach>
         </if>
         <if test="serviceType != null and serviceType.size() > 0">
@@ -2239,9 +2760,10 @@
                 #{serviceType}
             </foreach>
         </if>
+        <!-- 2026-10-21锛歷isit_time 涓虹┖锛堝崡鍗庢弧鎰忓害 3811/3811 鍏ㄤ负 NULL锛夋椂鎸夊畬鎴�/鍒涘缓鏃堕棿鍏滃簳 -->
         <if test="startTime != null and endTime!=null">
-            AND date_format(ss.visit_time,'%y%m%d') >= date_format(#{startTime},'%y%m%d')
-            AND date_format(ss.visit_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
+            AND date_format(IFNULL(ss.visit_time, IFNULL(ss.finishtime, ss.create_time)),'%y%m%d') >= date_format(#{startTime},'%y%m%d')
+            AND date_format(IFNULL(ss.visit_time, IFNULL(ss.finishtime, ss.create_time)),'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
         </if>
         <!-- 鐩墠鍙粺璁¤闊冲拰闂嵎 -->
         <if test="type != null">
@@ -2279,7 +2801,7 @@
                recordid,
                exrecallcount,
                pulltime,
-               deptcode,
+               leaveldeptcode,
                labelstatus,
                del_flag,
                upid,
@@ -2309,7 +2831,7 @@
                admindate,
                patid,
                service_type,
-               deptname,
+               leaveldeptname,
                score,
                drname,
                drcode,
@@ -2368,7 +2890,7 @@
     </select>
 
     <select id="getDeptRanking" parameterType="com.ruoyi.common.core.service.dto.PatMedReq" resultType="com.ruoyi.common.core.service.dto.PatMedRes">
-        SELECT deptname,
+        SELECT leaveldeptname AS deptname,
         count(1) AS rc
         FROM service_subtask
         WHERE DATE_FORMAT(visit_time, '%Y-%m-%d') >= #{startDate}
@@ -2381,7 +2903,7 @@
         <if test="campusid != null and campusid != ''">
             AND campusid = #{campusid}
         </if>
-        GROUP BY deptname
+        GROUP BY leaveldeptname
     </select>
 
     <!-- 鏈嶅姟缁熻鏌ヨ - 鎸夋椂闂寸淮搴︾粺璁″嚭闄�/闂ㄨ瘖闅忚鏁版嵁 -->
@@ -2432,7 +2954,7 @@
             AND campusid = #{campusid}
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator="," close=")">
                 #{deptcode}
             </foreach>
@@ -2581,46 +3103,24 @@
             AND phone = #{phone}
         </if>
         <if test="finishtimeStart != null">
-            AND date_format(finishtime,'%y%m%d') &gt;= date_format(#{finishtimeStart},'%y%m%d')
+            AND finishtime &gt;= DATE(#{finishtimeStart})
         </if>
         <if test="finishtimeEnd != null">
-            AND date_format(finishtime,'%y%m%d') &lt;= date_format(#{finishtimeEnd},'%y%m%d')
+            AND finishtime &lt; DATE_ADD(DATE(#{finishtimeEnd}), INTERVAL 1 DAY)
         </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>
+        <!--
+            绉戝/鐥呭尯绛涢�夛紙2026-11 淇锛夛細
+              绂婚櫌缁勶紙leaveldeptcodes / leavehospitaldistrictcodes锛変繚鎸佸師閫昏緫锛�
+              鍦ㄩ櫌缁勶紙deptcodes / hospitaldistrictcodes 鈫� deptcode / hospitaldistrictcode锛夋槸鏂板鐨勪竴缁勶紱
+              缁勫唴鎸� deptOrDistrict 鍙� AND('1') / OR('2')锛屼袱缁勪箣闂� AND锛堝彧浼犱竴缁勬椂鍙︿竴缁勪笉鎷硷級銆�
+        -->
+        <if test='deptOrDistrict == "1"'>
+            <include refid="filterLeaveDimAnd"/>
+            <include refid="filterInhospDimAnd"/>
         </if>
-        <if test="deptOrDistrict==2">
-            AND (1=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">
-                OR deptcode IN
-                <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
-                         close=")">
-                    #{leaveldeptcode}
-                </foreach>
-            </if>
-            )
+        <if test='deptOrDistrict == "2"'>
+            <include refid="filterLeaveDimOr"/>
+            <include refid="filterInhospDimOr"/>
         </if>
         <if test="visitCount != null and visitCount > 1 and visitDeptCodes != null and visitDeptCodes.size() > 0">
             AND visit_dept_code IN
@@ -2694,37 +3194,24 @@
             AND templatename LIKE CONCAT('%', #{templatename}, '%')
         </if>
         <if test="startOutHospTime != null">
-            AND DATE_FORMAT(endtime,'%y%m%d') &gt;= DATE_FORMAT(#{startOutHospTime},'%y%m%d')
+            AND endtime &gt;= DATE(#{startOutHospTime})
         </if>
         <if test="endOutHospTime != null">
-            AND DATE_FORMAT(endtime,'%y%m%d') &lt;= DATE_FORMAT(#{endOutHospTime},'%y%m%d')
+            AND endtime &lt; DATE_ADD(DATE(#{endOutHospTime}), INTERVAL 1 DAY)
         </if>
         <if test="startSendDateTime != null">
-            AND DATE_FORMAT(visit_time,'%y%m%d') &gt;= DATE_FORMAT(#{startSendDateTime},'%y%m%d')
+            AND visit_time &gt;= DATE(#{startSendDateTime})
         </if>
         <if test="endSendDateTime != null">
-            AND DATE_FORMAT(visit_time,'%y%m%d') &lt;= DATE_FORMAT(#{endSendDateTime},'%y%m%d')
+            AND visit_time &lt; DATE_ADD(DATE(#{endSendDateTime}), INTERVAL 1 DAY)
         </if>
         <if test="longSendTime != null">
-            AND DATE_FORMAT(long_send_time,'%y%m%d') &lt;= DATE_FORMAT(#{longSendTime},'%y%m%d')
+            AND long_send_time &lt; DATE_ADD(DATE(#{longSendTime}), INTERVAL 1 DAY)
         </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="deptcode != null and deptcode != ''">
-            AND deptcode = #{deptcode}
+        <!-- 2026-11锛氳繖閲屽師鏈繕鏈変竴浠戒笌涓婃柟 deptOrDistrict==1 瀹屽叏閲嶅鐨勭梾鍖�/绉戝鏉′欢锛�
+             宸插垹闄わ紙鏉′欢鏀圭敤 filterSubtaskWardDim / filterSubtaskDeptDim 鐗囨锛岄伩鍏嶄袱浠藉彛寰勪笉涓�鑷达級 -->
+        <if test="leaveldeptcode != null and leaveldeptcode != ''">
+            AND leaveldeptcode = #{leaveldeptcode}
         </if>
         <if test="sendstate != null">
             AND sendstate = #{sendstate}
@@ -2784,7 +3271,7 @@
             AND date_format( visit_time, '%y%m%d' ) &lt;= date_format(#{endDate},'%y%m%d')
         </if>
         <if test="deptcodeList != null and deptcodeList.size()>0">
-            and deptcode in
+            and leaveldeptcode in
             <foreach collection="deptcodeList" item="deptcode" open="(" separator="," close=")">
                 #{deptcode}
             </foreach>
@@ -2819,7 +3306,7 @@
             </foreach>
         </if>
         <if test=" leaveldeptcodes != null and leaveldeptcodes.size()>0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
                      close=")">
                 #{leaveldeptcode}
@@ -2867,14 +3354,14 @@
             parameterType="com.ruoyi.task.domain.VO.ServiceSubtaskCotinueCountVO"
             resultType="java.util.Map">
         SELECT
-        deptcode AS groupCode,
-        deptname AS groupName,
+        leaveldeptcode AS groupCode,
+        leaveldeptname AS groupName,
         SUM(CASE WHEN continue_flag = 1 THEN 1 ELSE 0 END) AS noContinueCnt,
         SUM(CASE WHEN continue_flag = 2 THEN 1 ELSE 0 END) AS continueCnt
         FROM service_subtask
         WHERE del_flag = 0
         AND continue_flag IN (1, 2)
-        AND deptcode IS NOT NULL AND deptcode != ''
+        AND leaveldeptcode IS NOT NULL AND leaveldeptcode != ''
         <if test="orgid != null and orgid != ''">
             AND orgid = #{orgid}
         </if>
@@ -2882,12 +3369,12 @@
             AND campusid = #{campusid}
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="code" open="(" separator="," close=")">
                 #{code}
             </foreach>
         </if>
-        GROUP BY deptcode, deptname
+        GROUP BY leaveldeptcode, leaveldeptname
         ORDER BY groupCode
     </select>
 
@@ -2900,8 +3387,8 @@
                 leavehospitaldistrictname AS groupName,
             </when>
             <otherwise>
-                deptcode AS groupCode,
-                deptname AS groupName,
+                leaveldeptcode AS groupCode,
+                leaveldeptname AS groupName,
             </otherwise>
         </choose>
         COUNT(*) AS totalCount,
@@ -2938,12 +3425,12 @@
             </when>
             <otherwise>
                 <if test="deptcodes != null and deptcodes.size() > 0">
-                    AND deptcode IN
+                    AND leaveldeptcode IN
                     <foreach collection="deptcodes" item="code" open="(" separator="," close=")">
                         #{code}
                     </foreach>
                 </if>
-                GROUP BY deptcode, deptname
+                GROUP BY leaveldeptcode, leaveldeptname
                 ORDER BY groupCode
             </otherwise>
         </choose>
@@ -2979,8 +3466,8 @@
         drname,
         leavehospitaldistrictcode,
         leavehospitaldistrictname,
-        deptcode,
-        deptname,
+        leaveldeptcode,
+        leaveldeptname,
         sendstate,
         preachform,
         current_preachform,
@@ -3033,7 +3520,7 @@
             </foreach>
         </if>
         <if test="deptcodes != null and deptcodes.size() > 0">
-            AND deptcode IN
+            AND leaveldeptcode IN
             <foreach collection="deptcodes" item="deptcode" open="(" separator=","
                      close=")">
                 #{deptcode}

--
Gitblit v1.9.3