<?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="VmisCarLedgerFrmbkVO">
        INSERT INTO tb_car_ledger_frmbk (
            CAR_LEDGER_FRMBK_ID,
            INFO_SYS_ID,
            INFO_SYS_IP,
            SIGUNGU_CODE,
            CNTC_INFO_CODE,
            CHARGER_ID,
            CHARGER_IP,
            CHARGER_NM,
            DMND_VHRNO,
            DMND_ONES_INFORMATION_OPEN,
            DMND_CPTTR_NM,
            DMND_CPTTR_IHIDNUM,
            DMND_CPTTR_LEGALDONG_CODE,
            DMND_ROUTE_SE_CODE,
            DMND_DETAIL_EXPRESSION,
            DMND_INQIRE_SE_CODE,
            REG_DT,
            RGTR
        ) VALUES (
            #{carLedgerFrmbkId},
            #{infoSysId},
            #{infoSysIp},
            #{sigunguCode},
            #{cntcInfoCode},
            #{chargerId},
            #{chargerIp},
            #{chargerNm},
            #{dmndVhrno},
            #{dmndOnesInformationOpen},
            #{dmndCpttrNm},
            #{dmndCpttrIhidnum},
            #{dmndCpttrLegaldongCode},
            #{dmndRouteSeCode},
            #{dmndDetailExpression},
            #{dmndInqireSeCode},
            NOW(),
            #{rgtr}
        )
    </insert>

    <!-- 응답 결과 UPDATE (마스터) -->
    <update id="updateCarLedgerFrmbk" parameterType="VmisCarLedgerFrmbkVO">
        UPDATE tb_car_ledger_frmbk
        <set>
            <if test="cntcResultCode != null">CNTC_RESULT_CODE = #{cntcResultCode},</if>
            <if test="cntcResultDtls != null">CNTC_RESULT_DTLS = #{cntcResultDtls},</if>
            <if test="ledgerGroupNo != null">LEDGER_GROUP_NO = #{ledgerGroupNo},</if>
            <if test="ledgerIndvdlzNo != null">LEDGER_INDVDLZ_NO = #{ledgerIndvdlzNo},</if>
            <if test="vhmno != null">VHMNO = #{vhmno},</if>
            <if test="vhrno != null">VHRNO = #{vhrno},</if>
            <if test="vin != null">VIN = #{vin},</if>
            <if test="vhctyAsortCode != null">VHCTY_ASORT_CODE = #{vhctyAsortCode},</if>
            <if test="vhctyAsortNm != null">VHCTY_ASORT_NM = #{vhctyAsortNm},</if>
            <if test="cnm != null">CNM = #{cnm},</if>
            <if test="colorCode != null">COLOR_CODE = #{colorCode},</if>
            <if test="colorNm != null">COLOR_NM = #{colorNm},</if>
            <if test="nmplStndrdCode != null">NMPL_STNDRD_CODE = #{nmplStndrdCode},</if>
            <if test="nmplStndrdNm != null">NMPL_STNDRD_NM = #{nmplStndrdNm},</if>
            <if test="prposSeCode != null">PRPOS_SE_CODE = #{prposSeCode},</if>
            <if test="prposSeNm != null">PRPOS_SE_NM = #{prposSeNm},</if>
            <if test="mtrsFomNm != null">MTRS_FOM_NM = #{mtrsFomNm},</if>
            <if test="fomNm != null">FOM_NM = #{fomNm},</if>
            <if test="acqsAmount != null">ACQS_AMOUNT = #{acqsAmount},</if>
            <if test="registDetailCode != null">REGIST_DETAIL_CODE = #{registDetailCode},</if>
            <if test="registDetailNm != null">REGIST_DETAIL_NM = #{registDetailNm},</if>
            <if test="frstRegistDe != null">FRST_REGIST_DE = #{frstRegistDe},</if>
            <if test="caagEndde != null">CAAG_ENDDE = #{caagEndde},</if>
            <if test="prye != null">PRYE = #{prye},</if>
            <if test="spmnno1 != null">SPMNNO1 = #{spmnno1},</if>
            <if test="spmnno2 != null">SPMNNO2 = #{spmnno2},</if>
            <if test="yblMd != null">YBL_MD = #{yblMd},</if>
            <if test="trvlDstnc != null">TRVL_DSTNC = #{trvlDstnc},</if>
            <if test="insptValidPdBgnde != null">INSPT_VALID_PD_BGNDE = #{insptValidPdBgnde},</if>
            <if test="insptValidPdEndde != null">INSPT_VALID_PD_ENDDE = #{insptValidPdEndde},</if>
            <if test="chckValidPdBgnde != null">CHCK_VALID_PD_BGNDE = #{chckValidPdBgnde},</if>
            <if test="chckValidPdEndde != null">CHCK_VALID_PD_ENDDE = #{chckValidPdEndde},</if>
            <if test="registReqstSeNm != null">REGIST_REQST_SE_NM = #{registReqstSeNm},</if>
            <if test="frstRegistRqrcno != null">FRST_REGIST_RQRCNO = #{frstRegistRqrcno},</if>
            <if test="nmplCsdyRemnrDe != null">NMPL_CSDY_REMNR_DE = #{nmplCsdyRemnrDe},</if>
            <if test="nmplCsdyAt != null">NMPL_CSDY_AT = #{nmplCsdyAt},</if>
            <if test="bssUsePd != null">BSS_USE_PD = #{bssUsePd},</if>
            <if test="octhtErsrPrvntcNticeDe != null">OCTHT_ERSR_PRVNTC_NTICE_DE = #{octhtErsrPrvntcNticeDe},</if>
            <if test="ersrRegistDe != null">ERSR_REGIST_DE = #{ersrRegistDe},</if>
            <if test="ersrRegistSeCode != null">ERSR_REGIST_SE_CODE = #{ersrRegistSeCode},</if>
            <if test="ersrRegistSeNm != null">ERSR_REGIST_SE_NM = #{ersrRegistSeNm},</if>
            <if test="mrtgcnt != null">MRTGCNT = #{mrtgcnt},</if>
            <if test="vhclecnt != null">VHCLECNT = #{vhclecnt},</if>
            <if test="stmdcnt != null">STMDCNT = #{stmdcnt},</if>
            <if test="adres1 != null">ADRES1 = #{adres1},</if>
            <if test="adresNm1 != null">ADRES_NM1 = #{adresNm1},</if>
            <if test="adres != null">ADRES = #{adres},</if>
            <if test="adresNm != null">ADRES_NM = #{adresNm},</if>
            <if test="indvdlBsnmAt != null">INDVDL_BSNM_AT = #{indvdlBsnmAt},</if>
            <if test="telno != null">TELNO = #{telno},</if>
            <if test="mberNm != null">MBER_NM = #{mberNm},</if>
            <if test="mberSeCode != null">MBER_SE_CODE = #{mberSeCode},</if>
            <if test="mberSeNo != null">MBER_SE_NO = #{mberSeNo},</if>
            <if test="taxxmptTrgterSeCode != null">TAXXMPT_TRGTER_SE_CODE = #{taxxmptTrgterSeCode},</if>
            <if test="taxxmptTrgterSeCodeNm != null">TAXXMPT_TRGTER_SE_CODE_NM = #{taxxmptTrgterSeCodeNm},</if>
            <if test="cntMatter != null">CNT_MATTER = #{cntMatter},</if>
            <if test="emdNm != null">EMD_NM = #{emdNm},</if>
            <if test="prvntccnt != null">PRVNTCCNT = #{prvntccnt},</if>
            <if test="xportFlflAtSttemntDe != null">XPORT_FLFL_AT_STTEMNT_DE = #{xportFlflAtSttemntDe},</if>
            <if test="partnRqrcno != null">PARTN_RQRCNO = #{partnRqrcno},</if>
            <if test="frstTrnsfrDe != null">FRST_TRNSFR_DE = #{frstTrnsfrDe},</if>
            <if test="processImprtyResnCode != null">PROCESS_IMPRTY_RESN_CODE = #{processImprtyResnCode},</if>
            <if test="processImprtyResnDtls != null">PROCESS_IMPRTY_RESN_DTLS = #{processImprtyResnDtls},</if>
        </set>
        WHERE CAR_LEDGER_FRMBK_ID = #{carLedgerFrmbkId}
    </update>

    <!-- 상세 INSERT -->
    <insert id="insertCarLedgerFrmbkDtl" parameterType="VmisCarLedgerFrmbkDtlVO">
        INSERT INTO tb_car_ledger_frmbk_dtl (
            CAR_LEDGER_FRMBK_DTL_ID,
            CAR_LEDGER_FRMBK_ID,
            MAINCHK,
            CHANGE_JOB_SE_CODE,
            MAINNO,
            SUBNO,
            DTLS,
            RQRCNO,
            VHMNO,
            LEDGER_GROUP_NO,
            LEDGER_INDVDLZ_NO,
            GUBUN_NM,
            CHANGE_DE,
            DETAIL_SN,
            FLAG,
            REG_DT,
            RGTR
        ) VALUES (
            #{carLedgerFrmbkDtlId},
            #{carLedgerFrmbkId},
            #{mainchk},
            #{changeJobSeCode},
            #{mainno},
            #{subno},
            #{dtls},
            #{rqrcno},
            #{vhmno},
            #{ledgerGroupNo},
            #{ledgerIndvdlzNo},
            #{gubunNm},
            #{changeDe},
            #{detailSn},
            #{flag},
            NOW(),
            #{rgtr}
        )
    </insert>

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

</mapper>
