salix/db/routines/vn/procedures/travel_moveRaids.sql

83 lines
2.1 KiB
MySQL
Raw Normal View History

DELIMITER $$
2024-08-20 08:06:10 +00:00
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`travel_moveRaids`()
BEGIN
/**
* Desplaza los travels en el futuro y avisa a los compradores
2024-11-21 14:09:58 +00:00
*
*/
DECLARE vDone BOOL DEFAULT FALSE;
DECLARE vBuyerEmail VARCHAR(40);
DECLARE vTravelLink TEXT;
DECLARE vMailBody TEXT;
DECLARE vIsMovible BOOL;
DECLARE vSubject VARCHAR(30);
DECLARE vTravels CURSOR FOR
SELECT GROUP_CONCAT(DISTINCT travelLink ORDER BY id SEPARATOR '\n\r'),
buyerEmail,
isMovable
FROM tTravelToMove
GROUP BY isMovable, buyerEmail;
2024-11-21 14:09:58 +00:00
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
RESIGNAL;
END;
CREATE OR REPLACE TEMPORARY TABLE tTravelToMove
WITH travels AS (
SELECT id ,
CONCAT('https://salix.verdnatura.es/#!/travel/', id,'/summary') travelLink,
util.VN_CURDATE() + INTERVAL daysInForward DAY newLanded,
shipped <= util.VN_CURDATE() + INTERVAL daysInForward DAY isMovable
FROM travel
2024-11-21 14:09:58 +00:00
WHERE isRaid
AND daysInForward
)SELECT t.*,
CONCAT(u.name, '@verdnatura.es') buyerEmail
FROM travels t
STRAIGHT_JOIN entry e ON e.travelFk = t.id
JOIN buy b ON b.entryFk = e.id
JOIN item i ON i.id = b.itemFk
JOIN itemType it ON it.id = i.typeFk
JOIN account.user u ON u.id = it.workerFk
GROUP BY t.id;
START TRANSACTION;
UPDATE travel tr
JOIN tTravelToMove ttm ON ttm.id = tr.id
SET tr.landed = ttm.newLanded
WHERE ttm.isMovable;
OPEN vTravels;
l: LOOP
SET vDone = FALSE;
FETCH vTravels INTO vTravelLink, vBuyerEmail, vIsMovible;
IF vDone THEN
LEAVE l;
END IF;
IF vIsMovible THEN
SET vSubject = 'Cambio de fecha en Redadas';
SET vMailBody = 'Se ha movido los siguientes travels';
ELSE
SET vSubject = 'ERROR al cambiar la fecha en Redadas';
SET vMailBody = 'Ha ocurrido un error con las fechas al mover los siguiente travels';
END IF;
SET vMailBody = CONCAT(vMailBody, ': \n\r ', vTravelLink);
CALL mail_insert(vBuyerEmail, 'noreply@verdnatura.es', vSubject, vMailBody);
END LOOP;
CLOSE vTravels;
COMMIT;
DROP TEMPORARY TABLE tTravelToMove;
END$$
DELIMITER ;