﻿DELIMITER $$

CREATE PROCEDURE `sp_get_all_resources_export`(
    -- 페이징 파라미터 제거
    IN p_sort_column VARCHAR(50),
    IN p_sort_direction VARCHAR(4),
    IN p_category_id TINYINT UNSIGNED,
    IN p_group_id BIGINT,
    IN p_user_id BIGINT
)
  COMMENT '전체 자산 목록 조회 (내보내기용 - 페이징 없음, 잠금 상태 포함)'
BEGIN
  DECLARE v_order_by_clause VARCHAR(255);
  DECLARE v_sql TEXT;

  IF UPPER(p_sort_direction) NOT IN ('ASC', 'DESC') THEN SET p_sort_direction = 'DESC'; END IF;

  -- 정렬 기준 설정 (기존과 동일, is_locked 포함)
  SET v_order_by_clause = CASE p_sort_column
    WHEN 'id' THEN CONCAT('ORDER BY r.resource_id ', p_sort_direction)
    WHEN 'name' THEN CONCAT('ORDER BY r.resource_name ', p_sort_direction)
    WHEN 'category' THEN CONCAT('ORDER BY rc.category_name ', p_sort_direction)
    WHEN 'code' THEN CONCAT('ORDER BY r.resource_code ', p_sort_direction)
    WHEN 'user' THEN CONCAT('ORDER BY user_display_name ', p_sort_direction)
    WHEN 'group' THEN CONCAT('ORDER BY g.group_name ', p_sort_direction)
    WHEN 'serial' THEN CONCAT('ORDER BY r.serial_num ', p_sort_direction)
    WHEN 'purchased' THEN CONCAT('ORDER BY r.purchase_date ', p_sort_direction)
    WHEN 'registered' THEN CONCAT('ORDER BY r.register_date ', p_sort_direction)
    WHEN 'updated' THEN CONCAT('ORDER BY r.update_date ', p_sort_direction)
    WHEN 'is_locked' THEN CONCAT('ORDER BY r.is_locked ', p_sort_direction)
    ELSE CONCAT('ORDER BY r.resource_id ', p_sort_direction)
  END;
  IF p_sort_column != 'id' THEN
    SET v_order_by_clause = CONCAT(v_order_by_clause, ', r.resource_id DESC');
  END IF;

  SET v_sql = CONCAT(
    'SELECT ', -- SQL_CALC_FOUND_ROWS 제거 (총 개수 필요 없음)
    '  r.resource_id, r.category_id, rc.category_name, r.resource_code, r.manufacturer, ',
    '  r.resource_name, r.serial_num, r.spec_value, r.spec_unit, r.user_id, ',
    '  CASE ',
    '    WHEN u.user_id IS NOT NULL AND u.display_name IS NOT NULL AND u.display_name != \'\' AND u.account_name IS NOT NULL THEN CONCAT(u.display_name, \' [\', u.account_name, \']\') ',
    '    WHEN u.display_name IS NOT NULL AND u.display_name != \'\' THEN u.display_name ',
    '    ELSE u.account_name ',
    '  END AS user_display_name, ',
    '  g.group_name, r.comments, r.purchase_date, r.register_date, r.update_date, r.is_locked ',
    'FROM resource_info r ',
    '  LEFT JOIN resource_category rc ON r.category_id = rc.category_id ',
    '  LEFT JOIN user_info u ON r.user_id = u.user_id ',
    '  LEFT JOIN group_info g ON u.group_id = g.group_id ',
    'WHERE (CAST(? AS SIGNED) IS NULL OR r.category_id = ?) ',
    '  AND (CAST(? AS SIGNED) IS NULL OR g.group_id = ?) ',
    '  AND (CAST(? AS SIGNED) IS NULL OR r.user_id = ?) ',
    v_order_by_clause
    -- LIMIT ... OFFSET ... 제거
  );

  SET @p_category_id_filter = p_category_id;
  SET @p_group_id_filter = p_group_id;
  SET @p_user_id_filter = p_user_id;

  PREPARE stmt FROM v_sql;
  EXECUTE stmt USING
    @p_category_id_filter, @p_category_id_filter,
    @p_group_id_filter, @p_group_id_filter,
    @p_user_id_filter, @p_user_id_filter;
  DEALLOCATE PREPARE stmt;
END
$$

DELIMITER ;