2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
|
|
|
CREATE OR REPLACE DEFINER=`root`@`localhost` PROCEDURE `srt`.`moving_clean`()
|
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Elimina movimientos por inactividad
|
|
|
|
*/
|
|
|
|
DECLARE vExpeditionFk INT;
|
|
|
|
DECLARE vBufferToFk INT;
|
|
|
|
DECLARE vBufferFromFk INT;
|
2024-07-18 12:12:37 +00:00
|
|
|
DECLARE vStateOutFk INT
|
|
|
|
DEFAULT (SELECT id FROM expeditionState WHERE `description` = 'OUT');
|
2024-07-08 10:35:29 +00:00
|
|
|
DECLARE vDone BOOL DEFAULT FALSE;
|
|
|
|
DECLARE vSorter CURSOR FOR
|
2024-01-15 11:31:03 +00:00
|
|
|
SELECT m.expeditionFk, m.bufferToFk, m.bufferFromFk
|
2024-07-08 10:35:29 +00:00
|
|
|
FROM moving m
|
|
|
|
JOIN (
|
|
|
|
SELECT bufferFk, SUM(isActive) hasBox
|
|
|
|
FROM photocell
|
|
|
|
GROUP BY bufferFk
|
|
|
|
) sub ON sub.bufferFk = m.bufferFromFk
|
|
|
|
WHERE m.created < (util.VN_NOW() - INTERVAL (SELECT movingMaxLife FROM config) MINUTE)
|
2024-01-15 11:31:03 +00:00
|
|
|
AND NOT sub.hasBox;
|
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
DECLARE EXIT HANDLER FOR SQLEXCEPTION
|
|
|
|
BEGIN
|
|
|
|
ROLLBACK;
|
|
|
|
RESIGNAL;
|
|
|
|
END;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
OPEN vSorter;
|
|
|
|
l: LOOP
|
|
|
|
SET vDone = FALSE;
|
|
|
|
FETCH vSorter INTO vExpeditionFk, vBufferToFk, vBufferFromFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
IF vDone THEN
|
|
|
|
LEAVE l;
|
2024-01-15 11:31:03 +00:00
|
|
|
END IF;
|
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
START TRANSACTION;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
SELECT id
|
|
|
|
FROM moving
|
|
|
|
WHERE expeditionFk = vExpeditionFk
|
|
|
|
FOR UPDATE;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
DELETE FROM moving
|
|
|
|
WHERE expeditionFk = vExpeditionFk;
|
|
|
|
|
|
|
|
SELECT id
|
|
|
|
FROM expedition
|
|
|
|
WHERE id = vExpeditionFk
|
|
|
|
OR (bufferFk = vBufferFromFk AND `position` > 0)
|
|
|
|
FOR UPDATE;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-18 12:12:37 +00:00
|
|
|
UPDATE expedition
|
2024-07-08 10:35:29 +00:00
|
|
|
SET bufferFk = NULL,
|
|
|
|
`position` = NULL,
|
2024-07-18 12:12:37 +00:00
|
|
|
stateFk = vStateOutFk
|
2024-07-08 10:35:29 +00:00
|
|
|
WHERE id = vExpeditionFk;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
UPDATE expedition
|
|
|
|
SET `position` = `position` - 1
|
|
|
|
WHERE bufferFk = vBufferFromFk
|
|
|
|
AND `position` > 0;
|
2024-01-15 11:31:03 +00:00
|
|
|
|
2024-07-08 10:35:29 +00:00
|
|
|
COMMIT;
|
|
|
|
END LOOP l;
|
|
|
|
CLOSE vSorter;
|
2024-01-15 11:31:03 +00:00
|
|
|
END$$
|
|
|
|
DELIMITER ;
|