<?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.system.config.mapper.BdTradeWhiteConfigMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.system.config.BdTradeWhiteConfig">
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <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="WITHDRAWAL_STATE" property="withdrawalState"/>
        <result column="TRANSFER_STATE" property="transferState"/>
        <result column="MEMBER_TYPE" property="memberType"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG
        ,
        CREATION_TIME,
        MODIFIED_TIME,
        PK_COUNTRY,
        PK_CREATOR,
        PK_MODIFIED,
        PK_ID, PK_MEMBER, WITHDRAWAL_STATE, TRANSFER_STATE
    </sql>


    <select id="selectTradeWhiteConfigList" resultType="com.hzs.common.domain.system.config.ext.BdTradeWhiteConfigExt">
        select c.pk_id pkId,
        c.pk_member pkMember,
        c.member_type memberType,
        m.member_code memberCode,
        m.member_name as memberName,
        c.withdrawal_state withdrawalState,
        c.transfer_state transferState,
        c.creation_time creationTime,
        awa.PK_TRANSACTION pkAwards,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName
        from bd_trade_white_config c, cu_member m
        LEFT JOIN cu_member_team ct ON ct.pk_id = m.pk_team_code
        left join bd_vertex ver on ver.pk_id=m.PK_VERTEX
        left join bd_awards awa on awa.pk_id = m.PK_AWARDS
        where c.pk_member = m.pk_id
        and c.del_flag = 0
        and c.pk_country = #{pkCountry}
        <if test="pkMember != null">
            and c.PK_MEMBER = #{pkMember}
        </if>
        <if test="withdrawalState != null">
            and c.WITHDRAWAL_STATE = #{withdrawalState}
        </if>
        <if test="transferState != null">
            and c.TRANSFER_STATE = #{transferState}
        </if>
        <if test="startDate != null and startDate != ''">
            and c.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and c.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="memberName != null and memberName != ''">
            and m.member_name like #{memberName} || '%'
        </if>

        <if test="vertexId != null">
            and m.PK_VERTEX=#{vertexId}
        </if>
        <if test="memberType != null">
            and c.MEMBER_TYPE = #{memberType}
        </if>

        <if test="teamId != null">
            and m.pk_team_code=#{teamId}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and m.member_code like #{memberCode} || '%'
        </if>
        <if test="pkAwards != null">
            and m.pk_awards=#{pkAwards}
        </if>
        order by c.creation_time desc
    </select>


    <select id="selectTradeWhiteConfigByCondition" resultMap="BaseResultMap">
        select t.*
        from (select c.*
        from bd_trade_white_config c
        where c.del_flag = 0
        and c.pk_country = #{pkCountry}
        <if test="pkMember != null">
            and c.PK_MEMBER = #{pkMember}
        </if>
        <if test="withdrawalState != null">
            and c.WITHDRAWAL_STATE = #{withdrawalState}
        </if>
        <if test="transferState != null">
            and c.TRANSFER_STATE = #{transferState}
        </if>
        order by c.creation_time desc) t
        where rownum = 1
    </select>


    <select id="selectTradeWhiteConfigByUnderBlood" resultType="long">
        select c.pk_member
        from bd_trade_white_config c
        where c.del_flag = 0
        and c.member_type = #{memberType}
        and c.pk_country = #{pkCountry}
        <if test="withdrawalState != null">
            and c.WITHDRAWAL_STATE = #{withdrawalState}
        </if>
        <if test="transferState != null">
            and c.TRANSFER_STATE = #{transferState}
        </if>
    </select>

</mapper>
