<?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.api.mapper.VmisCarLedgerFrmbkMapper">

    <!-- 시퀀스로 새로운 마스터/상세 ID 생성 -->
    <select id="selectNextCarLedgerFrmbkId" resultType="String">
        SELECT CONCAT('CLFB', LPAD(NEXTVAL(seq_car_ledger_frmbk), 16, '0')) AS id
    </select>

    <select id="selectNextCarLedgerFrmbkDtlId" resultType="String">
        SELECT CONCAT('CLFD', LPAD(NEXTVAL(seq_car_ledger_frmbk_dtl), 16, '0')) AS id
    </select>

    <!-- 최초 요청 정보 INSERT (마스터) -->
    <insert id="insertCarLedgerFrmbk" parameterType="CarLedgerFrmbkVO">
        INSERT INTO tb_car_ledger_frmbk (
            CAR_LEDGER_FRMBK_ID,
            INFO_SYS_ID,
            INFO_SYS_IP_ADDR,
            SGG_CD,
            LINK_INFO_CD,
            PIC_ID,
            PIC_IP_ADDR,
            PIC_NM,
            DMND_VHRNO,
            DMND_PRVC_RLS,
            DMND_CVLPR_NM,
            DMND_CVLPR_IDECNO,
            DMND_CVLPR_STDG_CD,
            DMND_PATH_SE_CD,
            DMND_DSCTN_INDCT,
            DMND_INQ_SE_CD,
            LINK_RSLT_CD,
            REG_DT,
            RGTR
        ) VALUES (
            #{carLedgerFrmbkId},
            #{infoSysId},
            #{infoSysIpAddr},
            #{sggCd},
            #{linkInfoCd},
            #{picId},
            #{picIpAddr},
            #{picNm},
            #{dmndVhrno},
            #{dmndPrvcRls},
            #{dmndCvlprNm},
            #{dmndCvlprIdecno},
            #{dmndCvlprStdgCd},
            #{dmndPathSeCd},
            #{dmndDsctnIndct},
            #{dmndInqSeCd},
            #{linkRsltCd},
            NOW(),
            #{rgtr}
        )
    </insert>

    <!-- 응답 결과 UPDATE (마스터) -->
    <update id="updateCarLedgerFrmbk" parameterType="CarLedgerFrmbkVO">
        UPDATE tb_car_ledger_frmbk
        <set>
            <if test="txId != null">TX_ID = #{txId},</if>
            <if test="linkRsltCd != null">LINK_RSLT_CD = #{linkRsltCd},</if>
            <if test="linkRsltDtl != null">LINK_RSLT_DTL = #{linkRsltDtl},</if>
            <if test="ledgerGroupNo != null">LEDGER_GROUP_NO = #{ledgerGroupNo},</if>
            <if test="ledgerIndivNo != null">LEDGER_INDIV_NO = #{ledgerIndivNo},</if>
            <if test="vhmno != null">VHMNO = #{vhmno},</if>
            <if test="vhrno != null">VHRNO = #{vhrno},</if>
            <if test="vin != null">VIN = #{vin},</if>
            <if test="carmdlAsortCd != null">CARMDL_ASORT_CD = #{carmdlAsortCd},</if>
            <if test="carmdlAsortNm != null">CARMDL_ASORT_NM = #{carmdlAsortNm},</if>
            <if test="atmbNm != null">ATMB_NM = #{atmbNm},</if>
            <if test="colorCd != null">COLOR_CD = #{colorCd},</if>
            <if test="colorNm != null">COLOR_NM = #{colorNm},</if>
            <if test="nopltSpcfctCd != null">NOPLT_SPCFCT_CD = #{nopltSpcfctCd},</if>
            <if test="nopltSpcfctNm != null">NOPLT_SPCFCT_NM = #{nopltSpcfctNm},</if>
            <if test="usgSeCd != null">USG_SE_CD = #{usgSeCd},</if>
            <if test="usgSeNm != null">USG_SE_NM = #{usgSeNm},</if>
            <if test="mtrsFomNm != null">MTRS_FOM_NM = #{mtrsFomNm},</if>
            <if test="fomNm != null">FOM_NM = #{fomNm},</if>
            <if test="acqsAmt != null">ACQS_AMT = #{acqsAmt},</if>
            <if test="regDtlCd != null">REG_DTL_CD = #{regDtlCd},</if>
            <if test="regDtlNm != null">REG_DTL_NM = #{regDtlNm},</if>
            <if test="frstRegYmd != null">FRST_REG_YMD = #{frstRegYmd},</if>
            <if test="veagEndYmd != null">VEAG_END_YMD = #{veagEndYmd},</if>
            <if test="yridnw != null">YRIDNW = #{yridnw},</if>
            <if test="spmnno1 != null">SPMNNO_1 = #{spmnno1},</if>
            <if test="spmnno2 != null">SPMNNO_2 = #{spmnno2},</if>
            <if test="fbctnYmd != null">FBCTN_YMD = #{fbctnYmd},</if>
            <if test="drvngDstnc != null">DRVNG_DSTNC = #{drvngDstnc},</if>
            <if test="inspVldPdBgngYmd != null">INSP_VLD_PD_BGNG_YMD = #{inspVldPdBgngYmd},</if>
            <if test="inspVldPdEndYmd != null">INSP_VLD_PD_END_YMD = #{inspVldPdEndYmd},</if>
            <if test="chckVldPdBgngYmd != null">CHCK_VLD_PD_BGNG_YMD = #{chckVldPdBgngYmd},</if>
            <if test="chckVldPdEndYmd != null">CHCK_VLD_PD_END_YMD = #{chckVldPdEndYmd},</if>
            <if test="regAplySeNm != null">REG_APLY_SE_NM = #{regAplySeNm},</if>
            <if test="frstRegAplyRcptNo != null">FRST_REG_APLY_RCPT_NO = #{frstRegAplyRcptNo},</if>
            <if test="nopltCsdyAvtsmtYmd != null">NOPLT_CSDY_AVTSMT_YMD = #{nopltCsdyAvtsmtYmd},</if>
            <if test="nopltCsdyYn != null">NOPLT_CSDY_YN = #{nopltCsdyYn},</if>
            <if test="bssUsePdYmd != null">BSS_USE_PD_YMD = #{bssUsePdYmd},</if>
            <if test="octhtErsrPrvntcAvtsmtYmd != null">OCTHT_ERSR_PRVNTC_AVTSMT_YMD = #{octhtErsrPrvntcAvtsmtYmd},</if>
            <if test="ersrRegYmd != null">ERSR_REG_YMD = #{ersrRegYmd},</if>
            <if test="ersrRegSeCd != null">ERSR_REG_SE_CD = #{ersrRegSeCd},</if>
            <if test="ersrRegSeNm != null">ERSR_REG_SE_NM = #{ersrRegSeNm},</if>
            <if test="mrtgCnt != null">MRTG_CNT = #{mrtgCnt},</if>
            <if test="szrCnt != null">SZR_CNT = #{szrCnt},</if>
            <if test="strctChgCnt != null">STRCT_CHG_CNT = #{strctChgCnt},</if>
            <if test="usgsrhldAddr1 != null">USGSRHLD_ADDR_1 = #{usgsrhldAddr1},</if>
            <if test="usgsrhldAddrDtl1 != null">USGSRHLD_ADDR_DTL_1 = #{usgsrhldAddrDtl1},</if>
            <if test="ownrAddr != null">OWNR_ADDR = #{ownrAddr},</if>
            <if test="ownrAddrDtl != null">OWNR_ADDR_DTL = #{ownrAddrDtl},</if>
            <if test="indvdlBzmnYn != null">INDVDL_BZMN_YN = #{indvdlBzmnYn},</if>
            <if test="rprsOwnrTelno != null">RPRS_OWNR_TELNO = #{rprsOwnrTelno},</if>
            <if test="rprsOwnrNm != null">RPRS_OWNR_NM = #{rprsOwnrNm},</if>
            <if test="rprsOwnrMbrSeCd != null">RPRS_OWNR_MBR_SE_CD = #{rprsOwnrMbrSeCd},</if>
            <if test="rprsvOwnrIdecno != null">RPRSV_OWNR_IDECNO = #{rprsvOwnrIdecno},</if>
            <if test="taxxmptTrprSeCd != null">TAXXMPT_TRPR_SE_CD = #{taxxmptTrprSeCd},</if>
            <if test="taxxmptAplcnSeCd != null">TAXXMPT_APLCN_SE_CD = #{taxxmptAplcnSeCd},</if>
            <if test="spcablMttrCnt != null">SPCABL_MTTR_CNT = #{spcablMttrCnt},</if>
            <if test="usgsrhldDongNm != null">USGSRHLD_DONG_NM = #{usgsrhldDongNm},</if>
            <if test="prvntcCnt != null">PRVNTC_CNT = #{prvntcCnt},</if>
            <if test="xportFlflYnDclrYmd != null">XPORT_FLFL_YN_DCLR_YMD = #{xportFlflYnDclrYmd},</if>
            <if test="issuNo != null">ISSU_NO = #{issuNo},</if>
            <if test="frstTrnsfrYmd != null">FRST_TRNSFR_YMD = #{frstTrnsfrYmd},</if>
            <if test="drivSrgbtryIdntfNo != null">DRIV_SRGBTRY_IDNTF_NO = #{drivSrgbtryIdntfNo},</if>
            <if test="prcsImprtyRsnCd != null">PRCS_IMPRTY_RSN_CD = #{prcsImprtyRsnCd},</if>
            <if test="prcsImprtyRsnDtls != null">PRCS_IMPRTY_RSN_DTLS = #{prcsImprtyRsnDtls},</if>
            <if test="carFfnlgTrgtId != null">CAR_FFNLG_TRGT_ID = #{carFfnlgTrgtId}</if>
        </set>
        WHERE CAR_LEDGER_FRMBK_ID = #{carLedgerFrmbkId}
    </update>

    <!-- 상세 INSERT -->
    <insert id="insertCarLedgerFrmbkDtl" parameterType="CarLedgerFrmbkDtlVO">
        INSERT INTO tb_car_ledger_frmbk_dtl (
            CAR_LEDGER_FRMBK_DTL_ID,
            CAR_LEDGER_FRMBK_ID,
            SZR_RMV_DTL_SN,
            CHG_TASK_SE_CD,
            MAIN_NO,
            SNO,
            SPCABL_MTTR,
            HSHLDR_NM,
            HSHLDR_IDECNO,
            APLY_RCPT_NO,
            VHMNO,
            LEDGER_GROUP_NO,
            LEDGER_INDIV_NO,
            CHG_TASK_SE_NM,
            CHG_YMD,
            DTL_SN,
            FLAG,
            REG_DT,
            RGTR
        ) VALUES (
            #{carLedgerFrmbkDtlId},
            #{carLedgerFrmbkId},
            #{szrRmvDtlSn},
            #{chgTaskSeCd},
            #{mainNo},
            #{sno},
            #{spcablMttr},
            #{hshldrNm},
            #{hshldrIdecno},
            #{aplyRcptNo},
            #{vhmno},
            #{ledgerGroupNo},
            #{ledgerIndivNo},
            #{chgTaskSeNm},
            #{chgYmd},
            #{dtlSn},
            #{flag},
            NOW(),
            #{rgtr}
        )
    </insert>

    <!-- ID로 조회 (선택) -->
    <select id="selectCarLedgerFrmbkById" parameterType="String" resultType="CarLedgerFrmbkVO">
        SELECT *
        FROM tb_car_ledger_frmbk
        WHERE CAR_LEDGER_FRMBK_ID = #{carLedgerFrmbkId}
    </select>

    <!-- txId로 CAR_FFNLG_TRGT_ID 업데이트 -->
    <update id="updateCarFfnlgTrgtIdByTxId">
        UPDATE tb_car_ledger_frmbk
        SET CAR_FFNLG_TRGT_ID = #{param2}
        WHERE TX_ID = #{param1}
    </update>

</mapper>
