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

    <!-- 撤销订单处理 -->
    <update id="cancelOrderHandle">
        update CU_MEMBER_TRADE_RECOIL cmtr
        set cmtr.del_flag      = 1,
            cmtr.modified_time = sysdate,
            cmtr.pk_modified   = #{pkMember}
        where cmtr.del_flag = 0
          and cmtr.source_pk_member = #{pkMember}
          and (select bg.grade_value
               from bd_grade bg
               where bg.pk_id = cmtr.pk_grade) >
              (select bg.grade_value from bd_grade bg where bg.pk_id = #{pkGrade})
    </update>

    <select id="selectUnissuedAmount" resultType="java.math.BigDecimal">
        SELECT NVL(SUM(tr.trade_amount), 0)
        FROM cu_member_trade_recoil tr
        WHERE tr.del_flag = 0
          AND tr.trade_statue = 1
          AND tr.pk_country = #{pkCountry}
          AND tr.pk_member = #{pkMember}
    </select>

    <select id="selectRecoilDetail" resultType="com.hzs.member.account.vo.CuMemberTradeVO">
        SELECT tr.trade_code tradeCode,
        tr.source_code sourceCode,
        tr.pk_account pkAccount,
        tr.trade_type tradeType,
        tr.trade_amount tradeAmount,
        tr.creation_time creationTime
        FROM cu_member_trade_recoil tr
        WHERE tr.del_flag = 0
        AND tr.trade_statue = 1
        and tr.pk_country = #{pkCountry}
        and tr.pk_member = #{pkMember}
        <if test="startDate != null and startDate != ''">
            and tr.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and tr.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ORDER BY tr.creation_time DESC, tr.PK_ID DESC
    </select>


    <select id="selectMemberRecoilDetailList" resultType="com.hzs.common.domain.member.account.CuMemberTradeExt">
        SELECT m.member_code memberCode,
        m.member_name memberName,
        cm.member_code sourceMemberCode,
        cm.member_name sourceMemberName,
        tr.*
        FROM cu_member_trade_recoil tr
        LEFT JOIN cu_member m
        ON tr.pk_member = m.pk_id
        AND m.del_flag = 0
        LEFT JOIN cu_member cm
        ON tr.source_pk_member = cm.pk_id
        AND cm.del_flag = 0
        WHERE tr.del_flag = 0
        AND tr.pk_country = #{pkCountry}
        <if test="memberName != null and memberName != ''">
            and m.member_name like #{memberName} || '%'
        </if>
        <if test="memberCode != null and memberCode != ''">
            and m.member_code like #{memberCode} || '%'
        </if>
        <if test="tradeAmount != null">
            and tr.TRADE_AMOUNT = #{tradeAmount}
        </if>
        <if test="pkAccount != null">
            and tr.PK_ACCOUNT = #{pkAccount}
        </if>
        <if test="tradeType != null">
            and tr.TRADE_TYPE = #{tradeType}
        </if>
        <if test="sourceCode != null and sourceCode != ''">
            and tr.SOURCE_CODE = #{sourceCode}
        </if>
        <if test="startDate != null and startDate != ''">
            and tr.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and tr.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ORDER BY tr.creation_time DESC, tr.PK_ID DESC
    </select>

</mapper>
