2024-01-15 11:31:03 +00:00
|
|
|
DELIMITER $$
|
2024-09-18 11:43:00 +00:00
|
|
|
CREATE OR REPLACE DEFINER=`vn`@`localhost` PROCEDURE `vn`.`worker_getHierarchy`(
|
|
|
|
vUserFk INT
|
|
|
|
)
|
2024-01-15 11:31:03 +00:00
|
|
|
BEGIN
|
|
|
|
/**
|
|
|
|
* Retorna una tabla temporal con los trabajadores que tiene
|
|
|
|
* el usuario por debajo en la jerarquía de la empresa.
|
|
|
|
*
|
|
|
|
* @param vUserFk Id de usuario.
|
|
|
|
* @return tmp.workerHierarchyList
|
|
|
|
*/
|
|
|
|
CREATE OR REPLACE TEMPORARY TABLE tmp.workerHierarchyList
|
|
|
|
(PRIMARY KEY (workerFk))
|
|
|
|
ENGINE = MEMORY
|
|
|
|
WITH RECURSIVE workerHierarchy AS (
|
2024-09-18 11:43:00 +00:00
|
|
|
SELECT id workerFk, bossFk, 0 `depth`, CAST(id AS CHAR(255)) `path`
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM vn.worker
|
|
|
|
WHERE id = vUserFk
|
|
|
|
UNION ALL
|
2024-09-18 11:43:00 +00:00
|
|
|
SELECT w.id, w.bossFk, wh.`depth` + 1, CONCAT(wh.`path`, ',', w.id)
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM vn.worker w
|
|
|
|
JOIN workerHierarchy wh ON w.bossFk = wh.workerFk
|
2024-09-18 11:43:00 +00:00
|
|
|
WHERE NOT FIND_IN_SET(w.id, wh.`path`)
|
2024-01-15 11:31:03 +00:00
|
|
|
)
|
2024-09-18 11:43:00 +00:00
|
|
|
SELECT *
|
2024-01-15 11:31:03 +00:00
|
|
|
FROM workerHierarchy
|
|
|
|
ORDER BY depth, workerFk;
|
|
|
|
END$$
|
|
|
|
DELIMITER ;
|