<?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.registration.mapper.CarFfnlgTrgtMapper">

    <!-- 공통 검색 조건 -->
    <sql id="searchCondition">
        <if test='schRcptYmdStart != null and schRcptYmdStart != ""'>
            AND t.RCPT_YMD &gt;= #{schRcptYmdStart}
        </if>
        <if test='schRcptYmdEnd != null and schRcptYmdEnd != ""'>
            AND t.RCPT_YMD &lt;= #{schRcptYmdEnd}
        </if>
        <if test='schVhclno != null and schVhclno != ""'>
            AND t.VHCLNO LIKE CONCAT('%', #{schVhclno}, '%')
        </if>
        <if test='schOwnrNm != null and schOwnrNm != ""'>
            AND t.OWNR_NM LIKE CONCAT('%', #{schOwnrNm}, '%')
        </if>
        <if test='schTaskPrcsSttsCd != null and schTaskPrcsSttsCd != ""'>
            AND t.TASK_PRCS_STTS_CD = #{schTaskPrcsSttsCd}
        </if>
        <if test='schInspYmdStart != null and schInspYmdStart != ""'>
            AND t.INSP_YMD &gt;= #{schInspYmdStart}
        </if>
        <if test='schInspYmdEnd != null and schInspYmdEnd != ""'>
            AND t.INSP_YMD &lt;= #{schInspYmdEnd}
        </if>
    </sql>

    <!-- 과태료 대상 목록 총 개수 조회 -->
    <select id="selectListTotalCount" parameterType="CarFfnlgTrgtVO" resultType="int">
        SELECT COUNT(*)
        FROM tb_car_ffnlg_trgt t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        LEFT JOIN (
            SELECT CD_ID, CD_NM
            FROM tb_cd_detail
            WHERE CD_GROUP_ID = 'TASK_PRCS_STTS_CD'
              AND USE_YN = 'Y'
        ) cd ON t.TASK_PRCS_STTS_CD = cd.CD_ID
        WHERE t.DEL_DT IS NULL
        <include refid="searchCondition"/>
    </select>
    
    <!-- 과태료 대상 목록 조회 -->
    <select id="selectList" parameterType="CarFfnlgTrgtVO" resultType="CarFfnlgTrgtVO">
        SELECT
            t.CAR_FFNLG_TRGT_ID AS carFfnlgTrgtId,
            t.RCPT_YMD AS rcptYmd,
            t.FFNLG_TRGT_SE_CD AS ffnlgTrgtSeCd,
            t.INSPSTN_CD AS inspstnCd,
            t.INSP_YMD AS inspYmd,
            t.VHCLNO AS vhclno,
            t.OWNR_NM AS ownrNm,
            ECL_DECRYPT(t.RRNO) AS rrno,
            t.CAR_NM AS carNm,
            t.CAR_KND AS carKnd,
            t.CAR_USG AS carUsg,
            t.INSP_END_YMD AS inspEndYmd,
            t.DAYCNT AS daycnt,
            t.FFNLG_AMT AS ffnlgAmt,
            t.LAST_REG_YMD AS lastRegYmd,
            t.ADDR AS addr,
            t.VLD_PRD_EXPRY_YMD AS vldPrdExpryYmd,
            t.TRD_GDS AS trdGds,
            t.TASK_PRCS_STTS_CD AS taskPrcsSttsCd,
            t.TASK_PRCS_YMD AS taskPrcsYmd,
            t.RMRK AS rmrk,
            t.CAR_BASS_MATTER_INQIRE_ID AS carBassMatterInqireId,
            t.CAR_LEDGER_FRMBK_ID AS carLedgerFrmbkId,
            t.CAR_BSC_MTTR_INQ_FLNM AS carBscMttrInqFlnm,
            t.CAR_BSC_MTTR_INQ_SGG_CD AS carBscMttrInqSggCd,
            t.CAR_BSC_MTTR_INQ_SGG_NM AS carBscMttrInqSggNm,
            t.CAR_REG_FRMBK_CHG_TASK_SE_CD AS carRegFrmbkChgTaskSeCd,
            t.CAR_REG_FRMBK_CHG_TASK_SE_NM AS carRegFrmbkChgTaskSeNm,
            t.CAR_REG_FRMBK_CHG_YMD AS carRegFrmbkChgYmd,
            t.CAR_REG_FRMBK_DTL AS carRegFrmbkDtl,
            t.REG_DT AS regDt,
            t.RGTR AS rgtr,
            t.DEL_YN AS delYn,
            t.DEL_DT AS delDt,
            t.DLTR AS dltr,
            cd.CD_NM AS taskPrcsSttsCdNm,
            u.USER_NM AS rgtrNm
        FROM tb_car_ffnlg_trgt t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        LEFT JOIN (
            SELECT CD_ID, CD_NM
            FROM tb_cd_detail
            WHERE CD_GROUP_ID = 'TASK_PRCS_STTS_CD'
              AND USE_YN = 'Y'
        ) cd ON t.TASK_PRCS_STTS_CD = cd.CD_ID
        WHERE t.DEL_DT IS NULL
        <include refid="searchCondition"/>
        ORDER BY t.CAR_FFNLG_TRGT_ID ASC
        <if test='pagingYn == "Y"'>
            limit #{startIndex}, #{perPage}    /* 서버사이드 페이징 처리 */
        </if>
    </select>
    
    <!-- 과태료 대상 상세 조회 -->
    <select id="selectOne" parameterType="CarFfnlgTrgtVO" resultType="CarFfnlgTrgtVO">
        SELECT
            t.CAR_FFNLG_TRGT_ID AS carFfnlgTrgtId,
            t.RCPT_YMD AS rcptYmd,
            t.FFNLG_TRGT_SE_CD AS ffnlgTrgtSeCd,
            t.INSPSTN_CD AS inspstnCd,
            t.INSP_YMD AS inspYmd,
            t.VHCLNO AS vhclno,
            t.OWNR_NM AS ownrNm,
            ECL_DECRYPT(t.RRNO) AS rrno,
            t.CAR_NM AS carNm,
            t.CAR_KND AS carKnd,
            t.CAR_USG AS carUsg,
            t.INSP_END_YMD AS inspEndYmd,
            t.DAYCNT AS daycnt,
            t.FFNLG_AMT AS ffnlgAmt,
            t.LAST_REG_YMD AS lastRegYmd,
            t.ADDR AS addr,
            t.VLD_PRD_EXPRY_YMD AS vldPrdExpryYmd,
            t.TRD_GDS AS trdGds,
            t.TASK_PRCS_STTS_CD AS taskPrcsSttsCd,
            t.TASK_PRCS_YMD AS taskPrcsYmd,
            t.RMRK AS rmrk,
            t.CAR_BASS_MATTER_INQIRE_ID AS carBassMatterInqireId,
            t.CAR_LEDGER_FRMBK_ID AS carLedgerFrmbkId,
            t.CAR_BSC_MTTR_INQ_FLNM AS carBscMttrInqFlnm,
            t.CAR_BSC_MTTR_INQ_SGG_CD AS carBscMttrInqSggCd,
            t.CAR_BSC_MTTR_INQ_SGG_NM AS carBscMttrInqSggNm,
            t.CAR_REG_FRMBK_CHG_TASK_SE_CD AS carRegFrmbkChgTaskSeCd,
            t.CAR_REG_FRMBK_CHG_TASK_SE_NM AS carRegFrmbkChgTaskSeNm,
            t.CAR_REG_FRMBK_CHG_YMD AS carRegFrmbkChgYmd,
            t.CAR_REG_FRMBK_DTL AS carRegFrmbkDtl,
            t.REG_DT AS regDt,
            t.RGTR AS rgtr,
            t.DEL_YN AS delYn,
            t.DEL_DT AS delDt,
            t.DLTR AS dltr,
            cd.CD_NM AS taskPrcsSttsCdNm,
            u.USER_NM AS rgtrNm
        FROM tb_car_ffnlg_trgt t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        LEFT JOIN (
            SELECT CD_ID, CD_NM
            FROM tb_cd_detail
            WHERE CD_GROUP_ID = 'TASK_PRCS_STTS_CD'
              AND USE_YN = 'Y'
        ) cd ON t.TASK_PRCS_STTS_CD = cd.CD_ID
        WHERE t.CAR_FFNLG_TRGT_ID = #{carFfnlgTrgtId}
          AND t.DEL_DT IS NULL
    </select>


    <!-- 과태료 대상 목록 엑셀 다운로드용 조회 -->
    <select id="selectListForExcel" parameterType="CarFfnlgTrgtVO" resultType="CarFfnlgTrgtExcelVO">
        SELECT
            DATE_FORMAT(STR_TO_DATE(t.RCPT_YMD, '%Y%m%d'), '%Y-%m-%d') AS rcptYmd,
            t.INSPSTN_CD AS inspstnCd,
            DATE_FORMAT(STR_TO_DATE(t.INSP_YMD, '%Y%m%d'), '%Y-%m-%d') AS inspYmd,
            t.VHCLNO AS vhclno,
            t.OWNR_NM AS ownrNm,
            ECL_DECRYPT(t.RRNO) AS rrno,
            t.CAR_NM AS carNm,
            t.CAR_KND AS carKnd,
            t.CAR_USG AS carUsg,
            DATE_FORMAT(STR_TO_DATE(t.INSP_END_YMD, '%Y%m%d'), '%Y-%m-%d') AS inspEndYmd,
            t.DAYCNT AS daycnt,
            t.FFNLG_AMT AS ffnlgAmt,
            DATE_FORMAT(STR_TO_DATE(t.LAST_REG_YMD, '%Y%m%d'), '%Y-%m-%d') AS lastRegYmd,
            t.ADDR AS addr,
            DATE_FORMAT(STR_TO_DATE(t.VLD_PRD_EXPRY_YMD, '%Y%m%d'), '%Y-%m-%d') AS vldPrdExpryYmd,
            t.TRD_GDS AS trdGds,
            cd.CD_NM AS taskPrcsSttsCdNm,
            DATE_FORMAT(STR_TO_DATE(t.TASK_PRCS_YMD, '%Y%m%d'), '%Y-%m-%d') AS taskPrcsYmd,
            t.RMRK AS rmrk,
            t.CAR_BSC_MTTR_INQ_FLNM AS carBscMttrInqFlnm,
            t.CAR_BSC_MTTR_INQ_SGG_NM AS carBscMttrInqSggNm,
            t.CAR_REG_FRMBK_CHG_TASK_SE_NM AS carRegFrmbkChgTaskSeNm,
            DATE_FORMAT(STR_TO_DATE(t.CAR_REG_FRMBK_CHG_YMD, '%Y%m%d'), '%Y-%m-%d') AS carRegFrmbkChgYmd,
            t.CAR_REG_FRMBK_DTL AS carRegFrmbkDtl,
            DATE_FORMAT(t.REG_DT, '%Y-%m-%d %H:%i:%s') AS regDt,
            u.USER_NM AS rgtrNm
        FROM tb_car_ffnlg_trgt t
        LEFT JOIN tb_user u ON t.RGTR = u.USER_ID
        LEFT JOIN (
            SELECT CD_ID, CD_NM
            FROM tb_cd_detail
            WHERE CD_GROUP_ID = 'TASK_PRCS_STTS_CD'
              AND USE_YN = 'Y'
        ) cd ON t.TASK_PRCS_STTS_CD = cd.CD_ID
        WHERE t.DEL_DT IS NULL
        <include refid="searchCondition"/>
        ORDER BY t.CAR_FFNLG_TRGT_ID ASC
    </select>

    <!-- 과태료 대상 등록 -->
    <insert id="insert" parameterType="CarFfnlgTrgtVO">
        INSERT INTO tb_car_ffnlg_trgt (
            CAR_FFNLG_TRGT_ID,
            RCPT_YMD,
            FFNLG_TRGT_SE_CD,
            INSPSTN_CD,
            INSP_YMD,
            VHCLNO,
            OWNR_NM,
            RRNO,
            CAR_NM,
            CAR_KND,
            CAR_USG,
            INSP_END_YMD,
            DAYCNT,
            FFNLG_AMT,
            LAST_REG_YMD,
            ADDR,
            VLD_PRD_EXPRY_YMD,
            TRD_GDS,
            TASK_PRCS_STTS_CD,
            TASK_PRCS_YMD,
            RMRK,
            CAR_BASS_MATTER_INQIRE_ID,
            CAR_LEDGER_FRMBK_ID,
            CAR_BSC_MTTR_INQ_FLNM,
            CAR_BSC_MTTR_INQ_SGG_CD,
            CAR_BSC_MTTR_INQ_SGG_NM,
            CAR_REG_FRMBK_CHG_TASK_SE_CD,
            CAR_REG_FRMBK_CHG_TASK_SE_NM,
            CAR_REG_FRMBK_CHG_YMD,
            CAR_REG_FRMBK_DTL,
            REG_DT,
            RGTR,
            DEL_YN
        ) VALUES (
            CONCAT('CFT', LPAD(NEXTVAL(seq_car_ffnlg_trgt_id), 17, '0')),
            #{rcptYmd},
            #{ffnlgTrgtSeCd},
            #{inspstnCd},
            #{inspYmd},
            #{vhclno},
            #{ownrNm},
            ECL_ENCRYPT(#{rrno}),
            #{carNm},
            #{carKnd},
            #{carUsg},
            #{inspEndYmd},
            #{daycnt},
            #{ffnlgAmt},
            #{lastRegYmd},
            #{addr},
            #{vldPrdExpryYmd},
            #{trdGds},
            #{taskPrcsSttsCd},
            #{taskPrcsYmd},
            #{rmrk},
            #{carBassMatterInqireId},
            #{carLedgerFrmbkId},
            #{carBscMttrInqFlnm},
            #{carBscMttrInqSggCd},
            #{carBscMttrInqSggNm},
            #{carRegFrmbkChgTaskSeCd},
            #{carRegFrmbkChgTaskSeNm},
            #{carRegFrmbkChgYmd},
            #{carRegFrmbkDtl},
            NOW(),
            #{rgtr},
            'N'
        )
    </insert>
    
    <!-- 과태료 대상 수정 -->
    <update id="update" parameterType="CarFfnlgTrgtVO">
        UPDATE tb_car_ffnlg_trgt
        SET TASK_PRCS_STTS_CD = #{taskPrcsSttsCd},
            TASK_PRCS_YMD = #{taskPrcsYmd},
            RMRK = #{rmrk},
            CAR_BASS_MATTER_INQIRE_ID = #{carBassMatterInqireId},
            CAR_LEDGER_FRMBK_ID = #{carLedgerFrmbkId},
            CAR_BSC_MTTR_INQ_FLNM = #{carBscMttrInqFlnm},
            CAR_BSC_MTTR_INQ_SGG_CD = #{carBscMttrInqSggCd},
            CAR_BSC_MTTR_INQ_SGG_NM = #{carBscMttrInqSggNm},
            CAR_REG_FRMBK_CHG_TASK_SE_CD = #{carRegFrmbkChgTaskSeCd},
            CAR_REG_FRMBK_CHG_TASK_SE_NM = #{carRegFrmbkChgTaskSeNm},
            CAR_REG_FRMBK_CHG_YMD = #{carRegFrmbkChgYmd},
            CAR_REG_FRMBK_DTL = #{carRegFrmbkDtl}
        WHERE CAR_FFNLG_TRGT_ID = #{carFfnlgTrgtId}
          AND DEL_DT IS NULL
    </update>

    <!-- 과태료 대상의 처리상태와 비고만 수정 -->
    <update id="updateTaskPrcsSttsCdAndRmrk" parameterType="CarFfnlgTrgtVO">
        UPDATE tb_car_ffnlg_trgt
        SET TASK_PRCS_STTS_CD = #{taskPrcsSttsCd},
            TASK_PRCS_YMD = #{taskPrcsYmd},
            RMRK = #{rmrk}
        WHERE CAR_FFNLG_TRGT_ID = #{carFfnlgTrgtId}
          AND DEL_DT IS NULL
    </update>

    <!-- 과태료 대상 삭제 (논리삭제) -->
    <update id="delete" parameterType="CarFfnlgTrgtVO">
        UPDATE tb_car_ffnlg_trgt
        SET DEL_DT = NOW(),
            DLTR = #{dltr}
        WHERE CAR_FFNLG_TRGT_ID = #{carFfnlgTrgtId}
          AND DEL_DT IS NULL
    </update>
    
    <!-- 차량번호 중복 체크 -->
    <select id="checkDuplicateVhclno" parameterType="CarFfnlgTrgtVO" resultType="int">
        SELECT COUNT(*)
        FROM tb_car_ffnlg_trgt
        WHERE VHCLNO = #{vhclno}
          AND INSP_YMD = #{inspYmd}
          AND DEL_DT IS NULL
    </select>

    <!-- 시군구 코드로 시군구명 조회 -->
    <select id="selectSggNmBySggCd" parameterType="String" resultType="String">
        SELECT SGG_NM
        FROM tb_sgg_cd
        WHERE SGG_CD = #{sggCd}
          AND DEL_YN = 'N'
        LIMIT 1
    </select>


</mapper>
