﻿DELIMITER $$

CREATE PROCEDURE `sp_get_resources_by_search_export`(
    IN p_search_term VARCHAR(100), -- ★★★ 검색어 입력 파라미터 ★★★
    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;
  DECLARE v_search_like_term VARCHAR(105); -- 검색어 앞뒤에 % 추가

  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;

  -- 검색어 LIKE 조건용 문자열 생성
  SET v_search_like_term = IF(p_search_term IS NOT NULL AND p_search_term != '', CONCAT('%', p_search_term, '%'), NULL);

  SET v_sql = CONCAT(
    'SELECT ',
    '  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 = ?) ',  -- category_id 필터
    '  AND (CAST(? AS SIGNED) IS NULL OR g.group_id = ?) ',     -- group_id 필터
    '  AND (CAST(? AS SIGNED) IS NULL OR r.user_id = ?) ',      -- user_id 필터
    '  AND (? IS NULL OR ( ',  -- ★★★ 검색어 조건 시작 (v_search_like_term 이 NULL이면 이 AND 조건 전체가 참이 됨) ★★★
    '       LOWER(r.resource_name) LIKE LOWER(?) OR ',
    '       LOWER(r.serial_num) LIKE LOWER(?) OR ',
    '       LOWER(r.resource_code) LIKE LOWER(?) OR ',
    '       (u.user_id IS NOT NULL AND LOWER(u.display_name) LIKE LOWER(?)) OR ', -- u.display_name 이 NULL 이 아닐 때만 검색
    '       (u.user_id IS NOT NULL AND LOWER(u.account_name) LIKE LOWER(?)) OR ', -- u.account_name 이 NULL 이 아닐 때만 검색
    '       LOWER(r.manufacturer) LIKE LOWER(?) OR ',
    '       LOWER(r.comments) LIKE LOWER(?) ',
    '  )) ', -- ★★★ 검색어 조건 끝 ★★★
    v_order_by_clause
  );

  -- Prepared Statement 에 사용할 변수 설정
  SET @p_category_id_param = p_category_id;
  SET @p_group_id_param = p_group_id;
  SET @p_user_id_param = p_user_id;
  SET @p_search_term_param = v_search_like_term; -- %가 포함된 검색어 또는 NULL

  PREPARE stmt FROM v_sql;
  EXECUTE stmt USING
    @p_category_id_param, @p_category_id_param,
    @p_group_id_param, @p_group_id_param,
    @p_user_id_param, @p_user_id_param,
    @p_search_term_param, -- 검색어 NULL 체크용
    @p_search_term_param, @p_search_term_param, @p_search_term_param, -- resource 관련 필드 검색
    @p_search_term_param, @p_search_term_param, -- user 관련 필드 검색
    @p_search_term_param, @p_search_term_param; -- manufacturer, comments 검색
  DEALLOCATE PREPARE stmt;
END
$$

DELIMITER ;