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

  <!-- ========== 검색 조건 ========== -->
  <sql id="barcodeLabelSearchCondition">
    <if test="search_text != null and search_text != ''">
      AND (LABEL_NAME_KOR ILIKE '%' || #{search_text} || '%'
           OR LABEL_NAME_ENG ILIKE '%' || #{search_text} || '%')
    </if>
    <if test="use_yn != null and use_yn != ''">
      AND USE_YN = #{use_yn}
    </if>
  </sql>

  <!-- ========== 라벨 목록 ========== -->
  <select id="getBarcodeLabelList" parameterType="map" resultType="map">
    SELECT
        LABEL_ID
        , LABEL_NAME_KOR
        , LABEL_NAME_ENG
        , DESCRIPTION
        , WIDTH_MM
        , HEIGHT_MM
        , USE_YN
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM BARCODE_LABELS
    WHERE 1=1
    <include refid="barcodeLabelSearchCondition"/>
    <include refid="common.dynamicOrderBy"/>
    <include refid="common.pagination"/>
  </select>

  <select id="getBarcodeLabelListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM BARCODE_LABELS
    WHERE 1=1
    <include refid="barcodeLabelSearchCondition"/>
  </select>

  <!-- ========== 라벨 단건 조회 ========== -->
  <select id="getBarcodeLabelInfo" parameterType="map" resultType="map">
    SELECT
        LABEL_ID
        , LABEL_NAME_KOR
        , LABEL_NAME_ENG
        , DESCRIPTION
        , WIDTH_MM
        , HEIGHT_MM
        , LAYOUT_JSON
        , USE_YN
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM BARCODE_LABELS
    WHERE LABEL_ID = #{label_id}
  </select>

  <!-- ========== 라벨 생성 ========== -->
  <insert id="insertBarcodeLabel" parameterType="map">
    INSERT INTO BARCODE_LABELS (
        LABEL_ID
        , LABEL_NAME_KOR
        , LABEL_NAME_ENG
        , DESCRIPTION
        , WIDTH_MM
        , HEIGHT_MM
        , LAYOUT_JSON
        , USE_YN
        , CREATED_BY
        , CREATED_DATE
    ) VALUES (
        #{label_id}
        , #{label_name_kor}
        , #{label_name_eng}
        , #{description}
        , #{width_mm}
        , #{height_mm}
        , #{layout_json}
        , 'Y'
        , #{created_by}
        , NOW()
    )
  </insert>

  <!-- ========== 라벨 수정 ========== -->
  <update id="updateBarcodeLabel" parameterType="map">
    UPDATE BARCODE_LABELS
    <set>
      <if test="label_name_kor != null">LABEL_NAME_KOR = #{label_name_kor},</if>
      <if test="label_name_eng != null">LABEL_NAME_ENG = #{label_name_eng},</if>
      <if test="description != null">DESCRIPTION = #{description},</if>
      <if test="use_yn != null">USE_YN = #{use_yn},</if>
      UPDATED_DATE = NOW()
      <if test="updated_by != null">, UPDATED_BY = #{updated_by}</if>
    </set>
    WHERE LABEL_ID = #{label_id}
  </update>

  <!-- ========== 라벨 삭제 ========== -->
  <delete id="deleteBarcodeLabel" parameterType="map">
    DELETE FROM BARCODE_LABELS
    WHERE LABEL_ID = #{label_id}
  </delete>

  <!-- ========== 레이아웃 저장 ========== -->
  <update id="updateBarcodeLabelLayout" parameterType="map">
    UPDATE BARCODE_LABELS
    SET
        WIDTH_MM = #{width_mm}
        , HEIGHT_MM = #{height_mm}
        , LAYOUT_JSON = #{layout_json}
        , UPDATED_DATE = NOW()
        <if test="updated_by != null">, UPDATED_BY = #{updated_by}</if>
    WHERE LABEL_ID = #{label_id}
  </update>

  <!-- ========== 라벨 복사 ========== -->
  <insert id="copyBarcodeLabel" parameterType="map">
    INSERT INTO BARCODE_LABELS (
        LABEL_ID
        , LABEL_NAME_KOR
        , LABEL_NAME_ENG
        , DESCRIPTION
        , WIDTH_MM
        , HEIGHT_MM
        , LAYOUT_JSON
        , USE_YN
        , CREATED_BY
        , CREATED_DATE
    )
    SELECT
        #{new_label_id}
        , LABEL_NAME_KOR || ' (복사)'
        , LABEL_NAME_ENG
        , DESCRIPTION
        , WIDTH_MM
        , HEIGHT_MM
        , LAYOUT_JSON
        , 'Y'
        , #{created_by}
        , NOW()
    FROM BARCODE_LABELS
    WHERE LABEL_ID = #{label_id}
  </insert>

  <!-- ========== 템플릿 목록 ========== -->
  <select id="getBarcodeLabelTemplateList" parameterType="map" resultType="map">
    SELECT
        TEMPLATE_ID
        , TEMPLATE_NAME_KOR
        , TEMPLATE_NAME_ENG
        , WIDTH_MM
        , HEIGHT_MM
        , LAYOUT_JSON
        , SORT_ORDER
    FROM BARCODE_LABEL_TEMPLATES
    ORDER BY SORT_ORDER ASC, TEMPLATE_ID ASC
  </select>

  <!-- ========== 템플릿 단건 조회 ========== -->
  <select id="getBarcodeLabelTemplateInfo" parameterType="map" resultType="map">
    SELECT
        TEMPLATE_ID
        , TEMPLATE_NAME_KOR
        , TEMPLATE_NAME_ENG
        , WIDTH_MM
        , HEIGHT_MM
        , LAYOUT_JSON
        , SORT_ORDER
    FROM BARCODE_LABEL_TEMPLATES
    WHERE TEMPLATE_ID = #{template_id}
  </select>

</mapper>
