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

  <!--
    node_flows 테이블
    컬럼: flow_id (PK, serial), flow_name, flow_description, flow_data (JSONB),
          company_code, CREATED_DATE, UPDATED_DATE

    snake_case 컬럼명 그대로 반환 (alias 없음)
  -->

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 목록 조회 (flow_data 제외)                             -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="getNodeFlowList" parameterType="map" resultType="map">
    SELECT
        FLOW_ID
        , FLOW_NAME
        , FLOW_DESCRIPTION
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    FROM NODE_FLOWS
    WHERE 1=1
    <if test="company_code != null and company_code != &quot;*&quot;">
      AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
    </if>
    ORDER BY UPDATED_DATE DESC
  </select>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 상세 조회 (flow_data 포함)                             -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="getNodeFlowInfo" parameterType="map" resultType="map">
    SELECT
        FLOW_ID
        , FLOW_NAME
        , FLOW_DESCRIPTION
        , FLOW_DATA
        , CREATED_DATE
        , UPDATED_DATE
    FROM NODE_FLOWS
    WHERE FLOW_ID = #{flow_id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- flow_data 만 조회 (소스 테이블 추출, 실행 전 로드)       -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="getNodeFlowData" parameterType="map" resultType="map">
    SELECT
        FLOW_ID
        , FLOW_NAME
        , FLOW_DATA
    FROM NODE_FLOWS
    WHERE FLOW_ID = #{flow_id}
  </select>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 감사 로그용 이름/설명 조회                              -->
  <!-- ────────────────────────────────────────────────────── -->
  <select id="getNodeFlowForAudit" parameterType="map" resultType="map">
    SELECT
        FLOW_NAME
        , FLOW_DESCRIPTION
        , COMPANY_CODE
    FROM NODE_FLOWS
    WHERE FLOW_ID = #{flow_id}
  </select>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 생성                                                   -->
  <!-- ────────────────────────────────────────────────────── -->
  <insert id="insertNodeFlow" parameterType="map"
          useGeneratedKeys="true" keyProperty="flow_id" keyColumn="flow_id">
    INSERT INTO NODE_FLOWS (
        FLOW_NAME
        , FLOW_DESCRIPTION
        , FLOW_DATA
        , COMPANY_CODE
    ) VALUES (
        #{flow_name}
        , #{flow_description}
        , #{flow_data}::JSONB
        , #{company_code}
    )
  </insert>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 수정                                                   -->
  <!-- ────────────────────────────────────────────────────── -->
  <update id="updateNodeFlow" parameterType="map">
    UPDATE NODE_FLOWS
    SET
        FLOW_NAME = #{flow_name}
        , FLOW_DESCRIPTION = #{flow_description}
        , FLOW_DATA = #{flow_data}::JSONB
        , UPDATED_DATE = NOW()
    WHERE FLOW_ID = #{flow_id}
  </update>

  <!-- ────────────────────────────────────────────────────── -->
  <!-- 삭제                                                   -->
  <!-- ────────────────────────────────────────────────────── -->
  <delete id="deleteNodeFlow" parameterType="map">
    DELETE FROM NODE_FLOWS
    WHERE FLOW_ID = #{flow_id}
  </delete>

</mapper>
