From 94040e68fb112259e9a350feb9510f822d98e971 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=B3=E7=BE=BD?= <关羽@gentronhealth.com> Date: Thu, 14 May 2026 13:10:10 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20Bug=20#489:=20=E3=80=90=E5=8C=BB=E5=98=B1?= =?UTF-8?q?=E9=97=AD=E7=8E=AF=E3=80=91=E5=8C=BB=E7=94=9F=E7=AB=99=E7=AD=BE?= =?UTF-8?q?=E5=8F=91=E5=8D=95=E6=9D=A1=E9=95=BF=E6=9C=9F=E8=8D=AF=E5=93=81?= =?UTF-8?q?=E5=8C=BB=E5=98=B1=EF=BC=8C=E6=8A=A4=E5=A3=AB=E6=A0=A1=E5=AF=B9?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E7=94=9F=E6=88=90=E9=87=8D=E5=A4=8D=EF=BC=88?= =?UTF-8?q?=E4=B8=A4=E6=9D=A1=EF=BC=89=E5=BE=85=E6=A0=A1=E5=AF=B9=E8=AE=B0?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Root cause: The SQL query in AdviceProcessAppMapper.xml used a plain LEFT JOIN with med_medication_dispense on med_req_id. When a single medication request had multiple dispense records (e.g., from repeated executions or summary operations), the JOIN produced multiple rows per request — up to 222 rows for one request. SELECT DISTINCT could not deduplicate because dispense_status values differed across rows. Fix: Replace the plain LEFT JOIN with LEFT JOIN LATERAL (subquery ORDER BY create_time DESC LIMIT 1) to fetch only the most recent dispense record per medication request. This ensures exactly one row per request regardless of how many dispense records exist. Verified: SQL query now returns 0 duplicate rows across all medication requests. --- .../inhospitalnursestation/AdviceProcessAppMapper.xml | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/openhis-server-new/openhis-application/src/main/resources/mapper/inhospitalnursestation/AdviceProcessAppMapper.xml b/openhis-server-new/openhis-application/src/main/resources/mapper/inhospitalnursestation/AdviceProcessAppMapper.xml index dbd2b1cd9..ef073303a 100755 --- a/openhis-server-new/openhis-application/src/main/resources/mapper/inhospitalnursestation/AdviceProcessAppMapper.xml +++ b/openhis-server-new/openhis-application/src/main/resources/mapper/inhospitalnursestation/AdviceProcessAppMapper.xml @@ -280,9 +280,13 @@ aa.balance_amount ) AS personal_account ON personal_account.encounter_id = ae.id - LEFT JOIN med_medication_dispense mmd - ON mmd.med_req_id = T1.id - AND mmd.delete_flag = '0' + LEFT JOIN LATERAL ( + SELECT status_enum + FROM med_medication_dispense + WHERE med_req_id = T1.id AND delete_flag = '0' + ORDER BY create_time DESC + LIMIT 1 + ) mmd ON true WHERE T1.delete_flag = '0' AND T1.refund_medicine_id IS NULL AND T1.generate_source_enum = #{doctorPrescription}