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

  <!-- ===== Report Master ===== -->

  <sql id="reportSearchCondition">
    <if test="keyword != null and keyword != ''">
      AND (REPORT_NAME_KOR ILIKE '%' || #{keyword} || '%'
        OR REPORT_NAME_ENG ILIKE '%' || #{keyword} || '%')
    </if>
    <if test="report_type != null and report_type != ''">
      AND REPORT_TYPE = #{report_type}
    </if>
    <if test="use_yn != null and use_yn != ''">
      AND USE_YN = #{use_yn}
    </if>
  </sql>

  <select id="getReportList" parameterType="map" resultType="map">
    SELECT
        REPORT_ID
        , REPORT_NAME_KOR
        , REPORT_NAME_ENG
        , TEMPLATE_ID
        , REPORT_TYPE
        , COMPANY_CODE
        , DESCRIPTION
        , USE_YN
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_MASTER
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <include refid="reportSearchCondition"/>
    <include refid="common.dynamicOrderBy"/>
    <include refid="common.pagination"/>
  </select>

  <select id="getReportListCnt" parameterType="map" resultType="int">
    SELECT COUNT(*)
    FROM REPORT_MASTER
    WHERE 1=1
    <include refid="common.companyCodeFilter"/>
    <include refid="reportSearchCondition"/>
  </select>

  <select id="getReportInfo" parameterType="map" resultType="map">
    SELECT
        REPORT_ID
        , REPORT_NAME_KOR
        , REPORT_NAME_ENG
        , TEMPLATE_ID
        , REPORT_TYPE
        , COMPANY_CODE
        , DESCRIPTION
        , USE_YN
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_MASTER
    WHERE REPORT_ID = #{report_id}
    <include refid="common.companyCodeFilter"/>
  </select>

  <insert id="insertReport" parameterType="map">
    INSERT INTO REPORT_MASTER (
        REPORT_ID
        , REPORT_NAME_KOR
        , REPORT_NAME_ENG
        , TEMPLATE_ID
        , REPORT_TYPE
        , COMPANY_CODE
        , DESCRIPTION
        , USE_YN
        , CREATED_BY
    ) VALUES (
        #{report_id}
        , #{report_name_kor}
        , #{report_name_eng, jdbcType=VARCHAR}
        , #{template_id, jdbcType=VARCHAR}
        , #{report_type}
        , #{company_code}
        , #{description, jdbcType=VARCHAR}
        , 'Y'
        , #{created_by}
    )
  </insert>

  <update id="updateReport" parameterType="map">
    UPDATE REPORT_MASTER
    SET
        UPDATED_DATE = NOW()
        <if test="report_name_kor != null">, REPORT_NAME_KOR = #{report_name_kor}</if>
        <if test="report_name_eng != null">, REPORT_NAME_ENG = #{report_name_eng}</if>
        <if test="report_type != null">, REPORT_TYPE = #{report_type}</if>
        <if test="description != null">, DESCRIPTION = #{description}</if>
        <if test="use_yn != null">, USE_YN = #{use_yn}</if>
        <if test="updated_by != null">, UPDATED_BY = #{updated_by}</if>
    WHERE REPORT_ID = #{report_id}
    <include refid="common.companyCodeFilter"/>
  </update>

  <delete id="deleteReport" parameterType="map">
    DELETE FROM REPORT_MASTER
    WHERE REPORT_ID = #{report_id}
    <include refid="common.companyCodeFilter"/>
  </delete>

  <!-- ===== Report Layout ===== -->

  <select id="getReportLayout" parameterType="map" resultType="map">
    SELECT
        LAYOUT_ID
        , REPORT_ID
        , CANVAS_WIDTH
        , CANVAS_HEIGHT
        , PAGE_ORIENTATION
        , MARGIN_TOP
        , MARGIN_BOTTOM
        , MARGIN_LEFT
        , MARGIN_RIGHT
        , COMPONENTS::TEXT AS COMPONENTS
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_LAYOUT
    WHERE REPORT_ID = #{report_id}
  </select>

  <insert id="insertReportLayout" parameterType="map">
    INSERT INTO REPORT_LAYOUT (
        LAYOUT_ID
        , REPORT_ID
        , CANVAS_WIDTH
        , CANVAS_HEIGHT
        , PAGE_ORIENTATION
        , MARGIN_TOP
        , MARGIN_BOTTOM
        , MARGIN_LEFT
        , MARGIN_RIGHT
        , COMPONENTS
        , CREATED_BY
    ) VALUES (
        #{layout_id}
        , #{report_id}
        , #{canvas_width, jdbcType=INTEGER}
        , #{canvas_height, jdbcType=INTEGER}
        , #{page_orientation, jdbcType=VARCHAR}
        , #{margin_top, jdbcType=NUMERIC}
        , #{margin_bottom, jdbcType=NUMERIC}
        , #{margin_left, jdbcType=NUMERIC}
        , #{margin_right, jdbcType=NUMERIC}
        , #{components}::jsonb
        , #{created_by}
    )
  </insert>

  <update id="updateReportLayout" parameterType="map">
    UPDATE REPORT_LAYOUT
    SET
        CANVAS_WIDTH = #{canvas_width, jdbcType=INTEGER}
        , CANVAS_HEIGHT = #{canvas_height, jdbcType=INTEGER}
        , PAGE_ORIENTATION = #{page_orientation, jdbcType=VARCHAR}
        , MARGIN_TOP = #{margin_top, jdbcType=NUMERIC}
        , MARGIN_BOTTOM = #{margin_bottom, jdbcType=NUMERIC}
        , MARGIN_LEFT = #{margin_left, jdbcType=NUMERIC}
        , MARGIN_RIGHT = #{margin_right, jdbcType=NUMERIC}
        , COMPONENTS = #{components}::jsonb
        , UPDATED_DATE = NOW()
        , UPDATED_BY = #{updated_by, jdbcType=VARCHAR}
    WHERE REPORT_ID = #{report_id}
  </update>

  <delete id="deleteReportLayout" parameterType="map">
    DELETE FROM REPORT_LAYOUT
    WHERE REPORT_ID = #{report_id}
  </delete>

  <!-- ===== Report Query ===== -->

  <select id="getReportQueryList" parameterType="map" resultType="map">
    SELECT
        QUERY_ID
        , REPORT_ID
        , QUERY_NAME
        , QUERY_TYPE
        , SQL_QUERY
        , PARAMETERS::TEXT AS PARAMETERS
        , EXTERNAL_CONNECTION_ID
        , DISPLAY_ORDER
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_QUERY
    WHERE REPORT_ID = #{report_id}
    ORDER BY DISPLAY_ORDER ASC, CREATED_DATE ASC
  </select>

  <select id="getReportQueryById" parameterType="map" resultType="map">
    SELECT
        QUERY_ID
        , REPORT_ID
        , QUERY_NAME
        , QUERY_TYPE
        , SQL_QUERY
        , PARAMETERS::TEXT AS PARAMETERS
        , EXTERNAL_CONNECTION_ID
        , DISPLAY_ORDER
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_QUERY
    WHERE QUERY_ID = #{query_id}
    AND REPORT_ID = #{report_id}
  </select>

  <insert id="insertReportQuery" parameterType="map">
    INSERT INTO REPORT_QUERY (
        QUERY_ID
        , REPORT_ID
        , QUERY_NAME
        , QUERY_TYPE
        , SQL_QUERY
        , PARAMETERS
        , EXTERNAL_CONNECTION_ID
        , DISPLAY_ORDER
        , CREATED_BY
    ) VALUES (
        #{query_id}
        , #{report_id}
        , #{query_name}
        , #{query_type}
        , #{sql_query}
        , #{parameters}::jsonb
        , #{external_connection_id, jdbcType=BIGINT}
        , #{display_order, jdbcType=INTEGER}
        , #{created_by}
    )
  </insert>

  <delete id="deleteReportQueriesByReportId" parameterType="map">
    DELETE FROM REPORT_QUERY
    WHERE REPORT_ID = #{report_id}
  </delete>

  <!-- ===== Report Template ===== -->

  <select id="getReportTemplateList" parameterType="map" resultType="map">
    SELECT
        TEMPLATE_ID
        , TEMPLATE_NAME_KOR
        , TEMPLATE_NAME_ENG
        , TEMPLATE_TYPE
        , IS_SYSTEM
        , THUMBNAIL_URL
        , DESCRIPTION
        , LAYOUT_CONFIG::TEXT AS LAYOUT_CONFIG
        , DEFAULT_QUERIES::TEXT AS DEFAULT_QUERIES
        , USE_YN
        , SORT_ORDER
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_TEMPLATE
    WHERE USE_YN = 'Y'
    ORDER BY IS_SYSTEM DESC, SORT_ORDER ASC
  </select>

  <select id="getReportTemplateById" parameterType="map" resultType="map">
    SELECT
        TEMPLATE_ID
        , TEMPLATE_NAME_KOR
        , TEMPLATE_NAME_ENG
        , TEMPLATE_TYPE
        , IS_SYSTEM
        , THUMBNAIL_URL
        , DESCRIPTION
        , LAYOUT_CONFIG::TEXT AS LAYOUT_CONFIG
        , DEFAULT_QUERIES::TEXT AS DEFAULT_QUERIES
        , USE_YN
        , SORT_ORDER
        , CREATED_DATE
        , CREATED_BY
        , UPDATED_DATE
        , UPDATED_BY
    FROM REPORT_TEMPLATE
    WHERE TEMPLATE_ID = #{template_id}
  </select>

  <insert id="insertReportTemplate" parameterType="map">
    INSERT INTO REPORT_TEMPLATE (
        TEMPLATE_ID
        , TEMPLATE_NAME_KOR
        , TEMPLATE_NAME_ENG
        , TEMPLATE_TYPE
        , IS_SYSTEM
        , DESCRIPTION
        , LAYOUT_CONFIG
        , DEFAULT_QUERIES
        , USE_YN
        , SORT_ORDER
        , CREATED_BY
    ) VALUES (
        #{template_id}
        , #{template_name_kor}
        , #{template_name_eng, jdbcType=VARCHAR}
        , #{template_type}
        , 'N'
        , #{description, jdbcType=VARCHAR}
        , #{layout_config}::jsonb
        , #{default_queries}::jsonb
        , 'Y'
        , COALESCE(#{sort_order, jdbcType=INTEGER}, 999)
        , #{created_by}
    )
  </insert>

  <delete id="deleteReportTemplate" parameterType="map">
    DELETE FROM REPORT_TEMPLATE
    WHERE TEMPLATE_ID = #{template_id}
    AND IS_SYSTEM = 'N'
  </delete>

  <!-- ===== Report Menu Mapping ===== -->

  <select id="getReportMenuMappings" parameterType="map" resultType="map">
    SELECT
        REPORT_ID
        , MENU_OBJID
        , COMPANY_CODE
        , CREATED_DATE
        , CREATED_BY
    FROM REPORT_MENU_MAPPING
    WHERE REPORT_ID = #{report_id}
    ORDER BY CREATED_DATE ASC
  </select>

  <insert id="insertReportMenuMapping" parameterType="map">
    INSERT INTO REPORT_MENU_MAPPING (
        REPORT_ID
        , MENU_OBJID
        , COMPANY_CODE
        , CREATED_BY
    ) VALUES (
        #{report_id}
        , #{menu_objid}
        , #{company_code}
        , #{created_by}
    )
  </insert>

  <delete id="deleteReportMenuMappingsByReportId" parameterType="map">
    DELETE FROM REPORT_MENU_MAPPING
    WHERE REPORT_ID = #{report_id}
  </delete>

</mapper>
