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

    <!-- ── dynamic_form_data ──────────────────────────────────────────────── -->

    <select id="selectFormData" parameterType="map" resultType="map">
        SELECT
            ID
            , SCREEN_ID
            , TABLE_NAME
            , FORM_DATA::TEXT AS FORM_DATA
            , CREATED_DATE
            , UPDATED_DATE
            , CREATED_BY
            , UPDATED_BY
        FROM DYNAMIC_FORM_DATA
        WHERE ID = #{id}
    </select>

    <select id="selectFormDataList" parameterType="map" resultType="map">
        SELECT
            ID
            , SCREEN_ID
            , TABLE_NAME
            , FORM_DATA::TEXT AS FORM_DATA
            , CREATED_DATE
            , UPDATED_DATE
            , CREATED_BY
            , UPDATED_BY
        FROM DYNAMIC_FORM_DATA
        WHERE SCREEN_ID = #{screen_id}
        <if test="search != null and search != ''">
            AND (FORM_DATA::TEXT ILIKE #{search} OR TABLE_NAME ILIKE #{search})
        </if>
        ORDER BY
        <choose>
            <when test='sort_by == "UPDATED_DATE"'>UPDATED_DATE</when>
            <when test='sort_by == "id"'>ID</when>
            <otherwise>CREATED_DATE</otherwise>
        </choose>
        <choose>
            <when test='sort_order == "asc"'>ASC</when>
            <otherwise>DESC</otherwise>
        </choose>
        LIMIT #{size} OFFSET #{offset}
    </select>

    <select id="countFormDataList" parameterType="map" resultType="int">
        SELECT
            COUNT(*)
        FROM DYNAMIC_FORM_DATA
        WHERE SCREEN_ID = #{screen_id}
        <if test="search != null and search != ''">
            AND (FORM_DATA::TEXT ILIKE #{search} OR TABLE_NAME ILIKE #{search})
        </if>
    </select>

    <!-- ── information_schema ─────────────────────────────────────────────── -->

    <select id="selectTableColumns" parameterType="map" resultType="map">
        SELECT
            COLUMN_NAME
            , DATA_TYPE
            , IS_NULLABLE
            , COLUMN_DEFAULT
            , CHARACTER_MAXIMUM_LENGTH
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = #{table_name}
          AND TABLE_SCHEMA = 'public'
        ORDER BY ORDINAL_POSITION
    </select>

    <select id="selectPrimaryKeys" parameterType="map" resultType="map">
        SELECT
            KCU.COLUMN_NAME
        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
        JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
          ON TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
        WHERE TC.TABLE_NAME = #{table_name}
          AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
          AND TC.TABLE_SCHEMA = 'public'
    </select>

    <select id="selectColumnNames" parameterType="map" resultType="map">
        SELECT
            COLUMN_NAME
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = #{table_name}
          AND TABLE_SCHEMA = 'public'
    </select>

    <select id="selectColumnTypes" parameterType="map" resultType="map">
        SELECT
            COLUMN_NAME
            , DATA_TYPE
        FROM INFORMATION_SCHEMA.COLUMNS
        WHERE TABLE_NAME = #{table_name}
          AND TABLE_SCHEMA = 'public'
    </select>

    <select id="selectTableType" parameterType="map" resultType="map">
        SELECT
            TABLE_TYPE
        FROM INFORMATION_SCHEMA.TABLES
        WHERE TABLE_NAME = #{table_name}
          AND TABLE_SCHEMA = 'public'
        LIMIT 1
    </select>

    <select id="selectViewDefinition" parameterType="map" resultType="map">
        SELECT
            VIEW_DEFINITION
        FROM INFORMATION_SCHEMA.VIEWS
        WHERE TABLE_NAME = #{table_name}
          AND TABLE_SCHEMA = 'public'
        LIMIT 1
    </select>

    <select id="selectPrimaryKeyWithType" parameterType="map" resultType="map">
        SELECT
            KCU.COLUMN_NAME
            , C.DATA_TYPE
        FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
        JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU
          ON TC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME
        JOIN INFORMATION_SCHEMA.COLUMNS C
          ON KCU.COLUMN_NAME = C.COLUMN_NAME
         AND KCU.TABLE_NAME = C.TABLE_NAME
         AND C.TABLE_SCHEMA = 'public'
        WHERE TC.TABLE_NAME = #{table_name}
          AND TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
          AND TC.TABLE_SCHEMA = 'public'
        LIMIT 1
    </select>

    <!-- ── vehicle_location_history ───────────────────────────────────────── -->

    <insert id="insertLocationHistory" parameterType="map"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        INSERT INTO VEHICLE_LOCATION_HISTORY (
            USER_ID
            , COMPANY_CODE
            , LATITUDE
            , LONGITUDE
            , ACCURACY
            , ALTITUDE
            , SPEED
            , HEADING
            , TRIP_ID
            , TRIP_STATUS
            , DEPARTURE
            , ARRIVAL
            , DEPARTURE_NAME
            , DESTINATION_NAME
            , RECORDED_AT
            , VEHICLE_ID
        ) VALUES (
            #{user_id}
            , #{company_code}
            , #{latitude}
            , #{longitude}
            , #{accuracy}
            , #{altitude}
            , #{speed}
            , #{heading}
            , #{trip_id}
            , #{trip_status}
            , #{departure}
            , #{arrival}
            , #{departure_name}
            , #{destination_name}
            , #{recorded_at}
            , #{vehicle_id}
        )
    </insert>

    <select id="selectLocationHistory" parameterType="map" resultType="map">
        SELECT
            ID
            , USER_ID
            , VEHICLE_ID
            , LATITUDE
            , LONGITUDE
            , ACCURACY
            , ALTITUDE
            , SPEED
            , HEADING
            , TRIP_ID
            , TRIP_STATUS
            , DEPARTURE
            , ARRIVAL
            , DEPARTURE_NAME
            , DESTINATION_NAME
            , RECORDED_AT
            , CREATED_DATE
            , COMPANY_CODE
        FROM VEHICLE_LOCATION_HISTORY
        <where>
            <if test='company_code != null and company_code != "*"'>
                AND (COMPANY_CODE = #{company_code} OR COMPANY_CODE = '*')
            </if>
            <if test="trip_id != null">
                AND TRIP_ID = #{trip_id}
            </if>
            <if test="user_id != null">
                AND USER_ID = #{user_id}
            </if>
            <if test="start_date != null">
                AND RECORDED_AT &gt;= #{start_date}
            </if>
            <if test="end_date != null">
                AND RECORDED_AT &lt;= #{end_date}
            </if>
        </where>
        ORDER BY RECORDED_AT ASC
        LIMIT #{limit}
    </select>

</mapper>
