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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberTrade" type="com.hzs.common.domain.member.account.CuMemberTrade">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="TRADE_CODE" property="tradeCode"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="TRADE_TYPE" property="tradeType"/>
        <result column="TRADE_CLASSIFY" property="tradeClassify"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="TRADE_AMOUNT" property="tradeAmount"/>
        <result column="TRADE_BALANCE" property="tradeBalance"/>
        <result column="TRADE_BEFORE_BALANCE" property="tradeBeforeBalance"/>
        <result column="SOURCE_CODE" property="sourceCode"/>
        <result column="TRADE_STATUE" property="tradeStatue"/>
        <result column="REMARKS" property="remarks"/>
    </resultMap>

    <sql id="select_base">
        select t.*, m.member_code as memberCode, m.member_name as memberName
        from cu_member_trade t
                 left join cu_member m
                           on t.pk_member = m.pk_id
    </sql>

    <sql id="ids_where">
        <where>
            t.del_flag='0'
            and t.pk_id in
            <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </sql>

    <sql id="list_where">
        <where>
            t.del_flag='0'
            <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 t.TRADE_AMOUNT = #{tradeAmount}
            </if>
            <if test="remarks != null and remarks != ''">
                and m.REMARKS like #{remarks} || '%'
            </if>
            <if test="pkAccount != null">
                and t.PK_ACCOUNT = #{pkAccount}
            </if>
            <if test="tradeType != null">
                and t.TRADE_TYPE = #{tradeType}
            </if>
            <if test="sourceCode != null and sourceCode != ''">
                and t.SOURCE_CODE = #{sourceCode}
            </if>
            <if test="pkCountry != null">
                and t.pk_country = #{pkCountry}
            </if>
            <if test="tradeClassify != null">
                and t.TRADE_CLASSIFY = #{tradeClassify}
            </if>
            <if test="startDate != null and startDate != ''">
                and t.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and t.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </sql>

    <select id="selectMemberTradeList" resultType="com.hzs.common.domain.member.account.CuMemberTradeExt">
        <include refid="select_base"/>
        <choose>
            <when test="pkIds != null and pkIds.size >0">
                <include refid="ids_where"></include>
            </when>
            <otherwise>
                <include refid="list_where"></include>
            </otherwise>
        </choose>
        order by t.creation_time Desc,t.PK_ID Desc
    </select>

    <select id="queryCuMemberTrade" resultMap="CuMemberTrade">
        select min(pk_id) pk_id, pk_account, sum(trade_amount) trade_amount, max(pk_country) pk_country
        from cu_member_trade
        where pk_member = #{pkMember}
          and (trade_classify != 4
              or
                (trade_classify = 4 and trade_type = 402)
            )
          and source_code = #{sourceCode}
          and del_flag = 0
        group by pk_account
        order by pk_id
    </select>


    <insert id="batchSaveTradeByMemberCode" parameterType="com.hzs.common.domain.member.account.CuMemberTrade">
        insert into CU_MEMBER_TRADE (PK_ID, PK_MEMBER, TRADE_CODE, PK_ACCOUNT, TRADE_TYPE,
        TRADE_CLASSIFY, PK_RATE, TRADE_AMOUNT, TRADE_BALANCE,TRADE_BEFORE_BALANCE,
        SOURCE_CODE, TRADE_STATUE, REMARKS, PK_COUNTRY, CREATION_TIME, PK_CREATOR)
        SELECT cu_member_trade_seq.nextval,t.* FROM
        (
        <foreach collection="cuMemberTrades" item="item" separator="UNION ALL">
            SELECT #{item.pkMember},#{item.tradeCode},#{item.pkAccount},#{item.tradeType}
            ,#{item.tradeClassify},#{item.pkRate},#{item.tradeAmount},#{item.tradeBalance},#{item.tradeBeforeBalance}
            ,#{item.sourceCode, jdbcType=VARCHAR},#{item.tradeStatue},
            <choose>
                <when test="item.remarks != null">
                    #{item.remarks},
                </when>
                <otherwise>
                    null,
                </otherwise>
            </choose>
            #{item.pkCountry},sysdate,#{item.pkCreator}
            FROM dual
        </foreach>
        ) t
    </insert>

    <select id="selectWattleDetail" resultType="com.hzs.common.domain.member.account.CuMemberTradeExt">
        select t.trade_code tradeCode,
        t.source_code sourceCode,
        t.pk_account pkAccount,
        t.trade_type tradeType,
        t.trade_amount tradeAmount,
        t.trade_balance tradeBalance,
        t.creation_time creationTime,
        t.remarks remarks,
        r.member_remarks memberRemarks
        from cu_member_trade t
        left join cu_member_recharge r
        on t.trade_code = r.recharge_code
        and r.del_flag = 0
        and r.approve_state = 2
        where t.del_flag=0
        and t.pk_country = #{pkCountry}
        and t.pk_member = #{pkMember}
        <if test="pkAccount != null">
            and t.PK_ACCOUNT = #{pkAccount}
        </if>
        <if test="tradeType != null">
            and t.TRADE_TYPE = #{tradeType}
        </if>
        <if test="startDate != null and startDate != ''">
            and t.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and t.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        order by t.creation_time desc,t.PK_ID desc
    </select>


    <select id="selectTradeType" resultType="Integer">
        select distinct t.trade_type
        from cu_member_trade t
        where t.del_flag = 0
          and t.pk_country = #{pkCountry}
          and t.pk_member = #{pkMember}
        order by t.trade_type
    </select>


    <select id="selectMemberTradeByTradeCodeAndTradeType" resultMap="CuMemberTrade">
        select t.pk_member,
               t.trade_code,
               t.pk_account,
               t.trade_type,
               t.trade_classify,
               t.pk_rate,
               t.trade_amount,
               t.trade_balance,
               t.trade_before_balance,
               t.source_code,
               t.trade_statue,
               t.remarks,
               t.pk_country
        from cu_member_trade t
        where t.del_flag = 0
          and t.source_code = #{tradeCode}
          and t.trade_type = #{tradeType}
    </select>

    <update id="updateSourceCodeByTradeCode">
        update cu_member_trade t set t.source_code = #{sourceCode},
        t.pk_modified = #{pkModified},
        t.modified_time = sysdate
        where t.trade_code in
        <foreach collection="tradeCodes" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <!-- 查询会员订单产生的最新交易明细 -->
    <select id="querySourceCodeLastTrade" resultMap="CuMemberTrade">
        select *
        from cu_member_trade cmt
        where cmt.pk_id in
        (select max(pk_id)
        from cu_member_trade cmt
        where cmt.del_flag = 0
        and cmt.pk_member = #{pkMember}
        and cmt.source_code = #{sourceCode}
        <if test="tradeClassify != null">
            and cmt.trade_classify = #{tradeClassify}
        </if>
        group by cmt.trade_classify, cmt.pk_account)
    </select>

    <select id="selectMemberTradeBySourceCode" resultMap="CuMemberTrade">
        select *
        from cu_member_trade t
                 left join bd_account a
                           on t.pk_account = a.pk_id
                               and a.del_flag = 0
        where t.del_flag = 0
          and a.field_value = 4
          and t.source_code = #{sourceCode}
    </select>

</mapper>
