<?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="com.oqpo.api.mapper.loan.LoanMgntMapper">

    <insert id="insertLoanMgntInfo" >
        insert into loan_mgnt_base_info
        (
            loan_mgnt_unq_no,store_id,brand_id,acct_cd,acct_no,
            acct_bal,loan_dvsn,credt_limit_amt,chrge_limit_amt,credt_limit_use_yn,
            use_amt_total,tmp_rcv_acct_total,use_yn, loan_desc,sys_reg_dttm,sys_reg_id,
            sys_chg_dttm,sys_chg_id
        ) values (
            #{entity.loanMgntUnqNo},#{entity.storeId},#{entity.brandId},#{entity.acctCd},#{entity.acctNo},
            #{entity.acctBal},#{entity.loanDvsn},#{entity.credtLimitAmt},#{entity.chrgeLimitAmt},#{entity.credtLimitUseYn},
            0,#{entity.tmpRcvAcctTotal},'Y', #{entity.loanDesc},now(),#{userId},
            now(),#{userId}
        )
    </insert>

    <insert id="insertAddLoanMgntInfo" >
        insert into loan_mgnt_base_info
        (
            loan_mgnt_unq_no,store_id,brand_id,acct_cd,acct_no,
            acct_bal,loan_dvsn,credt_limit_amt,chrge_limit_amt,credt_limit_use_yn, loan_desc,
            use_amt_total,tmp_rcv_acct_total,use_yn,sys_reg_dttm,sys_reg_id,sys_chg_dttm,sys_chg_id
        )
        select
            #{entity.newLoanMgntUnqNo} ,store_id,brand_id,acct_cd,acct_no,
            acct_bal, #{entity.loanDvsn}, #{entity.credtLimitAmt},#{entity.chrgeLimitAmt},#{entity.credtLimitUseYn},'여신구분변동',
            0,tmp_rcv_acct_total,'Y',now(),#{userId},now(),#{userId}
        from    loan_mgnt_base_info
        where   loan_mgnt_unq_no = #{entity.loanMgntUnqNo}
    </insert>


    <select id="selectLoanStoreInfo" resultType="com.oqpo.api.entity.loan.LoanInfoEntity">
        select loan_mgnt_unq_no, acct_bal, loan_dvsn, credt_limit_amt,chrge_limit_amt,credt_limit_use_yn, tmp_rcv_acct_total
        from loan_mgnt_base_info
        where loan_mgnt_unq_no     = #{loanMgntUnqNo}
    </select>

    <update id="updateLoanMgntCloseInfo">
        update loan_mgnt_base_info  /* updateLoanMgntInfo 변경에따른 이전loan 처리 0으로 세팅 */
        set	 sys_chg_dttm		    = now()
            ,sys_chg_id		        = #{userId}
            ,use_yn			        = 'N'
        where  loan_mgnt_unq_no     = #{loanMgntUnqNo}
    </update>


    <update id="updateLoanChgLimitInfo">
        update loan_mgnt_base_info  /* updateLoanChgLimitInfo 한도금액변경 */
        set	 sys_chg_dttm		= now()
            ,sys_chg_id		    = #{userId}
            ,credt_limit_amt	= #{entity.credtLimitAmt}
            ,chrge_limit_amt	= #{entity.chrgeLimitAmt}
            ,credt_limit_use_yn	= #{entity.credtLimitUseYn}
        where  loan_mgnt_unq_no = #{entity.loanMgntUnqNo}
    </update>

    <select id="selectLoanStoreNo" resultType="String">
        select loan_mgnt_unq_no
        from loan_mgnt_base_info
        where store_id     = #{storeId}
        and   use_yn = 'Y'
    </select>

</mapper>
