2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-07-22 09:52:42 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`item_deactivateUnused`()
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Cambia a false el campo isActive de la tabla vn.item para todos aquellos
|
|
|
|
* items que no se han comprado en los últimos meses configurados en
|
|
|
|
* @vn.itemConfig.monthToDeactivate
|
|
|
|
*/
|
|
|
|
DECLARE vMonthToDeactivate INT;
|
|
|
|
|
|
|
|
SELECT monthToDeactivate INTO vMonthToDeactivate
|
|
|
|
FROM vn.itemConfig;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.buyMaxCreated;
|
|
|
|
CREATE TEMPORARY TABLE tmp.buyMaxCreated
|
|
|
|
ENGINE = MEMORY
|
|
|
|
SELECT b.itemFk,
|
|
|
|
MAX(b.created) created
|
|
|
|
FROM vn.buy b
|
|
|
|
JOIN vn.item i ON i.id = b.itemFk
|
|
|
|
WHERE i.isActive
|
|
|
|
GROUP BY itemFk;
|
|
|
|
|
|
|
|
UPDATE vn.item i
|
|
|
|
JOIN tmp.buyMaxCreated bmc ON bmc.itemFk = i.id
|
|
|
|
SET i.isActive = FALSE
|
|
|
|
WHERE bmc.created < util.VN_CURDATE() - INTERVAL vMonthToDeactivate MONTH;
|
|
|
|
|
|
|
|
DROP TEMPORARY TABLE IF EXISTS tmp.buyMaxCreated;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|