<?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.loanmng.LoanStateMapper">
    <select id="selectLoanStateGridList" resultType="com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity">
        /* LoanStateMapper.selectLoanStateGridList */
        SELECT A.loan_mgnt_unq_no, A.store_id
            , FN_STORE_NM(A.brand_id,A.store_id) AS store_nm
            , A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm
            , A.acct_cd, A.acct_no
            , A.loan_dvsn, FN_CODE_NM('LOAN_DVSN', A.loan_dvsn) AS loan_dvsn_nm
            , A.use_yn, FN_CODE_NM('USE_YN',A.use_yn) AS use_yn_nm
            , A.acct_bal                -- 입금금액(잔액)
            , A.credt_limit_amt
            , A.chrge_limit_amt
            , A.credt_limit_use_yn
            , A.use_amt_total           -- 시용한금액
            , A.tmp_rcv_acct_total
            , CASE WHEN A.loan_dvsn = 'LD01' THEN A.chrge_limit_amt
                   WHEN A.loan_dvsn = 'LD02' THEN A.credt_limit_amt END AS limit_amt                            -- 한도금액
            , CASE WHEN A.loan_dvsn = 'LD01' THEN A.acct_bal + A.chrge_limit_amt - A.use_amt_total
                   WHEN A.loan_dvsn = 'LD02' THEN A.acct_bal + A.credt_limit_amt - A.use_amt_total END AS posb_limit_amt     -- 사용가능금액
            , DATE_FORMAT(A.sys_chg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_chg_dttm, A.sys_chg_id
            , FN_USER_NM(A.sys_chg_id) AS sys_chg_nm
        FROM loan_mgnt_base_info A
        WHERE 1 = 1
        <if test="sBrandId != null and sBrandId != ''">
            AND A.brand_id = #{sBrandId}
        </if>
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sLoanDvsn != null and sLoanDvsn != ''">
            AND A.loan_dvsn = #{sLoanDvsn}
        </if>
        <if test="sUseYn != null and sUseYn != ''">
            AND A.use_yn = #{sUseYn}
        </if>
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        ORDER BY A.sys_chg_dttm ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.sys_chg_dttm DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.sys_chg_dttm ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectLoanStateGridCnt" resultType="int">
        /* LoanStateMapper.selectLoanStateGridCnt */
        SELECT COUNT(*)
        FROM loan_mgnt_base_info A
        WHERE 1 = 1
        <if test="sBrandId != null and sBrandId != ''">
            AND A.brand_id = #{sBrandId}
        </if>
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sLoanDvsn != null and sLoanDvsn != ''">
            AND A.loan_dvsn = #{sLoanDvsn}
        </if>
        <if test="sUseYn != null and sUseYn != ''">
            AND A.use_yn = #{sUseYn}
        </if>
    </select>

    <select id="selectLoanMgntBaseInfo" resultType="com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity">
        /* LoanStateMapper.selectLoanMgntBaseInfo */
        SELECT A.loan_mgnt_unq_no,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.acct_cd, A.acct_no, A.acct_bal, A.loan_dvsn, FN_CODE_NM('LOAN_DVSN', A.loan_dvsn) AS loan_dvsn_nm,
            A.credt_limit_amt, A.chrge_limit_amt, A.credt_limit_use_yn, A.use_amt_total, A.tmp_rcv_acct_total,
            A.use_yn, FN_CODE_NM('USE_YN', A.use_yn) AS use_yn_nm, A.loan_desc,
            DATE_FORMAT(A.sys_reg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_reg_dttm, A.sys_reg_id,
            DATE_FORMAT(A.sys_chg_dttm,'%Y.%m.%d %H:%i:%s') AS sys_chg_dttm, A.sys_chg_id, FN_USER_NM(A.sys_chg_id) AS sys_chg_nm
        FROM loan_mgnt_base_info A
        WHERE A.loan_mgnt_unq_no = #{loanMgntUnqNo}
    </select>

    <select id="selectLoanHistGridList" resultType="com.oqpo.api.entity.loanmng.LoanMgntDtlHstEntity">
        /* LoanStateMapper.selectLoanHistGridList */
        SELECT A.loan_mgnt_unq_no,
            DATE_FORMAT(A.loan_reg_dt, '%Y.%m.%d') AS loan_reg_dt, TIME_FORMAT(A.loan_reg_tm, '%H:%i:%s') AS loan_reg_tm,
            DATE_FORMAT(CONCAT(A.loan_reg_dt, A.loan_reg_tm), '%Y.%m.%d %H:%i:%s') AS loan_reg_dtm,
            A.dpst_pay_dvsn, FN_CODE_NM('DPST_PAY_DVSN', A.dpst_pay_dvsn) AS dpst_pay_dvsn_nm,
            A.trsc_amt,
            CASE WHEN A.dpst_pay_dvsn = 'D' THEN A.trsc_amt END AS dpst_amt,
            CASE WHEN A.dpst_pay_dvsn = 'P' THEN A.trsc_amt END AS paym_amt,
            A.trsc_bf_bal, A.trsc_af_bal, A.media_dvsn, FN_CODE_NM('MEDIA_DVSN', A.media_dvsn) AS media_dvsn_nm,
            A.rcv_acct_no, A.paym_acct_no, A.dpst_dt, A.dpst_nm, A.dpst_bnk_cd, A.dspt_mgnt_no, A.loan_desc
        FROM loan_mgnt_dtl_hst A
        WHERE A.loan_mgnt_unq_no = #{sLoanMgntUnqNo}
        <if test="sDpstPayDvsn != null and sDpstPayDvsn != ''">
            AND A.dpst_pay_dvsn = #{sDpstPayDvsn}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
        AND A.loan_reg_dt BETWEEN #{fromDt} AND #{toDt}
        </if>
        <choose>
            <when test="gridRequest.sidx != null and gridRequest.sidx != ''">
                <if test="gridRequest.sidx == 'VIEW_NUM'.toString()">
                    <if test="gridRequest.sord == 'asc'.toString()">
                        ORDER BY A.loan_reg_dt ASC, A.loan_reg_tm ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.loan_reg_dt DESC, A.loan_reg_tm DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.loan_reg_dt DESC, A.loan_reg_tm DESC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectLoanHistGridCnt" resultType="int">
        /* LoanStateMapper.selectLoanHistGridCnt */
        SELECT COUNT(*)
        FROM loan_mgnt_dtl_hst A
        WHERE A.loan_mgnt_unq_no = #{sLoanMgntUnqNo}
        <if test="sDpstPayDvsn != null and sDpstPayDvsn != ''">
            AND A.dpst_pay_dvsn = #{sDpstPayDvsn}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.loan_reg_dt BETWEEN #{fromDt} AND #{toDt}
        </if>
    </select>

    <select id="selectLoanJobDataInfo" resultType="com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity">
        select loan_mgnt_unq_no, store_id, loan_dvsn, fn_code_nm('LOAN_DVSN', loan_dvsn) AS loan_dvsn_nm
                  , nvl(case when loan_dvsn = 'LD01' then (acct_bal + tmp_rcv_acct_total - use_amt_total)
                         when loan_dvsn = 'LD02' then (CREDT_LIMIT_AMT + acct_bal + tmp_rcv_acct_total - use_amt_total)
                         else 0 end,0) as ord_use_amt
        from   loan_mgnt_base_info
        where  store_id = #{storeId}
        and    use_yn = 'Y'
    </select>

    <update id="updateloanPchReqUseAmt">
        update loan_mgnt_base_info
        set   use_amt_total = use_amt_total + #{ordPchReqAmt}
        where loan_mgnt_unq_no = #{loanMgntUnqNo}
        and   store_id = #{storeId}
    </update>

    <update id="updateloanMinusPchReqUseAmt">
        update loan_mgnt_base_info
        set   use_amt_total = use_amt_total - #{ordPchReqAmt}
        where loan_mgnt_unq_no = #{loanMgntUnqNo}
        and   store_id = #{storeId}
    </update>

    <update id="updateloanMinusInstProc">
        update /* updateloanMinusInstProc */ loan_mgnt_base_info
        set   use_amt_total = use_amt_total - #{useAmtTotal}
        where loan_mgnt_unq_no = #{loanMgntUnqNo}
        and   store_id = #{storeId}
    </update>


    <update id="updateListLoanMinusProc" >
        /* updateListLoanMinusProc */
        <foreach collection="gridUpdateData" item="item" separator=";">
            update /* updateloanMinusInstProc */ loan_mgnt_base_info
            set   use_amt_total = use_amt_total - #{item.useAmtTotal}
                 , sys_chg_dttm = NOW()
                 , sys_chg_id = #{userId}
            where loan_mgnt_unq_no = #{item.loanMgntUnqNo}
            and   store_id = #{item.storeId}
        </foreach>
    </update>

    <update id="updateloanRtnacctBalAmt">
        update /* updateloanRtnacctBalAmt */ loan_mgnt_base_info
        set   sys_chg_dttm = now()
             ,sys_chg_id = #{userId}
             ,acct_bal = acct_bal + #{entity.acctBal}
        where loan_mgnt_unq_no = #{entity.loanMgntUnqNo}
        and   store_id = #{entity.storeId}
    </update>


    <select id="selectLoanRtnDataInfo" resultType="com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity">
        select loan_mgnt_unq_no, store_id, loan_dvsn, acct_bal
        from   loan_mgnt_base_info
        where  store_id = #{storeId}
        and    use_yn = 'Y'
    </select>


    <update id="updateloanAutoSttl">
        update loan_mgnt_base_info
        set    sys_chg_dttm = now()
             ,sys_chg_id = #{userId}
             ,use_amt_total = use_amt_total - #{sttlAmt}
             ,acct_bal = acct_bal  - #{sttlAmt}
        where loan_mgnt_unq_no = #{loanMgntUnqNo}
        and   store_id = #{storeId}
    </update>
</mapper>
