<?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="go.kr.project.carInspectionPenalty.history.mapper.VehicleApiHistoryMapper">

    <!-- ==================== 자동차 기본정보 조회 이력 ==================== -->

    <!-- 자동차 기본정보 조회 이력 목록 조회 -->
    <select id="selectCarBassMatterInqireList" parameterType="VehicleApiHistorySearchVO" resultType="CarBassMatterInqireVO">
        SELECT
            t.CAR_BASS_MATTER_INQIRE_ID,
            t.INFO_SYS_ID,
            t.INFO_SYS_IP,
            t.SIGUNGU_CODE,
            t.CNTC_INFO_CODE,
            t.CHARGER_ID,
            t.CHARGER_IP,
            t.CHARGER_NM,
            t.DMND_LEVY_STDDE,
            t.DMND_INQIRE_SE_CODE,
            t.DMND_VHRNO,
            t.DMND_VIN,
            t.CNTC_RESULT_CODE,
            t.CNTC_RESULT_DTLS,
            t.VHRNO,
            t.VIN,
            t.CNM,
            t.MBER_NM,
            t.REG_DT,
            t.RGTR,
            u.USER_NM AS rgtrNm
        FROM tb_car_bass_matter_inqire t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        WHERE 1=1
        <if test='searchStartDate != null and searchStartDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[>=]]> #{searchStartDate}
        </if>
        <if test='searchEndDate != null and searchEndDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[<=]]> #{searchEndDate}
        </if>
        <if test='vhrno != null and vhrno != ""'>
            AND (t.DMND_VHRNO LIKE CONCAT('%', #{vhrno}, '%') OR t.VHRNO LIKE CONCAT('%', #{vhrno}, '%'))
        </if>
        <if test='vin != null and vin != ""'>
            AND (t.DMND_VIN LIKE CONCAT('%', #{vin}, '%') OR t.VIN LIKE CONCAT('%', #{vin}, '%'))
        </if>
        <if test='chargerNm != null and chargerNm != ""'>
            AND t.CHARGER_NM LIKE CONCAT('%', #{chargerNm}, '%')
        </if>
        <if test='cntcResultCode != null and cntcResultCode != ""'>
            AND t.CNTC_RESULT_CODE = #{cntcResultCode}
        </if>
        ORDER BY t.REG_DT DESC
        <if test='pagingYn == "Y"'>
            LIMIT #{startIndex}, #{perPage}
        </if>
    </select>

    <!-- 자동차 기본정보 조회 이력 총 개수 조회 -->
    <select id="selectCarBassMatterInqireListTotalCount" parameterType="VehicleApiHistorySearchVO" resultType="int">
        SELECT COUNT(*)
        FROM tb_car_bass_matter_inqire t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        WHERE 1=1
        <if test='searchStartDate != null and searchStartDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[>=]]> #{searchStartDate}
        </if>
        <if test='searchEndDate != null and searchEndDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[<=]]> #{searchEndDate}
        </if>
        <if test='vhrno != null and vhrno != ""'>
            AND (t.DMND_VHRNO LIKE CONCAT('%', #{vhrno}, '%') OR t.VHRNO LIKE CONCAT('%', #{vhrno}, '%'))
        </if>
        <if test='vin != null and vin != ""'>
            AND (t.DMND_VIN LIKE CONCAT('%', #{vin}, '%') OR t.VIN LIKE CONCAT('%', #{vin}, '%'))
        </if>
        <if test='chargerNm != null and chargerNm != ""'>
            AND t.CHARGER_NM LIKE CONCAT('%', #{chargerNm}, '%')
        </if>
        <if test='cntcResultCode != null and cntcResultCode != ""'>
            AND t.CNTC_RESULT_CODE = #{cntcResultCode}
        </if>
    </select>

    <!-- 자동차 기본정보 조회 이력 상세 조회 -->
    <select id="selectCarBassMatterInqireOne" parameterType="String" resultType="CarBassMatterInqireVO">
        SELECT *
        FROM tb_car_bass_matter_inqire
        WHERE CAR_BASS_MATTER_INQIRE_ID = #{carBassMatterInqireId}
    </select>

    <!-- ==================== 자동차 등록원부(갑) 조회 이력 ==================== -->

    <!-- 자동차 등록원부(갑) 조회 이력 목록 조회 -->
    <select id="selectCarLedgerFrmbkList" parameterType="VehicleApiHistorySearchVO" resultType="CarLedgerFrmbkVO">
        SELECT
            t.CAR_LEDGER_FRMBK_ID,
            t.INFO_SYS_ID,
            t.INFO_SYS_IP,
            t.SIGUNGU_CODE,
            t.CNTC_INFO_CODE,
            t.CHARGER_ID,
            t.CHARGER_IP,
            t.CHARGER_NM,
            t.DMND_VHRNO,
            t.CNTC_RESULT_CODE,
            t.CNTC_RESULT_DTLS,
            t.VHRNO,
            t.VIN,
            t.CNM,
            t.MBER_NM,
            t.REG_DT,
            t.RGTR,
            u.USER_NM AS rgtrNm
        FROM tb_car_ledger_frmbk t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        WHERE 1=1
        <if test='searchStartDate != null and searchStartDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[>=]]> #{searchStartDate}
        </if>
        <if test='searchEndDate != null and searchEndDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[<=]]> #{searchEndDate}
        </if>
        <if test='vhrno != null and vhrno != ""'>
            AND (t.DMND_VHRNO LIKE CONCAT('%', #{vhrno}, '%') OR t.VHRNO LIKE CONCAT('%', #{vhrno}, '%'))
        </if>
        <if test='vin != null and vin != ""'>
            AND t.VIN LIKE CONCAT('%', #{vin}, '%')
        </if>
        <if test='chargerNm != null and chargerNm != ""'>
            AND t.CHARGER_NM LIKE CONCAT('%', #{chargerNm}, '%')
        </if>
        <if test='cntcResultCode != null and cntcResultCode != ""'>
            AND t.CNTC_RESULT_CODE = #{cntcResultCode}
        </if>
        ORDER BY t.REG_DT DESC
        <if test='pagingYn == "Y"'>
            LIMIT #{startIndex}, #{perPage}
        </if>
    </select>

    <!-- 자동차 등록원부(갑) 조회 이력 총 개수 조회 -->
    <select id="selectCarLedgerFrmbkListTotalCount" parameterType="VehicleApiHistorySearchVO" resultType="int">
        SELECT COUNT(*)
        FROM tb_car_ledger_frmbk t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        WHERE 1=1
        <if test='searchStartDate != null and searchStartDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[>=]]> #{searchStartDate}
        </if>
        <if test='searchEndDate != null and searchEndDate != ""'>
            AND DATE_FORMAT(t.REG_DT, '%Y%m%d') <![CDATA[<=]]> #{searchEndDate}
        </if>
        <if test='vhrno != null and vhrno != ""'>
            AND (t.DMND_VHRNO LIKE CONCAT('%', #{vhrno}, '%') OR t.VHRNO LIKE CONCAT('%', #{vhrno}, '%'))
        </if>
        <if test='vin != null and vin != ""'>
            AND t.VIN LIKE CONCAT('%', #{vin}, '%')
        </if>
        <if test='chargerNm != null and chargerNm != ""'>
            AND t.CHARGER_NM LIKE CONCAT('%', #{chargerNm}, '%')
        </if>
        <if test='cntcResultCode != null and cntcResultCode != ""'>
            AND t.CNTC_RESULT_CODE = #{cntcResultCode}
        </if>
    </select>

    <!-- 자동차 등록원부(갑) 조회 이력 상세 조회 -->
    <select id="selectCarLedgerFrmbkOne" parameterType="String" resultType="CarLedgerFrmbkVO">
        SELECT *
        FROM tb_car_ledger_frmbk
        WHERE CAR_LEDGER_FRMBK_ID = #{carLedgerFrmbkId}
    </select>

    <!-- ==================== 자동차 등록원부(갑) 상세 조회 이력 ==================== -->

    <!-- 자동차 등록원부(갑) 상세 조회 이력 목록 조회 -->
    <select id="selectCarLedgerFrmbkDtlList" parameterType="String" resultType="CarLedgerFrmbkDtlVO">
        SELECT *
        FROM tb_car_ledger_frmbk_dtl
        WHERE CAR_LEDGER_FRMBK_ID = #{carLedgerFrmbkId}
        ORDER BY REG_DT DESC
    </select>

    <!-- 자동차 등록원부(갑) 상세 조회 이력 상세 조회 -->
    <select id="selectCarLedgerFrmbkDtlOne" parameterType="String" resultType="CarLedgerFrmbkDtlVO">
        SELECT *
        FROM tb_car_ledger_frmbk_dtl
        WHERE CAR_LEDGER_FRMBK_DTL_ID = #{carLedgerFrmbkDtlId}
    </select>

    <!-- ==================== 최신 이력 ID 조회 ==================== -->

    <!-- 차량번호로 최신 자동차 기본정보 조회 이력 ID 조회 -->
    <select id="selectLatestCarBassMatterInqireIdByVhclno" parameterType="String" resultType="String">
        SELECT CAR_BASS_MATTER_INQIRE_ID
        FROM tb_car_bass_matter_inqire
        WHERE DMND_VHRNO = #{vhclno} OR VHRNO = #{vhclno}
        ORDER BY REG_DT DESC
        LIMIT 1
    </select>

    <!-- 차량번호로 최신 자동차 등록원부(갑) 조회 이력 ID 조회 -->
    <select id="selectLatestCarLedgerFrmbkIdByVhclno" parameterType="String" resultType="String">
        SELECT CAR_LEDGER_FRMBK_ID
        FROM tb_car_ledger_frmbk
        WHERE DMND_VHRNO = #{vhclno} OR VHRNO = #{vhclno}
        ORDER BY REG_DT DESC
        LIMIT 1
    </select>

</mapper>
