<?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.DsptMngMapper">

    <select id="selectDsptMngGridList" resultType="com.oqpo.api.entity.loanmng.LoanMgntDsptProcEntity">
        /* DsptMngMapper.selectDsptMngGridList */
        SELECT A.dspt_mgnt_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.dpst_amt, DATE_FORMAT(A.dpst_dt, '%Y.%m.%d') AS dpst_dt, A.dpst_nm,
            A.dpst_st_cd, FN_CODE_NM('DPST_ST_CD', A.dpst_st_cd) AS dpst_st_nm,
            A.dpst_bnk_cd, FN_CODE_NM('BNK_CD', A.dpst_bnk_cd) AS dpst_bnk_nm,
            A.rcv_acct_no,
            DATE_FORMAT(A.req_dt, '%Y.%m.%d') AS req_dt,
            DATE_FORMAT(A.dpst_auth_dt, '%Y.%m.%d') AS dpst_auth_dt,
            DATE_FORMAT(A.dspt_rjct_dt, '%Y.%m.%d') AS dspt_rjct_dt,
            A.dspt_rjct_rsn,
            B.loan_mgnt_unq_no,                                                 /* 여신관리고유번호 */
            C.loan_dvsn, FN_CODE_NM('LOAN_DVSN', C.loan_dvsn) AS loan_dvsn_nm   /* 정산타입 */
        FROM loan_mgnt_dspt_proc A
        INNER JOIN store_base_info B ON A.brand_id = B.brand_id AND A.store_id = B.store_id
        INNER JOIN loan_mgnt_base_info C ON B.brand_id = C.brand_id AND B.store_id = B.store_id AND B.loan_mgnt_unq_no = C.loan_mgnt_unq_no
        WHERE A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sDpstStCd != null and sDpstStCd != ''">
            AND A.dpst_st_cd = #{sDpstStCd}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.dpst_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.dspt_mgnt_no ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.dspt_mgnt_no DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.dspt_mgnt_no ASC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectDsptMngGridCnt" resultType="int">
        /* DsptMngMapper.selectDsptMngGridCnt */
        SELECT COUNT(*)
        FROM loan_mgnt_dspt_proc A
        INNER JOIN store_base_info B ON A.brand_id = B.brand_id AND A.store_id = B.store_id
        INNER JOIN loan_mgnt_base_info C ON B.brand_id = C.brand_id AND B.store_id = B.store_id AND B.loan_mgnt_unq_no = C.loan_mgnt_unq_no
        WHERE A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sDpstStCd != null and sDpstStCd != ''">
            AND A.dpst_st_cd = #{sDpstStCd}
        </if>
        <if test="fromDt != null and fromDt != '' and toDt != null and toDt != '' ">
            AND A.dpst_dt BETWEEN #{fromDt} AND #{toDt}
        </if>
    </select>

    <select id="selectLoanMgntDsptProcInfo" resultType="com.oqpo.api.entity.loanmng.LoanMgntDsptProcEntity">
        /* DsptMngMapper.selectLoanMgntDsptProcInfo */
        SELECT A.dspt_mgnt_no,
            DATE_FORMAT(A.req_dt, '%Y.%m.%d') AS req_dt, TIME_FORMAT(A.req_tm, '%H:%i:%s') AS req_tm,
            A.dpst_amt, DATE_FORMAT(A.dpst_dt, '%Y.%m.%d') AS dpst_dt, A.dpst_nm, A.rcv_acct_no,
            A.dpst_bnk_cd, FN_CODE_NM('BNK_CD', A.dpst_bnk_cd) AS dpst_bnk_nm,
            A.dpst_st_cd, FN_CODE_NM('DPST_ST_CD', A.dpst_st_cd) AS dpst_st_nm,
            A.dpst_req_id, A.dpst_req_nm, A.dpst_auth_id, A.dpst_auth_nm,
            DATE_FORMAT(A.dpst_auth_dt, '%Y.%m.%d') AS dpst_auth_dt, TIME_FORMAT(A.dpst_auth_tm, '%H:%i:%s') AS dpst_auth_tm,
            DATE_FORMAT(A.dspt_rjct_dt, '%Y.%m.%d') AS dspt_rjct_dt, TIME_FORMAT(A.dspt_rjct_tm, '%H:%i:%s') AS dspt_rjct_tm,
            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.dpst_auth_yn, A.dspt_rjct_rsn,
            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
        FROM loan_mgnt_dspt_proc A
        WHERE A.dspt_mgnt_no = #{dsptMgntNo}
    </select>

    <select id="selectLoanMgntDsptProcInfo4Org" resultType="com.oqpo.api.entity.loanmng.LoanMgntDsptProcEntity">
        /* DsptMngMapper.selectLoanMgntDsptProcInfo4Org */
        SELECT A.dspt_mgnt_no, A.req_dt, A.req_tm, A.dpst_amt, A.dpst_dt, A.dpst_nm, A.rcv_acct_no, A.dpst_bnk_cd,
            A.dpst_st_cd, A.dpst_req_id, A.dpst_req_nm, A.dpst_auth_id, A.dpst_auth_nm, A.dpst_auth_dt, A.dpst_auth_tm, A.dspt_rjct_dt,
            A.dspt_rjct_tm, A.store_id, A.brand_id, A.dpst_auth_yn, A.dspt_rjct_rsn,
            A.sys_reg_dttm, A.sys_reg_id, A.sys_chg_dttm, A.sys_chg_id
        FROM loan_mgnt_dspt_proc A
        WHERE A.dspt_mgnt_no = #{dsptMgntNo}
    </select>

    <insert id="insertLoanMgntDsptProcInfo" >
        /* DsptMngMapper.insertLoanMgntDsptProcInfo */
        INSERT INTO loan_mgnt_dspt_proc
            (dspt_mgnt_no, req_dt, req_tm, dpst_amt, dpst_dt, dpst_nm, rcv_acct_no, dpst_bnk_cd, dpst_st_cd, dpst_req_id,
             dpst_req_nm, dpst_auth_id, dpst_auth_nm, dpst_auth_dt, dpst_auth_tm, dspt_rjct_dt, dspt_rjct_tm,
             store_id, brand_id, dpst_auth_yn, dspt_rjct_rsn,
             po_ord_no, pg_id,
             sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.dsptMgntNo}, #{entity.reqDt}, #{entity.reqTm}, #{entity.dpstAmt}, #{entity.dpstDt}, #{entity.dpstNm},
             #{entity.rcvAcctNo}, #{entity.dpstBnkCd}, #{entity.dpstStCd}, #{entity.dpstReqId}, #{entity.dpstReqNm},
             #{entity.dpstAuthId}, #{entity.dpstAuthNm}, #{entity.dpstAuthDt}, #{entity.dpstAuthTm}, #{entity.dsptRjctDt},
             #{entity.dsptRjctTm}, #{entity.storeId}, #{entity.brandId}, #{entity.dpstAuthYn}, #{entity.dsptRjctRsn},
             #{entity.poOrdNo}, #{entity.pgId},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

    <update id="updateLoanMgntDsptProcInfo4Approve" >
        /* DsptMngMapper.updateLoanMgntDsptProcInfo4Approve */
        UPDATE loan_mgnt_dspt_proc SET
            dpst_st_cd = #{entity.dpstStCd},        /* 입금상태코드 */
            dpst_auth_id = #{entity.dpstAuthId},    /* 입금승인자아이디 */
            dpst_auth_nm = #{entity.dpstAuthNm},    /* 입금승인자명 */
            dpst_auth_dt = #{entity.dpstAuthDt},    /* 입금승인일자 */
            dpst_auth_tm = #{entity.dpstAuthTm},    /* 입금승인시각 */
            dpst_auth_yn = #{entity.dpstAuthYn},    /* 입금승인여부 */
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE dspt_mgnt_no = #{entity.dsptMgntNo}
    </update>

    <update id="updateLoanMgntDsptProcInfo4Reject" >
        /* DsptMngMapper.updateLoanMgntDsptProcInfo4Reject */
        UPDATE loan_mgnt_dspt_proc SET
            dpst_st_cd = #{entity.dpstStCd},        /* 입금상태코드 */
            dspt_rjct_dt = #{entity.dsptRjctDt},    /* 입금반려일자 */
            dspt_rjct_tm = #{entity.dsptRjctTm},    /* 입금반려시각 */
            dpst_auth_yn = #{entity.dpstAuthYn},    /* 입금승인여부 */
            dspt_rjct_rsn = #{entity.dsptRjctRsn},  /* 입금반려사유 */
            sys_chg_dttm = NOW(),
            sys_chg_id = #{entity.sysChgId}
        WHERE dspt_mgnt_no = #{entity.dsptMgntNo}
    </update>

    <delete id="deleteLoanMgntDsptProcInfo" >
        /* DsptMngMapper.deleteLoanMgntDsptProcInfo */
        DELETE FROM loan_mgnt_dspt_proc
        WHERE dspt_mgnt_no = #{entity.dsptMgntNo}
    </delete>

    <select id="selectLoanMgntBaseInfo" resultType="com.oqpo.api.entity.loanmng.LoanMgntBaseInfoEntity">
        /* DsptMngMapper.selectLoanMgntBaseInfo */
        SELECT A.loan_mgnt_unq_no, A.store_id, A.brand_id, A.acct_cd, A.acct_no, A.acct_bal, A.loan_dvsn, 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, 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
        FROM loan_mgnt_base_info A
        INNER JOIN store_base_info B ON A.LOAN_MGNT_UNQ_NO = B.LOAN_MGNT_UNQ_NO
        WHERE B.BRAND_ID = #{brandId}
          AND B.STORE_ID = #{storeId}
    </select>

    <update id="updateLoanMgntBaseInfo4AcctBal" >
        /* DsptMngMapper.updateLoanMgntBaseInfo4AcctBal */
        UPDATE loan_mgnt_base_info SET
            acct_bal = #{entity.acctBal},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE loan_mgnt_unq_no = #{entity.loanMgntUnqNo}
    </update>

    <insert id="insertLoanMgntDtlHst" >
        /* DsptMngMapper.insertLoanMgntDtlHst */
        INSERT INTO loan_mgnt_dtl_hst
            (loan_mgnt_unq_no, loan_reg_dt, loan_reg_tm, dpst_pay_dvsn, trsc_amt, trsc_bf_bal, trsc_af_bal, media_dvsn,
             rcv_acct_no, paym_acct_no, pay_type, dpst_dt, dpst_nm, dpst_bnk_cd, dspt_mgnt_no, loan_desc,
             sys_reg_dttm, sys_reg_id, sys_chg_dttm, sys_chg_id)
        VALUES
            (#{entity.loanMgntUnqNo}, #{entity.loanRegDt}, #{entity.loanRegTm}, #{entity.dpstPayDvsn}, #{entity.trscAmt},
             #{entity.trscBfBal}, #{entity.trscAfBal}, #{entity.mediaDvsn}, #{entity.rcvAcctNo}, #{entity.paymAcctNo},
             #{entity.payType}, #{entity.dpstDt}, #{entity.dpstNm}, #{entity.dpstBnkCd}, #{entity.dsptMgntNo}, #{entity.loanDesc},
             NOW(), #{userId}, NOW(), #{userId})
    </insert>

</mapper>
