<?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="webTypeStandard">

  <!-- WEB_TYPE_STANDARDS 는 시스템 전역 테이블이므로 COMPANY_CODE 필터 없음 -->

  <select id="selectWebTypes" parameterType="map" resultType="map">
    SELECT *
    FROM WEB_TYPE_STANDARDS
    WHERE 1=1
    <if test="active != null">
      AND IS_ACTIVE = #{active}
    </if>
    <if test="category != null">
      AND CATEGORY = #{category}
    </if>
    <if test="search != null">
      AND (
        TYPE_NAME     ILIKE #{search}
        OR TYPE_NAME_ENG ILIKE #{search}
        OR DESCRIPTION   ILIKE #{search}
      )
    </if>
    ORDER BY SORT_ORDER ASC, WEB_TYPE ASC
  </select>

  <select id="selectCategories" resultType="map">
    SELECT CATEGORY, COUNT(*) AS COUNT
    FROM WEB_TYPE_STANDARDS
    WHERE IS_ACTIVE = 'Y'
    GROUP BY CATEGORY
  </select>

  <select id="selectWebType" parameterType="map" resultType="map">
    SELECT *
    FROM WEB_TYPE_STANDARDS
    WHERE WEB_TYPE = #{web_type}
  </select>

  <insert id="insertWebType" parameterType="map">
    INSERT INTO WEB_TYPE_STANDARDS (
      WEB_TYPE, TYPE_NAME, TYPE_NAME_ENG, DESCRIPTION, CATEGORY,
      COMPONENT_NAME, CONFIG_PANEL, DEFAULT_CONFIG, VALIDATION_RULES,
      DEFAULT_STYLE, INPUT_PROPERTIES, SORT_ORDER, IS_ACTIVE,
      CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE
    ) VALUES (
      #{web_type}, #{type_name}, #{type_name_eng}, #{description}, #{category},
      #{component_name}, #{config_panel},
      #{default_config}::JSONB, #{validation_rules}::JSONB,
      #{default_style}::JSONB, #{input_properties}::JSONB,
      #{sort_order}, #{is_active},
      #{created_by}, NOW(), #{updated_by}, NOW()
    )
  </insert>

  <!-- 동적 UPDATE: 요청에 포함된 필드만 수정 -->
  <update id="updateWebType" parameterType="map">
    UPDATE WEB_TYPE_STANDARDS
    <set>
      <if test="type_name != null">TYPE_NAME        = #{type_name},</if>
      <if test="type_name_eng != null">TYPE_NAME_ENG    = #{type_name_eng},</if>
      <if test="description != null">DESCRIPTION      = #{description},</if>
      <if test="category != null">CATEGORY         = #{category},</if>
      <if test="component_name != null">COMPONENT_NAME   = #{component_name},</if>
      <if test="config_panel != null">CONFIG_PANEL     = #{config_panel},</if>
      <if test="default_config != null">DEFAULT_CONFIG   = #{default_config}::JSONB,</if>
      <if test="validation_rules != null">VALIDATION_RULES = #{validation_rules}::JSONB,</if>
      <if test="default_style != null">DEFAULT_STYLE    = #{default_style}::JSONB,</if>
      <if test="input_properties != null">INPUT_PROPERTIES = #{input_properties}::JSONB,</if>
      <if test="sort_order != null">SORT_ORDER       = #{sort_order},</if>
      <if test="is_active != null">IS_ACTIVE        = #{is_active},</if>
      UPDATED_BY   = #{updated_by},
      UPDATED_DATE = NOW()
    </set>
    WHERE WEB_TYPE = #{web_type}
  </update>

  <delete id="deleteWebType" parameterType="map">
    DELETE FROM WEB_TYPE_STANDARDS
    WHERE WEB_TYPE = #{web_type}
  </delete>

  <!-- 정렬 순서 일괄 업데이트용 단건 UPDATE -->
  <update id="updateSortOrder" parameterType="map">
    UPDATE WEB_TYPE_STANDARDS
    SET SORT_ORDER   = #{sort_order},
        UPDATED_BY   = #{user_id},
        UPDATED_DATE = NOW()
    WHERE WEB_TYPE = #{web_type}
  </update>

</mapper>
