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 2e39cdb..9900627 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 @@ -55,6 +55,57 @@ BEGIN Mcu_PatientIsActive = VALUES(Mcu_PatientIsActive), Mcu_PatientSyncedAt = NOW(); + DELETE FROM cpone_dashboard.mcu_patient_packet + WHERE Mcu_PatientPacketPreregisterID = p_preregister_id; + + INSERT INTO cpone_dashboard.mcu_patient_packet ( + Mcu_PatientPacketMcu_PatientID, + Mcu_PatientPacketPreregisterID, + Mcu_PatientPacketCode, + Mcu_PatientPacketName + ) + SELECT DISTINCT + mp.Mcu_PatientID, + pp.Mcu_PreregisterPatientsID, + seqs.order_code AS Mcu_PatientPacketCode, + tp.T_PacketName AS Mcu_PatientPacketName + FROM cpone.mcu_preregister_patients pp + INNER JOIN cpone_dashboard.mcu_patient mp + ON mp.Mcu_PatientPreregisterID = pp.Mcu_PreregisterPatientsID + INNER JOIN ( + SELECT + src.Mcu_PreregisterPatientsID, + TRIM( + SUBSTRING_INDEX( + SUBSTRING_INDEX(src.clean_orders, ',', nums.seq), + ',', + -1 + ) + ) AS order_code + FROM ( + SELECT + p.Mcu_PreregisterPatientsID, + REPLACE(IFNULL(p.Mcu_PreregisterPatientsOrders, ''), ' ', '') AS clean_orders + FROM cpone.mcu_preregister_patients p + WHERE p.Mcu_PreregisterPatientsID = p_preregister_id + ) src + INNER JOIN ( + SELECT ones.n + tens.n * 10 + 1 AS seq + FROM + (SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) ones + CROSS JOIN + (SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) tens + ) nums + ON nums.seq <= 1 + LENGTH(src.clean_orders) - LENGTH(REPLACE(src.clean_orders, ',', '')) + ) seqs + ON seqs.Mcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + INNER JOIN cpone.t_packet tp + ON tp.T_PacketSasCode = seqs.order_code + AND tp.T_PacketIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsID = p_preregister_id + AND LEFT(seqs.order_code, 2) IN ('PN', 'PR') + AND seqs.order_code <> ''; + SELECT ROW_COUNT() AS affected_rows; END$$ DELIMITER ; diff --git a/scripts/sql/2026-05-06_create_mcu_patient_packet_and_add_indexes.sql b/scripts/sql/2026-05-06_create_mcu_patient_packet_and_add_indexes.sql new file mode 100644 index 0000000..db02c34 --- /dev/null +++ b/scripts/sql/2026-05-06_create_mcu_patient_packet_and_add_indexes.sql @@ -0,0 +1,137 @@ +CREATE TABLE IF NOT EXISTS cpone_dashboard.mcu_patient_packet ( + Mcu_PatientPacketID INT AUTO_INCREMENT PRIMARY KEY, + Mcu_PatientPacketMcu_PatientID INT NOT NULL, + Mcu_PatientPacketPreregisterID INT NOT NULL, + Mcu_PatientPacketCode VARCHAR(50) NOT NULL, + Mcu_PatientPacketName VARCHAR(255) NOT NULL, + Mcu_PatientPacketCreatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + Mcu_PatientPacketUpdatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + UNIQUE KEY uq_mcu_patient_packet_unique ( + Mcu_PatientPacketMcu_PatientID, + Mcu_PatientPacketPreregisterID, + Mcu_PatientPacketCode + ), + KEY idx_mcu_patient_packet_patient_id (Mcu_PatientPacketMcu_PatientID), + KEY idx_mcu_patient_packet_preregister_id (Mcu_PatientPacketPreregisterID), + KEY idx_mcu_patient_packet_code (Mcu_PatientPacketCode), + KEY idx_mcu_patient_packet_name (Mcu_PatientPacketName) +); + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientDepartment' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientDepartment` (`Mcu_PatientDepartment`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientGender' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientGender` (`Mcu_PatientGender`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientNIP' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientNIP` (`Mcu_PatientNIP`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientAge' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientAge` (`Mcu_PatientAge`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientDivision' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientDivision` (`Mcu_PatientDivision`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientPosisi' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientPosisi` (`Mcu_PatientPosisi`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; + +SET @sql = ( + SELECT IF( + EXISTS ( + SELECT 1 + FROM information_schema.statistics + WHERE table_schema = 'cpone_dashboard' + AND table_name = 'mcu_patient' + AND index_name = 'Mcu_PatientIsRegistered' + ), + 'SELECT 1', + 'ALTER TABLE cpone_dashboard.mcu_patient ADD INDEX `Mcu_PatientIsRegistered` (`Mcu_PatientIsRegistered`)' + ) +); +PREPARE stmt FROM @sql; +EXECUTE stmt; +DEALLOCATE PREPARE stmt; 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 index f897275..5c222fe 100644 --- 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 @@ -98,6 +98,61 @@ BEGIN Mcu_PatientIsActive = VALUES(Mcu_PatientIsActive), Mcu_PatientSyncedAt = NOW(); + DELETE ppk + FROM cpone_dashboard.mcu_patient_packet ppk + INNER JOIN cpone_dashboard.mcu_patient mp + ON mp.Mcu_PatientID = ppk.Mcu_PatientPacketMcu_PatientID + WHERE mp.Mcu_PatientMcuID = p_mgm_mcuid; + + INSERT INTO cpone_dashboard.mcu_patient_packet ( + Mcu_PatientPacketMcu_PatientID, + Mcu_PatientPacketPreregisterID, + Mcu_PatientPacketCode, + Mcu_PatientPacketName + ) + SELECT DISTINCT + mp.Mcu_PatientID, + pp.Mcu_PreregisterPatientsID, + seqs.order_code AS Mcu_PatientPacketCode, + tp.T_PacketName AS Mcu_PatientPacketName + FROM cpone.mcu_preregister_patients pp + INNER JOIN cpone_dashboard.mcu_patient mp + ON mp.Mcu_PatientPreregisterID = pp.Mcu_PreregisterPatientsID + AND mp.Mcu_PatientMcuID = pp.Mcu_PreregisterPatientsMgm_McuID + INNER JOIN ( + SELECT + src.Mcu_PreregisterPatientsID, + TRIM( + SUBSTRING_INDEX( + SUBSTRING_INDEX(src.clean_orders, ',', nums.seq), + ',', + -1 + ) + ) AS order_code + FROM ( + SELECT + p.Mcu_PreregisterPatientsID, + REPLACE(IFNULL(p.Mcu_PreregisterPatientsOrders, ''), ' ', '') AS clean_orders + FROM cpone.mcu_preregister_patients p + WHERE p.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + ) src + INNER JOIN ( + SELECT ones.n + tens.n * 10 + 1 AS seq + FROM + (SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) ones + CROSS JOIN + (SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) tens + ) nums + ON nums.seq <= 1 + LENGTH(src.clean_orders) - LENGTH(REPLACE(src.clean_orders, ',', '')) + ) seqs + ON seqs.Mcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID + INNER JOIN cpone.t_packet tp + ON tp.T_PacketSasCode = seqs.order_code + AND tp.T_PacketIsActive = 'Y' + WHERE pp.Mcu_PreregisterPatientsMgm_McuID = p_mgm_mcuid + AND LEFT(seqs.order_code, 2) IN ('PN', 'PR') + AND seqs.order_code <> ''; + SELECT ROW_COUNT() AS affected_rows; END$$ DELIMITER ;