SET NAMES UTF8;

-- ----------------------------
-- Function structure for getRootChildId
-- ----------------------------
DROP FUNCTION IF EXISTS `getRootChildId`;
CREATE FUNCTION `getRootChildId`(rootId bigint) RETURNS varchar(1000) CHARSET utf8
    DETERMINISTIC
BEGIN
    DECLARE ptemp VARCHAR(1000);
    DECLARE ctemp VARCHAR(1000);
    SET ptemp = '#';
    SET ctemp = cast(rootId as CHAR);
    WHILE ctemp IS NOT NULL DO
      SET ptemp = concat(ptemp, ',', ctemp);
SELECT group_concat(id) INTO ctemp FROM sys_org
WHERE FIND_IN_SET(parent_id, ctemp) > 0;
END WHILE;
RETURN ptemp;
END