<?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.sttlmng.KiccPayMngMapper">

    <select id="selectKiccPayGridList" resultType="com.oqpo.api.entity.settmng.PgKiccPayPtclEntity">
        /* KiccPayMngMapper.selectKiccPayGridList */
        SELECT A.po_ord_no,
            A.brand_id, FN_BRAND_NM(A.brand_id) AS brand_nm,
            A.store_id, FN_STORE_NM(A.brand_id, A.store_id) AS store_nm,
            A.mall_id, A.payment_no, A.tran_type, FN_CODE_NM('TRAN_TYPE', A.tran_type) AS tran_type_nm,
            A.res_cd, A.res_msg, A.cno, A.amount,
            A.auth_no, DATE_FORMAT(A.tran_date,'%Y.%m.%d %H:%i:%S') AS tran_date,
            A.stat_cd, A.stat_msg, A.pay_type, A.card_no, A.issuer_cd, A.issuer_nm, A.acquirer_cd,
            A.acquirer_nm, A.install_period, A.noint, A.cancel_yn,
            DATE_FORMAT(A.cancel_date,'%Y.%m.%d %H:%i:%S') AS cancel_date
        FROM pg_kicc_pay_ptcl A
        WHERE A.del_yn = 'N'
        AND A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sTranType != null and sTranType != ''">
            AND A.tran_type = #{sTranType}
        </if>
        <if test="sAuthNo != null and sAuthNo != ''">
            AND A.auth_no = #{sAuthNo}
        </if>
        AND A.tran_date BETWEEN CONCAT(#{fromDt},'000000') AND CONCAT(#{toDt},'235959')
        <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.po_ord_no ASC
                    </if>
                    <if test="gridRequest.sord == 'desc'.toString()">
                        ORDER BY A.po_ord_no DESC
                    </if>
                </if>
            </when>
            <otherwise>
                ORDER BY A.po_ord_no DESC
            </otherwise>
        </choose>
        <if test="gridRequest.pagingYn == true">
            limit #{gridRequest.gridFirst}, #{gridRequest.gridSize}
        </if>
    </select>

    <select id="selectKiccPayGridCnt" resultType="int">
        /* KiccPayMngMapper.selectKiccPayGridCnt */
        SELECT COUNT(*)
        FROM pg_kicc_pay_ptcl A
        WHERE A.del_yn = 'N'
        AND A.brand_id = #{sBrandId}
        <if test="sStoreId != null and sStoreId != ''">
            AND A.store_id = #{sStoreId}
        </if>
        <if test="sTranType != null and sTranType != ''">
            AND A.tran_type = #{sTranType}
        </if>
        <if test="sAuthNo != null and sAuthNo != ''">
            AND A.auth_no = #{sAuthNo}
        </if>
        AND A.tran_date BETWEEN CONCAT(#{fromDt},'000000') AND CONCAT(#{toDt},'235959')
    </select>

    <select id="selectPgKiccPayPtcl" resultType="com.oqpo.api.entity.settmng.PgKiccPayPtclEntity">
        /* KiccPayMngMapper.selectPgKiccPayPtcl */
        SELECT A.po_ord_no, A.brand_id, A.store_id, A.mall_id, A.payment_no, A.tran_type, A.res_cd, A.res_msg, A.cno,
            A.amount, A.auth_no, A.tran_date, A.stat_cd, A.stat_msg, A.pay_type, A.card_no, A.issuer_cd, A.issuer_nm,
            A.acquirer_cd, A.acquirer_nm, A.install_period, A.noint, A.cancel_yn, A.cancel_date,
            A.del_yn, 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 pg_kicc_pay_ptcl A
        WHERE A.po_ord_no = #{poOrdNo}
    </select>

    <update id="updatePgKiccPayPtcl4Cancel" >
        /* KiccPayMngMapper.updatePgKiccPayPtcl4Cancel */
        UPDATE pg_kicc_pay_ptcl SET
            cancel_yn = #{entity.cancelYn},
            cancel_date = #{entity.cancelDate},
            sys_chg_dttm = NOW(),
            sys_chg_id = #{userId}
        WHERE po_ord_no = #{entity.poOrdNo}
    </update>

</mapper>
