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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.account.CuMemberAccountDetail">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="ACCOUNT1" property="account1"/>
        <result column="LOCK_ACCOUNT1" property="lockAccount1"/>
        <result column="LOCK_TRANSFER_ACCOUNT1" property="lockTransferAccount1"/>
        <result column="ACCOUNT2" property="account2"/>
        <result column="LOCK_ACCOUNT2" property="lockAccount2"/>
        <result column="LOCK_TRANSFER_ACCOUNT2" property="lockTransferAccount2"/>
        <result column="ACCOUNT3" property="account3"/>
        <result column="LOCK_ACCOUNT3" property="lockAccount3"/>
        <result column="LOCK_TRANSFER_ACCOUNT3" property="lockTransferAccount3"/>
        <result column="ACCOUNT4" property="account4"/>
        <result column="LOCK_ACCOUNT4" property="lockAccount4"/>
        <result column="LOCK_TRANSFER_ACCOUNT4" property="lockTransferAccount4"/>
        <result column="LOCK_WITHDRAW_ACCOUNT4" property="lockWithdrawAccount4"/>
        <result column="WITHDRAW_ACCOUNT4" property="withdrawAccount4"/>
        <result column="CONSUME_ACCOUNT4" property="consumeAccount4"/>
        <result column="ACCOUNT5" property="account5"/>
        <result column="LOCK_ACCOUNT5" property="lockAccount5"/>
        <result column="LOCK_TRANSFER_ACCOUNT5" property="lockTransferAccount5"/>
        <result column="ACCOUNT6" property="account6"/>
        <result column="LOCK_ACCOUNT6" property="lockAccount6"/>
        <result column="LOCK_TRANSFER_ACCOUNT6" property="lockTransferAccount6"/>
        <result column="ACCOUNT7" property="account7"/>
        <result column="LOCK_ACCOUNT7" property="lockAccount7"/>
        <result column="LOCK_TRANSFER_ACCOUNT7" property="lockTransferAccount7"/>
        <result column="ACCOUNT8" property="account8"/>
        <result column="LOCK_ACCOUNT8" property="lockAccount8"/>
        <result column="LOCK_TRANSFER_ACCOUNT8" property="lockTransferAccount8"/>
        <result column="ACCOUNT9" property="account9"/>
        <result column="LOCK_ACCOUNT9" property="lockAccount9"/>
        <result column="LOCK_TRANSFER_ACCOUNT9" property="lockTransferAccount9"/>
        <result column="ACCOUNT10" property="account10"/>
        <result column="LOCK_ACCOUNT10" property="lockAccount10"/>
        <result column="LOCK_TRANSFER_ACCOUNT10" property="lockTransferAccount10"/>
        <result column="ACCOUNT11" property="account11"/>
        <result column="ACCOUNT12" property="account12"/>
        <result column="ACCOUNT13" property="account13"/>
        <result column="ACCOUNT14" property="account14"/>
        <result column="ACCOUNT15" property="account15"/>
        <result column="ACCOUNT16" property="account16"/>
        <result column="ACCOUNT17" property="account17"/>
        <result column="ACCOUNT18" property="account18"/>
        <result column="ACCOUNT19" property="account19"/>
        <result column="ACCOUNT20" property="account20"/>
        <result column="CREATE_TIME" property="createTime"/>
        <result column="PK_RATE" property="pkRate"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG,
        PK_COUNTRY,
        PK_ID, PK_MEMBER, ACCOUNT1, LOCK_ACCOUNT1, LOCK_TRANSFER_ACCOUNT1, ACCOUNT2, LOCK_ACCOUNT2, LOCK_TRANSFER_ACCOUNT2, ACCOUNT3, LOCK_ACCOUNT3, LOCK_TRANSFER_ACCOUNT3, ACCOUNT4, LOCK_ACCOUNT4, LOCK_TRANSFER_ACCOUNT4, LOCK_WITHDRAW_ACCOUNT4, WITHDRAW_ACCOUNT4, CONSUME_ACCOUNT4, ACCOUNT5, LOCK_ACCOUNT5, LOCK_TRANSFER_ACCOUNT5, ACCOUNT6, LOCK_ACCOUNT6, LOCK_TRANSFER_ACCOUNT6, ACCOUNT7, LOCK_ACCOUNT7, LOCK_TRANSFER_ACCOUNT7, ACCOUNT8,
        LOCK_ACCOUNT8, LOCK_TRANSFER_ACCOUNT8, ACCOUNT9, LOCK_ACCOUNT9, LOCK_TRANSFER_ACCOUNT9, ACCOUNT10, LOCK_ACCOUNT10, LOCK_TRANSFER_ACCOUNT10,
        ACCOUNT11, ACCOUNT12, ACCOUNT13, ACCOUNT14, ACCOUNT15, ACCOUNT16, ACCOUNT17, ACCOUNT18, ACCOUNT19, ACCOUNT20, CREATE_TIME, PK_RATE
    </sql>

    <sql id="select_list">
     select su.create_time,
            sum(su.account1) as account1,
            sum(su.account2) as account2,
            sum(su.account3) as account3,
            sum(su.account4) as account4,
            sum(su.account5) as account5,
            sum(su.account6) as account6,
            sum(su.account7) as account7,
            sum(su.account8) as account8,
            sum(su.account9) as account9,
            sum(su.account10) as account10,
            sum(su.account11) as account11,
            sum(su.account12) as account12,
            sum(su.account13) as account13,
            sum(su.account14) as account14
       from cu_member_account_detail su
    </sql>

    <sql id="list_where">
        <where>
            su.del_flag=0
            <if test="pkCountry != null">
                and su.pk_country = #{pkCountry}
            </if>
            <if test="startDate != null and startDate != ''">
                and su.create_time >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and su.create_time &lt;= to_date(#{endDate}, 'yyyy-mm-dd')
            </if>
        </where>
        group by su.create_time
    </sql>


    <sql id="select_detail">
        select a.*,
               s.member_name memberName,
               s.member_code memberCode,
               s.team_name   teamName,
               s.vertex_name vertexName,
               s.vertex_id   vertexId,
               s.team_id     teamId
          from cu_member_account_detail a,
               (select u.member_code,
                       u.member_name,
                       u.pk_id,
                       v.pk_id       vertex_id,
                       t.pk_id       team_id,
                       v.vertex_name,
                       t.team_name
                  from cu_member u
                  left join bd_vertex v
                    on u.pk_vertex = v.pk_id
                   and v.del_flag = 0
                  left join cu_member_team t
                    on u.pk_team_code = t.pk_id
                   and t.del_flag = 0
                 where u.del_flag = 0) s
    </sql>

    <sql id="detail_where">
        <where>
            a.pk_member = s.pk_id
            and a.del_flag = 0
            <if test="createTime != null and createTime != ''">
                and a.create_time = to_date(#{createTime}, 'yyyy-mm-dd')
            </if>
            <if test="memberName != null and memberName != ''">
                and s.member_name like '%' || #{memberName} || '%'
            </if>
            <if test="memberCode != null and memberCode != ''">
                and s.member_code like '%'|| #{memberCode} || '%'
            </if>
            <if test="pkCountry != null">
                and a.PK_COUNTRY = #{pkCountry}
            </if>
            <if test="teamId != null">
                and s.team_id = #{teamId}
            </if>
            <if test="vertexId != null">
                and s.vertex_id = #{vertexId}
            </if>
        </where>
    </sql>
    <sql id="ids_detail_where">
        <where>
            a.del_flag=0
            and a.pk_id in
            <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </sql>

    <sql id="ids_where">
        <where>
            su.del_flag=0
            <if test="pkCountry != null">
                and su.pk_country = #{pkCountry}
            </if>
            and su.create_time in
            <foreach collection="createTimes" item="item" open="(" close=")" separator=",">
                to_date(#{item}, 'yyyy-mm-dd')
            </foreach>
        </where>
        group by su.create_time, su.pk_rate
    </sql>

    <select id="selectAccountDetailList" resultType="com.hzs.common.domain.member.account.CuMemberAccountDetail">
        <include refid="select_list"/>
        <choose>
            <when test="createTimes != null and createTimes.size >0">
                <include refid="ids_where"></include>
            </when>
            <otherwise>
                <include refid="list_where"></include>
            </otherwise>
        </choose>
        order by su.create_time
    </select>

    <select id="selectAccountDetailLookList" resultType="com.hzs.common.domain.member.account.CuMemberAccountDetailExt">
        <include refid="select_detail"/>
        <choose>
            <when test="pkIds != null and pkIds.size >0">
                <include refid="ids_detail_where"></include>
            </when>
            <otherwise>
                <include refid="detail_where"></include>
            </otherwise>
        </choose>
        order by a.create_time desc
    </select>

    <insert id="insertAccountDetail">
        insert into cu_member_account_detail
        (pk_id,
        pk_member,
        account1,
        lock_account1,
        lock_transfer_account1,
        account2,
        lock_account2,
        lock_transfer_account2,
        account3,
        lock_account3,
        lock_transfer_account3,
        account4,
        lock_account4,
        lock_transfer_account4,
        lock_withdraw_account4,
        withdraw_account4,
        consume_account4,
        account5,
        lock_account5,
        lock_transfer_account5,
        account6,
        lock_account6,
        lock_transfer_account6,
        account7,
        lock_account7,
        lock_transfer_account7,
        account8,
        lock_account8,
        lock_transfer_account8,
        account9,
        lock_account9,
        lock_transfer_account9,
        account10,
        lock_account10,
        lock_transfer_account10,
        account11,
        account12,
        account13,
        account14,
        account15,
        account16,
        account17,
        account18,
        account19,
        account20,
        create_time,
        pk_rate,
        del_flag,
        pk_country)
        select cu_member_account_detail_seq.nextval,
        a.pk_member,
        a.account1,
        a.lock_account1,
        a.lock_transfer_account1,
        a.account2,
        a.lock_account2,
        a.lock_transfer_account2,
        a.account3,
        a.lock_account3,
        a.lock_transfer_account3,
        a.account4,
        a.lock_account4,
        a.lock_transfer_account4,
        a.lock_withdraw_account4,
        a.withdraw_account4,
        a.consume_account4,
        a.account5,
        a.lock_account5,
        a.lock_transfer_account5,
        a.account6,
        a.lock_account6,
        a.lock_transfer_account6,
        a.account7,
        a.lock_account7,
        a.lock_transfer_account7,
        a.account8,
        a.lock_account8,
        a.lock_transfer_account8,
        a.account9,
        a.lock_account9,
        a.lock_transfer_account9,
        a.account10,
        a.lock_account10,
        a.lock_transfer_account10,
        a.account11,
        a.account12,
        a.account13,
        a.account14,
        a.account15,
        a.account16,
        a.account17,
        a.account18,
        a.account19,
        a.account20,
        trunc(sysdate) create_time,
        tt.rate pk_rate,
        a.del_flag,
        a.pk_country
        from cu_member_account a
        left join (select t.pk_id rate,
        t.pk_country,
        ROW_NUMBER() OVER(PARTITION BY t.pk_country ORDER BY t.effective_date DESC) rw
        from BD_CURRENCY t
        where t.del_flag = 0
        and t.enable_state = 0
        and t.effective_date &lt; sysdate) tt
        on a.pk_country = tt.pk_country
        and tt.rw = 1
        where a.del_flag = 0
        and tt.rate is not null
    </insert>

    <delete id="deleteAccontDetailByMonth">
        delete from CU_MEMBER_ACCOUNT_DETAIL d
        where d.create_time &lt; TRUNC(ADD_MONTHS(SYSDATE, -1), 'month')
    </delete>

</mapper>
