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

  <select id="findExcelMappingByHash" parameterType="map" resultType="map">
    SELECT
        ID
        , TABLE_NAME
        , EXCEL_COLUMNS::TEXT AS EXCEL_COLUMNS
        , EXCEL_COLUMNS_HASH
        , COLUMN_MAPPINGS::TEXT AS COLUMN_MAPPINGS
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    FROM EXCEL_MAPPING_TEMPLATE
    WHERE TABLE_NAME = #{table_name}
    AND EXCEL_COLUMNS_HASH = #{excel_columns_hash}
    ORDER BY UPDATED_DATE DESC
    LIMIT 1
  </select>

  <select id="findExcelMappingByHashAndCompany" parameterType="map" resultType="map">
    SELECT
        ID
        , TABLE_NAME
        , EXCEL_COLUMNS::TEXT AS EXCEL_COLUMNS
        , EXCEL_COLUMNS_HASH
        , COLUMN_MAPPINGS::TEXT AS COLUMN_MAPPINGS
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    FROM EXCEL_MAPPING_TEMPLATE
    WHERE TABLE_NAME = #{table_name}
    AND EXCEL_COLUMNS_HASH = #{excel_columns_hash}
    AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
    ORDER BY
        CASE WHEN COMPANY_CODE = #{company_code} THEN 0 ELSE 1 END
        , UPDATED_DATE DESC
    LIMIT 1
  </select>

  <select id="getExcelMappingListByTable" parameterType="map" resultType="map">
    SELECT
        ID
        , TABLE_NAME
        , EXCEL_COLUMNS::TEXT AS EXCEL_COLUMNS
        , EXCEL_COLUMNS_HASH
        , COLUMN_MAPPINGS::TEXT AS COLUMN_MAPPINGS
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    FROM EXCEL_MAPPING_TEMPLATE
    WHERE TABLE_NAME = #{table_name}
    ORDER BY UPDATED_DATE DESC
  </select>

  <select id="getExcelMappingListByTableAndCompany" parameterType="map" resultType="map">
    SELECT
        ID
        , TABLE_NAME
        , EXCEL_COLUMNS::TEXT AS EXCEL_COLUMNS
        , EXCEL_COLUMNS_HASH
        , COLUMN_MAPPINGS::TEXT AS COLUMN_MAPPINGS
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    FROM EXCEL_MAPPING_TEMPLATE
    WHERE TABLE_NAME = #{table_name}
    AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
    ORDER BY UPDATED_DATE DESC
  </select>

  <insert id="upsertExcelMapping" parameterType="map">
    INSERT INTO EXCEL_MAPPING_TEMPLATE (
        TABLE_NAME
        , EXCEL_COLUMNS
        , EXCEL_COLUMNS_HASH
        , COLUMN_MAPPINGS
        , COMPANY_CODE
        , CREATED_DATE
        , UPDATED_DATE
    ) VALUES (
        #{table_name}
        , #{excel_columns}::jsonb
        , #{excel_columns_hash}
        , #{column_mappings}::jsonb
        , #{company_code}
        , NOW()
        , NOW()
    )
    ON CONFLICT (TABLE_NAME, EXCEL_COLUMNS_HASH, COMPANY_CODE)
    DO UPDATE SET
        COLUMN_MAPPINGS = EXCLUDED.COLUMN_MAPPINGS
        , UPDATED_DATE = NOW()
  </insert>

  <delete id="deleteExcelMapping" parameterType="map">
    DELETE FROM EXCEL_MAPPING_TEMPLATE
    WHERE ID = #{id}
  </delete>

  <delete id="deleteExcelMappingByCompany" parameterType="map">
    DELETE FROM EXCEL_MAPPING_TEMPLATE
    WHERE ID = #{id}
    AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
  </delete>

</mapper>
