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

    <!-- 通用查询结果列
          SUM_AMOUNT     自己累计金额
          BIG_AMOUNT     大区金额
          SMALL_AMOUNT   小区金额
          REG_AMOUNT     注册金额
          UPG_AMOUNT    升级金额
          REP_AMOUNT    复购金额
          CON_AMOUNT    重消金额

          TEAM_SUM_AMOUNT 伞下累计金额
          TEAM_BIG_AMOUNT 伞下大区金额
          TEAM_SMALL_AMOUNT   伞下小区金额
          TEAM_REG_AMOUNT 伞下注册金额
		  TEAM_UPG_AMOUNT 伞下升级金额
		  TEAM_REP_AMOUNT 伞下复购金额
		  TEAM_CON_AMOUNT 伞下重消金额
    -->
    <sql id="CuMemberAmountBoxTable">
          pk_member     NUMBER(20) not null,
          PK_PARENT     NUMBER(20) not null,
          SUM_AMOUNT     NUMBER(17,6) default 0 not null,
          BIG_AMOUNT     NUMBER(17,6) default 0 not null,
          SMALL_AMOUNT   NUMBER(17,6) default 0 not null,
          REG_AMOUNT     NUMBER(17,6) default 0 not null,
          UPG_AMOUNT    NUMBER(17,6) default 0 not null,
          REP_AMOUNT    NUMBER(17,6) default 0 not null,
          CON_AMOUNT    NUMBER(17,6) default 0 not null,

          TEAM_SUM_AMOUNT NUMBER(17,6) default 0 not null,
          TEAM_BIG_AMOUNT NUMBER(17,6) default 0 not null,
          TEAM_SMALL_AMOUNT   NUMBER(17,6) default 0 not null,
          TEAM_REG_AMOUNT NUMBER(17,6) default 0 not null,
		  TEAM_UPG_AMOUNT NUMBER(17,6) default 0 not null,
		  TEAM_REP_AMOUNT NUMBER(17,6) default 0 not null,
		  TEAM_CON_AMOUNT NUMBER(17,6) default 0 not null,

          SUM_BOX        NUMBER(8) default 0,
          BIG_BOX        NUMBER(8) default 0,
          SMALL_BOX      NUMBER(8) default 0,
          REG_BOX        NUMBER(8) default 0,
		  UPG_BOX        NUMBER(8) default 0,
		  REP_BOX        NUMBER(8) default 0,
		  CON_BOX        NUMBER(8) default 0,
		  INFLATE_BIG_BOX NUMBER(8) default 0,
		  INFLATE_SMALL_BOX NUMBER(8) default 0,

          TEAM_SUM_BOX   NUMBER(18) default 0,
          TEAM_BIG_BOX   NUMBER(18) default 0,
          TEAM_SMALL_BOX NUMBER(8) default 0,
          TEAM_REG_BOX   NUMBER(18) default 0,
		  TEAM_UPG_BOX   NUMBER(18) default 0,
		  TEAM_REP_BOX   NUMBER(18) default 0,
		  TEAM_CON_BOX   NUMBER(18) default 0,

          SUM_PV         NUMBER(17,6) default 0 not null,
          BIG_PV         NUMBER(17,6) default 0 not null,
          SMALL_PV       NUMBER(17,6) default 0 not null,
          REG_PV         NUMBER(17,6) default 0 not null,
		  UPG_PV         NUMBER(17,6) default 0 not null,
		  REP_PV         NUMBER(17,6) default 0 not null,
		  CON_PV         NUMBER(17,6) default 0 not null,

          TEAM_SUM_PV    NUMBER(17,6) default 0 not null,
          TEAM_BIG_PV    NUMBER(17,6) default 0 not null,
          TEAM_SMALL_PV  NUMBER(17,6) default 0 not null,
          TEAM_REG_PV    NUMBER(17,6) default 0 not null,
		  TEAM_UPG_PV    NUMBER(17,6) default 0 not null,
		  TEAM_REP_PV    NUMBER(17,6) default 0 not null,
		  TEAM_CON_PV    NUMBER(17,6) default 0 not null,

          PK_COUNTRY     NUMBER(4) not null
    </sql>
    <!-- 查询伞下直推新增业绩 -会员 -->
    <select id="directPushNewList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        cm.pk_id pkMember,
        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,
        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
        <if test="memberCodeList != null and memberCodeList.size>0 ">
            and cm.MEMBER_CODE in
            <foreach collection="memberCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
    </select>

    <!--    查询伞下直推新增业绩 -业绩-->
    <select id="directPushNewAchieList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT
            (endm.orderAchieve - stam.orderAchieve)
            <if test="dpo.payTime!=null">
                + NVL(so.order_achieve, 0)
            </if> AS orderAchieve,
            (endm.regAchieve - stam.regAchieve)
            <if test="dpo.payTime!=null">
                + NVL(so.regAchieve, 0)
            </if> AS regAchieve,
            (endm.upgAchieve - stam.upgAchieve)
            <if test="dpo.payTime!=null">
                + NVL(so.upgAchieve, 0)
            </if> AS upgAchieve,
            (endm.repAchieve - stam.repAchieve)
            <if test="dpo.payTime!=null">
                + NVL(so.repAchieve, 0)
            </if> AS repAchieve,
            (endm.conAchieve - stam.conAchieve)
            <if test="dpo.payTime!=null">
                + NVL(so.conAchieve, 0)
            </if> AS conAchieve,

            (endm.orderAmount - stam.orderAmount)
            <if test="dpo.payTime!=null">
                + NVL(so.order_amount, 0)
            </if> AS orderAmount,
            (endm.regAmount - stam.regAmount)
            <if test="dpo.payTime!=null">
                + NVL(so.regAmount, 0)
            </if> AS regAmount,
            (endm.upgAmount - stam.upgAmount)
            <if test="dpo.payTime!=null">
                + NVL(so.upgAmount, 0)
            </if> AS upgAmount,
            (endm.repAmount - stam.repAmount)
            <if test="dpo.payTime!=null">
                + NVL(so.repAmount, 0)
            </if> AS repAmount,
            (endm.conAmount - stam.conAmount)
            <if test="dpo.payTime!=null">
                + NVL(so.conAmount, 0)
            </if> AS conAmount,
            #{pkMember} AS pkMember
            FROM (
            SELECT
            NVL(SUM(cma.SUM_AMOUNT + cma.TEAM_SUM_AMOUNT), 0) AS orderAmount,
            NVL(SUM(cma.SUM_PV + cma.TEAM_SUM_PV), 0) AS orderAchieve,
            NVL(SUM(cma.REG_AMOUNT + cma.TEAM_REG_AMOUNT), 0) AS regAmount,
            NVL(SUM(cma.REG_PV + cma.TEAM_REG_PV), 0) AS regAchieve,
            NVL(SUM(cma.upg_AMOUNT + cma.TEAM_upg_AMOUNT), 0) AS upgAmount,
            NVL(SUM(cma.upg_PV + cma.TEAM_upg_PV), 0) AS upgAchieve,
            NVL(SUM(cma.rep_AMOUNT + cma.TEAM_rep_AMOUNT), 0) AS repAmount,
            NVL(SUM(cma.rep_PV + cma.TEAM_rep_PV), 0) AS repAchieve,
            NVL(SUM(cma.con_AMOUNT + cma.TEAM_con_AMOUNT), 0) AS conAmount,
            NVL(SUM(cma.con_PV + cma.TEAM_con_PV), 0) AS conAchieve,
            #{pkMember} AS pkMember
            FROM ${dpo.achieveTableNameEnd} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PK_MEMBER = #{pkMember}
            ) endm
            JOIN (
            SELECT
            NVL(SUM(cma.SUM_AMOUNT + cma.TEAM_SUM_AMOUNT), 0) AS orderAmount,
            NVL(SUM(cma.SUM_PV + cma.TEAM_SUM_PV), 0) AS orderAchieve,
            NVL(SUM(cma.REG_AMOUNT + cma.TEAM_REG_AMOUNT), 0) AS regAmount,
            NVL(SUM(cma.REG_PV + cma.TEAM_REG_PV), 0) AS regAchieve,
            NVL(SUM(cma.upg_AMOUNT + cma.TEAM_upg_AMOUNT), 0) AS upgAmount,
            NVL(SUM(cma.upg_PV + cma.TEAM_upg_PV), 0) AS upgAchieve,
            NVL(SUM(cma.rep_AMOUNT + cma.TEAM_rep_AMOUNT), 0) AS repAmount,
            NVL(SUM(cma.rep_PV + cma.TEAM_rep_PV), 0) AS repAchieve,
            NVL(SUM(cma.con_AMOUNT + cma.TEAM_con_AMOUNT), 0) AS conAmount,
            NVL(SUM(cma.con_PV + cma.TEAM_con_PV), 0) AS conAchieve,
            #{pkMember} AS pkMember
            FROM ${dpo.achieveTableNameSta} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PK_MEMBER = #{pkMember}
            ) stam
            ON endm.pkMember = stam.pkMember
            <if test="dpo.payTime!=null">
                LEFT JOIN (
                SELECT
                NVL(SUM(so.order_achieve), 0) order_achieve,
                NVL(SUM(so.order_amount), 0) order_amount,
                SUM(CASE WHEN so.order_type = 41 THEN so.order_amount ELSE 0 END) AS regAmount,
                SUM(CASE WHEN so.order_type = 42 THEN so.order_amount ELSE 0 END) AS upgAmount,
                SUM(CASE WHEN so.order_type = 43 THEN so.order_amount ELSE 0 END) AS repAmount,
                SUM(CASE WHEN so.order_type = 44 THEN so.order_amount ELSE 0 END) AS conAmount,

                SUM(CASE WHEN so.order_type = 41 THEN so.order_achieve ELSE 0 END) AS regAchieve,
                SUM(CASE WHEN so.order_type = 42 THEN so.order_achieve ELSE 0 END) AS upgAchieve,
                SUM(CASE WHEN so.order_type = 43 THEN so.order_achieve ELSE 0 END) AS repAchieve,
                SUM(CASE WHEN so.order_type = 44 THEN so.order_achieve ELSE 0 END) AS conAchieve,
                #{pkMember} pkMember
                FROM sa_order so
                WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                <if test="dpo.payTime != null and dpo.payTime != '' ">
                    and so.pay_time >= to_date(#{dpo.payTime}, 'yyyy-mm-dd')
                </if>
                <if test="dpo.payTime != null and dpo.payTime != '' ">
                    and so.pay_time &lt;= to_date(#{dpo.payTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                AND so.pk_member IN (
                SELECT t0.pk_id
                FROM (SELECT m.pk_id,
                m.del_flag,
                m.category,
                m.pay_status,
                m.system_type
                FROM cu_member m
                START WITH m.pk_id = #{pkMember}
                CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
                WHERE t0.del_flag = 0
                AND t0.category = 0)
                ) so
                ON endm.pkMember = so.pkMember
            </if>

        </foreach>
    </select>

    <!--    查询伞下直推新增盒数-->
    <select id="directPushNewBoxList" resultType="com.hzs.report.member.vo.DirectPushBoxVo">
        <foreach collection="dpbList" item="dpb" separator="union all">
            SELECT
            (endm.totalBox - stam.totalBox)
            <if test="dpb.enableStatus == 0 and dpo.payTime != null and dpo.payTime != ''">
                + NVL(so.BOX_NUM, 0)
            </if> AS totalBox,
            #{dpb.pkMember} AS pkMember
            FROM (
            SELECT
            NVL(SUM(cma.SUM_BOX + cma.TEAM_SUM_BOX), 0) AS totalBox,
            #{dpb.pkMember} AS pkMember
            FROM ${dpo.achieveTableNameEnd} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PK_MEMBER = #{dpb.pkMember}
            ) endm
            JOIN (
            SELECT
            NVL(SUM(cma.SUM_BOX + cma.TEAM_SUM_BOX), 0) AS totalBox,
            #{dpb.pkMember} AS pkMember
            FROM ${dpo.achieveTableNameSta} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PK_MEMBER = #{dpb.pkMember}
            ) stam
            ON endm.pkMember = stam.pkMember
            <if test="dpo.payTime!=null">
                LEFT JOIN (
                SELECT
                NVL(SUM(so.BOX_NUM), 0) BOX_NUM,
                #{dpb.pkMember} pkMember
                FROM sa_order so
                WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                <if test="dpo.payTime != null and dpo.payTime != '' ">
                    and so.pay_time >= to_date(#{dpo.payTime}, 'yyyy-mm-dd')
                </if>
                <if test="dpo.payTime != null and dpo.payTime != '' ">
                    and so.pay_time &lt;= to_date(#{dpo.payTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                AND so.pk_member IN (
                SELECT t0.pk_id
                FROM (SELECT m.pk_id,
                m.del_flag,
                m.category,
                m.pay_status,
                m.system_type
                FROM cu_member m
                START WITH m.pk_id = #{dpb.pkMember}
                CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
                WHERE t0.del_flag = 0
                AND t0.category = 0)
                ) so
                ON endm.pkMember = so.pkMember
            </if>

        </foreach>
    </select>


    <!--    查询伞下直推新增业绩 -业绩-->
    <select id="directPushNewAchieUpList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT
            (endm.orderAchieve - stam.orderAchieve)
            <if test="dpo.payTime!=null">
                + NVL(so.order_achieve, 0)
            </if> AS orderAchieve,
            #{pkMember} AS pkMember
            FROM (
            SELECT
            NVL(SUM(cma.CONSUME_PV + cma.TEAM_CONSUME_PV), 0) AS orderAchieve,
            #{pkMember} AS pkMember
            FROM ${dpo.achieveTableNameEnd} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PK_MEMBER = #{pkMember}
            ) endm
            JOIN (
            SELECT
            NVL(SUM(cma.CONSUME_PV + cma.TEAM_CONSUME_PV), 0) AS orderAchieve,
            #{pkMember} AS pkMember
            FROM ${dpo.achieveTableNameSta} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PK_MEMBER = #{pkMember}
            ) stam
            ON endm.pkMember = stam.pkMember
            <if test="dpo.payTime!=null">
                LEFT JOIN (
                    SELECT
                    NVL(SUM(so.order_achieve), 0) order_achieve,
                    #{pkMember} pkMember
                    FROM sa_order so
                    WHERE so.del_flag = 0
                    AND so.order_status = 1
                    AND so.order_type in (41, 42, 43, 44)
                <if test="dpo.payTime != null and dpo.payTime != '' ">
                    and so.pay_time >= to_date(#{dpo.payTime}, 'yyyy-mm-dd')
                </if>
                <if test="dpo.payTime != null and dpo.payTime != '' ">
                    and so.pay_time &lt;= to_date(#{dpo.payTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                    AND so.pk_member IN (
                    SELECT t0.pk_id
                    FROM (SELECT m.pk_id,
                    m.del_flag,
                    m.category,
                    m.pay_status,
                    m.system_type
                    FROM cu_member m
                    START WITH m.pk_id = #{pkMember}
                    CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
                    WHERE t0.del_flag = 0
                    AND t0.category = 0)
                ) so
                ON endm.pkMember = so.pkMember
            </if>

        </foreach>
    </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,e.amount,
        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,nvl(sum(so.ORDER_AMOUNT - nvl(so.consume_amount, 0)), 0) amount
        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
        <if test="pkCountry != null">
            and pk_country=#{pkCountry}
        </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>
        <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,nvl(sum(so.ORDER_AMOUNT - nvl(so.consume_amount, 0)), 0) amount
        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
        <if test="pkCountry != null">
            and pk_country=#{pkCountry}
        </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>
        <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="selectBrollyNewABAchieve"
            resultType="com.hzs.report.member.vo.MemberResettleAchieveVo">
        select cm.PAY_TIME orderTime,
        cm.pk_id pkMember,
        bc.NATIONAL_FLAG1 nationalFlag,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        ba.AWARDS_NAME awardsName,
        mt.TEAM_NAME teamCodeName,
        bv.VERTEX_NAME vertexName,
        nvl(cmpv.aSumAmount,0) aSumAmount,
        nvl(cmpv.bSumAmount,0) bSumAmount,
        nvl(cmpv.aSumPv,0) aSumPv,
        nvl(cmpv.bSumPv,0) bSumPv
        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,
        sum(a_sum_amount) aSumAmount,
        sum(b_sum_amount) bSumAmount,
        sum(a_sum_pv) aSumPv,
        sum(b_sum_pv) bSumPv
        from ${achieveTableName} where PERIOD=#{period} and PK_COUNTRY=#{pkCountry} and PK_MEMBER =#{pkMember}
        )cmpv on cm.pk_id=cmpv.pkMember
        where cm.del_flag = 0
        <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>
    </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,
        sum(oi.price * oi.QUANTITY) productPrice,
        min(a.productUnit) productUnit,
        min(a.pkClassify) pkClassify
        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.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
        ) 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
    </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
        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
        and so.del_flag = 0
        and so.ORDER_STATUS = 1
        <if test="pkCountry != null">
            and so.PK_COUNTRY=#{pkCountry}
        </if>
        <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="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="orderTypeList != null  and orderTypeList.size > 0">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="productName != null and productName != ''">
            and bp.PRODUCT_NAME like #{productName} || '%'
        </if>
    </select>

    <select id="productDetailQueryList" resultType="com.hzs.report.member.vo.ProductDetailQueryVo">
        select
        bp.PRODUCT_CODE productCode,
        bp.PRODUCT_NAME productName,
        so.ORDER_CODE orderCode,
        so.ORDER_TYPE orderType,
        so.CREATION_TIME createTime,
        cme.MEMBER_NAME memberName,
        cme.MEMBER_CODE memberCode,
        cme.PHONE phone,
        bv.VERTEX_NAME vertexName,
        mt.TEAM_NAME teamName,
        so.REC_NAME recName,
        so.REC_PHONE recPhone,
        cm.MEMBER_CODE purchaseCode,
        cm.MEMBER_NAME purchaseName,
        so.REC_PROVINCE recProvince,
        so.REC_CITY recCity,
        so.REC_COUNTY recCounty,
        so.REC_ADDRESS recAddress,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.POSTAGE postage,
        so.DELIVERY_WAY deliveryWay,
        so.PAY_TIME payTime,
        oi.PRICE price,
        oi.QUANTITY quantity,
        bw.WARES_CODE waresCode,
        bw.WARES_NAME waresName

        from SA_ORDER_ITEMS oi
        inner 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 cu_member cm on cm.PK_ID = so.PK_CREATOR
        left join cu_member cme on cme.PK_ID = so.PK_MEMBER
        left join bd_vertex bv on bv.PK_ID = cme.PK_VERTEX
        left join cu_member_team mt on mt.PK_ID = cm.PK_TEAM_CODE

        left join BD_WARES bw on bw.pk_id = oi.PK_WARES
        where oi.del_flag = 0 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="waresCode != null and waresCode != ''">
            and bw.WARES_CODE like #{waresCode} || '%'
        </if>
        <if test="waresName != null and waresName != ''">
            and bw.WARES_NAME like #{waresName} || '%'
        </if>
        <if test="productCode != null and productCode != ''">
            and bp.PRODUCT_CODE like #{productCode} || '%'
        </if>
        <if test="productName != null and productName != ''">
            and bp.PRODUCT_NAME like #{productName} || '%'
        </if>
        <if test="pkMember != null">
            and cm.pk_id in (
            SELECT m.pk_id
            FROM CU_MEMBER m
            start with m.PK_PARENT = #{pkMember}
            connect by m.PK_PARENT = prior m.pk_id

            )
        </if>
        <if test="memberName != null and memberName != ''">
            and cme.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="pkVertex != null">
            and cm.PK_VERTEX = #{pkVertex}
        </if>
        <if test="pkCountry != null">
            and cm.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkTeamCode != null">
            and cm.PK_TEAM_CODE = #{pkTeamCode}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cme.MEMBER_CODE = #{memberCode}
        </if>

        <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>
        order by oi.pk_id desc

    </select>


    <select id="selectSmallAreaNewAdd" resultType="com.hzs.report.member.vo.SmallAreaNewAddAchieveVO">
        SELECT m.member_code memberCode,
        m.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        t5.*
        FROM (SELECT *
        FROM (SELECT CASE
        WHEN t2.dept = 2 THEN
        t6.rightSumPv
        WHEN t2.dept = 1 THEN
        t6.leftSumPv
        END achieve,
        t6.rightSumPv,
        t6.leftSumPv,
        t2.dept,
        t2.a_sum_pv historyLeftSumPv,
        t2.b_sum_pv historyRightSumPv,
        t6.pk_member
        FROM (SELECT NVL(SUM(t1.a_new_pv), 0) leftSumPv,
        NVL(SUM(t1.b_new_pv), 0) rightSumPv,
        t1.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t1
        GROUP BY t1.pk_member) t6
        LEFT JOIN (SELECT ms.pk_member pkMember,
        ms.a_sum_pv,
        ms.b_sum_pv,
        CASE
        WHEN ms.a_sum_pv > ms.b_sum_pv THEN
        2
        WHEN ms.a_sum_pv &lt;= ms.b_sum_pv THEN
        1
        END dept
        FROM ${settleTable} ms
        WHERE ms.period = #{beforePeriod}) t2
        ON t6.pk_member = t2.pkmember) t3
        WHERE t3.achieve >= #{achieve}) t5
        LEFT JOIN cu_member m
        ON m.pk_id = t5.pk_member
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        LEFT JOIN bd_awards a
        ON a.pk_id = m.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = m.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
    </select>


    <select id="selectSmallAreaNewAddNemMember" resultType="com.hzs.report.member.vo.SmallAreaNewAddAchieveVO">
        SELECT t5.member_code memberCode,
        t5.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        NVL(t5.b_new_pv, 0)
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        NVL(t5.a_new_pv, 0)
        END achieve,
        t5.a_new_pv leftSumPv,
        t5.b_new_pv rightSumPv,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        2
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        1
        END dept,
        0 historyLeftSumPv,
        0 historyRightSumPv
        FROM (SELECT t1.pk_id,
        t1.member_code,
        t1.member_name,
        t1.pk_awards,
        t1.pk_vertex,
        t3.a_new_pv,
        t3.b_new_pv
        FROM (SELECT m.pk_id,
        m.member_code,
        m.member_name,
        m.pk_awards,
        m.pk_vertex
        FROM cu_member m
        WHERE m.creation_time >= to_date(#{startDate}, 'yyyy-mm-dd')
        AND m.creation_time &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        AND m.pk_settle_country= #{pkCountry}
        ) t1
        LEFT JOIN (SELECT NVL(SUM(t2.a_new_pv), 0) a_new_pv,
        NVL(SUM(t2.b_new_pv), 0) b_new_pv,
        t2.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t2
        GROUP BY t2.pk_member) t3
        ON t1.pk_id = t3.pk_member
        WHERE t3.a_new_pv >= #{achieve}
        AND t3.b_new_pv >= #{achieve}) t5
        LEFT JOIN bd_awards a
        ON a.pk_id = t5.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = t5.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
    </select>


    <select id="selectSmallAreaNewAddPage" resultType="com.hzs.report.member.vo.SmallAreaNewAddAchieveVO">
        SELECT m.member_code memberCode,
        m.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        t5.achieve,t5.leftsumpv,t5.rightsumpv,t5.dept,t5.historyleftsumpv,t5.historyrightsumpv
        FROM (SELECT *
        FROM (SELECT CASE
        WHEN t2.dept = 2 THEN
        t6.rightSumPv
        WHEN t2.dept = 1 THEN
        t6.leftSumPv
        END achieve,
        t6.rightSumPv,
        t6.leftSumPv,
        t2.dept,
        t2.a_sum_pv historyLeftSumPv,
        t2.b_sum_pv historyRightSumPv,
        t6.pk_member
        FROM (SELECT NVL(SUM(t1.a_new_pv), 0) leftSumPv,
        NVL(SUM(t1.b_new_pv), 0) rightSumPv,
        t1.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t1
        GROUP BY t1.pk_member) t6
        LEFT JOIN (SELECT ms.pk_member pkMember,
        ms.a_sum_pv,
        ms.b_sum_pv,
        CASE
        WHEN ms.a_sum_pv > ms.b_sum_pv THEN
        2
        WHEN ms.a_sum_pv &lt;= ms.b_sum_pv THEN
        1
        END dept
        FROM ${settleTable} ms
        WHERE ms.period = #{beforePeriod}) t2
        ON t6.pk_member = t2.pkmember) t3
        WHERE t3.achieve >= #{achieve}) t5
        LEFT JOIN cu_member m
        ON m.pk_id = t5.pk_member
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        LEFT JOIN bd_awards a
        ON a.pk_id = m.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = m.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
        UNION
        SELECT t5.member_code memberCode,
        t5.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        NVL(t5.b_new_pv, 0)
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        NVL(t5.a_new_pv, 0)
        END achieve,
        t5.a_new_pv leftSumPv,
        t5.b_new_pv rightSumPv,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        2
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        1
        END dept,
        0 historyLeftSumPv,
        0 historyRightSumPv
        FROM (SELECT t1.pk_id,
        t1.member_code,
        t1.member_name,
        t1.pk_awards,
        t1.pk_vertex,
        t3.a_new_pv,
        t3.b_new_pv
        FROM (SELECT m.pk_id,
        m.member_code,
        m.member_name,
        m.pk_awards,
        m.pk_vertex
        FROM cu_member m
        WHERE m.creation_time >= to_date(#{startDate}, 'yyyy-mm-dd')
        AND m.creation_time &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        AND m.pk_settle_country= #{pkCountry}
        ) t1
        LEFT JOIN (SELECT NVL(SUM(t2.a_new_pv), 0) a_new_pv,
        NVL(SUM(t2.b_new_pv), 0) b_new_pv,
        t2.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t2
        GROUP BY t2.pk_member) t3
        ON t1.pk_id = t3.pk_member
        WHERE t3.a_new_pv >= #{achieve}
        AND t3.b_new_pv >= #{achieve}) t5
        LEFT JOIN bd_awards a
        ON a.pk_id = t5.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = t5.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
    </select>

    <sql id="select_base">
        SELECT mt.*,
        t1.member_code AS memberCode,
        t1.member_name AS memberName,
        v.VERTEX_NAME AS vertexName,
        c.TEAM_NAME AS teamName
        FROM cu_member_trade mt
        INNER JOIN (SELECT m.pk_id,
        m.member_code,
        m.member_name,
        m.pk_vertex,
        m.pk_team_code
        FROM cu_member m
        WHERE m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        AND m.pk_settle_country = #{pkCountry}
        START WITH m.member_code = #{memberCode}
        <if test='treeType == "2"'>
            CONNECT BY PRIOR m.pk_id = m.pk_place_parent
        </if>
        <if test='treeType == "1"'>
            CONNECT BY PRIOR m.pk_id = m.pk_parent
        </if>
        ) t1
        ON mt.pk_member = t1.pk_id
        LEFT JOIN cu_member_team c
        ON c.pk_id = t1.pk_team_code
        AND c.del_flag = 0
        LEFT JOIN bd_vertex v
        ON v.pk_id = t1.PK_VERTEX
        AND v.del_flag = 0
    </sql>

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

    <select id="selectMemberUnderWalletList" resultType="com.hzs.report.member.vo.CuMemberWalletVO">
        <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>
        <!-- 2024.05.15 业务沟通，伞下钱包明细去掉已经退单的数据 -->
        and not exists (
        select 1
        from sa_order_charge_log socl
        where socl.del_flag = 0
        and socl.charge_type = 233
        and socl.approval_status = 2
        and instr(socl.after_data, mt.source_code) > 0
        )
        order by mt.creation_time Desc,mt.PK_ID Desc
    </select>

    <select id="productSaleStatList" resultType="com.hzs.report.member.vo.ProductSaleStatVo">
        select a.* from ( select min(oi.SKU_CODE) skuCode ,min(bp.PRODUCT_NAME) productName ,
        oi.PK_PRODUCT pkProduct, min(bu.NAME) unitStr, min(bw.WARES_NAME) waresName ,
        bw.pk_id pkWares, min(oi.SPECS_NAME) specsName,oi.SPECS_NAME_ID specsNameId,sum(oi.WARES_QUANTITY) waresQuantity,sum(oi.QUANTITY) productQuantity,
        min(bw.WARES_CODE) waresCode,sum(wd.QUANTITY) specsQuantity
        from SA_ORDER_ITEMS oi
        left join SA_ORDER so on so.pk_id = oi.PK_ORDER
        inner join bd_product bp on bp.pk_id = oi.PK_PRODUCT
        left join bd_wares bw on bw.pk_id = oi.PK_WARES and bw.del_flag = 0
        left join bd_wares_detail wd on wd.pk_id = oi.PK_WARES_DETAIL and wd.del_flag = 0
        left join bd_unit bu on bu.PK_ID = bp.UNIT


        where oi.del_flag = 0 and bp.del_flag = 0 and oi.pk_country = #{pkCountry} and bp.pk_country = #{pkCountry}

        <if test="skuCode != null and skuCode != ''">
            and oi.SKU_CODE like '%' || #{skuCode} || '%'
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE = #{orderType}
        </if>
        <if test="startTime != null">
            and so.PAY_TIME &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            and so.PAY_TIME &lt;= #{endTime}
        </if>
        <if test="productName != null and productName != ''">
            and bp.PRODUCT_NAME like '%' || #{productName} || '%'
        </if>
        group by bw.pk_id, oi.PK_PRODUCT , oi.SPECS_NAME_ID ) a order by a.pkproduct desc


    </select>


    <select id="sumQuantity" resultType="integer">
        select

        sum(oi.QUANTITY)quantity

        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.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
        ) a on a.pkProduct = oi.PK_PRODUCT
        where a.productCode is not null
        and oi.del_flag = 0 and so.del_flag = 0
        <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>

    </select>

    <!--     查询表是否存在-->
    <select id="selectExistTable" resultType="Integer">
        select count(1)
        from user_tables t
        where t.table_name = upper('${achieveTable}')
    </select>

<!--    查询直推会员-->
    <select id="selectDirectPushByPkMember" resultType="long">
        SELECT m.pk_id
        from cu_member m
        WHERE m.del_flag = 0
          AND m.category = 0
          AND m.pk_parent = #{pkMember}
    </select>

<!--    查询会员业绩-->
    <select id="selectMemberOrderAchieveByPkMember" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        SELECT sum(arealPv) arealPv
        FROM (SELECT NVL(mr.MONTH_CONSUME_PV, 0) arealPv
              FROM ${achieveTableNameEnd} mr
              WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
              union all
              SELECT NVL(SUM(so.order_achieve), 0) arealPv
              FROM sa_order so
              WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                AND so.pay_time >= #{endPayTime}
                AND so.pay_time &lt;= #{zonpayTime}
                AND so.pk_member = #{pkMember})
    </select>

<!--    查询会员个人业绩金额-->
    <select id="querMemberAchieveAmount" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        SELECT
        NVL(MR1.arealPv, 0) - NVL(MR2.SUM_PV, 0) AS arealPv,
        NVL(MR1.arealAm, 0) - NVL(MR2.SUM_AMOUNT, 0) AS arealAm
        FROM
        (
            SELECT sum(arealPv) arealPv,
            sum(arealAm) arealAm,
            #{pkMember} pkMember
            FROM (SELECT NVL(mr.SUM_PV, 0) arealPv,
            NVL(mr.SUM_AMOUNT, 0) arealAm
            FROM ${achieveTableNameEnd} mr
            WHERE mr.pk_member = #{pkMember}
            <if test="payTime!=null and payTime!=''">
                union all
                SELECT NVL(SUM(so.order_achieve), 0) arealPv,
                NVL(SUM(so.ORDER_AMOUNT), 0) arealAm
                FROM sa_order so
                WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                AND so.pay_time >= #{endPayTime}
                AND so.pay_time &lt;= #{zonpayTime}
                AND so.pk_member = #{pkMember}
            </if>
            )
        ) MR1
        FULL OUTER JOIN
        ${achieveTableNameSta} MR2
        ON
        MR1.pkMember = MR2.PK_MEMBER
        WHERE
        MR1.pkMember = #{pkMember}
        OR MR2.PK_MEMBER = #{pkMember}

    </select>

    <select id="selectMemberAchieve" resultType="com.hzs.report.member.vo.DirectPushNewVo">
            <foreach collection="pkMemberList" item="pkMember" separator="union all">
                SELECT SUM(t1.arealPv) arealPv ,#{pkMember} pkMember
                FROM (
                SELECT NVL(mr.MONTH_CONSUME_PV + mr.TEAM_MONTH_PV,0) arealPv
                FROM ${achieveTableNameEnd} mr
                WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
                UNION ALL
                SELECT NVL(SUM(so.order_achieve), 0) arealPv
                FROM sa_order so
                WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                AND so.pay_time >= #{endPayTime}
                AND so.pay_time &lt;= #{zonpayTime}
                AND so.pk_member IN (
                SELECT t0.pk_id
                FROM (SELECT m.pk_id,
                m.del_flag,
                m.category,
                m.pay_status,
                m.system_type
                FROM cu_member m
                START WITH m.pk_id = #{pkMember}
                CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
                WHERE t0.del_flag = 0
                AND t0.category = 0)) t1
            </foreach>
    </select>

    <select id="querMemberAchieveAmount" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        SELECT
        NVL(MR1.arealPv, 0) - NVL(MR2.SUM_PV, 0) AS arealPv,
        NVL(MR1.arealAm, 0) - NVL(MR2.SUM_AMOUNT, 0) AS arealAm
        FROM
        (
        SELECT sum(arealPv) arealPv,
        sum(arealAm) arealAm,
        #{pkMember} pkMember
        FROM (SELECT NVL(mr.SUM_PV, 0) arealPv,
        NVL(mr.SUM_AMOUNT, 0) arealAm
        FROM ${achieveTableNameEnd} mr
        WHERE mr.pk_member = #{pkMember}
        <if test="payTime!=null and payTime!=''">
            union all
            SELECT NVL(SUM(so.order_achieve), 0) arealPv,
            NVL(SUM(so.ORDER_AMOUNT), 0) arealAm
            FROM sa_order so
            WHERE so.del_flag = 0
            AND so.order_status = 1
            AND so.order_type in (41, 42, 43, 44)
            AND so.pay_time >= #{endPayTime}
            AND so.pay_time &lt;= #{zonpayTime}
            AND so.pk_member = #{pkMember}
        </if>
        )
        ) MR1
        FULL OUTER JOIN
        ${achieveTableNameSta} MR2
        ON
        MR1.pkMember = MR2.PK_MEMBER
        WHERE
        MR1.pkMember = #{pkMember}
        OR MR2.PK_MEMBER = #{pkMember}

    </select>

<!--    查询会员直推伞下业绩金额盒数-->
    <select id="querMemberAchieveAmountTeam" resultType="com.hzs.report.member.vo.DirectPushNewVo">
<!--        <foreach collection="pkMemberList" item="pkMember" separator="union all">-->
            SELECT
        COALESCE (NVL(MR1.arealPv, 0), 0) - COALESCE(NVL(MR2.arealPv, 0), 0) AS arealPv,
        COALESCE (NVL(MR1.arealAm, 0), 0) - COALESCE(NVL(MR2.arealAm, 0), 0) AS arealAm,
        COALESCE (NVL(MR1.arealBox, 0), 0) - COALESCE(NVL(MR2.arealBox, 0), 0) AS arealBox,
        COALESCE (MR1.PKMEMBER, MR2.PK_MEMBER) AS PKMEMBER
            FROM
            (
            SELECT sum(arealPv) arealPv,
            sum(arealAm) arealAm,
            sum(arealBox) arealBox,
            pk_member pkMember
            FROM (
            SELECT
            mr.pk_member,
           decode (PK_MEMBER,#{pkMember},NVL(MR.SUM_PV, 0), NVL(MR.SUM_PV, 0) + NVL(MR.TEAM_SUM_PV, 0)) arealPv,
           decode (PK_MEMBER,#{pkMember},NVL(MR.SUM_AMOUNT, 0), NVL(MR.SUM_AMOUNT, 0) + NVL(MR.TEAM_SUM_AMOUNT, 0)) arealAm,
           decode (PK_MEMBER,#{pkMember},NVL(MR.SUM_BOX, 0), NVL(MR.SUM_BOX, 0) + NVL(MR.TEAM_SUM_BOX, 0)) arealBox
            FROM ${achieveTableNameEnd} mr
            WHERE mr.pk_member in (
                SELECT m.pk_member
                FROM ${achieveTableNameEnd} m
                 WHERE m.pk_parent = #{pkMember} or (m.pk_member=#{pkMember})
                 )
            <if test="payTime!=null and payTime!=''">
                UNION ALL
                SELECT now.pk_member,
                now.arealPv,
                now.arealAm,
                now.arealBox
                FROM (
                SELECT
                COALESCE(a.pk_member, b.pk_member) AS pk_member,
                COALESCE(b.arealPv, 0) AS arealPv,
                COALESCE(a.arealAm, 0) + COALESCE(b.arealAm, 0) AS arealAm,
                COALESCE(b.arealBox, 0) AS arealBox
                FROM (
                    SELECT
                    NVL(SUM(so.ORDER_AMOUNT), 0) AS arealAm,
                    so.pk_member
                    FROM sa_order so
                    WHERE
                    so.del_flag = 0
                    AND so.order_status = 1
                    AND so.order_type IN (41, 42, 43, 44)
                    AND so.pay_time >= #{endPayTime}
                    AND so.pay_time &lt;= #{zonpayTime}
                    AND so.pk_member IN (
                    SELECT m.pk_id
                    FROM cu_member m
                    WHERE
                    m.del_flag = 0
                    AND m.category = 0
                    AND (m.pk_parent = #{pkMember} OR m.pk_id = #{pkMember})
                    )
                    GROUP BY so.pk_member
                    ) a
                    FULL OUTER JOIN (
                SELECT DECODE(PK_MEMBER, #{pkMember}, NVL(RS.NEW_CONSUME_PV, 0), NVL(RS.NEW_CONSUME_PV, 0) + NVL(RS.TEAM_NEW_PV, 0)) AREALPV,
                DECODE(PK_MEMBER, #{pkMember}, 0, 0 + NVL(RS.TEAM_NEW_AMOUNT, 0)) AREALAM,
                DECODE(PK_MEMBER, #{pkMember}, NVL(RS.NEW_BOX_NUM, 0), NVL(RS.NEW_BOX_NUM, 0) + NVL(RS.TEAM_NEW_BOX_NUM, 0)) AREALBOX,
                RS.PK_MEMBER
                FROM ${achieveTableNameNow} RS
                WHERE RS.PK_PARENT = #{pkMember} OR RS.PK_MEMBER = #{pkMember}
                    ) b
                    ON a.pk_member = b.pk_member
                ) now

                </if>
            )
           GROUP BY pk_member
            ) MR1
            FULL OUTER JOIN (
            SELECT
            PK_MEMBER,
            decode (PK_MEMBER,#{pkMember},NVL(MR.SUM_PV, 0), NVL(MR.SUM_PV, 0) + NVL(MR.TEAM_SUM_PV, 0)) arealPv,
            decode (PK_MEMBER,#{pkMember},NVL(MR.SUM_AMOUNT, 0), NVL(MR.SUM_AMOUNT, 0) + NVL(MR.TEAM_SUM_AMOUNT, 0)) arealAm,
            decode (PK_MEMBER,#{pkMember},NVL(MR.SUM_BOX, 0), NVL(MR.SUM_BOX, 0) + NVL(MR.TEAM_SUM_BOX, 0)) arealBox
            FROM ${achieveTableNameSta} mr
            WHERE pk_member in (
                SELECT m.pk_member
                FROM ${achieveTableNameSta} m
                WHERE m.pk_parent = #{pkMember} or (m.pk_member=#{pkMember})
        )

        )  MR2 ON MR1.pkMember = MR2.PK_MEMBER




<!--        </foreach>-->
    </select>

<!--     添加新的表-->
    <update id="createCuMemberAmountBoxTable">
        create table ${tableName}
        (
        <include refid="CuMemberAmountBoxTable"></include>
        )
    </update>
<!--    创建表字段索引-->
    <update id="createPkMemberIndex">
        CREATE INDEX ${indexName} ON ${tableName} (pk_member)
    </update>



    <!-- 查询订单金额，业绩，盒数，批量添加会员个人操作 -->
    <update id="innertMemberAmountBoxTable">
        merge into ${tableName} cmab
            using (
                        SELECT
                        nvl(t.SUM_AMOUNT+ nvl(cmr.SUM_AMOUNT,0),0) SUM_AMOUNT,
                        nvl(t.REG_AMOUNT+ nvl(cmr.REG_AMOUNT,0),0) REG_AMOUNT,
                        nvl(t.UPG_AMOUNT+ nvl(cmr.UPG_AMOUNT,0),0) UPG_AMOUNT,
                        nvl(t.REP_AMOUNT+ nvl(cmr.REP_AMOUNT,0),0) REP_AMOUNT,
                        nvl(t.CON_AMOUNT+ nvl(cmr.CON_AMOUNT,0),0) CON_AMOUNT,

                        nvl(t.SUM_PV+ nvl(cmr.SUM_PV,0),0)SUM_PV,
                        nvl(t.REG_PV+ nvl(cmr.REG_PV,0),0)REG_PV,
                        nvl(t.UPG_PV+ nvl(cmr.UPG_PV,0),0)UPG_PV,
                        nvl(t.REP_PV+ nvl(cmr.REP_PV,0),0)REP_PV,
                        nvl(t.CON_PV+ nvl(cmr.CON_PV,0),0)CON_PV,

                        nvl(t.SUM_BOX+ nvl(cmr.SUM_BOX,0),0) SUM_BOX,
                        nvl(t.REG_BOX+ nvl(cmr.REG_BOX,0),0) REG_BOX,
                        nvl(t.UPG_BOX+ nvl(cmr.UPG_BOX,0),0) UPG_BOX,
                        nvl(t.REP_BOX+ nvl(cmr.REP_BOX,0),0) REP_BOX,
                        nvl(t.CON_BOX+ nvl(cmr.CON_BOX,0),0) CON_BOX,

                        t.pk_member,
                        t.pk_parent,
                        t.BIG_BOX_NUM,
                        t.SMALL_BOX_NUM,
                        t.PK_COUNTRY,
                        nvl(cmr.team_sum_amount,0) team_sum_amount,
                        nvl(cmr.team_REG_amount,0) team_REG_amount,
                        nvl(cmr.team_UPG_amount,0) team_UPG_amount,
                        nvl(cmr.team_REP_amount,0) team_REP_amount,
                        nvl(cmr.team_CON_amount,0) team_CON_amount,

						nvl(cmr.team_sum_pv,0) team_sum_pv,
						nvl(cmr.team_REG_pv,0) team_REG_pv,
						nvl(cmr.team_UPG_pv,0) team_UPG_pv,
						nvl(cmr.team_REP_pv,0) team_REP_pv,
						nvl(cmr.team_CON_pv,0) team_CON_pv,

						nvl(cmr.team_sum_box,0) team_sum_box,
						nvl(cmr.team_REG_box,0) team_REG_box,
						nvl(cmr.team_UPG_box,0) team_UPG_box,
						nvl(cmr.team_REP_box,0) team_REP_box,
						nvl(cmr.team_CON_box,0) team_CON_box
                        FROM (
                        SELECT  SUM(nvl(so.ORDER_AMOUNT,0)) SUM_AMOUNT,
                        SUM(CASE WHEN so.order_type = 41 THEN nvl(so.ORDER_AMOUNT, 0) ELSE 0 END) AS REG_AMOUNT,
                        SUM(CASE WHEN so.order_type = 42 THEN nvl(so.ORDER_AMOUNT, 0) ELSE 0 END) AS UPG_AMOUNT,
                        SUM(CASE WHEN so.order_type = 43 THEN nvl(so.ORDER_AMOUNT, 0) ELSE 0 END) AS REP_AMOUNT,
                        SUM(CASE WHEN so.order_type = 44 THEN nvl(so.ORDER_AMOUNT, 0) ELSE 0 END) AS CON_AMOUNT,

                        SUM(nvl(so.ORDER_ACHIEVE,0)) SUM_PV,
                        SUM(CASE WHEN so.order_type = 41 THEN nvl(so.ORDER_ACHIEVE, 0) ELSE 0 END) AS REG_PV,
                        SUM(CASE WHEN so.order_type = 42 THEN nvl(so.ORDER_ACHIEVE, 0) ELSE 0 END) AS UPG_PV,
                        SUM(CASE WHEN so.order_type = 43 THEN nvl(so.ORDER_ACHIEVE, 0) ELSE 0 END) AS REP_PV,
                        SUM(CASE WHEN so.order_type = 44 THEN nvl(so.ORDER_ACHIEVE, 0) ELSE 0 END) AS CON_PV,

                        SUM(nvl(so.BOX_NUM,0)) SUM_BOX,
                        SUM(CASE WHEN so.order_type = 41 THEN nvl(so.BOX_NUM, 0) ELSE 0 END) AS REG_BOX,
                        SUM(CASE WHEN so.order_type = 42 THEN nvl(so.BOX_NUM, 0) ELSE 0 END) AS UPG_BOX,
                        SUM(CASE WHEN so.order_type = 43 THEN nvl(so.BOX_NUM, 0) ELSE 0 END) AS REP_BOX,
                        SUM(CASE WHEN so.order_type = 44 THEN nvl(so.BOX_NUM, 0) ELSE 0 END) AS CON_BOX,
                        rag.pk_member,
                        rag.pk_parent,
                        SUM(nvl(rag.BIG_BOX_NUM,0)) BIG_BOX_NUM,
                        SUM(nvl(rag.SMALL_BOX_NUM,0)) SMALL_BOX_NUM,
                        rag.PK_COUNTRY
                        FROM ${rangeTabeName} rag
                        left join sa_order so on rag.pk_member=so.pk_member and so.del_flag=0 AND so.ORDER_STATUS=1
                        AND so.PAY_TIME >= to_date(#{staDate}, 'yyyy-mm-dd')
                        AND so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                        GROUP BY rag.pk_member, rag.pk_parent,rag.PK_COUNTRY
                        ) t
                        left JOIN ${beforeTabeName} cmr ON cmr.pk_member=t.pk_member
                     ) tmp
            on (cmab.pk_member = tmp.pk_member)
            when not matched then
                insert
                    (PK_MEMBER,PK_PARENT,SUM_AMOUNT,REG_AMOUNT,UPG_AMOUNT,REP_AMOUNT,CON_AMOUNT,
                    SUM_BOX,REG_BOX,UPG_BOX,REP_BOX,CON_BOX,
                    SUM_PV,REG_PV,UPG_PV,REP_PV,CON_PV,
                    PK_COUNTRY,
                    TEAM_SUM_AMOUNT,TEAM_REG_AMOUNT,TEAM_UPG_AMOUNT,TEAM_REP_AMOUNT,TEAM_CON_AMOUNT,
                    TEAM_SUM_BOX,TEAM_REG_BOX,TEAM_UPG_BOX,TEAM_REP_BOX,TEAM_CON_BOX,
                    TEAM_SUM_PV,TEAM_REG_PV,TEAM_UPG_PV,TEAM_REP_PV,TEAM_CON_PV,BIG_BOX,SMALL_BOX
                    )
                    values (tmp.pk_member,
                            tmp.PK_PARENT,
                            tmp.SUM_AMOUNT,
                            tmp.REG_AMOUNT,
                            tmp.UPG_AMOUNT,
                            tmp.REP_AMOUNT,
                            tmp.CON_AMOUNT,

                            tmp.SUM_BOX,
                            tmp.REG_BOX,
                            tmp.UPG_BOX,
                            tmp.REP_BOX,
                            tmp.CON_BOX,

                            tmp.SUM_PV,
                            tmp.REG_PV,
                            tmp.UPG_PV,
                            tmp.REP_PV,
                            tmp.CON_PV,
                            tmp.PK_COUNTRY,
                            tmp.TEAM_SUM_AMOUNT,
                            tmp.TEAM_REG_AMOUNT,
                            tmp.TEAM_UPG_AMOUNT,
                            tmp.TEAM_REP_AMOUNT,
                            tmp.TEAM_CON_AMOUNT,

                            tmp.TEAM_SUM_BOX,
                            tmp.TEAM_REG_BOX,
                            tmp.TEAM_UPG_BOX,
                            tmp.TEAM_REP_BOX,
                            tmp.TEAM_CON_BOX,

                            tmp.TEAM_SUM_PV,
                            tmp.TEAM_REG_PV,
                            tmp.TEAM_UPG_PV,
                            tmp.TEAM_REP_PV,
                            tmp.TEAM_CON_PV,
                            tmp.BIG_BOX_NUM,
                            tmp.SMALL_BOX_NUM
                            )
            when matched then
                update set
                cmab.SUM_AMOUNT = tmp.SUM_AMOUNT,
                cmab.REG_AMOUNT = tmp.REG_AMOUNT,
                cmab.UPG_AMOUNT = tmp.UPG_AMOUNT,
                cmab.REP_AMOUNT = tmp.REP_AMOUNT,
                cmab.CON_AMOUNT = tmp.CON_AMOUNT,

                cmab.SUM_BOX = tmp.SUM_BOX,
                cmab.REG_BOX = tmp.REG_BOX,
                cmab.UPG_BOX = tmp.UPG_BOX,
                cmab.REP_BOX = tmp.REP_BOX,
                cmab.CON_BOX = tmp.CON_BOX,

                cmab.SUM_PV = tmp.SUM_PV,
                cmab.REG_PV = tmp.REG_PV,
                cmab.UPG_PV = tmp.UPG_PV,
                cmab.REP_PV = tmp.REP_PV,
                cmab.CON_PV = tmp.CON_PV,

                cmab.TEAM_SUM_AMOUNT = tmp.TEAM_SUM_AMOUNT,
                cmab.TEAM_REG_AMOUNT = tmp.TEAM_REG_AMOUNT,
                cmab.TEAM_UPG_AMOUNT = tmp.TEAM_UPG_AMOUNT,
                cmab.TEAM_REP_AMOUNT = tmp.TEAM_REP_AMOUNT,
                cmab.TEAM_CON_AMOUNT = tmp.TEAM_CON_AMOUNT,

                cmab.TEAM_SUM_BOX = tmp.TEAM_SUM_BOX,
                cmab.TEAM_REG_BOX = tmp.TEAM_REG_BOX,
                cmab.TEAM_UPG_BOX = tmp.TEAM_UPG_BOX,
                cmab.TEAM_REP_BOX = tmp.TEAM_REP_BOX,
                cmab.TEAM_CON_BOX = tmp.TEAM_CON_BOX,

                cmab.TEAM_SUM_PV = tmp.TEAM_SUM_PV,
                cmab.TEAM_REG_PV = tmp.TEAM_REG_PV,
                cmab.TEAM_UPG_PV = tmp.TEAM_UPG_PV,
                cmab.TEAM_REP_PV = tmp.TEAM_REP_PV,
                cmab.TEAM_CON_PV = tmp.TEAM_CON_PV,
                cmab.BIG_BOX = tmp.BIG_BOX_NUM,
                cmab.SMALL_BOX = tmp.SMALL_BOX_NUM

    </update>


    <!-- 查询订单金额，业绩，盒数，批量添加会员伞下操作 -->
    <update id="innertTeamAmountBoxTable">
        merge into ${tableName} cmab
            using (
                     SELECT DISTINCT
                    nvl(#{apb.orderAmount}, 0)  +nvl(cmrr.TEAM_SUM_AMOUNT, 0) AS TEAM_SUM_AMOUNT,
                    nvl(#{apb.orderAmountReg}, 0)  +nvl(cmrr.TEAM_REG_AMOUNT, 0) AS TEAM_REG_AMOUNT,
                    nvl(#{apb.orderAmountUpg}, 0)  +nvl(cmrr.TEAM_UPG_AMOUNT, 0) AS TEAM_UPG_AMOUNT,
                    nvl(#{apb.orderAmountRep}, 0)  +nvl(cmrr.TEAM_REP_AMOUNT, 0) AS TEAM_REP_AMOUNT,
                    nvl(#{apb.orderAmountCon}, 0)  +nvl(cmrr.TEAM_CON_AMOUNT, 0) AS TEAM_CON_AMOUNT,
                    nvl(#{apb.orderAchieve}, 0) +nvl(cmrr.TEAM_SUM_PV, 0) AS TEAM_SUM_PV,
                    nvl(#{apb.orderAchieveReg}, 0)  +nvl(cmrr.TEAM_REG_PV, 0) AS TEAM_REG_PV,
                    nvl(#{apb.orderAchieveUpg}, 0)  +nvl(cmrr.TEAM_UPG_PV, 0) AS TEAM_UPG_PV,
                    nvl(#{apb.orderAchieveRep}, 0)  +nvl(cmrr.TEAM_REP_PV, 0) AS TEAM_REP_PV,
                    nvl(#{apb.orderAchieveCon}, 0)  +nvl(cmrr.TEAM_CON_PV, 0) AS TEAM_CON_PV,
                    nvl(#{apb.boxNum}, 0) +nvl(cmrr.TEAM_SUM_BOX, 0) AS TEAM_SUM_BOX,
                    nvl(#{apb.boxNumReg}, 0)  +nvl(cmrr.TEAM_REG_BOX, 0) AS TEAM_REG_BOX,
                    nvl(#{apb.boxNumUpg}, 0)  +nvl(cmrr.TEAM_UPG_BOX, 0) AS TEAM_UPG_BOX,
                    nvl(#{apb.boxNumRep}, 0)  +nvl(cmrr.TEAM_REP_BOX, 0) AS TEAM_REP_BOX,
                    nvl(#{apb.boxNumCon}, 0)  +nvl(cmrr.TEAM_CON_BOX, 0) AS TEAM_CON_BOX,
                    nvl(mab.BIG_BOX_NUM, 0)  AS INFLATE_BIG_BOX,
                    nvl(mab.SMALL_BOX_NUM, 0)   AS INFLATE_SMALL_BOX,
                    mab.pk_member
                FROM
                    ${rangeTabeName} mab
                    LEFT JOIN
                    ${tableName} cmrr
                    ON cmrr.pk_member = mab.pk_member
                WHERE
                    mab.pk_member IN (
                        SELECT DISTINCT rag.pk_parent
                        FROM ${rangeTabeName} rag
                        START WITH rag.pk_member = #{apb.pkMember}
                        CONNECT BY PRIOR rag.pk_parent = rag.pk_member
                         )
                     ) tmp
            on (cmab.pk_member = tmp.pk_member)
            when matched then
                update set
                cmab.TEAM_SUM_AMOUNT = tmp.TEAM_SUM_AMOUNT,
                cmab.TEAM_REG_AMOUNT = tmp.TEAM_REG_AMOUNT,
                cmab.TEAM_UPG_AMOUNT = tmp.TEAM_UPG_AMOUNT,
                cmab.TEAM_REP_AMOUNT = tmp.TEAM_REP_AMOUNT,
                cmab.TEAM_CON_AMOUNT = tmp.TEAM_CON_AMOUNT,
                cmab.TEAM_SUM_BOX = tmp.TEAM_SUM_BOX,
                cmab.TEAM_REG_BOX = tmp.TEAM_REG_BOX,
                cmab.TEAM_UPG_BOX = tmp.TEAM_UPG_BOX,
                cmab.TEAM_REP_BOX = tmp.TEAM_REP_BOX,
                cmab.TEAM_CON_BOX = tmp.TEAM_CON_BOX,
                cmab.TEAM_SUM_PV = tmp.TEAM_SUM_PV,
                cmab.TEAM_REG_PV = tmp.TEAM_REG_PV,
                cmab.TEAM_UPG_PV = tmp.TEAM_UPG_PV,
                cmab.TEAM_REP_PV = tmp.TEAM_REP_PV,
                cmab.TEAM_CON_PV = tmp.TEAM_CON_PV
    </update>
           <!--  查询会员订单-->
          <select id="querMemberOrder" resultType="com.hzs.report.member.vo.DirectPushAmountPvBoxVo">
              select
              sum(so.order_amount)  orderAmount,
              SUM(CASE WHEN so.order_type = 41 THEN so.order_amount ELSE 0 END) AS orderAmountReg,
              SUM(CASE WHEN so.order_type = 42 THEN so.order_amount ELSE 0 END) AS orderAmountUpg,
              SUM(CASE WHEN so.order_type = 43 THEN so.order_amount ELSE 0 END) AS orderAmountRep,
              SUM(CASE WHEN so.order_type = 44 THEN so.order_amount ELSE 0 END) AS orderAmountCon,
              sum(so.order_achieve) orderAchieve,
              SUM(CASE WHEN so.order_type = 41 THEN so.order_achieve ELSE 0 END) AS orderAchieveReg,
              SUM(CASE WHEN so.order_type = 42 THEN so.order_achieve ELSE 0 END) AS orderAchieveUpg,
              SUM(CASE WHEN so.order_type = 43 THEN so.order_achieve ELSE 0 END) AS orderAchieveRep,
              SUM(CASE WHEN so.order_type = 44 THEN so.order_achieve ELSE 0 END) AS orderAchieveCon,
              sum(so.box_num)  boxNum,
              SUM(CASE WHEN so.order_type = 41 THEN so.box_num ELSE 0 END) AS boxNumReg,
              SUM(CASE WHEN so.order_type = 42 THEN so.box_num ELSE 0 END) AS boxNumUpg,
              SUM(CASE WHEN so.order_type = 43 THEN so.box_num ELSE 0 END) AS boxNumRep,
              SUM(CASE WHEN so.order_type = 44 THEN so.box_num ELSE 0 END) AS boxNumCon,
              so.pk_member pkMember
              FROM  sa_order so
              WHERE 	so.del_flag = 0
              AND so.ORDER_STATUS = 1
              AND so.PAY_TIME >= to_date(#{staDate}, 'yyyy-mm-dd')
              AND so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
              GROUP BY so.pk_member
          </select>
   <!--    7天重算删除会员统计业绩金额盒数表数据-->
    <update id="delMemberAmountBox">
        delete from ${tableName}
    </update>

    <!--   未激活的会员盒数为零-->
    <update id="updMemberBox">
        update ${tableName} set SUM_BOX=0,TEAM_SUM_BOX=0 where PK_MEMBER in(
         select PK_MEMBER from ${rangeTabeName} rag where enable_status=1
        )
    </update>

<!--     初始化会员直推金额业绩盒数数据-->
    <update id="washMemberAmountBox">
        merge into ${tableName} cmab
            using ( SELECT  rag.pk_member,
                            rag.pk_parent,
                            rag.CONSUME_PV SUM_PV,
                            rag.CONSUME_BOX_NUM SUM_BOX,
                            rag.TEAM_CONSUME_PV TEAM_SUM_PV,
                            rag.TEAM_BOX_NUM TEAM_SUM_BOX,
                            rag.TEAM_CONSUME_AMOUNT TEAM_SUM_AMOUNT,
                            rag.BIG_BOX_NUM  AS INFLATE_BIG_BOX,
                            rag.SMALL_BOX_NUM  AS INFLATE_SMALL_BOX,
                            rag.PK_COUNTRY
                        FROM ${rangeTabeName} rag
                     ) tmp
            on (cmab.pk_member = tmp.pk_member)
            when not matched then
                insert
                    (PK_MEMBER,PK_PARENT,SUM_PV,SUM_BOX,TEAM_SUM_PV,TEAM_SUM_BOX,TEAM_SUM_AMOUNT,
                    INFLATE_BIG_BOX,
                    INFLATE_SMALL_BOX,
                    PK_COUNTRY)
                    values (tmp.pk_member,
                            tmp.PK_PARENT,
                            tmp.SUM_PV,
                            tmp.SUM_BOX,
                            tmp.TEAM_SUM_PV,
                            tmp.TEAM_SUM_BOX,
                            tmp.TEAM_SUM_AMOUNT,
                            tmp.INFLATE_BIG_BOX,
                            tmp.INFLATE_SMALL_BOX,
                            tmp.PK_COUNTRY
                            )
            when matched then
                update set cmab.SUM_BOX = tmp.SUM_BOX,
                           cmab.SUM_PV = tmp.SUM_PV,
                           cmab.TEAM_SUM_PV=tmp.TEAM_SUM_PV,
                           cmab.TEAM_SUM_BOX=tmp.TEAM_SUM_BOX,
                           cmab.TEAM_SUM_AMOUNT=tmp.TEAM_SUM_AMOUNT,
                           cmab.INFLATE_BIG_BOX=tmp.INFLATE_BIG_BOX,
                           cmab.INFLATE_SMALL_BOX=tmp.INFLATE_SMALL_BOX
    </update>

<!--          秒结表查询会员是否激活-->
        <select id="selectMemberActiveState" resultType="com.hzs.report.member.vo.DirectPushBoxVo">
            <foreach collection="pkMemberList" item="pkMember" separator="union all">
                SELECT min(t0.enable_status) enableStatus,
                #{pkMember} pkMember
                FROM (SELECT mr.enable_status
                FROM ${dpb.memberRetailTable} mr
                where mr.pk_member = #{pkMember}
                <if test="dpb.payTime!=null and dpb.payTime!=''">
                    union all
                    SELECT mrs.enable_status
                    FROM ${dpb.memberRetailSTable} mrs
                    where mrs.pk_member = #{pkMember}
                </if>
                ) t0
            </foreach>

        </select>
<!--    查询总盒数-->
    <select id="selectMemberTotalBox" resultType="integer">
        SELECT nvl(sum(totalAreaBox), 0) totalAreaBox
        FROM (SELECT NVL(mr.team_box_num + mr.consume_box_num, 0) totalAreaBox
              FROM ${memberRetailTable} mr
              WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
            <if test="payTime!=null and payTime!=''">
                union all
                select NVL(rs.new_box_num + rs.team_new_box_num, 0) totalAreaBox
                from ${memberRetailSTable} rs
                where rs.pk_member = #{pkMember}
            </if>
        )
    </select>

    <select id="selectWaterInjection" resultType="integer">
        SELECT count(1)
        FROM (SELECT sr.team_box_num, sr.small_box_num, sr.period_date
              FROM cu_member_spe_retail sr
              where sr.del_flag = 0
                and sr.pk_country = 1
                and sr.period_date &lt; TRUNC(SYSDATE)
                and sr.member_code = #{memberCode}
              ORDER BY sr.period_date desc) t0
        where rownum = 1
    </select>

    <select id="selectMemberBoxWaterInjection" resultType="integer">
        SELECT NVL(mr.big_box_num, 0) smallAreaBox
        FROM ${memberRetailTable} mr
        WHERE mr.category = 0
          AND mr.pk_member = #{pkMember}
    </select>

    <select id="selectMemberBoxNoWaterInjection" resultType="integer">
        SELECT nvl(max(smallAreaBox), 0) smallareabox
        from
        (
        <if test="pkMemberList != null and pkMemberList.size > 0">
            <include refid="selectMemberDirectPushBox"/>
            union all
        </if>
        SELECT nvl(sum(smallAreaBox), 0) smallAreaBox
        FROM (SELECT NVL(mr.consume_box_num, 0) smallAreaBox
        FROM ${memberRetailTable} mr
        WHERE mr.category = 0
        AND mr.pk_member = #{pkMember}
        <if test="payTime!=null and payTime!=''">
            union all
            select rs.new_box_num smallAreaBox
            from ${memberRetailSTable} rs
            where rs.pk_member = #{pkMember}
        </if>
         )
        )
    </select>

    <sql id="selectMemberDirectPushBox">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT SUM(t1.smallAreaBox) smallAreaBox
            FROM (
            SELECT NVL(mr.team_box_num + mr.consume_box_num-mr.big_box_num-mr.small_box_num, 0) smallAreaBox
            FROM ${memberRetailTable} mr
            WHERE mr.category = 0
            AND mr.pk_member = #{pkMember}
            <if test="payTime!=null and payTime!=''">
                union all
                select NVL(rs.new_box_num + rs.team_new_box_num ,0) smallAreaBox
                from ${memberRetailSTable} rs
                where rs.pk_member = #{pkMember}
            </if>
            ) t1
        </foreach>
    </sql>

    <!-- 查询直推会员信息 -->
    <select id="querDirectPushDetails" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        cm.pk_id pkMember,
        bc.NATIONAL_FLAG1 nationalFlag1,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode ,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        ba.AWARDS_NAME awardsName,
        bv.VERTEX_NAME vertexName,
        cmt.team_name teamName
        from ${achieveTableNameEnd} cma
        left join cu_member cm on cm.pk_id=cma.pk_member
        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
        <if test="pkMember!=null">
            and cma.PK_PARENT=#{pkMember}
        </if>
        <if test="memberCode!=null and memberCode!=''">
            and cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
    </select>



</mapper>
