
USE parts;

DROP PROCEDURE IF EXISTS parts.p_dog_save_dog_command_link;

DELIMITER //
CREATE PROCEDURE parts.p_dog_save_dog_command_link (
    IN a_comment VARCHAR(500),
	IN a_guid BINARY(36),
    IN a_id_user INT,
    IN a_debug BIT
)
BEGIN
    DECLARE v_can_admin BIT;
    DECLARE v_can_create BIT;
	DECLARE v_code_type_error_bad_data VARCHAR(100);
    DECLARE v_id_access_level_edit INT;
    DECLARE v_id_change_set INT;
    DECLARE v_id_permission_dog_new INT;
    DECLARE v_id_type_error_bad_data INT;
    DECLARE v_time_start TIMESTAMP(6);
    
    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(250)
			, msg TEXT NOT NULL
		);

        INSERT INTO tmp_Msg_Error (
			  id_type
			, code
            , msg
		)
        SELECT 
			  MET.id_type
            , @errno
            , @text
		FROM parts.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 parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
        ;

		DROP TABLE IF EXISTS tmp_Msg_Error;
    END;
    
    SET SESSION group_concat_max_len=15000;

    SET v_time_start := CURRENT_TIMESTAMP(6);
    SET v_code_type_error_bad_data := 'BAD_DATA';
    SET v_id_type_error_bad_data := (SELECT ERROR_TYPE.id_type FROM parts.CORE_Msg_Error_Type ERROR_TYPE WHERE ERROR_TYPE.code = v_code_type_error_bad_data LIMIT 1);
    SET v_id_permission_dog_new := (SELECT PERMISSION.id_permission FROM parts.DOG_Permission PERMISSION WHERE PERMISSION.code = 'DOG_CREATE' LIMIT 1);
    SET v_id_access_level_edit := (SELECT ACCESS_LEVEL.id_access_level FROM parts.DOG_Access_Level ACCESS_LEVEL WHERE ACCESS_LEVEL.code = 'EDIT' LIMIT 1);
    
    CALL parts.p_core_validate_guid ( a_guid );
    
    DROP TABLE IF EXISTS tmp_Dog_Command_Link_Copy;
    DROP TABLE IF EXISTS tmp_Dog_Command_Link;
    
    CREATE TEMPORARY TABLE tmp_Dog_Command_Link (
          id_temp INT
        , id_link INT
        , id_dog INT
        , id_command INT
        , hand_signal_description TEXT
        , notes TEXT
        , active BIT
        , is_new BIT
        , name_error VARCHAR(250)
    );

    CREATE TEMPORARY TABLE tmp_Dog_Command_Link_Copy (
          id_temp INT
        , id_link INT
        , id_dog INT
        , id_command INT
        , hand_signal_description TEXT
        , notes TEXT
        , active BIT
        , is_new BIT
        , name_error 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(250)
        , msg TEXT NOT NULL
	);
        
    
    -- Get data from Temp table
    INSERT INTO tmp_Dog_Command_Link (
		  id_temp
        , id_link
        , id_dog
        , id_command
        , hand_signal_description
        , notes
        , active
        , is_new
	)
    SELECT 
		  DOG_COMMAND_LINK_T.id_temp
		, COALESCE(DOG_COMMAND_LINK.id_link, DOG_COMMAND_LINK_T.id_link)
        , COALESCE(
              DOG_COMMAND_LINK_T.id_dog
            , DOG_COMMAND_LINK.id_dog
        ) AS id_dog
        , COALESCE(
              DOG_COMMAND_LINK_T.id_command
            , DOG_COMMAND_LINK.id_command
        ) AS id_command
        /*
        , NULLIF(
            COALESCE(
                  DOG_COMMAND_LINK_T.hand_signal_description
                , DOG_COMMAND_LINK.hand_signal_description
            )
            , ''
        ) AS hand_signal_description
        , NULLIF(
            COALESCE(
                  DOG_COMMAND_LINK_T.notes
                , DOG_COMMAND_LINK.notes
            )
            , ''
        ) AS notes
        */
        , COALESCE(
              DOG_COMMAND_LINK_T.hand_signal_description
            , DOG_COMMAND_LINK.hand_signal_description
        ) AS hand_signal_description
        , COALESCE(
              DOG_COMMAND_LINK_T.notes
            , DOG_COMMAND_LINK.notes
        ) AS notes
        , COALESCE(DOG_COMMAND_LINK_T.active, 1) AS active
        , CASE WHEN COALESCE(DOG_COMMAND_LINK.id_link, DOG_COMMAND_LINK_T.id_link, 0) < 1 THEN 1 ELSE 0 END AS is_new
	FROM parts.DOG_Dog_Command_Link_Temp DOG_COMMAND_LINK_T
    LEFT JOIN parts.DOG_Dog_Command_Link DOG_COMMAND_LINK 
        ON  DOG_COMMAND_LINK_T.id_link = DOG_COMMAND_LINK.id_link
        OR  (
                DOG_COMMAND_LINK_T.id_dog = DOG_COMMAND_LINK.id_dog
            AND DOG_COMMAND_LINK_T.id_command = DOG_COMMAND_LINK.id_command
            AND (
                    DOG_COMMAND_LINK.hand_signal_description IS NULL
                OR  DOG_COMMAND_LINK_T.hand_signal_description <=> DOG_COMMAND_LINK.hand_signal_description
            )
            AND (
                    DOG_COMMAND_LINK.notes IS NULL
                OR  DOG_COMMAND_LINK_T.notes <=> DOG_COMMAND_LINK.notes
            )
        )
    WHERE DOG_COMMAND_LINK_T.guid = a_guid
    ;

    IF a_debug = 1 THEN
        SELECT 'Dog_Command_Link_Temp records';
        SELECT * FROM tmp_Dog_Command_Link;
        SELECT COUNT(*) FROM tmp_Dog_Command_Link;
    END IF;

    -- Error names
    UPDATE tmp_Dog_Command_Link t_DOG_COMMAND_LINK
    LEFT JOIN parts.DOG_Dog DOG ON t_DOG_COMMAND_LINK.id_dog = DOG.id_dog
    LEFT JOIN parts.DOG_Command COMMAND ON t_DOG_COMMAND_LINK.id_command = COMMAND.id_command
    SET t_DOG_COMMAND_LINK.name_error = CASE WHEN 
            ISNULL(DOG.id_dog)
        AND ISNULL(COMMAND.id_command)
    THEN COALESCE(t_DOG_COMMAND_LINK.hand_signal_description, t_DOG_COMMAND_LINK.notes, '(No Dog Command Link)')
    ELSE CONCAT(
          COALESCE(DOG.name, t_DOG_COMMAND_LINK.id_dog, '(No Dog)')
        , ' - '
        , COALESCE(COMMAND.name, t_DOG_COMMAND_LINK.id_command, '(No Command)')
    ) END
    ;

    IF a_debug = 1 THEN
        SELECT 'After set name_error';
        SELECT * FROM tmp_Dog_Command_Link;
        SELECT COUNT(*) FROM tmp_Dog_Command_Link;
    END IF;
    
    /*
    -- Missing Dog Command Link Ids
    UPDATE tmp_Dog_Command_Link t_DOG_COMMAND_LINK
    LEFT JOIN parts.DOG_Dog_Command_Link DOG_COMMAND_LINK 
        -- ON t_DOG_COMMAND_LINK.id_link = DOG_COMMAND_LINK.id_link
        ON  t_DOG_COMMAND_LINK.id_dog = DOG_COMMAND_LINK.id_dog
        AND t_DOG_COMMAND_LINK.id_command = DOG_COMMAND_LINK.id_command
    SET 
        t_DOG_COMMAND_LINK.id_link = DOG_COMMAND_LINK.id_link
        , t_DOG_COMMAND_LINK.is_new = 0
    WHERE 
            IFNULL(t_DOG_COMMAND_LINK.id_link, 0) < 1
        AND NOT ISNULL(DOG_COMMAND_LINK.id_link)
    ;

    IF a_debug = 1 THEN
        SELECT 'After set missing id_link';
        SELECT * FROM tmp_Dog_Command_Link;
        SELECT COUNT(*) FROM tmp_Dog_Command_Link;
    END IF;
    */

    -- Validation
    -- Missing mandatory fields
    -- id_dog
    IF EXISTS (
        SELECT *
        FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
        LEFT JOIN demo.DOG_Dog DOG ON t_DOG_COMMAND_LINK.id_dog = DOG.id_dog
        WHERE 
                ISNULL(t_DOG_COMMAND_LINK.id_dog)
            OR  ISNULL(DOG.id_dog)
            OR  DOG.active = 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('The following Dog Command Link(s) do not have a valid Dog: ', GROUP_CONCAT(t_DOG_COMMAND_LINK.name_error SEPARATOR ', ')) AS msg
        FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
        LEFT JOIN parts.DOG_Dog DOG ON t_DOG_COMMAND_LINK.id_dog = DOG.id_dog
        WHERE 
                ISNULL(t_DOG_COMMAND_LINK.id_dog)
            OR  ISNULL(DOG.id_dog)
            OR  DOG.active = 0
        ;
    END IF;
    -- id_command
    IF EXISTS (
        SELECT *
        FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
        LEFT JOIN demo.DOG_Command COMMAND ON t_DOG_COMMAND_LINK.id_command = COMMAND.id_command
        WHERE 
                ISNULL(t_DOG_COMMAND_LINK.id_command)
            OR  ISNULL(COMMAND.id_command)
            OR  COMMAND.active = 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('The following Dog Command Link(s) do not have a valid Command: ', GROUP_CONCAT(t_DOG_COMMAND_LINK.name_error SEPARATOR ', ')) AS msg
        FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
        LEFT JOIN parts.DOG_Command COMMAND ON t_DOG_COMMAND_LINK.id_command = COMMAND.id_command
        WHERE 
                ISNULL(t_DOG_COMMAND_LINK.id_command)
            OR  ISNULL(COMMAND.id_command)
            OR  COMMAND.active = 0
        ;
    END IF;

    -- Duplicates
    INSERT INTO tmp_Dog_Command_Link_Copy (
        id_temp
        , id_link
        , id_dog
        , id_command
        , hand_signal_description
        , notes
        , active
        , is_new
        , name_error
    )
    SELECT 
          t_DOG_COMMAND_LINK.id_temp
        , t_DOG_COMMAND_LINK.id_link
        , t_DOG_COMMAND_LINK.id_dog
        , t_DOG_COMMAND_LINK.id_command
        , t_DOG_COMMAND_LINK.hand_signal_description
        , t_DOG_COMMAND_LINK.notes
        , t_DOG_COMMAND_LINK.active
        , t_DOG_COMMAND_LINK.is_new
        , t_DOG_COMMAND_LINK.name_error
    FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
    ;

    IF a_debug = 1 THEN
        SELECT COUNT(*) AS Count_Temp_Link FROM tmp_Dog_Command_Link;
        SELECT COUNT(*) AS Count_Temp_Link_Copy FROM tmp_Dog_Command_Link_Copy;

        WITH 
              Combined_Links AS (
                SELECT 
                      DOG_COMMAND_LINK.id_link
                    , DOG_COMMAND_LINK.id_dog
                    , DOG_COMMAND_LINK.id_command
                    , IFNULL(
                        t_DOG_COMMAND_LINK_COPY.name_error
                        , CONCAT(
                            COALESCE(DOG.name, t_DOG_COMMAND_LINK_COPY.id_dog, '(No Dog)')
                            , ' - '
                            , COALESCE(COMMAND.name, t_DOG_COMMAND_LINK_COPY.id_command, '(No Command)')
                        )
                    ) AS name_error
                FROM parts.DOG_Dog_Command_Link DOG_COMMAND_LINK 
                LEFT JOIN tmp_Dog_Command_Link_Copy t_DOG_COMMAND_LINK_COPY 
                    ON  DOG_COMMAND_LINK.id_dog = t_DOG_COMMAND_LINK_COPY.id_dog
                    AND DOG_COMMAND_LINK.id_command = t_DOG_COMMAND_LINK_COPY.id_command
                INNER JOIN parts.DOG_Dog DOG ON DOG_COMMAND_LINK.id_dog = DOG.id_dog
                INNER JOIN parts.DOG_Command COMMAND ON DOG_COMMAND_LINK.id_command = COMMAND.id_command
                WHERE t_DOG_COMMAND_LINK_COPY.is_new = 1
                UNION
                SELECT 
                      t_DOG_COMMAND_LINK.id_link
                    , t_DOG_COMMAND_LINK.id_dog
                    , t_DOG_COMMAND_LINK.id_command
                    , t_DOG_COMMAND_LINK.name_error
                FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
            )
            , Duplicate_Link_Row_Numbers AS (
                SELECT 
                      COMBINED_LINK.id_link
                    , COMBINED_LINK.id_dog
                    , COMBINED_LINK.id_command
                    , COMBINED_LINK.name_error
                    , ROW_NUMBER() OVER (PARTITION BY COMBINED_LINK.id_dog, COMBINED_LINK.id_command ORDER BY COMBINED_LINK.name_error ASC) AS index_link_as_duplicate
                FROM Combined_Links COMBINED_LINK
            )
        SELECT *
        FROM Duplicate_Link_Row_Numbers;
    END IF;
    
    IF EXISTS (
        WITH 
              Combined_Links AS (
                SELECT 
                      DOG_COMMAND_LINK.id_link
                    , DOG_COMMAND_LINK.id_dog
                    , DOG_COMMAND_LINK.id_command
                    , IFNULL(
                        t_DOG_COMMAND_LINK_COPY.name_error
                        , CONCAT(
                            COALESCE(DOG.name, t_DOG_COMMAND_LINK_COPY.id_dog, '(No Dog)')
                            , ' - '
                            , COALESCE(COMMAND.name, t_DOG_COMMAND_LINK_COPY.id_command, '(No Command)')
                        )
                    ) AS name_error
                FROM parts.DOG_Dog_Command_Link DOG_COMMAND_LINK 
                LEFT JOIN tmp_Dog_Command_Link_Copy t_DOG_COMMAND_LINK_COPY 
                    ON  DOG_COMMAND_LINK.id_dog = t_DOG_COMMAND_LINK_COPY.id_dog
                    AND DOG_COMMAND_LINK.id_command = t_DOG_COMMAND_LINK_COPY.id_command
                INNER JOIN parts.DOG_Dog DOG ON DOG_COMMAND_LINK.id_dog = DOG.id_dog
                INNER JOIN parts.DOG_Command COMMAND ON DOG_COMMAND_LINK.id_command = COMMAND.id_command
                WHERE t_DOG_COMMAND_LINK_COPY.is_new = 1
                UNION
                SELECT 
                      t_DOG_COMMAND_LINK.id_link
                    , t_DOG_COMMAND_LINK.id_dog
                    , t_DOG_COMMAND_LINK.id_command
                    , t_DOG_COMMAND_LINK.name_error
                FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
            )
            , Duplicate_Link_Row_Numbers AS (
                SELECT 
                      COMBINED_LINK.id_link
                    , COMBINED_LINK.id_dog
                    , COMBINED_LINK.id_command
                    , COMBINED_LINK.name_error
                    , ROW_NUMBER() OVER (PARTITION BY COMBINED_LINK.id_dog, COMBINED_LINK.id_command ORDER BY COMBINED_LINK.name_error ASC) AS index_link_as_duplicate
                FROM Combined_Links COMBINED_LINK
            )
        SELECT *
        FROM Duplicate_Link_Row_Numbers DUPLICATE_LINK
        WHERE DUPLICATE_LINK.index_link_as_duplicate > 1
        GROUP BY DUPLICATE_LINK.id_dog, DUPLICATE_LINK.id_command
    ) THEN
        INSERT INTO tmp_Msg_Error (
              id_type
            , code
            , msg
        )
        WITH 
              Combined_Links AS (
                SELECT 
                      DOG_COMMAND_LINK.id_link
                    , DOG_COMMAND_LINK.id_dog
                    , DOG_COMMAND_LINK.id_command
                    , IFNULL(
                        t_DOG_COMMAND_LINK_COPY.name_error
                        , CONCAT(
                            COALESCE(DOG.name, t_DOG_COMMAND_LINK_COPY.id_dog, '(No Dog)')
                            , ' - '
                            , COALESCE(COMMAND.name, t_DOG_COMMAND_LINK_COPY.id_command, '(No Command)')
                        )
                    ) AS name_error
                FROM parts.DOG_Dog_Command_Link DOG_COMMAND_LINK 
                LEFT JOIN tmp_Dog_Command_Link_Copy t_DOG_COMMAND_LINK_COPY 
                    ON  DOG_COMMAND_LINK.id_dog = t_DOG_COMMAND_LINK_COPY.id_dog
                    AND DOG_COMMAND_LINK.id_command = t_DOG_COMMAND_LINK_COPY.id_command
                INNER JOIN parts.DOG_Dog DOG ON DOG_COMMAND_LINK.id_dog = DOG.id_dog
                INNER JOIN parts.DOG_Command COMMAND ON DOG_COMMAND_LINK.id_command = COMMAND.id_command
                WHERE t_DOG_COMMAND_LINK_COPY.is_new = 1
                UNION
                SELECT 
                      t_DOG_COMMAND_LINK.id_link
                    , t_DOG_COMMAND_LINK.id_dog
                    , t_DOG_COMMAND_LINK.id_command
                    , t_DOG_COMMAND_LINK.name_error
                FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
            )
            , Duplicate_Link_Row_Numbers AS (
                SELECT 
                      COMBINED_LINK.id_link
                    , COMBINED_LINK.id_dog
                    , COMBINED_LINK.id_command
                    , COMBINED_LINK.name_error
                    , ROW_NUMBER() OVER (PARTITION BY COMBINED_LINK.id_dog, COMBINED_LINK.id_command ORDER BY COMBINED_LINK.name_error ASC) AS index_link_as_duplicate
                FROM Combined_Links COMBINED_LINK
            )
        SELECT
              v_id_type_error_bad_data
            , v_code_type_error_bad_data
            , CONCAT('Attempt to create duplicate or overwrite existing Dog Command Links on: ', GROUP_CONCAT(DUPLICATE_LINK.name_error SEPARATOR ', ')) AS msg
        FROM Duplicate_Link_Row_Numbers DUPLICATE_LINK
        WHERE DUPLICATE_LINK.index_link_as_duplicate > 1
        GROUP BY 
              DUPLICATE_LINK.id_dog
            , DUPLICATE_LINK.id_command
        ;
    END IF;
    
    
    -- Permissions
    -- Can Create
	CALL parts.p_dog_calc_user(
          a_guid
        , 0 -- get_all_user
        , 0 -- get_inactive_user
        , a_id_user -- ids_user
        , '' -- a_auth0_ids_user
        , '' -- a_names_user
        , '' -- a_emails_user
        , 1 -- a_require_all_id_search_filters_met
        , 1 -- a_require_any_id_search_filters_met
        , 0 -- a_require_all_non_id_search_filters_met
        , 0 -- a_require_any_non_id_search_filters_met
        , v_id_permission_dog_new -- ids_permission
        , v_id_access_level_edit -- ids_access_level
        , 0 -- a_show_errors
        , 0 -- a_debug
    );
	
    SELECT
          IFNULL(CU_T.has_access, 0)
    INTO
          v_can_create
    FROM parts.DOG_Calc_User_Temp CU_T
    WHERE CU_T.GUID = a_guid
    LIMIT 1
    ;
	
	CALL parts.p_dog_clear_calc_user(
		  a_guid
        , 0 -- a_debug
	);
    
    IF v_can_create = 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 edit Commands.'
		)
        ;
    END IF;

    IF EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN parts.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_Command_Link;
	    END IF;

        DELETE FROM tmp_Dog_Command_Link;
    END IF;

    IF NOT EXISTS (SELECT * FROM tmp_Msg_Error t_ERROR INNER JOIN parts.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
		START TRANSACTION;
		
			INSERT INTO parts.DOG_Dog_Change_Set ( 
                  comment
                , id_user_updated_last_by
                , updated_last_on
            )
			VALUES ( 
                  a_comment
                , a_id_user
                , v_time_start
            )
			;
			
			SET v_id_change_set := LAST_INSERT_ID();
			
			UPDATE parts.DOG_Dog_Command_Link DOG_COMMAND_LINK
			INNER JOIN tmp_Dog_Command_Link t_DOG_COMMAND_LINK 
				ON  DOG_COMMAND_LINK.id_link = t_DOG_COMMAND_LINK.id_link
				AND t_DOG_COMMAND_LINK.is_new = 0
			SET 
				  DOG_COMMAND_LINK.id_dog = t_DOG_COMMAND_LINK.id_dog
				, DOG_COMMAND_LINK.id_command = t_DOG_COMMAND_LINK.id_command
				, DOG_COMMAND_LINK.hand_signal_description = t_DOG_COMMAND_LINK.hand_signal_description
                , DOG_COMMAND_LINK.notes = t_DOG_COMMAND_LINK.notes
				, DOG_COMMAND_LINK.active = t_DOG_COMMAND_LINK.active
				, DOG_COMMAND_LINK.id_change_set = v_id_change_set
			;
			
			INSERT INTO parts.DOG_Dog_Command_Link (
				  id_dog
				, id_command
				, hand_signal_description
                , notes
				, active
				, id_user_created_by
				, created_on
			)
			SELECT
				  t_DOG_COMMAND_LINK.id_dog                     AS id_dog
				, t_DOG_COMMAND_LINK.id_command                   AS id_command
				, t_DOG_COMMAND_LINK.hand_signal_description     AS hand_signal_description
                , t_DOG_COMMAND_LINK.notes                       AS notes
				, t_DOG_COMMAND_LINK.active                      AS active
				, a_id_user                                     AS created_by
				, v_time_start                                  AS created_on
			FROM tmp_Dog_Command_Link t_DOG_COMMAND_LINK
			WHERE 
				    t_DOG_COMMAND_LINK.is_new = 1
				AND t_DOG_COMMAND_LINK.active = 1
			;
			
		COMMIT;
    END IF;
	
	START TRANSACTION;

		DELETE FROM parts.DOG_Dog_Command_Link_Temp
		WHERE GUID = a_guid
        ;
		
	COMMIT;
    
    -- 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 parts.CORE_Msg_Error_Type ERROR_TYPE ON t_ERROR.id_type = ERROR_TYPE.id_type
	;
    
	IF a_debug = 1 THEN
		SELECT * FROM tmp_Dog_Command_Link;
	END IF;

    DROP TEMPORARY TABLE tmp_Dog_Command_Link;
    DROP TEMPORARY TABLE tmp_Msg_Error;
    
	IF a_debug = 1 THEN
		CALL parts.p_core_debug_timing_reporting ( v_time_start );
	END IF;
END //
DELIMITER ;



/*

'ripplesipplenippletippledipplekipple'
DELETE FROM parts.DOG_Dog_Command_Link WHERE id_link > 740;
* /
delete
from parts.DOG_Dog_Command_Link_Audit
where id_link > 768
;
delete
from parts.DOG_Dog_Command_Link
where id_link > 768
;
delete
from parts.DOG_Dog_Command_Link_Temp
;
select 
	*
    -- COUNT(*)
-- delete
from parts.DOG_Dog_Command_Link_Temp
;
select COUNT(*)
from parts.DOG_Dog_Command_Link_Temp
;
select 
	*
    -- COUNT(*)
-- delete
from parts.DOG_Dog_Command_Link
;
select COUNT(*)
from parts.DOG_Dog_Command_Link
;

INSERT INTO parts.DOG_Dog_Command_Link_Temp (
      id_link
    , id_dog
    , id_command
    , hand_signal_description
    , notes
    , active
    , guid
)
VALUES (
      -1 -- id_link
    , 1 -- id_dog
    , 1 -- id_command
    , 'Test deez noots' -- hand_signal_description
    , NULL -- notes
    , 1 -- active
    , 'ripplesipplenippletippledipplekipple'
);

CALL parts.p_dog_save_dog_command_link (
	'nipples'
    , 'ripplesipplenippletippledipplekipple'
    , 1
    , 1
);


select 
	*
    -- COUNT(*)
-- delete
from parts.DOG_Dog_Command_Link_Temp
;
select COUNT(*)
from parts.DOG_Dog_Command_Link_Temp
;
select 
	*
    -- COUNT(*)
-- delete
from parts.DOG_Dog_Command_Link
;
select COUNT(*)
from parts.DOG_Dog_Command_Link
;

*/