 
USE demo;

DROP PROCEDURE IF EXISTS demo.p_DOG_get_many_dog_command;

DELIMITER //
CREATE PROCEDURE demo.p_DOG_get_many_dog_command (
	  IN a_id_user INT
    , IN a_get_all_dog BIT
	, IN a_get_inactive_dog BIT
	, IN a_ids_dog TEXT
    , IN a_get_all_command BIT
	, IN a_get_inactive_command BIT
	, IN a_ids_command TEXT
    , IN a_get_all_understanding_level BIT
	, IN a_get_inactive_understanding_level BIT
	, IN a_ids_understanding_level TEXT
    , IN a_get_all_obedience_level BIT
	, IN a_get_inactive_obedience_level BIT
	, IN a_ids_obedience_level TEXT
    , IN a_debug BIT
)
BEGIN
    DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_code_type_error_no_permission VARCHAR(100);
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_id_type_error_no_permission INT;
    DECLARE v_has_filter_command BIT;
    DECLARE v_has_filter_dog BIT;
    DECLARE v_has_filter_obedience_level BIT;
    DECLARE v_has_filter_understanding_level BIT;
    DECLARE v_guid BINARY(36);
    DECLARE v_id_permission_dog_view INT;
    DECLARE v_id_minimum INT;
    DECLARE v_time_start TIMESTAMP(6);
    DECLARE v_can_view BIT;
    
    DECLARE exit handler for SQLEXCEPTION
    BEGIN
        GET DIAGNOSTICS CONDITION 1
            @sqlstate = RETURNED_SQLSTATE
            , @errno = MYSQL_ERRNO
            , @text = MESSAGE_TEXT
		;
        
        ROLLBACK;
        
		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error (
			  id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
			, id_type INT NULL
			, code VARCHAR(100) NOT NULL
			, msg VARCHAR(4000) NOT NULL
		);

        INSERT INTO tmp_Msg_Error (
			  id_type
            , code
            , msg
		)
        SELECT 
			  MET.id_type
            , @errno
            , @text
		FROM demo.CORE_Msg_Error_Type MET
        WHERE MET.code = 'MYSQL_ERROR'
		;
        
        SELECT 
            t_ERROR.id_error
            , t_ERROR.id_type
            , t_ERROR.code
            , ERROR_TYPE.name
            , ERROR_TYPE.description
            , ERROR_TYPE.is_breaking_error
            , ERROR_TYPE.background_colour
            , ERROR_TYPE.text_colour
            , t_ERROR.msg
        FROM tmp_Msg_Error t_ERROR
        INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;
        
		DROP TABLE IF EXISTS tmp_Msg_Error;
    END;

    SET v_time_start := CURRENT_TIMESTAMP(6);
    SET v_guid := UUID();
    SET v_code_type_error_bad_data := 'BAD_DATA';
    SET v_code_type_error_no_permission := 'NO_PERMISSION';
    SET v_id_type_error_bad_data := (SELECT ERROR_TYPE.id_type FROM demo.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_bad_data LIMIT 1);
    SET v_id_type_error_no_permission := (SELECT ERROR_TYPE.id_type FROM demo.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_no_permission LIMIT 1);
    SET v_id_permission_dog_view := (SELECT PERMISSION.id_permission FROM demo.DOG_Permission PERMISSION WHERE PERMISSION.code = 'DOG_VIEW' LIMIT 1);
    
    SET a_id_user := IFNULL(a_id_user, 0);
    SET a_get_all_dog := IFNULL(a_get_all_dog, 0);
    SET a_get_inactive_dog := IFNULL(a_get_inactive_dog, 0);
    SET a_ids_dog := TRIM(IFNULL(a_ids_dog, ''));
    SET a_get_all_command := IFNULL(a_get_all_command, 0);
    SET a_get_inactive_command := IFNULL(a_get_inactive_command, 0);
    SET a_ids_command := TRIM(IFNULL(a_ids_command, ''));
    SET a_get_all_understanding_level := IFNULL(a_get_all_understanding_level, 0);
    SET a_get_inactive_understanding_level := IFNULL(a_get_inactive_understanding_level, 0);
    SET a_ids_understanding_level := TRIM(IFNULL(a_ids_understanding_level, ''));
    SET a_get_all_obedience_level := IFNULL(a_get_all_obedience_level, 0);
    SET a_get_inactive_obedience_level := IFNULL(a_get_inactive_obedience_level, 0);
    SET a_ids_obedience_level := TRIM(IFNULL(a_ids_obedience_level, ''));
    SET a_debug := IFNULL(a_debug, 0);
    
    IF a_debug = 1 THEN
		SELECT 
			  a_id_user
            , a_get_all_dog
            , a_get_inactive_dog
            , a_ids_dog
            , a_get_all_command
            , a_get_inactive_command
            , a_ids_command
            , a_get_all_understanding_level
            , a_get_inactive_understanding_level
            , a_ids_understanding_level
            , a_get_all_obedience_level
            , a_get_inactive_obedience_level
            , a_ids_obedience_level
            , a_debug
		;

        SELECT
              v_id_type_error_bad_data
            , v_id_type_error_no_permission
            , v_guid
            , v_id_permission_dog_view
            , v_time_start
        ;
    END IF;

    DROP TEMPORARY TABLE IF EXISTS tmp_Split;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Understanding_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Dog;
    
    CREATE TEMPORARY TABLE tmp_Dog (
		  id_dog INT NOT NULL
        , name VARCHAR(250)
    );
    
    CREATE TEMPORARY TABLE tmp_Command (
		  id_command INT NOT NULL
        , id_command_category INT NOT NULL
        , name_command_category VARCHAR(250)
        , has_button
    --  , code VARCHAR(100)
        , name VARCHAR(250)
    );
    
    CREATE TEMPORARY TABLE tmp_Understanding_Level (
		  id_understanding_level INT NOT NULL
        , code VARCHAR(100)
        , name VARCHAR(250)
    );
    
    CREATE TEMPORARY TABLE tmp_Obedience_Level (
		  id_obedience_level INT NOT NULL
        , code VARCHAR(100)
        , name VARCHAR(250)
    );
    
	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Msg_Error (
          id_error INT NOT NULL PRIMARY KEY AUTO_INCREMENT
		, id_type INT NULL
		, code VARCHAR(100) NOT NULL
        , msg VARCHAR(4000) NOT NULL
	);
    
    CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Split (
		  substring VARCHAR(4000) NOT NULL
        , as_int INT NULL
	);
    DELETE FROM tmp_Split;
    
    CALL demo.p_core_validate_guid ( v_guid );
    
    SET v_has_filter_dog = CASE WHEN a_ids_dog = '' THEN 0 ELSE 1 END;
    SET v_has_filter_command = CASE WHEN a_ids_command = '' THEN 0 ELSE 1 END;
    SET v_has_filter_understanding_level = CASE WHEN a_ids_understanding_level = '' THEN 0 ELSE 1 END;
    SET v_has_filter_obedience_level = CASE WHEN a_ids_obedience_level = '' THEN 0 ELSE 1 END;

    -- Dogs
    IF v_has_filter_dog = 1 THEN
		CALL demo.p_split(v_guid, a_ids_dog, ',', a_debug);
		
		INSERT INTO tmp_Split (
			  substring
			, as_int
		)
		SELECT 
			  SPLIT_T.substring
			, CONVERT(SPLIT_T.substring, DECIMAL(10,0)) AS as_int
		FROM demo.CORE_Split_Temp SPLIT_T
		WHERE 
                SPLIT_T.GUID = v_guid
			AND IFNULL(SPLIT_T.substring, '') <> ''
		;
		
		CALL demo.p_clear_split_temp( v_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		IF EXISTS (
			SELECT * 
            FROM tmp_Split t_SPLIT 
            LEFT JOIN demo.DOG_Dog DOG ON t_SPLIT.as_int = DOG.id_dog 
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
                OR  ISNULL(DOG.id_dog)
				OR  (
					DOG.active = 0
					AND a_get_inactive_dog = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Dog IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split t_SPLIT
			LEFT JOIN demo.DOG_Dog DOG ON t_SPLIT.as_int = DOG.id_dog
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
				OR  ISNULL(DOG.id_dog)
				OR  (
					DOG.active = 0
					AND a_get_inactive_dog = 0
				)
			;
		ELSE
			INSERT INTO tmp_Dog (
				  id_dog
                , name
			)
			SELECT 
				  DOG.id_dog
                , DOG.name
			FROM tmp_Split t_SPLIT
			RIGHT JOIN demo.DOG_Dog DOG ON t_SPLIT.as_int = DOG.id_dog
			WHERE 
                (
					    a_get_all_dog = 1
					OR  (
						    v_has_filter_dog = 1
						AND NOT ISNULL(t_SPLIT.as_int)
					)
				)
				AND (
					    a_get_inactive_dog = 1
					OR  DOG.active = 1
				)
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split;
    
    -- Commands
    IF v_has_filter_command = 1 THEN
		CALL demo.p_split(v_guid, a_ids_command, ',', a_debug);
		
		INSERT INTO tmp_Split (
			  substring
			, as_int
		)
		SELECT 
			  substring
			, CONVERT(substring, DECIMAL(10,0)) AS as_int
		FROM demo.CORE_Split_Temp
		WHERE 
                GUID = v_guid
			AND NOT ISNULL(substring)
			AND substring <> ''
		;
		
		CALL demo.p_clear_split_temp( v_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		IF EXISTS (
			SELECT * 
            FROM tmp_Split t_SPLIT 
            LEFT JOIN demo.DOG_Command COMMAND ON t_SPLIT.as_int = COMMAND.id_command 
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
                OR  ISNULL(COMMAND.id_command)
				OR  (
					COMMAND.active = 0
					AND a_get_inactive_command = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error (
				  id_type
		        , code
				, msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Command IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split t_SPLIT
			LEFT JOIN demo.DOG_Command COMMAND ON t_SPLIT.as_int = COMMAND.id_command
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
				OR  ISNULL(COMMAND.id_command)
				OR  (
					COMMAND.active = 0
					AND a_get_inactive_command = 0
				)
			;
		ELSE
			INSERT INTO tmp_Command (
				  id_command
                , id_command_category
                , name_command_category
                , name
			)
			SELECT 
				  COMMAND.id_command
                , COMMAND.id_command_category
                , COMMAND_CATEGORY.name AS name_command_category
            --  , COMMAND.code
                , COMMAND.name
			FROM tmp_Split t_SPLIT
			RIGHT JOIN demo.DOG_Command COMMAND ON t_SPLIT.as_int = COMMAND.id_command
            LEFT JOIN demo.DOG_Command_Category COMMAND_CATEGORY ON COMMAND.id_command_category = COMMAND_CATEGORY.id_command_category
			WHERE 
                (
					    a_get_all_command = 1
					OR  (
						    v_has_filter_command = 1
						AND NOT ISNULL(t_SPLIT.as_int)
					)
				)
				AND (
					    a_get_inactive_command = 1
					OR  COMMAND.active = 1
				)
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split;
    
    -- Understanding Levels
    IF v_has_filter_understanding_level = 1 THEN
		CALL demo.p_split(v_guid, a_ids_understanding_level, ',', a_debug);
		
		INSERT INTO tmp_Split (
			  substring
			, as_int
		)
		SELECT 
			  substring
			, CONVERT(substring, DECIMAL(10,0)) AS as_int
		FROM demo.CORE_Split_Temp
		WHERE 
                GUID = v_guid
			AND NOT ISNULL(substring)
			AND substring <> ''
		;
		
		CALL demo.p_clear_split_temp( v_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		IF EXISTS (
			SELECT * 
            FROM tmp_Split t_SPLIT 
            LEFT JOIN demo.DOG_Understanding_Level UNDERSTANDING_LEVEL ON t_SPLIT.as_int = UNDERSTANDING_LEVEL.id_understanding_level 
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
                OR  ISNULL(UNDERSTANDING_LEVEL.id_understanding_level)
				OR  (
					UNDERSTANDING_LEVEL.active = 0
					AND a_get_inactive_understanding_level = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error (
				  id_type
				, code
                , msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Understanding Level IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split t_SPLIT
			LEFT JOIN demo.DOG_Understanding_Level UNDERSTANDING_LEVEL ON t_SPLIT.as_int = UNDERSTANDING_LEVEL.id_understanding_level
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
				OR  ISNULL(UNDERSTANDING_LEVEL.id_understanding_level)
				OR  (
					UNDERSTANDING_LEVEL.active = 0
					AND a_get_inactive_understanding_level = 0
				)
			;
		ELSE
			INSERT INTO tmp_Understanding_Level (
				  id_understanding_level
                , code
                , name
			)
			SELECT 
				  UNDERSTANDING_LEVEL.id_understanding_level
                , UNDERSTANDING_LEVEL.code
                , UNDERSTANDING_LEVEL.name
			FROM tmp_Split t_SPLIT
			RIGHT JOIN demo.DOG_Understanding_Level UNDERSTANDING_LEVEL ON t_SPLIT.as_int = UNDERSTANDING_LEVEL.id_understanding_level
			WHERE 
                (
					    a_get_all_understanding_level = 1
					OR  (
						    v_has_filter_understanding_level = 1
						AND NOT ISNULL(t_SPLIT.as_int)
					)
				)
				AND (
					    a_get_inactive_understanding_level = 1
					OR  UNDERSTANDING_LEVEL.active = 1
				)
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split;
    
    -- Obedience Levels
    IF v_has_filter_obedience_level = 1 THEN
		CALL demo.p_split(v_guid, a_ids_obedience_level, ',', a_debug);
		
		INSERT INTO tmp_Split (
			  substring
			, as_int
		)
		SELECT 
			  substring
			, CONVERT(substring, DECIMAL(10,0)) AS as_int
		FROM demo.CORE_Split_Temp
		WHERE 
                GUID = v_guid
			AND NOT ISNULL(substring)
			AND substring <> ''
		;
		
		CALL demo.p_clear_split_temp( v_guid );
	END IF;
    
    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
		IF EXISTS (
			SELECT * 
            FROM tmp_Split t_SPLIT 
            LEFT JOIN demo.DOG_Obedience_Level OBEDIENCE_LEVEL ON t_SPLIT.as_int = OBEDIENCE_LEVEL.id_obedience_level 
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
                OR  ISNULL(OBEDIENCE_LEVEL.id_obedience_level)
				OR  (
					OBEDIENCE_LEVEL.active = 0
					AND a_get_inactive_obedience_level = 0
				)
		) THEN
			INSERT INTO tmp_Msg_Error (
				  id_type
				, code
                , msg
			)
			SELECT
				  v_id_type_error_bad_data
                , v_code_type_error_bad_data
				, CONCAT('Invalid or inactive Obedience Level IDs: ', IFNULL(GROUP_CONCAT(t_SPLIT.substring SEPARATOR ', '), 'NULL'))
			FROM tmp_Split t_SPLIT
			LEFT JOIN demo.DOG_Obedience_Level OBEDIENCE_LEVEL ON t_SPLIT.as_int = OBEDIENCE_LEVEL.id_obedience_level
			WHERE 
				    ISNULL(t_SPLIT.as_int) 
				OR  ISNULL(OBEDIENCE_LEVEL.id_obedience_level)
				OR  (
					OBEDIENCE_LEVEL.active = 0
					AND a_get_inactive_obedience_level = 0
				)
			;
		ELSE
			INSERT INTO tmp_Obedience_Level (
				  id_obedience_level
                , code
                , name
			)
			SELECT 
				  OBEDIENCE_LEVEL.id_obedience_level
                , OBEDIENCE_LEVEL.code
                , OBEDIENCE_LEVEL.name
			FROM tmp_Split t_SPLIT
			RIGHT JOIN demo.DOG_Obedience_Level OBEDIENCE_LEVEL ON t_SPLIT.as_int = OBEDIENCE_LEVEL.id_obedience_level
			WHERE 
                (
					    a_get_all_obedience_level = 1
					OR  (
						    v_has_filter_obedience_level = 1
						AND NOT ISNULL(t_SPLIT.as_int)
					)
				)
				AND (
					    a_get_inactive_obedience_level = 1
					OR  OBEDIENCE_LEVEL.active = 1
				)
			;
		END IF;
	END IF;
    
    DELETE FROM tmp_Split;
    

    -- Permissions
    IF a_debug = 1 THEN
		SELECT 
			  v_guid
			, a_id_user
			, FALSE -- a_get_inactive_user
			, v_id_permission_dog_view
			, 0 -- a_debug
        ;
    END IF;
    
	CALL demo.p_DOG_calc_user(
          v_guid
        , a_id_user
        , FALSE -- a_get_inactive_user
        , v_id_permission_dog_view
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CALC_USER_T.has_access, 0)
    INTO
          v_can_view
    FROM demo.DOG_Calc_User_Temp CALC_USER_T
    WHERE CALC_USER_T.GUID = v_guid
    LIMIT 1
    ;
    
    IF a_debug = 1 THEN
        SELECT v_can_view;
    END IF;
    
    IF (v_can_view = 0) THEN
		DELETE t_ME
        FROM tmp_Msg_Error t_ME
        WHERE t_ME.id_type <> v_id_type_error_no_permission
        ;
		INSERT INTO tmp_Msg_Error (
			  id_type
			, code
            , msg
		)
		VALUES (
			  v_id_type_error_no_permission
            , v_code_type_error_no_permission
			, 'You do not have permission to view Commands.'
		)
        ;
    END IF;
	
	CALL demo.p_DOG_clear_calc_user(
		v_guid
        , 0 -- a_debug
	);
    
    IF EXISTS(SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type WHERE ERROR_TYPE.is_breaking_error = 1 LIMIT 1) THEN
        IF a_debug = 1 THEN
            SELECT * FROM tmp_Dog;
            SELECT * FROM tmp_Command;
            SELECT * FROM tmp_Obedience_Level;
        END IF;

        DELETE FROM tmp_Dog;
        DELETE FROM tmp_Command;
        SELECT * FROM tmp_Understanding_Level;
        DELETE FROM tmp_Obedience_Level;
    END IF;

    -- Outputs
    -- Commands
    SELECT 
          DOG_COMMAND_LINK.id_link AS id_dog_command_link
        , t_DOG.id_dog
        , t_DOG.name AS name_dog
        , t_COMMAND.id_command_category
        , t_COMMAND.name_command_category
        , t_COMMAND.id_command
    --  , t_COMMAND.code
        , t_COMMAND.name AS name_command
        , t_UNDERSTANDING_LEVEL.id_understanding_level
        , t_UNDERSTANDING_LEVEL.code AS code_understanding_level
        , t_UNDERSTANDING_LEVEL.name AS name_understanding_level
        , t_OBEDIENCE_LEVEL.id_obedience_level
        , t_OBEDIENCE_LEVEL.code AS code_obedience_level
        , t_OBEDIENCE_LEVEL.name AS name_obedience_level
        , DOG_COMMAND_LINK.hand_signal_description
        , DOG_COMMAND_LINK.notes
        , DOG_COMMAND_LINK.active
    FROM demo.DOG_Dog_Command_Link DOG_COMMAND_LINK
    INNER JOIN tmp_Dog t_DOG ON DOG_COMMAND_LINK.id_dog = t_DOG.id_dog
    INNER JOIN tmp_Command t_COMMAND ON DOG_COMMAND_LINK.id_command = t_COMMAND.id_command
    INNER JOIN tmp_Understanding_Level t_UNDERSTANDING_LEVEL ON DOG_COMMAND_LINK.id_understanding_level = t_UNDERSTANDING_LEVEL.id_understanding_level
    INNER JOIN tmp_Obedience_Level t_OBEDIENCE_LEVEL ON DOG_COMMAND_LINK.id_obedience_level = t_OBEDIENCE_LEVEL.id_obedience_level
	;
    
    -- Errors
    SELECT 
          t_ERROR.id_error
		, t_ERROR.id_type
        , t_ERROR.code
        , ERROR_TYPE.name
        , ERROR_TYPE.description
        , ERROR_TYPE.is_breaking_error
        , ERROR_TYPE.background_colour
        , ERROR_TYPE.text_colour
		, t_ERROR.msg
    FROM tmp_Msg_Error t_ERROR
	INNER JOIN demo.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
	;
    
    IF a_debug = 1 AND v_can_view = 1 THEN
        SELECT * FROM tmp_Dog;
        SELECT * FROM tmp_Command;
        SELECT * FROM tmp_Understanding_Level;
        SELECT * FROM tmp_Obedience_Level;
    END IF;
    
    DROP TEMPORARY TABLE IF EXISTS tmp_Split;
    DROP TEMPORARY TABLE IF EXISTS tmp_Msg_Error;
    DROP TEMPORARY TABLE IF EXISTS tmp_Obedience_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Understanding_Level;
    DROP TEMPORARY TABLE IF EXISTS tmp_Command;
    DROP TEMPORARY TABLE IF EXISTS tmp_Dog;
    
    IF a_debug = 1 THEN
		CALL demo.p_core_debug_timing_reporting ( v_time_start );
    END IF;
END //
DELIMITER ;


/*

CALL demo.p_DOG_get_many_dog_command (
	  1 -- 'auth0|6582b95c895d09a70ba10fef',	-- a_id_user
    , 1                                     	-- a_get_all_dog
	, 0                                     	-- a_get_inactive_dog
	, ''                                    	-- a_ids_dog
    , 1                                     	-- a_get_all_command
	, 0                                     	-- a_get_inactive_command
	, ''                                    	-- a_ids_command
    , 1                                     	-- a_get_all_understanding_level
	, 0                                     	-- a_get_inactive_understanding_level
	, ''                                    	-- a_ids_understanding_level
    , 1                                     	-- a_get_all_obedience_level
	, 0                                     	-- a_get_inactive_obedience_level
	, ''                                    	-- a_ids_obedience_level
    , 1                                     	-- a_debug
);

*/