32 lines
900 B
SQL
32 lines
900 B
SQL
DELIMITER $$
|
|
CREATE OR REPLACE DEFINER=`vn-admin`@`localhost` PROCEDURE `vn`.`item_deactivateUnused`()
|
|
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 ;
|