-- ============================================================================
-- Journal d'audit : déclencheurs critiques (fichier GÉNÉRÉ, ne pas modifier à la main)
-- Objet : Table n_resultat_examen
-- Niveau : T
-- Générateur : App\Support\Audit\InstallateurAudit, version 1.0
-- Empreinte de config/audit.php : dfdfb3756374
-- Généré le : 2026-10-16 00:21:05
-- Régénération : php artisan audit:exporter-declencheurs
-- Ré-exécutable (DROP TRIGGER IF EXISTS avant chaque CREATE) ; MySQL 8 et MariaDB >= 10.3.
-- ============================================================================
SET NAMES utf8mb4;
DELIMITER $$

-- Déclencheurs de n_resultat_examen
DROP TRIGGER IF EXISTS `trg_aud_bi_n_resultat_examen`$$
CREATE TRIGGER `trg_aud_bi_n_resultat_examen` BEFORE INSERT ON `n_resultat_examen` FOR EACH ROW
BEGIN
SET NEW.`created_by` = COALESCE(NEW.`created_by`, @audit_user_id);
SET NEW.`created_at` = COALESCE(NEW.`created_at`, CURRENT_TIMESTAMP);
END$$
DROP TRIGGER IF EXISTS `trg_aud_bu_n_resultat_examen`$$
CREATE TRIGGER `trg_aud_bu_n_resultat_examen` BEFORE UPDATE ON `n_resultat_examen` FOR EACH ROW
BEGIN
SET NEW.`created_by` = IF(OLD.`created_by` IS NULL, NEW.`created_by`, OLD.`created_by`);
SET NEW.`created_at` = IF(OLD.`created_at` IS NULL, NEW.`created_at`, OLD.`created_at`);
END$$
DROP TRIGGER IF EXISTS `trg_aud_ai_n_resultat_examen`$$
CREATE TRIGGER `trg_aud_ai_n_resultat_examen` AFTER INSERT ON `n_resultat_examen` FOR EACH ROW
BEGIN
INSERT INTO `n_journal_audit` (aud_table, aud_ligne_id, aud_action, aud_valeurs_avant, aud_valeurs_apres, aud_user_id, aud_ip, aud_user_agent, aud_request_id, aud_source, aud_contexte, aud_niveau) VALUES ('n_resultat_examen', CAST(COALESCE(NEW.`not_id`, 0) AS UNSIGNED), 'creation', NULL, JSON_OBJECT('not_id', NEW.`not_id`, 'not_ins_id', NEW.`not_ins_id`, 'not_exam_id', NEW.`not_exam_id`, 'not_id_origine', NEW.`not_id_origine`, 'not_statut', NEW.`not_statut`, 'not_est_calcule', NEW.`not_est_calcule`, 'not_statut_id', NEW.`not_statut_id`, 'not_note', NEW.`not_note`, 'not_est_absent', NEW.`not_est_absent`, 'not_modif_par', NEW.`not_modif_par`, 'not_commentaire', NEW.`not_commentaire`, 'created_at', NEW.`created_at`, 'updated_at', NEW.`updated_at`, 'deleted_at', NEW.`deleted_at`, 'created_by', NEW.`created_by`, 'updated_by', NEW.`updated_by`, 'not_active_ins_key', NEW.`not_active_ins_key`, 'not_active_exam_key', NEW.`not_active_exam_key`), @audit_user_id, @audit_ip, @audit_ua, @audit_request_id, COALESCE(@audit_source, 'cli'), @audit_context, 'trigger');
END$$
DROP TRIGGER IF EXISTS `trg_aud_ad_n_resultat_examen`$$
CREATE TRIGGER `trg_aud_ad_n_resultat_examen` AFTER DELETE ON `n_resultat_examen` FOR EACH ROW
BEGIN
INSERT INTO `n_journal_audit` (aud_table, aud_ligne_id, aud_action, aud_valeurs_avant, aud_valeurs_apres, aud_user_id, aud_ip, aud_user_agent, aud_request_id, aud_source, aud_contexte, aud_niveau) VALUES ('n_resultat_examen', CAST(COALESCE(OLD.`not_id`, 0) AS UNSIGNED), 'suppression', JSON_OBJECT('not_id', OLD.`not_id`, 'not_ins_id', OLD.`not_ins_id`, 'not_exam_id', OLD.`not_exam_id`, 'not_id_origine', OLD.`not_id_origine`, 'not_statut', OLD.`not_statut`, 'not_est_calcule', OLD.`not_est_calcule`, 'not_statut_id', OLD.`not_statut_id`, 'not_note', OLD.`not_note`, 'not_est_absent', OLD.`not_est_absent`, 'not_modif_par', OLD.`not_modif_par`, 'not_commentaire', OLD.`not_commentaire`, 'created_at', OLD.`created_at`, 'updated_at', OLD.`updated_at`, 'deleted_at', OLD.`deleted_at`, 'created_by', OLD.`created_by`, 'updated_by', OLD.`updated_by`, 'not_active_ins_key', OLD.`not_active_ins_key`, 'not_active_exam_key', OLD.`not_active_exam_key`), NULL, @audit_user_id, @audit_ip, @audit_ua, @audit_request_id, COALESCE(@audit_source, 'cli'), @audit_context, 'trigger');
END$$
DROP TRIGGER IF EXISTS `trg_aud_au_n_resultat_examen`$$
CREATE TRIGGER `trg_aud_au_n_resultat_examen` AFTER UPDATE ON `n_resultat_examen` FOR EACH ROW
BEGIN
DECLARE v_avant JSON DEFAULT JSON_OBJECT();
DECLARE v_apres JSON DEFAULT JSON_OBJECT();
IF NOT (OLD.`not_id` <=> NEW.`not_id`) THEN SET v_avant = JSON_SET(v_avant, '$."not_id"', OLD.`not_id`); SET v_apres = JSON_SET(v_apres, '$."not_id"', NEW.`not_id`); END IF;
IF NOT (OLD.`not_ins_id` <=> NEW.`not_ins_id`) THEN SET v_avant = JSON_SET(v_avant, '$."not_ins_id"', OLD.`not_ins_id`); SET v_apres = JSON_SET(v_apres, '$."not_ins_id"', NEW.`not_ins_id`); END IF;
IF NOT (OLD.`not_exam_id` <=> NEW.`not_exam_id`) THEN SET v_avant = JSON_SET(v_avant, '$."not_exam_id"', OLD.`not_exam_id`); SET v_apres = JSON_SET(v_apres, '$."not_exam_id"', NEW.`not_exam_id`); END IF;
IF NOT (OLD.`not_id_origine` <=> NEW.`not_id_origine`) THEN SET v_avant = JSON_SET(v_avant, '$."not_id_origine"', OLD.`not_id_origine`); SET v_apres = JSON_SET(v_apres, '$."not_id_origine"', NEW.`not_id_origine`); END IF;
IF NOT (OLD.`not_statut` <=> NEW.`not_statut`) THEN SET v_avant = JSON_SET(v_avant, '$."not_statut"', OLD.`not_statut`); SET v_apres = JSON_SET(v_apres, '$."not_statut"', NEW.`not_statut`); END IF;
IF NOT (OLD.`not_est_calcule` <=> NEW.`not_est_calcule`) THEN SET v_avant = JSON_SET(v_avant, '$."not_est_calcule"', OLD.`not_est_calcule`); SET v_apres = JSON_SET(v_apres, '$."not_est_calcule"', NEW.`not_est_calcule`); END IF;
IF NOT (OLD.`not_statut_id` <=> NEW.`not_statut_id`) THEN SET v_avant = JSON_SET(v_avant, '$."not_statut_id"', OLD.`not_statut_id`); SET v_apres = JSON_SET(v_apres, '$."not_statut_id"', NEW.`not_statut_id`); END IF;
IF NOT (OLD.`not_note` <=> NEW.`not_note`) THEN SET v_avant = JSON_SET(v_avant, '$."not_note"', OLD.`not_note`); SET v_apres = JSON_SET(v_apres, '$."not_note"', NEW.`not_note`); END IF;
IF NOT (OLD.`not_est_absent` <=> NEW.`not_est_absent`) THEN SET v_avant = JSON_SET(v_avant, '$."not_est_absent"', OLD.`not_est_absent`); SET v_apres = JSON_SET(v_apres, '$."not_est_absent"', NEW.`not_est_absent`); END IF;
IF NOT (OLD.`not_modif_par` <=> NEW.`not_modif_par`) THEN SET v_avant = JSON_SET(v_avant, '$."not_modif_par"', OLD.`not_modif_par`); SET v_apres = JSON_SET(v_apres, '$."not_modif_par"', NEW.`not_modif_par`); END IF;
IF NOT (OLD.`not_commentaire` <=> NEW.`not_commentaire`) THEN SET v_avant = JSON_SET(v_avant, '$."not_commentaire"', OLD.`not_commentaire`); SET v_apres = JSON_SET(v_apres, '$."not_commentaire"', NEW.`not_commentaire`); END IF;
IF NOT (OLD.`deleted_at` <=> NEW.`deleted_at`) THEN SET v_avant = JSON_SET(v_avant, '$."deleted_at"', OLD.`deleted_at`); SET v_apres = JSON_SET(v_apres, '$."deleted_at"', NEW.`deleted_at`); END IF;
IF JSON_LENGTH(v_apres) > 0 THEN
INSERT INTO `n_journal_audit` (aud_table, aud_ligne_id, aud_action, aud_valeurs_avant, aud_valeurs_apres, aud_user_id, aud_ip, aud_user_agent, aud_request_id, aud_source, aud_contexte, aud_niveau) VALUES ('n_resultat_examen', CAST(COALESCE(NEW.`not_id`, 0) AS UNSIGNED), CASE WHEN OLD.`deleted_at` IS NULL AND NEW.`deleted_at` IS NOT NULL THEN 'suppression' WHEN OLD.`deleted_at` IS NOT NULL AND NEW.`deleted_at` IS NULL THEN 'restauration' ELSE 'modification' END, v_avant, v_apres, @audit_user_id, @audit_ip, @audit_ua, @audit_request_id, COALESCE(@audit_source, 'cli'), @audit_context, 'trigger');
END IF;
END$$

DELIMITER ;
