<?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.report.member.mapper.MemberReportMapper">


    <!--    查询伞下直推新增业绩 -会员 -->
    <select id="directPushNewList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        bc.NATIONAL_FLAG1 nationalFlag1,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode ,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        cm.PAY_TIME payTime ,
        ba.AWARDS_NAME awardsName,
        bv.VERTEX_NAME vertexName,
        cm.PK_SETTLE_COUNTRY pkCountry,
        cmt.team_name teamName
        from cu_member cm
        left join BD_COUNTRY bc on bc.PK_ID = cm.PK_SETTLE_COUNTRY
        left join bd_awards ba on ba.pk_id = cm.PK_AWARDS
        left join bd_vertex bv on bv.PK_ID = cm.PK_VERTEX
        left join cu_member_team cmt on cm.pk_team_code = cmt.pk_id
        where cm.del_flag = 0 and cm.pk_id=#{pkMember}
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME=#{memberName}
        </if>
        <if test="pkVertex != null">
            and cm.PK_VERTEX=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and cm.PK_TEAM_CODE=#{pkTeamCode}
        </if>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
    </select>

    <!--    查询伞下直推新增业绩 -业绩-->
    <select id="directPushNewAchieList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        CASE WHEN sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end orderAchieve,
        CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end orderAmount
        from SA_ORDER so
        where so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and so.pk_member in(
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and CATEGORY=0
        START WITH cm.pk_parent = #{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
        )
        <if test="orderTypeList != null">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startPayTime != null and endPayTime != null">
            and so.PAY_TIME between #{startPayTime, jdbcType=TIMESTAMP} and #{endPayTime, jdbcType=TIMESTAMP}
        </if>
    </select>

    <!--    查询个人直推新增业绩-->
    <select id="personDirectPushAchieList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        CASE WHEN sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end orderAchieve,
        CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end orderAmount
        from SA_ORDER so
        where so.DEL_FLAG=0
        and so.ORDER_STATUS=1
        and so.pk_member in(
        SELECT
        cm.pk_id
        FROM
        cu_member cm
        WHERE
        cm.del_flag = 0
        and CATEGORY=0
        and cm.pk_parent = #{pkMember}
        )
        <if test="orderTypeList != null">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startPayTime != null and endPayTime != null">
            and so.PAY_TIME between #{startPayTime, jdbcType=TIMESTAMP} and #{endPayTime, jdbcType=TIMESTAMP}
        </if>
    </select>


    <select id="memberConsumptionList" resultType="com.hzs.report.member.vo.MemberConsumptionVo">
        select cm.MEMBER_CODE memberCode ,cm.MEMBER_NAME
        memberName,nvl(a.orderAmount,0)orderAmount,nvl(a.orderAchieve,0)orderAchieve,cm.pk_id pkMember
        from cu_member cm

        left join (
        select nvl(sum(si.PRICE*si.QUANTITY),0) orderAmount, nvl(sum(si.ACHIEVEMENT*si.QUANTITY),0)
        orderAchieve,so.PK_CREATOR from SA_ORDER_ITEMS si
        left join SA_ORDER so on so.PK_ID = si.PK_ORDER
        where si.del_flag = 0 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkWares != null">
            and si.PK_WARES=#{pkWares}
        </if>
        group by so.PK_CREATOR
        )a on a.PK_CREATOR = cm.pk_id
        where cm.del_flag = 0
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME=#{memberName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE=#{memberCode}
        </if>

    </select>

    <select id="memberResettleAchieve" resultType="com.hzs.report.member.vo.MemberResettleAchieveVo">
        select bc.NATIONAL_FLAG1 nationalFlag,e.achieve,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        cm.PK_AWARDS,
        cm.PK_TEAM_CODE,
        cm.CONSUME_MONEY consumeMoney,
        cm.CONSUME_ACHIEVE consumeAchieve,
        cm.PAY_TIME payTime,
        ba.AWARDS_NAME awardsName,
        mt.TEAM_NAME teamCodeName,
        bv.VERTEX_NAME vertexName,
        cm.PAY_TIME orderTime,
        cm.pk_id pkMember,
        e.place_dept placeDept
        from cu_member cm
        left join BD_COUNTRY bc
        on bc.PK_ID = cm.PK_SETTLE_COUNTRY
        left join bd_awards ba
        on ba.pk_id = cm.PK_AWARDS
        left join cu_member_team mt
        on mt.pk_id = cm.PK_TEAM_CODE
        left join bd_vertex bv
        on bv.pk_id = cm.PK_VERTEX


        inner join (
        select #{pkMember} pkMember, 1 place_dept,nvl(sum(so.ORDER_ACHIEVE), 0) achieve
        from SA_ORDER so
        inner join(
        select a.pk_id from (
        SELECT pk_id,del_flag,CUSTOMER_TYPE
        FROM cu_member where 1 = 1
        <if test="pkAwards != null">
            and PK_AWARDS=#{pkAwards}
        </if>
        <if test="pkVertex != null">
            and PK_VERTEX=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and PK_TEAM_CODE=#{pkTeamCode}
        </if>
        START WITH pk_place_parent = #{pkMember} and place_dept=1
        CONNECT BY pk_place_parent = PRIOR pk_id) a
        where a.del_flag =0
        ) a
        on so.pk_member=a.pk_id
        where so.del_flag = 0
        and so.ORDER_STATUS = 1
        and pk_country=#{pkCountry}
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        union all

        select #{pkMember} pkMember, 2 place_dept,nvl(sum(so.ORDER_ACHIEVE), 0) achieve
        from SA_ORDER so
        inner join(
        select a.pk_id from (
        SELECT pk_id,del_flag,CUSTOMER_TYPE
        FROM cu_member where 1 = 1
        <if test="pkAwards != null">
            and PK_AWARDS=#{pkAwards}
        </if>
        <if test="pkVertex != null">
            and PK_VERTEX=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and PK_TEAM_CODE=#{pkTeamCode}
        </if>
        START WITH pk_place_parent = #{pkMember} and place_dept=2
        CONNECT BY pk_place_parent = PRIOR pk_id) a
        where a.del_flag =0
        ) a
        on so.pk_member=a.pk_id
        where so.del_flag = 0
        and so.ORDER_STATUS = 1
        and pk_country=#{pkCountry}
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null  and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ) e on e.pkMember = cm.pk_id
        where cm.del_flag = 0
        and cm.pk_id=#{pkMember}
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME=#{memberName}
        </if>

        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>

    </select>

    <select id="getOrderCurrencyList" resultType="com.hzs.report.member.vo.OrderCurrencyVo">

        select cm.MEMBER_CODE memberCode,cm.MEMBER_NAME memberName,so.ORDER_CODE orderCode,so.ORDER_TYPE
        orderType,so.ORDER_AMOUNT orderAmount,so.POSTAGE postage
        from sa_order so
        left join CU_MEMBER cm on cm.pk_id = so.PK_CREATOR
        where 1 = 1 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="orderCode != null and orderCode != ''">
            and so.ORDER_CODE like #{orderCode} || '%'
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE like #{orderType} || '%'
        </if>
    </select>

    <select id="getBySourceCodeMemberTradeList" resultType="com.hzs.report.member.vo.MemberTradeVo">

        select mt.* from cu_member_trade mt where 1 = 1
        <if test="orderCodeList != null and orderCodeList.size > 0">
            and mt.SOURCE_CODE in
            <foreach collection="orderCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getByProvinceOrderDistribution" resultType="com.hzs.report.member.vo.OrderDistribution">
        select nvl(sum(sa.ORDER_ACHIEVE),0) orderAchieve,ba.NAME province from sa_order sa
        left join bd_area ba on ba.pk_id = sa.REC_PROVINCE
        where sa.del_flag = 0 and sa.ORDER_STATUS = 1 and sa.PK_COUNTRY = #{pkCountry}
        <if test="startTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by sa.REC_PROVINCE ,ba.NAME
    </select>

    <select id="getByCityOrderDistribution" resultType="com.hzs.report.member.vo.OrderDistribution">
        select nvl(sum(sa.ORDER_ACHIEVE),0) orderAchieve,ba.NAME province,ci.NAME city from sa_order sa
        left join bd_area ba on ba.pk_id = sa.REC_PROVINCE
        left join bd_area ci on ci.pk_id = sa.REC_CITY
        where sa.del_flag = 0 and sa.ORDER_STATUS = 1 and sa.PK_COUNTRY = #{pkCountry}
        <if test="startTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by sa.REC_PROVINCE ,ci.NAME,ba.NAME
    </select>

    <select id="getByDistrictOrderDistribution" resultType="com.hzs.report.member.vo.OrderDistribution">
        select nvl(sum(sa.ORDER_ACHIEVE),0) orderAchieve,ba.NAME province,ci.NAME city,ar.NAME district from sa_order sa
        left join bd_area ba on ba.pk_id = sa.REC_PROVINCE
        left join bd_area ci on ci.pk_id = sa.REC_CITY
        left join bd_area ar on ar.pk_id = sa.REC_COUNTY
        where sa.del_flag = 0 and sa.ORDER_STATUS = 1 and sa.PK_COUNTRY = #{pkCountry}
        <if test="startTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by sa.REC_PROVINCE ,ar.NAME , ci.NAME,ba.NAME
    </select>

    <select id="getProductOrderInfo" resultType="com.hzs.report.member.vo.ProductOrderInfoVo">
        select oi.PK_PRODUCT pkProduct,so.ORDER_TYPE orderType ,sum(oi.QUANTITY)quantity,min(a.productCode)
        productCode,min(a.productName) productName, min(a.classifyName) classifyName,max(oi.PRICE)
        productPrice,min(a.productUnit) productUnit,min(a.pkClassify) pkClassify,
        so.PK_COUNTRY pkCountry
        from SA_ORDER_ITEMS oi
        left join sa_order so on oi.PK_ORDER = so.pk_id
        left join (
        select bp.PRODUCT_CODE productCode,bp.PRODUCT_NAME productName,bc.NAME classifyName,bc.pk_id
        pkClassify,bp.RETAIL_PRICE productPrice,bp.pk_id pkProduct,bu.NAME productUnit
        from BD_PRODUCT bp
        left join bd_classify bc on bc.PK_ID = bp.PK_CLASSIFY
        left join bd_unit bu on bu.PK_ID = bp.UNIT
        where bp.del_flag = 0
        <if test="pkCountry != null">
            and bp.PK_COUNTRY = #{pkCountry}
        </if>
        ) a on a.pkProduct = oi.PK_PRODUCT
        where a.productCode is not null
        and oi.del_flag = 0 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="productName != null and productName !=''">
            and a.productName = #{productName}
        </if>
        <if test="pkClassify != null">
            and a.pkClassify = #{pkClassify}
        </if>
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by oi.PK_PRODUCT , so.ORDER_TYPE,so.PK_COUNTRY
    </select>

    <select id="reportSummaryList" resultType="com.hzs.report.member.vo.ReportSummaryVo">
        select cm.MEMBER_CODE memberCode,cm.MEMBER_NAME memberName,bv.VERTEX_NAME vertexName,mt.TEAM_NAME teamName,
        cm.REGISTER_AUTHORITY,cm.REGISTER_AUTHORITY registerAuthority,a.orderAmount consumeMoney ,a.orderAchieve
        consumeAchieve,
        cm.PK_SETTLE_COUNTRY pkCountry
        from CU_MEMBER cm
        left join bd_vertex bv on bv.PK_ID = cm.PK_VERTEX
        left join cu_member_team mt on mt.PK_ID = cm.PK_TEAM_CODE
        left join (
        select nvl(sum(so.ORDER_AMOUNT),0) orderAmount ,nvl(sum(ORDER_ACHIEVE),0) orderAchieve,so.pk_creator pkMember
        from SA_ORDER so
        where so.ORDER_TYPE in (1, 2, 4, 5)
        and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startCreateTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startCreateTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endCreateTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endCreateTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by so.pk_creator
        )a on a.pkMember = cm.PK_ID
        where 1 =1 and cm.REGISTER_AUTHORITY in (1,2) and cm.del_flag = 0 and cm.PAY_STATUS = 1
        and a.orderAmount > 0
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME = #{memberName}
        </if>
        <if test="pkVertex != null">
            and cm.PK_VERTEX = #{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and cm.PK_TEAM_CODE = #{pkTeamCode}
        </if>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="registerAuthority != null">
            and cm.REGISTER_AUTHORITY = #{registerAuthority}
        </if>

    </select>


    <select id="brollyBelowResettle" resultType="com.hzs.report.member.vo.MemberPerformanceVo">
        select 1 placeDept , nvl(sum(so.ORDER_ACHIEVE),0) achieve
        from SA_ORDER so
        left join cu_member t on so.PK_CREATOR = t.pk_id and t.PLACE_DEPT = 1
        where so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="orderTypeList != null">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>

        union all
        select 2 placeDept , nvl(sum(so.ORDER_ACHIEVE),0) achieve
        from SA_ORDER so
        left join cu_member t on so.PK_CREATOR = t.pk_id and t.PLACE_DEPT = 2
        where so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="orderTypeList != null">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>

    </select>

    <select id="productShippingDetailsList" resultType="com.hzs.report.member.vo.ProductShippingDetailsInfoVo">
        select bp.PRODUCT_CODE productCode,bp.PRODUCT_NAME productName,bc.NAME classifyName,oi.PK_AWARDS
        pkAwards,oi.QUANTITY quantity,
        oi.PRICE price,bw.AWARDS_NAME awardsName
        from sa_order_items oi
        left join sa_order so on so.pk_id = oi.pk_order
        left join BD_PRODUCT bp on bp.pk_id = oi.PK_PRODUCT
        left join bd_classify bc on bc.PK_ID = bp.PK_CLASSIFY
        left join bd_awards bw on bw.pk_id = oi.PK_AWARDS
        where 1 = 1
        and oi.del_flag = 0
        <if test="pkCountry != null">
            and oi.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkProduct != null">
            and oi.PK_PRODUCT = #{pkProduct}
        </if>
        <if test="pkClassify != null">
            and bc.pk_id = #{pkClassify}
        </if>
        <if test="pkAwards != null">
            and bw.pk_id = #{pkAwards}
        </if>
        <if test="orderType != null">
            and so.order_type = #{orderType}
        </if>

        <if test="productName != null and productName != ''">
            and bp.PRODUCT_NAME like #{productName} || '%'
        </if>
    </select>


    <select id="memberResettleAchieveInfo" resultType="com.hzs.report.member.vo.MemberResettleAchieveVo">
        select bc.NATIONAL_FLAG1 nationalFlag,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        cm.PK_AWARDS,
        cm.PK_TEAM_CODE,
        cm.CONSUME_MONEY consumeMoney,
        cm.CONSUME_ACHIEVE consumeAchieve,
        cm.PAY_TIME payTime,
        ba.AWARDS_NAME awardsName,
        mt.TEAM_NAME teamCodeName,
        bv.VERTEX_NAME vertexName,
        cm.PAY_TIME orderTime,
        cm.pk_id pkMember
        from cu_member cm
        left join BD_COUNTRY bc
        on bc.PK_ID = cm.PK_SETTLE_COUNTRY
        left join bd_awards ba
        on ba.pk_id = cm.PK_AWARDS
        left join cu_member_team mt
        on mt.pk_id = cm.PK_TEAM_CODE
        left join bd_vertex bv
        on bv.pk_id = cm.PK_VERTEX
        where cm.del_flag = 0
        and cm.pk_id=#{pkMember}
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME=#{memberName}
        </if>

        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="pkAwards != null">
            and ba.AWARDS_VALUE=#{pkAwards}
        </if>



    </select>

    <select id="queryMemberPlaceDeptList" resultType="integer">
        select cm.PLACE_DEPT
        from CU_MEMBER cm
        where cm.del_flag = 0
          and cm.pk_place_parent = #{pkMember}
    </select>

    <select id="queryMemberPlaceDeptAchieve" resultType="com.hzs.report.member.vo.MemberAchieveVO">

        select #{pkMember} pkMember,
        #{placeDept} place_dept,
        nvl(sum(so.ORDER_ACHIEVE), 0) achieve
        from SA_ORDER so
        inner join (select a.pk_id
        from (SELECT pk_id, del_flag, CUSTOMER_TYPE
        FROM cu_member
        where 1 = 1
        <if test="memberName != null and memberName != ''">
            and MEMBER_NAME=#{memberName}
        </if>
        <if test="pkAwards != null">
            and PK_AWARDS=#{pkAwards}
        </if>
        <if test="pkVertex != null">
            and PK_VERTEX=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and PK_TEAM_CODE=#{pkTeamCode}
        </if>
        START WITH pk_place_parent = #{pkMember}
        and place_dept = #{placeDept}
        CONNECT BY pk_place_parent = PRIOR pk_id) a
        where a.del_flag = 0) a
        on so.pk_member = a.pk_id
        where so.del_flag = 0
        and so.ORDER_STATUS = 1
        <choose>
            <when test="orderTypeList != null and orderTypeList.size() > 0">
                and so.ORDER_TYPE in
                <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and SO.ORDER_TYPE in (1, 2, 30, 20)
            </otherwise>
        </choose>
        <if test="startPayTime != null">
            and SO.PAY_TIME &gt;= #{startPayTime}
        </if>
        <if test="endPayTime != null">
            and SO.PAY_TIME &lt;= #{endPayTime}
        </if>
    </select>

    <select id="memberActiveList" resultType="com.hzs.report.member.vo.ActivationVO">

        select cm.MEMBER_CODE memberCode,cm.MEMBER_NAME memberName,bg.GRADE_NAME gradeName,ba.AWARDS_NAME awardsName ,
        a.payTime activeTime from CU_MEMBER cm
        inner join (
        select so.PK_CREATOR pkCreator , max(so.PAY_TIME) payTime from sa_order so
        where so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startTime != null">
            and so.PAY_TIME &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            and so.PAY_TIME &lt;= #{endTime}
        </if>
        group by so.PK_CREATOR

        ) a on a.pkCreator = cm.pk_id
        left join bd_grade bg on bg.pk_id = cm.PK_SETTLE_GRADE
        left join bd_awards ba on ba.pk_id = cm.PK_AWARDS
        where cm.del_flag = 0
        <if test="pkCountry != null">
            and cm.PK_COUNTRY=#{pkCountry}
        </if>
        order by a.payTime desc
    </select>

</mapper>
