<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="entitySearch">

  <!-- ================================================================
       정적 쿼리: 테이블 컬럼 목록 조회
       ================================================================ -->

  <select id="getTableColumnList" parameterType="map" resultType="map">
    SELECT column_name

    FROM information_schema.columns

    WHERE table_schema = 'public'
      AND table_name = #{table_name}
  </select>

  <!-- ================================================================
       정적 쿼리: category_values 라벨 조회
       ================================================================ -->

  <select id="getCategoryValueList" parameterType="map" resultType="map">
    SELECT value_code, value_label

    FROM category_values

    WHERE table_name = #{table_name}
      AND column_name = #{column_name}
      AND value_code IN
      <foreach collection="rawValues" item="v" open="(" separator="," close=")">
        #{v}
      </foreach>
      AND is_active = TRUE
    <if test="company_code != null and company_code != &quot;*&quot;">
      AND (company_code = #{company_code} OR company_code = '*')
    </if>
  </select>

  <!-- ================================================================
       정적 쿼리: table_type_columns의 code_info 조회
       ================================================================ -->

  <select id="getCodeCategoryInfo" parameterType="map" resultType="map">
    SELECT code_info

    FROM table_type_columns

    WHERE table_name = #{table_name}
      AND column_name = #{column_name}
      AND code_info IS NOT NULL

    LIMIT 1
  </select>

  <!-- ================================================================
       정적 쿼리: code_info 코드명 조회
       ================================================================ -->

  <select id="getCodeInfoList" parameterType="map" resultType="map">
    SELECT code_value, code_name

    FROM code_info

    WHERE code_info = #{code_info}
      AND code_value IN
      <foreach collection="rawValues" item="v" open="(" separator="," close=")">
        #{v}
      </foreach>
      AND is_active = 'Y'
    <if test="company_code != null and company_code != &quot;*&quot;">
      AND (company_code = #{company_code} OR company_code = '*')
    </if>
  </select>

  <!-- ================================================================
       정적 쿼리: PK 컬럼 조회 (pg_index 사용)
       ================================================================ -->

  <select id="getPrimaryKeyInfo" parameterType="map" resultType="map">
    SELECT a.attname AS column_name

    FROM pg_index i

    JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)

    WHERE i.indrelid = #{table_name}::regclass
      AND i.indisprimary

    ORDER BY array_position(i.indkey, a.attnum)
    LIMIT 1
  </select>

  <!-- ================================================================
       동적 쿼리: 엔티티 검색
       tableName, whereClause, orderByColumn: ${...} 치환 (Java에서 sanitize)
       condVal_N: #{...} 바인딩 (안전한 파라미터)
       ================================================================ -->

  <select id="getEntitySearchList" parameterType="map" resultType="map">
    SELECT * FROM ${tableName}

    WHERE 1=1 ${whereClause}

    ORDER BY ${orderByColumn} DESC
    LIMIT #{page_limit} OFFSET #{page_offset}
  </select>

  <select id="getEntitySearchListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*) FROM ${tableName}

    WHERE 1=1 ${whereClause}
  </select>

  <!-- ================================================================
       동적 쿼리: 엔티티 옵션 (V2Select용)
       selectColumns: "valueCol as value, labelCol as label [, extra...]"
       ================================================================ -->

  <select id="getEntityOptionList" parameterType="map" resultType="map">
    SELECT ${selectColumns}

    FROM ${tableName}

    WHERE 1=1 ${whereClause}

    ORDER BY ${labelColumn} ASC
    LIMIT 500
  </select>

  <!-- ================================================================
       동적 쿼리: 컬럼 DISTINCT 값 조회
       ================================================================ -->

  <select id="getDistinctValueList" parameterType="map" resultType="map">
    SELECT DISTINCT ${valueColumn} AS value, ${labelColumn} AS label

    FROM ${tableName}

    WHERE ${valueColumn} IS NOT NULL
      AND ${valueColumn}::text != ''
      ${whereClause}

    ORDER BY ${labelColumn} ASC
    LIMIT 500
  </select>

</mapper>
