Add SP and sync endpoint for MCU participant daily by preregister

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
sas.fajri
2026-05-11 14:39:34 +07:00
parent f9d3e0674b
commit 2d9ff5b1b7
2 changed files with 2174 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
DROP PROCEDURE IF EXISTS cpone.sp_refresh_mcu_participant_daily_by_preregister;
DELIMITER $$
CREATE PROCEDURE cpone.sp_refresh_mcu_participant_daily_by_preregister(
IN p_preregister_id INT
)
main_block: BEGIN
DECLARE v_mgm_mcuid INT DEFAULT 0;
DECLARE v_schedule_date DATE DEFAULT NULL;
DECLARE v_participant_daily_id INT DEFAULT 0;
DECLARE v_mcu_patient_id INT DEFAULT 0;
DECLARE v_total_participant INT DEFAULT 0;
SELECT
pp.Mcu_PreregisterPatientsMgm_McuID,
pd.Mcu_PreregisterDateCheckinSchedule
INTO v_mgm_mcuid, v_schedule_date
FROM cpone.mcu_preregister_patients pp
JOIN cpone.mcu_preregister_date pd
ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID
AND pd.Mcu_PreregisterDateIsActive = 'Y'
WHERE pp.Mcu_PreregisterPatientsID = p_preregister_id
LIMIT 1;
IF v_mgm_mcuid = 0 OR v_schedule_date IS NULL THEN
SELECT 0 AS participant_daily_id, 0 AS total_participant, 0 AS total_details;
LEAVE main_block;
END IF;
START TRANSACTION;
INSERT INTO cpone_dashboard.mcu_patient_schedule (
Mcu_PatientSchedulePreregisterID,
Mcu_PatientScheduleDate,
Mcu_PatientScheduleIsActive,
Mcu_PatientScheduleSyncedAt
) VALUES (p_preregister_id, v_schedule_date, 'Y', NOW())
ON DUPLICATE KEY UPDATE
Mcu_PatientScheduleIsActive = 'Y',
Mcu_PatientScheduleSyncedAt = NOW();
SELECT COUNT(DISTINCT pp.Mcu_PreregisterPatientsID)
INTO v_total_participant
FROM cpone.mcu_preregister_patients pp
JOIN cpone.mcu_preregister_date pd
ON pd.Mcu_PreregisterDateMcu_PreregisterPatientsID = pp.Mcu_PreregisterPatientsID
AND pd.Mcu_PreregisterDateCheckinSchedule = v_schedule_date
AND pd.Mcu_PreregisterDateIsActive = 'Y'
WHERE pp.Mcu_PreregisterPatientsMgm_McuID = v_mgm_mcuid
AND pp.Mcu_PreregisterPatientsIsActive = 'Y';
INSERT INTO cpone_dashboard.mcu_participant_daily (
Mcu_ParticipantDailyMcuID,
Mcu_ParticipantDailyDate,
Mcu_ParticipantDailyTotal,
Mcu_ParticipantDailyIsActive
) VALUES (v_mgm_mcuid, v_schedule_date, v_total_participant, 'Y')
ON DUPLICATE KEY UPDATE
Mcu_ParticipantDailyID = LAST_INSERT_ID(Mcu_ParticipantDailyID),
Mcu_ParticipantDailyTotal = v_total_participant,
Mcu_ParticipantDailyIsActive = 'Y';
SET v_participant_daily_id = LAST_INSERT_ID();
SELECT Mcu_PatientID
INTO v_mcu_patient_id
FROM cpone_dashboard.mcu_patient
WHERE Mcu_PatientPreregisterID = p_preregister_id
AND Mcu_PatientMcuID = v_mgm_mcuid
LIMIT 1;
IF v_participant_daily_id > 0 AND v_mcu_patient_id > 0 THEN
INSERT INTO cpone_dashboard.mcu_participant_daily_details (
Mcu_ParticipantDailyDetailsParticipantDailyID,
Mcu_ParticipantDailyDetailsMcu_PatientID,
Mcu_ParticipantDailyDetailsIsActive
) VALUES (v_participant_daily_id, v_mcu_patient_id, 'Y')
ON DUPLICATE KEY UPDATE
Mcu_ParticipantDailyDetailsIsActive = 'Y';
END IF;
COMMIT;
SELECT
v_participant_daily_id AS participant_daily_id,
v_total_participant AS total_participant,
(
SELECT COUNT(*)
FROM cpone_dashboard.mcu_participant_daily_details d
WHERE d.Mcu_ParticipantDailyDetailsParticipantDailyID = v_participant_daily_id
) AS total_details;
END main_block$$
DELIMITER ;