From 8145e088b1a6c1003ecad681da60118b9c01a1e1 Mon Sep 17 00:00:00 2001 From: "sas.fajri" Date: Wed, 6 May 2026 09:39:55 +0700 Subject: [PATCH] Update MCU patient sync SPs --- ...reate_sp_refresh_mcu_participant_daily.sql | 27 +++++++++ ...p_refresh_mcu_participant_daily_by_mcu.sql | 39 ++++++++++++ ...026-05-05_create_sp_upsert_mcu_patient.sql | 3 + ...026-05-06_alter_mcu_patient_add_orders.sql | 3 + ...ate_sp_upsert_mcu_patient_by_mgm_mcuid.sql | 60 +++++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily_by_mcu.sql create mode 100644 scripts/sql/2026-05-06_alter_mcu_patient_add_orders.sql create mode 100644 scripts/sql/2026-05-06_create_sp_upsert_mcu_patient_by_mgm_mcuid.sql diff --git a/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql b/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql index d075e50..73ca6cd 100644 --- a/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql +++ b/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily.sql @@ -20,6 +20,33 @@ BEGIN WHERE Mcu_ParticipantDailyMcuID = p_mgm_mcuid AND Mcu_ParticipantDailyDate = p_schedule_date; + DELETE s + FROM cpone_dashboard.mcu_patient_schedule s + INNER JOIN cpone.mcu_preregister_patients pp + ON pp.Mcu_PreregisterPatientsID = s.Mcu_PatientSchedulePreregisterID + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + AND s.Mcu_PatientScheduleDate = p_schedule_date; + + INSERT INTO cpone_dashboard.mcu_patient_schedule ( + Mcu_PatientSchedulePreregisterID, + Mcu_PatientScheduleDate, + Mcu_PatientScheduleIsActive, + Mcu_PatientScheduleSyncedAt + ) + SELECT + DISTINCT + pp.Mcu_PreregisterPatientsID, + p_schedule_date, + 'Y', + NOW() + FROM cpone.mcu_preregister_patients pp + INNER JOIN cpone.mcu_preregister_date pd + ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + AND pd.Mcu_PreregisterDateCheckinSchedule = p_schedule_date + AND pd.Mcu_PreregisterDateIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + AND pp.Mcu_PreregisterPatientsIsActive = 'Y'; + INSERT INTO cpone_dashboard.mcu_participant_daily ( Mcu_ParticipantDailyMcuID, Mcu_ParticipantDailyDate, diff --git a/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily_by_mcu.sql b/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily_by_mcu.sql new file mode 100644 index 0000000..452f000 --- /dev/null +++ b/scripts/sql/2026-05-05_create_sp_refresh_mcu_participant_daily_by_mcu.sql @@ -0,0 +1,39 @@ +DROP PROCEDURE IF EXISTS cpone_dashboard.sp_refresh_mcu_participant_daily_by_mcu; +DELIMITER $$ +CREATE PROCEDURE cpone_dashboard.sp_refresh_mcu_participant_daily_by_mcu(IN p_mgm_mcuid INT) +BEGIN + DECLARE v_done INT DEFAULT 0; + DECLARE v_schedule_date DATE; + DECLARE v_total_dates INT DEFAULT 0; + + DECLARE cur_dates CURSOR FOR + SELECT DISTINCT pd.Mcu_PreregisterDateCheckinSchedule + FROM cpone.mcu_preregister_patients pp + INNER JOIN cpone.mcu_preregister_date pd + ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + AND pd.Mcu_PreregisterDateIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + AND pp.Mcu_PreregisterPatientsIsActive = 'Y' + AND pd.Mcu_PreregisterDateCheckinSchedule IS NOT NULL + AND pd.Mcu_PreregisterDateCheckinSchedule <> '0000-00-00' + ORDER BY pd.Mcu_PreregisterDateCheckinSchedule; + + DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = 1; + + OPEN cur_dates; + + dates_loop: LOOP + FETCH cur_dates INTO v_schedule_date; + IF v_done = 1 THEN + LEAVE dates_loop; + END IF; + + CALL cpone.sp_refresh_mcu_participant_daily_by_mcu_date(p_mgm_mcuid, v_schedule_date); + SET v_total_dates = v_total_dates + 1; + END LOOP; + + CLOSE cur_dates; + + SELECT v_total_dates AS processed_dates; +END$$ +DELIMITER ; diff --git a/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql b/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql index f1c155a..2e39cdb 100644 --- a/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql +++ b/scripts/sql/2026-05-05_create_sp_upsert_mcu_patient.sql @@ -12,6 +12,7 @@ BEGIN Mcu_PatientDepartment, Mcu_PatientDivision, Mcu_PatientPosisi, + Mcu_PatientOrders, Mcu_PatientIsRegistered, Mcu_PatientOrderID, Mcu_PatientIsActive, @@ -27,6 +28,7 @@ BEGIN pp.Mcu_PreregisterPatientsDepartment, pp.Mcu_PreregisterPatientsDivisi, pp.Mcu_PreregisterPatientsPosisi, + pp.Mcu_PreregisterPatientsOrders, pp.Mcu_PreregisterPatientsIsRegistered, CASE WHEN pp.Mcu_PreregisterPatientsIsRegistered = 'Y' @@ -47,6 +49,7 @@ BEGIN Mcu_PatientDepartment = VALUES(Mcu_PatientDepartment), Mcu_PatientDivision = VALUES(Mcu_PatientDivision), Mcu_PatientPosisi = VALUES(Mcu_PatientPosisi), + Mcu_PatientOrders = VALUES(Mcu_PatientOrders), Mcu_PatientIsRegistered = VALUES(Mcu_PatientIsRegistered), Mcu_PatientOrderID = VALUES(Mcu_PatientOrderID), Mcu_PatientIsActive = VALUES(Mcu_PatientIsActive), diff --git a/scripts/sql/2026-05-06_alter_mcu_patient_add_orders.sql b/scripts/sql/2026-05-06_alter_mcu_patient_add_orders.sql new file mode 100644 index 0000000..174080f --- /dev/null +++ b/scripts/sql/2026-05-06_alter_mcu_patient_add_orders.sql @@ -0,0 +1,3 @@ +ALTER TABLE cpone_dashboard.mcu_patient + ADD COLUMN Mcu_PatientOrders LONGTEXT NULL + AFTER Mcu_PatientPosisi; diff --git a/scripts/sql/2026-05-06_create_sp_upsert_mcu_patient_by_mgm_mcuid.sql b/scripts/sql/2026-05-06_create_sp_upsert_mcu_patient_by_mgm_mcuid.sql new file mode 100644 index 0000000..9ec187a --- /dev/null +++ b/scripts/sql/2026-05-06_create_sp_upsert_mcu_patient_by_mgm_mcuid.sql @@ -0,0 +1,60 @@ +DROP PROCEDURE IF EXISTS cpone.sp_upsert_mcu_patient_by_mgm_mcuid; +DELIMITER $$ +CREATE PROCEDURE cpone.sp_upsert_mcu_patient_by_mgm_mcuid(IN p_mgm_mcuid INT) +BEGIN + INSERT INTO cpone_dashboard.mcu_patient ( + Mcu_PatientPreregisterID, + Mcu_PatientMcuID, + Mcu_PatientName, + Mcu_PatientNIP, + Mcu_PatientGender, + Mcu_PatientDOB, + Mcu_PatientDepartment, + Mcu_PatientDivision, + Mcu_PatientPosisi, + Mcu_PatientOrders, + Mcu_PatientIsRegistered, + Mcu_PatientOrderID, + Mcu_PatientIsActive, + Mcu_PatientSyncedAt + ) + SELECT + pp.Mcu_PreregisterPatientsID, + pp.Mcu_PreregisterPatientsMgm_McuID, + pp.Mcu_PreregisterPatientsPatientName, + pp.Mcu_PreregisterPatientsNIP, + pp.Mcu_PreregisterPatientsGender, + pp.Mcu_PreregisterPatientsDOB, + pp.Mcu_PreregisterPatientsDepartment, + pp.Mcu_PreregisterPatientsDivisi, + pp.Mcu_PreregisterPatientsPosisi, + pp.Mcu_PreregisterPatientsOrders, + pp.Mcu_PreregisterPatientsIsRegistered, + CASE + WHEN pp.Mcu_PreregisterPatientsIsRegistered = 'Y' + AND IFNULL(pp.Mcu_PreregisterPatientsT_OrderHeaderID, 0) > 0 + THEN pp.Mcu_PreregisterPatientsT_OrderHeaderID + ELSE NULL + END AS Mcu_PatientOrderID, + pp.Mcu_PreregisterPatientsIsActive AS Mcu_PatientIsActive, + NOW() AS Mcu_PatientSyncedAt + FROM cpone.mcu_preregister_patients pp + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + ON DUPLICATE KEY UPDATE + Mcu_PatientMcuID = VALUES(Mcu_PatientMcuID), + Mcu_PatientName = VALUES(Mcu_PatientName), + Mcu_PatientNIP = VALUES(Mcu_PatientNIP), + Mcu_PatientGender = VALUES(Mcu_PatientGender), + Mcu_PatientDOB = VALUES(Mcu_PatientDOB), + Mcu_PatientDepartment = VALUES(Mcu_PatientDepartment), + Mcu_PatientDivision = VALUES(Mcu_PatientDivision), + Mcu_PatientPosisi = VALUES(Mcu_PatientPosisi), + Mcu_PatientOrders = VALUES(Mcu_PatientOrders), + Mcu_PatientIsRegistered = VALUES(Mcu_PatientIsRegistered), + Mcu_PatientOrderID = VALUES(Mcu_PatientOrderID), + Mcu_PatientIsActive = VALUES(Mcu_PatientIsActive), + Mcu_PatientSyncedAt = NOW(); + + SELECT ROW_COUNT() AS affected_rows; +END$$ +DELIMITER ;