<?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.hzs.member.account.mapper.CuMemberRechargeMatchMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.account.CuMemberRechargeMatch">
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="REMITTER" property="remitter"/>
        <result column="REMITTANCE_AMOUNT" property="remittanceAmount"/>
        <result column="REMITTANCE_DATE" property="remittanceDate"/>
        <result column="REMARKS" property="remarks"/>
        <result column="STATUS" property="status"/>
        <result column="VOUCHER_NUMBER" property="voucherNumber"/>
        <result column="CONFIRM_TIME" property="confirmTime"/>
        <result column="PK_CONFIRM" property="pkConfirm"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG,
        PK_COUNTRY,
        CREATION_TIME,
        MODIFIED_TIME,
        PK_CREATOR,
        PK_MODIFIED,
        PK_ID, PK_MEMBER, REMITTER, REMITTANCE_AMOUNT, REMITTANCE_DATE, REMARKS, STATUS, VOUCHER_NUMBER, CONFIRM_TIME, PK_CONFIRM
    </sql>


    <select id="selectRepeatRecord" resultMap="BaseResultMap">
        SELECT *
        FROM cu_member_recharge_match rm
        WHERE rm.del_flag = 0
        AND rm.pk_country = #{pkCountry}
        AND (rm.remitter, rm.remittance_amount,to_char(rm.remittance_date,'dd/MM/yyyy'),rm.voucher_number) IN
        <foreach item="rechargeMatch" collection="rechargeMatchList" open="(" separator="," close=")">
            <choose>
                <when test="rechargeMatch.voucherNumber!=null">
                    (#{rechargeMatch.remitter},#{rechargeMatch.remittanceAmount},#{rechargeMatch.remittanceDate},#{rechargeMatch.voucherNumber})
                </when>
                <otherwise>
                    (#{rechargeMatch.remitter},#{rechargeMatch.remittanceAmount},#{rechargeMatch.remittanceDate},0)
                </otherwise>
            </choose>

        </foreach>
    </select>


    <select id="findRechargeMatchList" resultType="com.hzs.common.domain.member.account.CuMemberRechargeMatchExt">
        SELECT rm.pk_id             pkId,
               rm.remitter          remitter,
               rm.remittance_amount remittanceAmount,
               rm.remittance_date   remittanceDate,
               rm.remarks           remarks,
               rm.voucher_number    voucherNumber,
               pem.member_code      memberCode,
               rm.creation_time     creationTime,
               rm.status            status,
               con.member_name      ConfirmName,
               con.member_code      ConfirmCode,
               rm.confirm_time      confirmTime,
               rm.bank_name         bankName
        FROM cu_member_recharge_match rm
                 LEFT JOIN cu_member pem
                           ON rm.pk_member = pem.pk_id
                               AND pem.del_flag = 0
                               AND pem.pay_status = 1
                               AND pem.category = 0
                 LEFT JOIN cu_member con
                           ON rm.pk_confirm = con.pk_id
                               AND con.del_flag = 0
                               AND con.pay_status = 1
                               AND con.category = 0
        WHERE rm.del_flag = 0
          AND rm.pk_country = #{pkCountry}
        <if test="remitter != null and remitter != ''">
            and rm.remitter like '%' || #{remitter} || '%'
        </if>
        <if test="remittanceAmount != null">
            and  rm.remittance_amount = #{remittanceAmount}
        </if>
        <if test="startDate != null and startDate != ''">
            and rm.remittance_date  >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and rm.remittance_date  &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="remarks != null and remarks != ''">
            and rm.remarks like '%'|| #{remarks} || '%'
        </if>
        <if test="memberCode != null and memberCode != ''">
            and pem.member_code like '%'|| #{memberCode} || '%'
        </if>
        <if test="status != null">
            and rm.status = #{status}
        </if>
        ORDER BY rm.creation_time DESC
    </select>



    <select id="findRechargeMatchMemberList" resultType="com.hzs.common.domain.member.account.CuMemberRechargeMatch">
        SELECT rm.pk_id             pkId,
            rm.remitter          remitter,
            rm.remittance_amount remittanceAmount,
            rm.remittance_date   remittanceDate,
            rm.remarks           remarks,
            rm.voucher_number    voucherNumber,
            rm.bank_name         bankName
        FROM cu_member_recharge_match rm
        WHERE rm.del_flag = 0
        AND rm.status = 1
        AND rm.pk_country = #{pkCountry}
        <if test="remitter != null and remitter != ''">
            AND regexp_like(rm.remitter,'^'|| #{remitter} ||'|[[:space:]]'|| #{remitter},'i')
        </if>
        <if test="remittanceAmount != null">
            and  rm.remittance_amount = #{remittanceAmount}
        </if>
        <if test="startDate != null and startDate != ''">
            and rm.remittance_date = to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="voucherNumber != null">
            and rm.VOUCHER_NUMBER = #{voucherNumber}
        </if>
        ORDER BY rm.creation_time DESC
    </select>

</mapper>
