Fix Bug #489: 【医嘱闭环】医生站签发单条长期药品医嘱,护士校对界面生成重复(两条)待校对记录
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.
This commit is contained in:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user