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

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 목록 조회                                              -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="selectList" parameterType="map" resultType="map">
    SELECT *
    FROM DATAFLOW_DIAGRAMS
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="search_term != null and search_term != ''">
      AND DIAGRAM_NAME ILIKE '%' || #{search_term} || '%'
    </if>
    ORDER BY UPDATED_DATE DESC
    <include refid="common.pagination"/>
  </select>

  <!-- 총 개수 -->
  <select id="countList" parameterType="map" resultType="int">
    SELECT
        COUNT(*)
    FROM DATAFLOW_DIAGRAMS
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <if test="search_term != null and search_term != ''">
      AND DIAGRAM_NAME ILIKE '%' || #{search_term} || '%'
    </if>
  </select>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 단건 조회                                              -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="selectById" parameterType="map" resultType="map">
    SELECT *
    FROM DATAFLOW_DIAGRAMS
    WHERE DIAGRAM_ID = #{diagram_id}
    <include refid="common.companyCodeFilter"/>
    LIMIT 1
  </select>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 생성                                                   -->
  <!-- ────────────────────────────────────────────────────── -->
  <insert id="insert" parameterType="map"
          useGeneratedKeys="true" keyProperty="diagram_id" keyColumn="diagram_id">
    INSERT INTO DATAFLOW_DIAGRAMS (
        DIAGRAM_NAME
        , RELATIONSHIPS
        , NODE_POSITIONS
        , CATEGORY
        , CONTROL
        , PLAN
        , COMPANY_CODE
        , CREATED_BY
        , UPDATED_BY
        , CREATED_DATE
        , UPDATED_DATE
    ) VALUES (
        #{diagram_name}
        , #{relationships}::JSONB
        , <choose>
            <when test="node_positions != null">#{node_positions}::JSONB</when>
            <otherwise>NULL</otherwise>
          </choose>
        , <choose>
            <when test="category != null">#{category}::JSONB</when>
            <otherwise>NULL</otherwise>
          </choose>
        , <choose>
            <when test="control != null">#{control}::JSONB</when>
            <otherwise>NULL</otherwise>
          </choose>
        , <choose>
            <when test="plan != null">#{plan}::JSONB</when>
            <otherwise>NULL</otherwise>
          </choose>
        , #{company_code}
        , #{created_by}
        , #{updated_by}
        , NOW()
        , NOW()
    )
  </insert>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 수정 (동적 SET)                                        -->
  <!--                                                        -->
  <!-- updateNodePositions / updateCategory / updateControl / -->
  <!-- updatePlan 플래그가 true 이면 해당 컬럼을 SET 에 포함  -->
  <!-- (플래그 없으면 = 프론트에서 보내지 않은 필드 → 유지)  -->
  <!-- ────────────────────────────────────────────────────── -->
  <update id="update" parameterType="map">
    UPDATE DATAFLOW_DIAGRAMS
    <set>
      UPDATED_BY = #{updated_by},
      UPDATED_DATE = NOW(),
      <if test="diagram_name != null">DIAGRAM_NAME = #{diagram_name},</if>
      <if test="relationships != null">RELATIONSHIPS = #{relationships}::JSONB,</if>
      <if test="update_node_positions == true">
        NODE_POSITIONS = <choose>
          <when test="node_positions != null">#{node_positions}::JSONB</when>
          <otherwise>NULL</otherwise>
        </choose>,
      </if>
      <if test="update_category == true">
        CATEGORY = <choose>
          <when test="category != null">#{category}::JSONB</when>
          <otherwise>NULL</otherwise>
        </choose>,
      </if>
      <if test="update_control == true">
        CONTROL = <choose>
          <when test="control != null">#{control}::JSONB</when>
          <otherwise>NULL</otherwise>
        </choose>,
      </if>
      <if test="update_plan == true">
        PLAN = <choose>
          <when test="plan != null">#{plan}::JSONB</when>
          <otherwise>NULL</otherwise>
        </choose>,
      </if>
    </set>
    WHERE DIAGRAM_ID = #{diagram_id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 삭제                                                   -->
  <!-- ────────────────────────────────────────────────────── -->
  <delete id="delete" parameterType="map">
    DELETE FROM DATAFLOW_DIAGRAMS
    WHERE DIAGRAM_ID = #{diagram_id}
    <include refid="common.companyCodeFilter"/>
  </delete>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 복제 이름 생성용: 패턴 일치 이름 조회                  -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="selectNamesLike" parameterType="map" resultType="map">
    SELECT
        DIAGRAM_NAME
    FROM DATAFLOW_DIAGRAMS
    WHERE DIAGRAM_NAME LIKE #{pattern}
    <include refid="common.companyCodeFilter"/>
  </select>

</mapper>
