From b1b1b15bbf4fd5a4ed21efa91279227fc915e960 Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期一, 16 十一月 2026 16:35:48 +0800
Subject: [PATCH] 【丽水】JOIN报错调整

---
 ruoyi-inhospvisit/src/main/resources/mapper/inhospvisit/PatMedInhospMapper.xml |  339 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 217 insertions(+), 122 deletions(-)

diff --git a/ruoyi-inhospvisit/src/main/resources/mapper/inhospvisit/PatMedInhospMapper.xml b/ruoyi-inhospvisit/src/main/resources/mapper/inhospvisit/PatMedInhospMapper.xml
index 7a0dc7d..015d62e 100644
--- a/ruoyi-inhospvisit/src/main/resources/mapper/inhospvisit/PatMedInhospMapper.xml
+++ b/ruoyi-inhospvisit/src/main/resources/mapper/inhospvisit/PatMedInhospMapper.xml
@@ -163,6 +163,140 @@
         from pat_med_inhosp
     </sql>
 
+    <sql id="selectPatMedInhospListFromWhere">
+        pat_med_inhosp b
+        JOIN
+        pat_archive a ON a.id = b.patid
+        where 1=1
+        <!--
+          del_flag 蹇呴』鐢ㄥ瓧绗︿覆甯搁噺姣旇緝锛氬畠鍙栧�兼槸 '0'/'1'锛坈har 鍒楋級銆�
+          鍐欐垚 del_flag = 0 鏃� MySQL 浼氭妸銆愬垪銆戞寜 DOUBLE 闅愬紡杞崲锛岃鏉′欢灏卞彧鏄繃婊ゃ��
+          涓嶈兘鐢ㄦ潵瀹氫綅绱㈠紩锛涜�� b.del_flag 姝f槸 idx_inhosp_optimize / idx_pmi_del_org_end
+          鐨勯鍒� 鈥斺�� 鏁板瓧鍐欐硶绛変簬鎶婄储寮曢鍒楃櫧缁欍��
+          鍚屼竴缁撹瑙� sql/sltdMydTotalByDimension_index_20260924.sql 绗� 117-134 琛屻��
+          锛堝垪鏄� int 鏃跺啓 '0' 涔熷畨鍏細杞崲鍙戠敓鍦ㄥ父閲忎晶锛岀储寮曠収鏍峰彲鐢ㄣ�傦級
+        -->
+        AND a.del_flag = '0'
+        AND b.del_flag = '0'
+        AND a.idcardno IS NOT NULL
+        <if test="inhospid != null">and b.inhospid = #{inhospid}</if>
+        <!-- 鏃堕棿鑼冨洿淇濇寔鈥滄寜鏃ユ湡銆佺粨鏉熸棩鍚綋澶┾�濈殑璇箟锛涘垪淇濇寔瑁稿垪锛岄伩鍏� date_format 瀵艰嚧绱㈠紩澶辨晥 -->
+        <if test="startOutHospTime != null ">and b.endtime &gt;= DATE(#{startOutHospTime})</if>
+        <if test="endOutHospTime != null ">and b.endtime &lt; DATE_ADD(DATE(#{endOutHospTime}), INTERVAL 1 DAY)</if>
+        <if test="startInHospTime != null ">and b.starttime &gt;= DATE(#{startInHospTime})</if>
+        <if test="endInHospTime != null ">and b.starttime &lt; DATE_ADD(DATE(#{endInHospTime}), INTERVAL 1 DAY)</if>
+        <if test="hospitalname != null  and hospitalname != ''">and hospitalname like concat('%', #{hospitalname},
+            '%')
+        </if>
+        <if test="patname != null  and patname != ''">and patname like concat('%', #{patname}, '%')</if>
+        <if test="hospitaldistrictname != null  and hospitaldistrictname != ''">and hospitaldistrictname like
+            concat('%', #{hospitaldistrictname}, '%')
+        </if>
+        <if test="endtime != null ">and b.endtime = #{endtime}</if>
+        <if test="bedNo != null ">and b.bed_no = #{bedNo}</if>
+        <if test="inhospMydFlag != null ">and b.inhosp_myd_flag = #{inhospMydFlag}</if>
+        <if test="managementDoctor != null ">and b.management_doctor = #{managementDoctor}</if>
+        <if test="managementDoctorCode != null ">and b.management_doctor_code = #{managementDoctorCode}</if>
+        <if test="inhospno != null ">and b.inhospno = #{inhospno}</if>
+        <if test="fuflag != null ">and b.fuflag = #{fuflag}</if>
+        <if test="orgid != null ">and b.orgid = #{orgid}</if>
+        <if test="campusid != null ">and b.campusid = #{campusid}</if>
+        <if test="serialnum != null ">and b.serialnum = #{serialnum}</if>
+        <if test="patno != null ">and b.patno = #{patno}</if>
+        <if test="nurseId != null ">and b.nurse_id = #{nurseId}</if>
+        <if test="nurseName != null and nurseName != ''">and b.nurse_name = #{nurseName}</if>
+        <if test="deptcheckFlag != null ">and b.deptcheck_flag = #{deptcheckFlag}</if>
+        <if test="scheduleCheckFlag != null ">and b.schedulecheck_flag = #{scheduleCheckFlag}</if>
+        <if test="wardcheckFlag != null ">and b.wardcheck_flag = #{wardcheckFlag}</if>
+        <if test="inDeptcheckFlag != null ">and b.in_deptcheck_flag = #{inDeptcheckFlag}</if>
+        <if test="inWardcheckFlag != null ">and b.in_wardcheck_flag = #{inWardcheckFlag}</if>
+        <if test="diagcheckFlag != null ">and b.diagcheck_flag = #{diagcheckFlag}</if>
+        <if test="encounterSourceId != null ">and b.encounter_source_id = #{encounterSourceId}</if>
+        <if test="importanceFlagCode != null ">and b.importance_flag_code = #{importanceFlagCode}</if>
+        <if test="emergencyLevelCode != null ">and b.emergency_level_code = #{emergencyLevelCode}</if>
+        <!--
+          鍦ㄩ櫌锛坈ry=0锛夛細涓や釜鏉′欢閮借銆�
+          鈶� b.endtime is null 鈥斺�� 鍘� XML 灏辨湁锛屼繚鎸佷笉鍔紱
+          鈶� b.inhospstate = '0' 鈥斺�� 杩佸嚭鍓嶇敱 PatMedInhospServiceImpl 琛ワ紙cry=0 鏃�
+             setInhospstate("0")锛夛紝杩佸埌鏈ā鍧楀悗杩欐潯閾剧洿杩� Mapper锛岃繖鍙ュ氨涓簡銆�
+          涓轰粈涔堝繀椤昏ˉ鍥炴潵锛堜笉鍙槸"鎭㈠鍘熻涓�"锛屼篃鏄�ц兘闂锛夛細
+            路 绱㈠紩 idx_inhosp_optimize / idx_pmi_del_org_state_end 鐨勫墠涓夊垪鏄�
+              (del_flag, orgid, inhospstate)锛屽彧鏈変笁涓瓑鍊兼潯浠堕綈浜嗘墠鑳界簿纭畾浣�"鍦ㄩ櫌"閭e嚑鍗冭锛�
+              灏戜簡 inhospstate锛屽氨鍙兘鐢ㄥ埌 (del_flag, orgid) 鍓嶇紑 = 璇ユ満鏋勩�愬叏閮ㄥ巻鍙蹭綇闄㈣褰曘�戯紝
+              鍐嶅姞涓婂悗闈㈢殑 ORDER BY/GROUP BY锛屽疄娴嬪氨鏄� 4~5 绉掔殑鏉ユ簮銆�
+            路 cry=1/2/3 鏈潵灏辨槸鎸� inhospstate 鍒ょ殑锛屽彧鏈� cry=0 鐢� endtime 鍒� 鈥斺�� 琛ラ綈鍚庢墠瀵圭О銆�
+          涓轰粈涔堝姞 inhospstate == null 杩欎釜鍓嶆彁锛氳皟鐢ㄦ柟鑻ュ凡鏄惧紡鎸囧畾 inhospstate锛屽氨鍒拰瀹冩墦鏋�
+          锛堝惁鍒� cry=0 + inhospstate='2' 浼氬彉鎴�"鍜岃嚜韬煕鐩�"鐨勭┖缁撴灉锛夈��
+        -->
+        <if test="cry != null and cry == 0 ">and b.endtime is null</if>
+        <if test="cry != null and cry == 0 and inhospstate == null ">and b.inhospstate = '0'</if>
+        <!-- 鍚� del_flag锛歩nhospstate 鏄彇鍊艰寖鍥� '0'~'3' 鐨勫瓧绗﹀垪锛屽繀椤荤敤瀛楃涓插父閲忥紝
+             鍚﹀垯绱㈠紩涓婄敤涓嶅埌杩欎釜绛夊�兼潯浠讹紙鍑洪櫌/杞/棰勫叆闄� 涓変釜瑙嗗浘璧扮殑灏辨槸杩欎笁琛岋級 -->
+        <if test="cry != null and cry == 1 ">and b.inhospstate='1'</if>
+        <if test="cry != null and cry == 2 ">and b.inhospstate='2'</if>
+        <if test="cry != null and cry == 3 ">and b.inhospstate='3'</if>
+        <if test="inhospstate != null ">and b.inhospstate = #{inhospstate}</if>
+        <if test="schemestatus != null ">and b.schemestatus = #{schemestatus}</if>
+        <if test="outWayId != null ">and b.out_way_id = #{outWayId}</if>
+        <if test="outWayName != null ">and b.out_way_name = #{outWayName}</if>
+        <if test="leaveldeptcode != null ">and b.leaveldeptcode = #{leaveldeptcode}</if>
+        <if test="leavediagname != null  and leavediagname != ''">and b.leavediagname like concat('%',
+            #{leavediagname}, '%')
+        </if>
+        <if test="drname != null  and drname != ''">and b.drname like concat('%', #{drname}, '%')</if>
+        <if test="leaveldeptname != null  and leaveldeptname != ''">and b.leaveldeptname like concat('%',
+            #{leaveldeptname}, '%')
+        </if>
+        <if test="deptname != null  and deptname != ''">and b.deptname like concat('%',
+            #{deptname}, '%')
+        </if>
+        <if test="patid != null  and patid != ''">and a.id =#{patid}
+        </if>
+        <if test="leavehospitaldistrictname != null  and leavehospitaldistrictname != ''">and
+            b.leavehospitaldistrictname like concat('%', #{leavehospitaldistrictname}, '%')
+        </if>
+
+        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size()>0">
+            AND b.leavehospitaldistrictcode IN
+            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{leavehospitaldistrictcode}
+            </foreach>
+        </if>
+
+        <if test="hospitaldistrictcodeList != null and hospitaldistrictcodeList.size()>0">
+            AND b.hospitaldistrictcode IN
+            <foreach collection="hospitaldistrictcodeList" item="hospitaldistrictcode" open="(" separator=","
+                     close=")">
+                #{hospitaldistrictcode}
+            </foreach>
+        </if>
+        <if test="leaveldeptcodes != null and leaveldeptcodes.size()>0">
+            AND b.leaveldeptcode IN
+            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
+                     close=")">
+                #{leaveldeptcode}
+            </foreach>
+        </if>
+        <if test="deptcodeList != null and deptcodeList.size()>0">
+            AND b.deptcode IN
+            <foreach collection="deptcodeList" item="deptcode" open="(" separator=","
+                     close=")">
+                #{deptcode}
+            </foreach>
+        </if>
+        <if test="icd10codeList != null and icd10codeList.size()>0">
+            AND b.leaveicd10code IN
+            <foreach collection="icd10codeList" item="icd10code" open="(" separator=","
+                     close=")">
+                #{icd10code}
+            </foreach>
+        </if>
+        <if test="medicalCode != null and medicalCode != ''">and b.medical_code = #{medicalCode}</if>
+        <if test="medicalName != null and medicalName != ''">and b.medical_name = #{medicalName}</if>
+        <if test="medicalCompany != null and medicalCompany != ''">and b.medical_company = #{medicalCompany}</if>
+    </sql>
+
     <select id="selectPatMedInhospList" parameterType="com.ruoyi.inhospvisit.domain.PatMedInhosp" resultMap="PatMedInhospResult">
         select a.age AS age,
         a.telcode as telcode,
@@ -239,127 +373,14 @@
         b.medical_company,
         a.idcardno
         FROM
-        pat_med_inhosp b
-        JOIN
-        pat_archive a ON a.id = b.patid
-        where 1=1
-        AND a.del_flag = 0
-        AND b.del_flag = 0
-        AND a.idcardno IS NOT NULL
-        <if test="inhospid != null">and b.inhospid = #{inhospid}</if>
-        <if test="startOutHospTime != null ">and date_format(b.endtime,'%y%m%d') &gt;=
-            date_format(#{startOutHospTime},'%y%m%d')
-        </if>
-        <if test="endOutHospTime != null ">and date_format(b.endtime,'%y%m%d') &lt;=
-            date_format(#{endOutHospTime},'%y%m%d')
-        </if>
-        <if test="startInHospTime != null ">and date_format(b.starttime,'%y%m%d') &gt;=
-            date_format(#{startInHospTime},'%y%m%d')
-        </if>
-        <if test="endInHospTime != null ">and date_format(b.starttime,'%y%m%d') &lt;=
-            date_format(#{endInHospTime},'%y%m%d')
-        </if>
-        <if test="hospitalname != null  and hospitalname != ''">and hospitalname like concat('%', #{hospitalname},
-            '%')
-        </if>
-        <if test="patname != null  and patname != ''">and patname like concat('%', #{patname}, '%')</if>
-        <if test="hospitaldistrictname != null  and hospitaldistrictname != ''">and hospitaldistrictname like
-            concat('%', #{hospitaldistrictname}, '%')
-        </if>
-        <if test="endtime != null ">and b.endtime = #{endtime}</if>
-        <if test="bedNo != null ">and b.bed_no = #{bedNo}</if>
-        <if test="inhospMydFlag != null ">and b.inhosp_myd_flag = #{inhospMydFlag}</if>
-        <if test="managementDoctor != null ">and b.management_doctor = #{managementDoctor}</if>
-        <if test="managementDoctorCode != null ">and b.management_doctor_code = #{managementDoctorCode}</if>
-        <if test="inhospno != null ">and b.inhospno = #{inhospno}</if>
-        <if test="fuflag != null ">and b.fuflag = #{fuflag}</if>
-        <if test="orgid != null ">and b.orgid = #{orgid}</if>
-        <if test="campusid != null ">and b.campusid = #{campusid}</if>
-        <if test="serialnum != null ">and b.serialnum = #{serialnum}</if>
-        <if test="endtime != null ">and b.endtime = #{endtime}</if>
-        <if test="patno != null ">and b.patno = #{patno}</if>
-        <if test="nurseId != null ">and b.nurse_id = #{nurseId}</if>
-        <if test="nurseName != null and nurseName != ''">and b.nurse_name = #{nurseName}</if>
-        <if test="deptcheckFlag != null ">and b.deptcheck_flag = #{deptcheckFlag}</if>
-        <if test="scheduleCheckFlag != null ">and b.schedulecheck_flag = #{scheduleCheckFlag}</if>
-        <if test="wardcheckFlag != null ">and b.wardcheck_flag = #{wardcheckFlag}</if>
-        <if test="inDeptcheckFlag != null ">and b.in_deptcheck_flag = #{inDeptcheckFlag}</if>
-        <if test="inWardcheckFlag != null ">and b.in_wardcheck_flag = #{inWardcheckFlag}</if>
-        <if test="diagcheckFlag != null ">and b.diagcheck_flag = #{diagcheckFlag}</if>
-        <if test="encounterSourceId != null ">and b.encounter_source_id = #{encounterSourceId}</if>
-        <if test="importanceFlagCode != null ">and b.importance_flag_code = #{importanceFlagCode}</if>
-        <if test="emergencyLevelCode != null ">and b.emergency_level_code = #{emergencyLevelCode}</if>
-        <if test="cry != null and cry == 0 ">and b.endtime is null</if>
-        <if test="cry != null and cry == 1 ">and b.inhospstate=1</if>
-        <if test="cry != null and cry == 2 ">and b.inhospstate=2</if>
-        <if test="cry != null and cry == 3 ">and b.inhospstate=3</if>
-        <if test="inhospstate != null ">and b.inhospstate = #{inhospstate}</if>
-        <if test="schemestatus != null ">and b.schemestatus = #{schemestatus}</if>
-        <if test="outWayId != null ">and b.out_way_id = #{outWayId}</if>
-        <if test="outWayName != null ">and b.out_way_name = #{outWayName}</if>
-        <if test="leaveldeptcode != null ">and b.leaveldeptcode = #{leaveldeptcode}</if>
-        <if test="leavediagname != null  and leavediagname != ''">and b.leavediagname like concat('%',
-            #{leavediagname}, '%')
-        </if>
-        <if test="drname != null  and drname != ''">and b.drname like concat('%', #{drname}, '%')</if>
-        <if test="leaveldeptname != null  and leaveldeptname != ''">and b.leaveldeptname like concat('%',
-            #{leaveldeptname}, '%')
-        </if>
-        <if test="deptname != null  and deptname != ''">and b.deptname like concat('%',
-            #{deptname}, '%')
-        </if>
-        <if test="patid != null  and patid != ''">and a.id =#{patid}
-        </if>
-        <if test="leavehospitaldistrictname != null  and leavehospitaldistrictname != ''">and
-            b.leavehospitaldistrictname like concat('%', #{leavehospitaldistrictname}, '%')
-        </if>
-
-        <if test="leavehospitaldistrictcodes != null and leavehospitaldistrictcodes.size()>0">
-            AND b.leavehospitaldistrictcode IN
-            <foreach collection="leavehospitaldistrictcodes" item="leavehospitaldistrictcode" open="(" separator=","
-                     close=")">
-                #{leavehospitaldistrictcode}
-            </foreach>
-        </if>
-
-        <if test="hospitaldistrictcodeList != null and hospitaldistrictcodeList.size()>0">
-            AND b.hospitaldistrictcode IN
-            <foreach collection="hospitaldistrictcodeList" item="hospitaldistrictcode" open="(" separator=","
-                     close=")">
-                #{hospitaldistrictcode}
-            </foreach>
-        </if>
-        <if test="leaveldeptcodes != null and leaveldeptcodes.size()>0">
-            AND b.leaveldeptcode IN
-            <foreach collection="leaveldeptcodes" item="leaveldeptcode" open="(" separator=","
-                     close=")">
-                #{leaveldeptcode}
-            </foreach>
-        </if>
-        <if test="deptcodeList != null and deptcodeList.size()>0">
-            AND b.deptcode IN
-            <foreach collection="deptcodeList" item="deptcode" open="(" separator=","
-                     close=")">
-                #{deptcode}
-            </foreach>
-        </if>
-        <if test="icd10codeList != null and icd10codeList.size()>0">
-            AND b.leaveicd10code IN
-            <foreach collection="icd10codeList" item="icd10code" open="(" separator=","
-                     close=")">
-                #{icd10code}
-            </foreach>
-        </if>
-        <if test="medicalCode != null and medicalCode != ''">and b.medical_code = #{medicalCode}</if>
-        <if test="medicalName != null and medicalName != ''">and b.medical_name = #{medicalName}</if>
-        <if test="medicalCompany != null and medicalCompany != ''">and b.medical_company = #{medicalCompany}</if>
-        GROUP BY
-        b.inhospid,
-        a.age,
-        a.age_unit,
-        a.telcode,
-        a.id,
-        a.sex
+        <include refid="selectPatMedInhospListFromWhere"/>
+        <!--
+          鎬ц兘璇存槑锛歱at_med_inhosp.inhospid 鍞竴锛宲at_archive.id 鏄富閿紝a.id = b.patid 涓�瀵逛竴锛�
+          鍘� GROUP BY 鏄浣欑殑锛屼笖浼氳 MySQL 鍏堢墿鍖� 60 鍒椾复鏃惰〃鍐嶆帓搴忥紝LIMIT 鏃犳硶鎻愬墠缁撴潫銆�
+          涓婄嚎鍓嶈纭浠ヤ笅涓ゆ潯鍧囨棤閲嶅锛�
+            SELECT inhospid, COUNT(*) c FROM pat_med_inhosp GROUP BY inhospid HAVING c > 1 LIMIT 10;
+            SELECT id, COUNT(*) c FROM pat_archive GROUP BY id HAVING c > 1 LIMIT 10;
+        -->
         <if test="cry != null and cry==1">
             order by b.endtime desc
         </if>
@@ -403,6 +424,55 @@
                 </otherwise>
             </choose>
         </if>
+    </select>
+
+    <!--
+      鍒嗛〉鎬绘暟锛圥ageHelper 鎵嬪啓 count锛宨d = 涓绘煡璇� id + _COUNT 浼氳鑷姩閲囩敤锛夈��
+
+      涓绘煡璇㈠凡鍘绘帀 GROUP BY锛歱at_med_inhosp.inhospid 鍞竴銆乸at_archive.id 鏄富閿紝
+      a.id = b.patid 涓�瀵逛竴锛屽洜姝� join 缁撴灉琛屾暟灏辨槸涓氬姟璁板綍鏁般��
+      杩欓噷鐩存帴瀵圭浉鍚岀殑 FROM/WHERE 鍋� COUNT锛屼笉鍐嶅寘涓�灞傚垎缁勫瓙鏌ヨ銆�
+
+      鈿狅笍鈿狅笍 銆愭湰鎺ュ彛鐪熸鐨勭儹鐐规槸杩欐潯 count锛屼笉鏄笂闈㈢殑鍒楄〃 SQL銆�
+
+      2026-11-13 瀹炴祴锛坰martor_sltd锛歱at_med_inhosp 50,175 琛屻�乸at_archive 249,045 琛屻��
+      MySQL 5.7.34銆乮nnodb_buffer_pool_size=128MB锛夛細
+
+        鍒楄〃  ORDER BY b.endtime DESC LIMIT 10  鈫�   25 ~ 35 ms
+        count 鏈潯璇彞                            鈫� 10467 ms锛堝喎锛�/ 350 ms锛堢儹锛�
+
+      鐢ㄦ埛鐪嬪埌鐨勩�屽叏鏃堕棿娈� 20s / 3 涓湀 8s銆嶅氨鏄畠锛氫竴娆$炕椤� = count + 鍒楄〃銆�
+      鍘熷洜鏈変笁鏉★紝鏀� SQL 瑙e喅涓嶄簡锛屽繀椤诲湪搴撲笂鍋氾細
+
+        1) b 渚у繀椤绘湁 (del_flag, orgid, inhospstate, endtime) 澶嶅悎绱㈠紩锛�
+           鍚﹀垯 MySQL 瑕佹寜 orgid 鍥炶〃閫愯杩囨护 del_flag / inhospstate锛�
+           鏈変簡瀹冿紝count(1) 鏄� Using index锛堜笉鍥炶〃锛夛紝鍒楄〃涔熻兘鎸夌储寮曢『搴忔彁鍓� LIMIT銆�
+        2) MySQL 5.7 瀵广�屼笉鍦ㄧ储寮曢噷鐨勭瓑鍊兼潯浠躲�嶆寜 10% 浼帮紙EXPLAIN 鐨� filtered=10.0锛夛紝
+           浼氭妸銆屾病鏈� inhospstate 鐨勭储寮曘�嶇畻寰楁洿渚垮疁锛屼粠鑰岄�夐敊銆�
+           sltd 涓婂疄娴嬶細寮鸿璧� idx_pmi_del_org_state_end = 349ms锛�
+           榛樿鎵ц璁″垝 = 2085ms銆傛墍浠ュ厜寤虹储寮曚笉澶燂紝杩樿娓呮帀绔炰簤绱㈠紩鎴栧己鍒剁储寮曘��
+        3) pat_archive 鏄� 60+ 鍒楃殑澶ц〃锛宑ount 姣忓懡涓竴琛岄兘瑕佹寜涓婚敭鍥炶〃涓�娆�
+           锛坰ltd 鏄� 38,881 娆¢殢鏈鸿锛夈�傜紦瀛樺懡涓� 0.35s銆佸喎缂撳瓨 10s锛�
+           宸窛鍏ㄥ湪纾佺洏闅忔満璇� 鈫� 瑙佺 5 姝� innodb_buffer_pool_size銆�
+
+      閰嶅鑴氭湰锛歴ql/optimize_pat_med_inhosp_list_count_20261113.sql
+
+      鈿狅笍 銆愪笉瑕佺粰 pat_med_inhosp 鍔� idx_pmi_patid (patid)銆�
+         瀹炴祴鍔犱笂鍚庝紭鍖栧櫒浼氭敼鎴愩�屽厛鎵� pat_archive 鍐嶅洖鏌� pat_med_inhosp銆嶏紝
+         鍒楄〃鏌ヨ 25 ms 鈫� 481 ms锛堟參 20 鍊嶏級锛孫RDER BY 涔熺敤涓嶄笂绱㈠紩銆�
+         鏈煡璇㈢殑椹卞姩琛ㄥ繀椤诲浐瀹氭槸 b銆�
+
+      鈿狅笍 銆愪笉瑕佷负浜嗗揩鎶� a 鐨� join 鍘绘帀銆�
+         a.id = b.patid 涓嶆槸鎭掔瓑鐨勶細pat_med_inhosp.patid 鍙兘涓虹┖鎴栧湪 pat_archive 閲�
+         鎵句笉鍒般��2026-11-13 瀹炴祴 smartor_shiyi 鏈� 5,713 琛� patid 涓虹┖銆�
+         smartor_lishui 鏈� 9,270 琛屽鍎胯 鈥斺�� 鍘绘帀 join 浼氭妸杩欎簺琛岀畻杩� total锛�
+         鍙細鍦ㄣ�屽鍎胯鎭板ソ涓� 0銆嶇殑搴撲笂鐪嬭捣鏉ユ纭��
+    -->
+    <select id="selectPatMedInhospList_COUNT" parameterType="com.ruoyi.inhospvisit.domain.PatMedInhosp"
+            resultType="java.lang.Long">
+        select count(1)
+        FROM
+        <include refid="selectPatMedInhospListFromWhere"/>
     </select>
 
     <select id="selectPatMedInhospListBySerialnum" parameterType="com.ruoyi.inhospvisit.domain.PatMedInhosp"
@@ -501,6 +571,31 @@
             and b.serialnum = #{serialnum}
         </if>
         order by b.inhospid desc
+    </select>
+
+    <!--
+      鍦ㄩ櫌淇℃伅鍒锋柊鐨勫彇鏁帮細鍙煡 pat_med_inhosp 鑷韩銆佸彧鍙栧埛鏂拌鐢ㄧ殑鍒椼��
+      鍒绘剰涓嶅鐢� selectPatMedInhospList 鈥斺�� 閭d釜鏌ヨ join 浜� pat_archive 涓旇姹�
+      a.idcardno IS NOT NULL锛屼細璁�"璇佷欢鍙疯繕娌¤ˉ涓婄殑鏂板叆闄㈡偅鑰�"闈欓粯婕忓埛鏂般��
+      WHERE (del_flag, orgid, inhospstate) 鍛戒腑 idx_inhosp_optimize 鐨勫墠缂�銆�
+    -->
+    <select id="selectInhospRefreshCandidates" resultMap="PatMedInhospResult">
+        select inhospid,
+               serialnum,
+               patname,
+               orgid,
+               campusid,
+               deptcode,
+               deptname,
+               hospitaldistrictcode,
+               hospitaldistrictname,
+               hospitaldistrictid,
+               bed_no
+        from pat_med_inhosp
+        where inhospstate = '0'
+          and del_flag = '0'
+          and orgid = #{orgid}
+        order by inhospid asc
     </select>
 
     <select id="selectPatMedInhospListByCondition" parameterType="com.ruoyi.inhospvisit.domain.PatMedInhospVO"
@@ -1157,7 +1252,7 @@
             AND visit_time &lt; DATE_ADD(#{endDate}, INTERVAL 1 DAY)
         </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>

--
Gitblit v1.9.3