DROP TRIGGER IF EXISTS InsertAwbexportEncUrl;

DELIMITER $$

CREATE TRIGGER 
	InsertAwbexportEncUrl
AFTER INSERT
ON awbexport FOR EACH ROW
BEGIN
	INSERT INTO 
		encoded_url (
			awb, sid, scadenza, user_ins, data_ins, user_mod, data_mod, hash
		) VALUES (
			NEW.awb, NULL, DATE_ADD(NOW(), INTERVAL 2 DAY), 1, NOW(), 1, NOW(),
			SHA2(NEW.awb, 512)			
		)
  	ON DUPLICATE KEY UPDATE 
		hash = SHA2(NEW.awb, 512)
		, scadenza = DATE_ADD(NOW(), INTERVAL 2 DAY)
		, user_mod = 1
		, data_mod = NOW()
	;

END$$    

DELIMITER ;

