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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.report.stat.CuLianchuangDataStat">
        <id column="PK_ID" property="pkId" />
    <result column="DEL_FLAG" property="delFlag" />
    <result column="CREATION_TIME" property="creationTime" />
    <result column="MODIFIED_TIME" property="modifiedTime" />
    <result column="PK_COUNTRY" property="pkCountry" />
    <result column="PK_CREATOR" property="pkCreator" />
    <result column="PK_MODIFIED" property="pkModified" />
        <result column="VERTEX_CODE" property="vertexCode" />
        <result column="VERTEX_NAME" property="vertexName" />
        <result column="ADD_REGION" property="addRegion" />
        <result column="REDUCE_REGION" property="reduceRegion" />
        <result column="SYSTEM_TYPE" property="systemType" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        DEL_FLAG,
        CREATION_TIME,
        MODIFIED_TIME,
        PK_COUNTRY,
        PK_CREATOR,
        PK_MODIFIED,
        PK_ID, VERTEX_CODE, VERTEX_NAME, ADD_REGION, REDUCE_REGION
    </sql>


    <!-- 查询联创数据报表-->
    <select id="lcStatList" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
        <foreach collection="lcList" item="item" separator="UNION ALL">
        select
            #{item.pkId} pkId,
            #{item.vertexCode} vertexCode,
            #{item.vertexName} vertexName,
            #{item.endDate} payTime,
            sum(addfirstPurchaseMo) firstPurchaseMo,
            sum(addfirstPurchasePv) firstPurchasePv,
            sum(addfirstPurchaseSumMo) firstPurchaseSumMo,
            sum(addfirstPurchaseSumPv) firstPurchaseSumPv,
            sum(addrepurchaseOrderMo) repurchaseOrderMo,
            sum(addrepurchaseOrderPv) repurchaseOrderPv,
            sum(addrepurchaseOrderSumMo) repurchaseOrderSumMo,
            sum(addrepurchaseOrderSumPv) repurchaseOrderSumPv,
            sum(addrepurchaseMo) repurchaseMo,
            sum(addrepurchasePv) repurchasePv,
            sum(addrepurchaseSumMo) repurchaseSumMo,
            sum(addrepurchaseSumPv) repurchaseSumPv,
            sum(addthirdOrderMo) thirdOrderMo,
            sum(addthirdOrderPv) thirdOrderPv,
            sum(addthirdOrderSumMo) thirdOrderSumMo,
            sum(addthirdOrderSumPv) thirdOrderSumPv
            from(

            select
            sum (addArea.firstPurchaseMo) addfirstPurchaseMo,
            sum (addArea.firstPurchasePv) addfirstPurchasePv,
            sum (addArea.firstPurchaseSumMo) addfirstPurchaseSumMo,
            sum (addArea.firstPurchaseSumPv) addfirstPurchaseSumPv,
            sum (addArea.repurchaseOrderMo) addrepurchaseOrderMo,
            sum (addArea.repurchaseOrderPv) addrepurchaseOrderPv,
            sum (addArea.repurchaseOrderSumMo) addrepurchaseOrderSumMo,
            sum (addArea.repurchaseOrderSumPv) addrepurchaseOrderSumPv,
            sum (addArea.repurchaseMo) addrepurchaseMo,
            sum (addArea.repurchasePv) addrepurchasePv,
            sum (addArea.repurchaseSumMo) addrepurchaseSumMo,
            sum (addArea.repurchaseSumPv) addrepurchaseSumPv,
            sum (addArea.thirdOrderMo) addthirdOrderMo,
            sum (addArea.thirdOrderPv) addthirdOrderPv,
            sum (addArea.thirdOrderSumMo) addthirdOrderSumMo,
            sum (addArea.thirdOrderSumPv) addthirdOrderSumPv
            from (
            <foreach collection="item.addRegionList" item="addItem" separator="UNION ALL">
                select
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end firstPurchaseMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(1,2,20,24,25)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end firstPurchaseSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(1,2,20,24,25)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end repurchaseOrderMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,26,22,14,12,18,19,28,13)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end repurchaseOrderSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,26,22,14,12,18,19,28,13)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT-nvl(so.CONSUME_AMOUNT,0)) end repurchaseMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,10,26,22,14,12,18,19,7,28,27,13,31,40)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>

                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT-nvl(so.CONSUME_AMOUNT,0) ) end repurchaseSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,10,26,22,14,12,18,19,7,28,27,13,31,40)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>

                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end thirdOrderMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(21)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>

                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end thirdOrderSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{addItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(21)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>


            </foreach>
            ) addArea
            <if test="item.reduceRegionList!=null">
            UNION ALL
            select
            -sum (redArea.firstPurchaseMo) addfirstPurchaseMo,
            -sum (redArea.firstPurchasePv) addfirstPurchasePv,
            -sum (redArea.firstPurchaseSumMo) addfirstPurchaseSumMo,
            -sum (redArea.firstPurchaseSumPv) addfirstPurchaseSumPv,
            -sum (redArea.repurchaseOrderMo) addrepurchaseOrderMo,
            -sum (redArea.repurchaseOrderPv) addrepurchaseOrderPv,
            -sum (redArea.repurchaseOrderSumMo) addrepurchaseOrderSumMo,
            -sum (redArea.repurchaseOrderSumPv) addrepurchaseOrderSumPv,
            -sum (redArea.repurchaseMo) addrepurchaseMo,
            -sum (redArea.repurchasePv) addrepurchasePv,
            -sum (redArea.repurchaseSumMo) addrepurchaseSumMo,
            -sum (redArea.repurchaseSumPv) addrepurchaseSumPv,
            -sum (redArea.thirdOrderMo) addthirdOrderMo,
            -sum (redArea.thirdOrderPv) addthirdOrderPv,
            -sum (redArea.thirdOrderSumMo) addthirdOrderSumMo,
            -sum (redArea.thirdOrderSumPv) addthirdOrderSumPv
            from (

            <foreach collection="item.reduceRegionList" item="reduceItem" separator="UNION ALL">
                select
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end firstPurchaseMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(1,2,20,24,25)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end firstPurchaseSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(1,2,20,24,25)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end repurchaseOrderMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,26,22,14,12,18,19,28,13)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end repurchaseOrderSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,26,22,14,12,18,19,28,13)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end repurchaseMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,26,22,14,12,18,19,7,28,13,31,40)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>

                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end repurchaseSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(3,26,22,14,12,18,19,7,28,13,31,40)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>

                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end thirdOrderMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end thirdOrderPv,
                0 thirdOrderSumMo,
                0 thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(21)
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.endDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>

                UNION ALL
                select
                0 firstPurchaseMo,
                0 firstPurchasePv,
                0 firstPurchaseSumMo,
                0 firstPurchaseSumPv,
                0 repurchaseOrderMo,
                0 repurchaseOrderPv,
                0 repurchaseOrderSumMo,
                0 repurchaseOrderSumPv,
                0 repurchaseMo,
                0 repurchasePv,
                0 repurchaseSumMo,
                0 repurchaseSumPv,
                0 thirdOrderMo,
                0 thirdOrderPv,
                CASE WHEN sum( so.ORDER_AMOUNT ) is null THEN 0 ELSE sum( so.ORDER_AMOUNT ) end thirdOrderSumMo,
                CASE WHEN  sum( so.ORDER_ACHIEVE ) is null THEN 0 ELSE sum( so.ORDER_ACHIEVE ) end thirdOrderSumPv
                from SA_ORDER so
                where so.DEL_FLAG=0
                and so.ORDER_STATUS=1
                and so.PK_COUNTRY=#{item.pkCountry}
                and so.pk_member in(
                SELECT
                cm.pk_id
                FROM
                cu_member cm
                WHERE
                cm.del_flag = 0 and cm.CATEGORY=0
                START WITH cm.pk_parent = #{reduceItem} CONNECT BY cm.pk_parent = PRIOR cm.pk_id
                )
                and so.ORDER_TYPE in(21)
                <if test="item.startDate!=null and item.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{item.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="item.endDate!=null and item.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{item.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
            </foreach>
            ) redArea
            </if>

            ) t
            order by pkId asc

        </foreach>

    </select>

    <select id="lcStatListByOrdeType" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
            select
            #{orderType} orderType,
            #{lcDate.pkId} pkId,
            sum(addfirstPurchaseMo) amount,
            sum(addfirstPurchasePv) achieve,
            sum(addfirstPurchaseSumMo) amountSum,
            sum(addfirstPurchaseSumPv) achieveSumPv
            from(
            select
            nvl(sum (addArea.firstPurchaseMo), 0) addfirstPurchaseMo,
            nvl(sum (addArea.firstPurchasePv), 0) addfirstPurchasePv,
            nvl(sum (addArea.firstPurchaseSumMo), 0) addfirstPurchaseSumMo,
            nvl(sum (addArea.firstPurchaseSumPv), 0) addfirstPurchaseSumPv
            from (
            <foreach collection="lcDate.addRegionList" item="addItem" separator="UNION ALL">
                select
                CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_AMOUNT, 0)) else 0 end firstPurchaseMo,
                CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_ACHIEVE, 0)) else 0 end firstPurchasePv,
                CASE WHEN sum(nvl(so.ORDER_AMOUNT, 0)) is null THEN 0 ELSE sum(so.ORDER_AMOUNT) end firstPurchaseSumMo,
                CASE WHEN sum(nvl(so.ORDER_ACHIEVE, 0)) is null THEN 0 ELSE sum(so.ORDER_ACHIEVE) end firstPurchaseSumPv
                from SA_ORDER so
                where so.DEL_FLAG = 0
                and so.ORDER_STATUS = 1
                and so.PK_COUNTRY = #{lcDate.pkCountry}
                and so.pk_member in
                (SELECT cm.pk_id
                FROM cu_member cm
                WHERE cm.del_flag = 0
                and cm.CATEGORY = 0
                START WITH cm.pk_parent = #{addItem}
                CONNECT BY cm.pk_parent = PRIOR cm.pk_id)
                <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="lcDate.startDate!=null and lcDate.startDate!='' ">
                    and so.PAY_TIME >= to_date(#{lcDate.startDate}, 'yyyy-mm-dd')
                </if>
                <if test="lcDate.endDate!=null and lcDate.endDate!='' ">
                    and so.PAY_TIME &lt;= to_date(#{lcDate.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                group by to_char(so.PAY_TIME, 'yyyy-mm-dd')
            </foreach>
            ) addArea
            <if test="lcDate.reduceRegionList!=null">
                UNION ALL
                select
                -nvl(sum (redArea.firstPurchaseMo), 0) addfirstPurchaseMo,
                -nvl(sum (redArea.firstPurchasePv), 0) addfirstPurchasePv,
                -nvl(sum (redArea.firstPurchaseSumMo), 0) addfirstPurchaseSumMo,
                -nvl(sum (redArea.firstPurchaseSumPv), 0) addfirstPurchaseSumPv
                from (

                <foreach collection="lcDate.reduceRegionList" item="reduceItem" separator="UNION ALL">
                    select
                    CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_AMOUNT, 0)) else 0 end firstPurchaseMo,
                    CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_ACHIEVE, 0)) else 0 end firstPurchasePv,
                    CASE WHEN sum(nvl(so.ORDER_AMOUNT ,0)) is null THEN 0 ELSE sum(so.ORDER_AMOUNT) end firstPurchaseSumMo,
                    CASE WHEN sum(nvl(so.ORDER_ACHIEVE, 0)) is null THEN 0 ELSE sum(so.ORDER_ACHIEVE) end firstPurchaseSumPv
                    from SA_ORDER so
                    where so.DEL_FLAG = 0
                    and so.ORDER_STATUS = 1
                    and so.PK_COUNTRY = #{lcDate.pkCountry}
                    and so.pk_member in
                    (SELECT cm.pk_id
                    FROM cu_member cm
                    WHERE cm.del_flag = 0
                    and cm.CATEGORY = 0
                    START WITH cm.pk_parent = #{reduceItem}
                    CONNECT BY cm.pk_parent = PRIOR cm.pk_id)
                    <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="lcDate.startDate!=null and lcDate.startDate!='' ">
                        and so.PAY_TIME >= to_date(#{lcDate.startDate}, 'yyyy-mm-dd')
                    </if>
                    <if test="lcDate.endDate!=null and lcDate.endDate!='' ">
                        and so.PAY_TIME &lt;= to_date(#{lcDate.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                    </if>
                    group by to_char(so.PAY_TIME, 'yyyy-mm-dd')
                </foreach>
                ) redArea
            </if>

            ) t
            order by pkId asc
    </select>
<!--    联创数据查询(优化后)-->
    <select id="newLcStatListByOrdeType" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
        select
        #{pkMember} pkMember,
        nvl(sum(firstPurchaseMo) , 0) firstPurchaseMo,
        nvl(sum(firstPurchasePv), 0) firstPurchasePv,
        nvl(sum(firstPurchaseSumMo), 0) firstPurchaseSumMo,
        nvl(sum(firstPurchaseSumPv), 0) firstPurchaseSumPv,
        nvl(sum(beforePurchaseSumMo), 0) beforePurchaseSumMo,
        nvl(sum(beforePurchaseSumPv), 0) beforePurchaseSumPv
        from(
            select
            sum(nvl(so.ORDER_AMOUNT, 0)) firstPurchaseMo,
            sum(nvl(so.ORDER_ACHIEVE, 0)) firstPurchasePv,
            0  firstPurchaseSumMo,
            0 firstPurchaseSumPv,
            0 beforePurchaseSumMo,
            0 beforePurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG = 0
            and so.ORDER_STATUS = 1
            and so.PK_COUNTRY = #{lcDate.pkCountry}
            and so.pk_member in
            (SELECT cm.pk_id
            FROM cu_member cm
            WHERE cm.del_flag = 0
            and cm.CATEGORY = 0
            START WITH cm.pk_parent = #{pkMember}
            CONNECT BY cm.pk_parent = PRIOR cm.pk_id)
            and so.ORDER_TYPE in(41,42,43,44)
            <if test="lcDate.payTime!=null and lcDate.payTime!='' ">
                and so.PAY_TIME >= to_date(#{lcDate.payTime}, 'yyyy-mm-dd')
            </if>
            <if test="lcDate.endDate!=null and lcDate.endDate!='' ">
                and so.PAY_TIME &lt;= to_date(#{lcDate.payTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
            group by to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        0 firstPurchaseMo,
        0 firstPurchasePv,
        sum(nvl(so.ORDER_AMOUNT, 0))  firstPurchaseSumMo,
        sum(nvl(so.ORDER_ACHIEVE, 0)) firstPurchaseSumPv,
        0 beforePurchaseSumMo,
        0 beforePurchaseSumPv
        from SA_ORDER so
        where so.DEL_FLAG = 0
        and so.ORDER_STATUS = 1
        and so.PK_COUNTRY = #{lcDate.pkCountry}
        and so.pk_member in
        (SELECT cm.pk_id
        FROM cu_member cm
        WHERE cm.del_flag = 0
        and cm.CATEGORY = 0
        START WITH cm.pk_parent = #{pkMember}
        CONNECT BY cm.pk_parent = PRIOR cm.pk_id)
        and so.ORDER_TYPE in(41,42,43,44)
        <if test="lcDate.startDate!=null and lcDate.startDate!='' ">
            and so.PAY_TIME >= to_date(#{lcDate.startDate}, 'yyyy-mm-dd')
        </if>
        <if test="lcDate.endDate!=null and lcDate.endDate!='' ">
            and so.PAY_TIME &lt;= to_date(#{lcDate.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by to_char(so.PAY_TIME, 'yyyy-mm-dd')
        UNION ALL
        select
        0 firstPurchaseMo,
        0 firstPurchasePv,
        0 firstPurchaseSumMo,
        0 firstPurchaseSumPv,
        sum(nvl(so.ORDER_AMOUNT, 0)) beforePurchaseSumMo,
        sum(nvl(so.ORDER_ACHIEVE, 0)) beforePurchaseSumPv
        from SA_ORDER so
        where so.DEL_FLAG = 0
        and so.ORDER_STATUS = 1
        and so.PK_COUNTRY = #{lcDate.pkCountry}
        and so.pk_member in
        (SELECT cm.pk_id
        FROM cu_member cm
        WHERE cm.del_flag = 0
        and cm.CATEGORY = 0
        START WITH cm.pk_parent = #{pkMember}
        CONNECT BY cm.pk_parent = PRIOR cm.pk_id)
        and so.ORDER_TYPE in(41,42,43,44)
        <if test="lcDate.beforeStaDate!=null and lcDate.beforeStaDate!='' ">
            and so.PAY_TIME >= to_date(#{lcDate.beforeStaDate}, 'yyyy-mm-dd')
        </if>
        <if test="lcDate.beforeEndDate!=null and lcDate.beforeEndDate!='' ">
            and so.PAY_TIME &lt;= to_date(#{lcDate.beforeEndDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by to_char(so.PAY_TIME, 'yyyy-mm-dd')
        )t
    </select>

<!--    数据报表-联创统计历史业绩新增数据-->
    <select id="lcStatOrdeHistoryList" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
        select
        #{lcDate.pkId} pkId,
        sum(firstPurchaseMo) firstPurchaseMo,
        sum(firstPurchasePv) firstPurchasePv,
        sum(repurchaseMo) repurchaseMo,
        sum(repurchasePv) repurchasePv
        from(
        select
        nvl(sum (addArea.firstPurchaseMo), 0) firstPurchaseMo,
        nvl(sum (addArea.firstPurchasePv), 0) firstPurchasePv,
        nvl(sum (addArea.repurchaseMo), 0) repurchaseMo,
        nvl(sum (addArea.repurchasePv), 0) repurchasePv
        from (
            <foreach collection="lcDate.addRegionList" item="addItem" separator="UNION ALL">
                select
                nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
                nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
                nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
                nvl(cma.REPURCHASE_PV,0) repurchasePv
                FROM
                ${cds.achieveTableNameEnd} cma
                WHERE cma.PERIOD=#{cds.periodEnd}
                AND cma.PK_COUNTRY=#{cds.pkCountry}
                AND cma.pk_member IN (
                SELECT
                cmt.PK_MEMBER
                FROM
                ${cds.settleTableNameEnd} cmt
                WHERE
                cmt.PERIOD=#{cds.periodEnd}
                and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
                START WITH cmt.pk_parent = #{addItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
                UNION ALL
                select
                -nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
                -nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
                -nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
                -nvl(cma.REPURCHASE_PV,0) repurchasePv
                FROM
                ${cds.achieveTableNameBefore} cma
                WHERE cma.PERIOD=#{cds.periodBefore}
                AND cma.PK_COUNTRY=#{cds.pkCountry}
                AND cma.pk_member IN (
                SELECT
                cmt.PK_MEMBER
                FROM
                ${cds.settleTableNameBefore} cmt
                WHERE
                cmt.PERIOD=#{cds.periodBefore}
                and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
                START WITH cmt.pk_parent = #{addItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)

            </foreach>
        ) addArea
        <if test="lcDate.reduceRegionList!=null">
            UNION ALL
            select
            -nvl(sum (redArea.firstPurchaseMo), 0) firstPurchaseMo,
            -nvl(sum (redArea.firstPurchasePv), 0) firstPurchasePv,
            -nvl(sum (redArea.repurchaseMo), 0) repurchaseMo,
            -nvl(sum (redArea.repurchasePv), 0) repurchasePv
            from (
            <foreach collection="lcDate.reduceRegionList" item="reduceItem" separator="UNION ALL">
                select
                nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
                nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
                nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
                nvl(cma.REPURCHASE_PV,0) repurchasePv
                FROM
                ${cds.achieveTableNameEnd} cma
                WHERE cma.PERIOD=#{cds.periodEnd}
                AND cma.PK_COUNTRY=#{cds.pkCountry}
                AND cma.pk_member IN (
                SELECT
                cmt.PK_MEMBER
                FROM
                ${cds.settleTableNameEnd} cmt
                WHERE
                cmt.PERIOD=#{cds.periodEnd}
                and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
                START WITH cmt.pk_parent = #{reduceItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
                UNION ALL
                select
                -nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
                -nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
                -nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
                -nvl(cma.REPURCHASE_PV,0) repurchasePv
                FROM
                ${cds.achieveTableNameBefore} cma
                WHERE cma.PERIOD=#{cds.periodBefore}
                AND cma.PK_COUNTRY=#{cds.pkCountry}
                AND cma.pk_member IN (
                SELECT
                cmt.PK_MEMBER
                FROM
                ${cds.settleTableNameBefore} cmt
                WHERE
                cmt.PERIOD=#{cds.periodBefore}
                and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
                START WITH cmt.pk_parent = #{reduceItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
            </foreach>
            ) redArea
        </if>

        ) t
        order by pkId asc
    </select>

    <!--    数据报表-联创统计历史业绩累计数据-->
    <select id="lcStatOrderHistorySumList" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
        select
        #{lcDate.pkId} pkId,
        sum(firstPurchaseMo) firstPurchaseSumMo,
        sum(firstPurchasePv) firstPurchaseSumPv,
        sum(repurchaseMo) repurchaseSumMo,
        sum(repurchasePv) repurchaseSumPv
        from(
        select
        nvl(sum (addArea.firstPurchaseMo), 0) firstPurchaseMo,
        nvl(sum (addArea.firstPurchasePv), 0) firstPurchasePv,
        nvl(sum (addArea.repurchaseMo), 0) repurchaseMo,
        nvl(sum (addArea.repurchasePv), 0) repurchasePv
        from (
        <foreach collection="lcDate.addRegionList" item="addItem" separator="UNION ALL">
            select
            nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
            nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
            nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
            nvl(cma.REPURCHASE_PV,0) repurchasePv
            FROM
            ${cds.achieveTableNameEnd} cma
            WHERE cma.PERIOD=#{cds.periodEnd}
            AND cma.PK_COUNTRY=#{cds.pkCountry}
            AND cma.pk_member IN (
            SELECT
            cmt.PK_MEMBER
            FROM
            ${cds.settleTableNameEnd} cmt
            WHERE
            cmt.PERIOD=#{cds.periodEnd}
            and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
            START WITH cmt.pk_parent = #{addItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
            UNION ALL
            select
            -nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
            -nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
            -nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
            -nvl(cma.REPURCHASE_PV,0) repurchasePv
            FROM
            ${cds.achieveTableNameSta} cma
            WHERE cma.PERIOD=#{cds.periodSta}
            AND cma.PK_COUNTRY=#{cds.pkCountry}
            AND cma.pk_member IN (
            SELECT
            cmt.PK_MEMBER
            FROM
            ${cds.settleTableNameSta} cmt
            WHERE
            cmt.PERIOD=#{cds.periodSta}
            and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
            START WITH cmt.pk_parent = #{addItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
        </foreach>
        ) addArea
        <if test="lcDate.reduceRegionList!=null">
            UNION ALL
            select
            -nvl(sum (redArea.firstPurchaseMo), 0) firstPurchaseMo,
            -nvl(sum (redArea.firstPurchasePv), 0) firstPurchasePv,
            -nvl(sum (redArea.repurchaseMo), 0) repurchaseMo,
            -nvl(sum (redArea.repurchasePv), 0) repurchasePv
            from (
            <foreach collection="lcDate.reduceRegionList" item="reduceItem" separator="UNION ALL">
                select
                nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
                nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
                nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
                nvl(cma.REPURCHASE_PV,0) repurchasePv
                FROM
                ${cds.achieveTableNameEnd} cma
                WHERE cma.PERIOD=#{cds.periodEnd}
                AND cma.PK_COUNTRY=#{cds.pkCountry}
                AND cma.pk_member IN (
                SELECT
                cmt.PK_MEMBER
                FROM
                ${cds.settleTableNameEnd} cmt
                WHERE
                cmt.PERIOD=#{cds.periodEnd}
                and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
                START WITH cmt.pk_parent = #{reduceItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
                UNION ALL
                select
                -nvl(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT,0) firstPurchaseMo,
                -nvl(cma.REGISTER_PV+cma.UPGRADE_PV,0) firstPurchasePv,
                -nvl(cma.REPURCHASE_AMOUNT,0) repurchaseMo,
                -nvl(cma.REPURCHASE_PV,0) repurchasePv
                FROM
                ${cds.achieveTableNameSta} cma
                WHERE cma.PERIOD=#{cds.periodSta}
                AND cma.PK_COUNTRY=#{cds.pkCountry}
                AND cma.pk_member IN (
                SELECT
                cmt.PK_MEMBER
                FROM
                ${cds.settleTableNameSta} cmt
                WHERE
                cmt.PERIOD=#{cds.periodSta}
                and cmt.PK_SETTLE_COUNTRY=#{cds.pkCountry}
                START WITH cmt.pk_parent = #{reduceItem} CONNECT BY cmt.pk_parent = PRIOR cmt.pk_id)
            </foreach>
            ) redArea
        </if>

        ) t
        order by pkId asc
    </select>



   <!--     查询国外数据报表-->
    <select id="abroadStatList" resultType="com.hzs.report.member.vo.AbroadDataStatVO">
        select
        shortName,
        pkCountry,
        registerPurchaseMo,
        registerPurchasePv,
        upgradePurchaseMo,
        upgradePurchasePv,
        firstPurchaseMo,
        firstPurchasePv,
        repurchaseMo,
        repurchasePv,
        registerPurchaseSumMo,
        registerPurchaseSumPv,
        upgradePurchaseSumMo,
        upgradePurchaseSumPv,
        firstPurchaseSumMo,
        firstPurchaseSumPv,
        repurchaseSumMo,
        repurchaseSumPv,
        #{endDate} payTime
        from(
        <foreach collection="pkCountryList" item="countryItem" separator="UNION ALL">
          select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            nvl(sum( registerPurchaseMo),0) as registerPurchaseMo,
            nvl(sum( registerPurchasePv),0) as registerPurchasePv,
            nvl(sum( upgradePurchaseMo ),0) as upgradePurchaseMo,
            nvl(sum( upgradePurchasePv ),0) as upgradePurchasePv,
            nvl(sum( firstPurchaseMo),0) as firstPurchaseMo,
            nvl(sum( firstPurchasePv),0) as firstPurchasePv,
            nvl(sum( repurchaseMo ),0) as repurchaseMo,
            nvl(sum( repurchasePv ),0) as repurchasePv,
            nvl(sum( registerPurchaseSumMo ),0) as registerPurchaseSumMo,
            nvl(sum( registerPurchaseSumPv ),0) as registerPurchaseSumPv,
            nvl(sum( upgradePurchaseSumMo ),0) as upgradePurchaseSumMo,
            nvl(sum( upgradePurchaseSumPv),0) as upgradePurchaseSumPv,
            nvl(sum( firstPurchaseSumMo ),0) as firstPurchaseSumMo,
            nvl(sum( firstPurchaseSumPv ),0) as firstPurchaseSumPv,
            nvl(sum( repurchaseSumMo ),0) as repurchaseSumMo,
            nvl(sum( repurchaseSumPv ),0) as repurchaseSumPv
           from (
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            nvl(sum( so.ORDER_AMOUNT ),0) as registerPurchaseMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            0 as firstPurchaseMo,
            0 as firstPurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            0 firstPurchaseSumMo,
            0 firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(1)
            and so.PAY_TIME >= to_date(#{endDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            nvl(sum( so.ORDER_AMOUNT ),0) as upgradePurchaseMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as upgradePurchasePv,
            0 as firstPurchaseMo,
            0 as firstPurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            0 firstPurchaseSumMo,
            0 firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(2)
            and so.PAY_TIME >= to_date(#{endDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            nvl(sum( so.ORDER_AMOUNT ),0) as firstPurchaseMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as firstPurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            0 firstPurchaseSumMo,
            0 firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(1,2)
            and so.PAY_TIME >= to_date(#{endDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            0 firstPurchaseMo,
            0 firstPurchasePv,
            nvl(sum( so.ORDER_AMOUNT ),0) as repurchaseMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            0 firstPurchaseSumMo,
            0 firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(3,26,22,14,12,18,19,7,28,13)
            and so.PAY_TIME >= to_date(#{endDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            0 firstPurchaseMo,
            0 firstPurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            nvl(sum( so.ORDER_AMOUNT ),0) as registerPurchaseSumMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            0 as firstPurchaseSumMo,
            0 as firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(1)
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 firstPurchaseMo,
            0 firstPurchasePv,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            nvl(sum( so.ORDER_AMOUNT ),0) as upgradePurchaseSumMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as upgradePurchaseSumPv,
            0 as firstPurchaseSumMo,
            0 as firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(2)
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            0 firstPurchaseMo,
            0 firstPurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            nvl(sum( so.ORDER_AMOUNT ),0) as firstPurchaseSumMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as firstPurchaseSumPv,
            0 repurchaseSumMo,
            0 repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(1,2)
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            UNION ALL
            select
            #{countryItem.shortName} as shortName,
            #{countryItem.pkCountry} as pkCountry,
            0 as registerPurchaseMo,
            0 as registerPurchasePv,
            0 as upgradePurchaseMo,
            0 as upgradePurchasePv,
            0 firstPurchaseMo,
            0 firstPurchasePv,
            0 repurchaseMo,
            0 repurchasePv,
            0 as registerPurchaseSumMo,
            0 as registerPurchaseSumPv,
            0 as upgradePurchaseSumMo,
            0 as upgradePurchaseSumPv,
            0 firstPurchaseSumMo,
            0 firstPurchaseSumPv,
            nvl(sum( so.ORDER_AMOUNT ),0) as repurchaseSumMo,
            nvl(sum( so.ORDER_ACHIEVE ),0) as repurchaseSumPv
            from SA_ORDER so
            where so.DEL_FLAG=0
            and so.ORDER_STATUS=1
            and so.PK_COUNTRY=#{countryItem.pkCountry}
            and so.ORDER_TYPE in(3,26,22,14,12,18,19,7,28,13)
            and so.PAY_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            )
        </foreach>

        ) order by pkCountry asc
    </select>
<!--     国家列表-->
    <select id="countryList" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
       select
            bc.SHORT_NAME as shortName,
            bc.PK_ID as pkCountry,
		    bcy.IN_EXCHANGE_RATE rate
			from  BD_COUNTRY bc
			INNER JOIN BD_CURRENCY bcy on bc.PK_ID=bcy.PK_COUNTRY and bcy.ENABLE_STATE=0
			 where bc.DEL_FLAG=0
			 and bc.PK_ID!=1
			 and bcy.DEL_FLAG=0
    </select>



    <!-- 新联创查询会员金额和业绩 -->
    <select id="getLianchuangDifference" resultType="com.hzs.report.member.vo.CuLianchuangDataStatVO">
        SELECT
        #{member.vertexCode},
        #{member.vertexName},
        group1.add_AMOUNT - group2.add_AMOUNT AS firstPurchaseMo,
        group1.add_PV - group2.add_PV AS firstPurchasePv,
        group1.SUM_AMOUNT - group2.SUM_AMOUNT AS firstPurchaseSumMo,
        group1.SUM_PV - group2.SUM_PV AS firstPurchaseSumPv,
        group1.ber_sum_AMOUNT - group2.ber_sum_AMOUNT AS beforePurchaseSumMo,
        group1.ber_sum_PV - group2.ber_sum_PV AS beforePurchaseSumPv
        FROM
        (
        SELECT
        SUM(add_AMOUNT) AS add_AMOUNT,
        SUM(add_PV) AS add_PV,
        SUM(SUM_AMOUNT) AS SUM_AMOUNT,
        SUM(SUM_PV) AS SUM_PV,
        SUM(ber_sum_AMOUNT) AS ber_sum_AMOUNT,
        SUM(ber_sum_PV) AS ber_sum_PV
        FROM (
        <!-- 子查询1 -->
        SELECT
        NVL(SUM(cmend.SUM_AMOUNT + cmend.TEAM_SUM_AMOUNT) - SUM(cmber.SUM_AMOUNT + cmber.TEAM_SUM_AMOUNT), 0) AS add_AMOUNT,
        NVL(SUM(cmend.SUM_PV + cmend.TEAM_SUM_PV) - SUM(cmber.SUM_PV + cmber.TEAM_SUM_PV), 0) AS add_PV,
        0 AS SUM_AMOUNT,
        0 AS SUM_PV,
        0 AS ber_sum_AMOUNT,
        0 AS ber_sum_PV
        FROM ${clds.achieveTableNameEnd} cmend
        FULL OUTER JOIN ${clds.achieveTableNameBef} cmber
        ON cmend.pk_member = cmber.pk_member
        WHERE cmend.pk_member IN
        <foreach item="memberId" collection="member.addRegionList" open="(" separator="," close=")">
            #{memberId}
        </foreach>

        UNION ALL

        <!-- 子查询2 -->
        SELECT
        0 AS add_AMOUNT,
        0 AS add_PV,
        NVL(SUM(cmend.SUM_AMOUNT + cmend.TEAM_SUM_AMOUNT) - SUM(cmsta.SUM_AMOUNT + cmsta.TEAM_SUM_AMOUNT), 0) AS SUM_AMOUNT,
        NVL(SUM(cmend.SUM_PV + cmend.TEAM_SUM_PV) - SUM(cmsta.SUM_PV + cmsta.TEAM_SUM_PV), 0) AS SUM_PV,
        0 AS ber_sum_AMOUNT,
        0 AS ber_sum_PV
        FROM ${clds.achieveTableNameEnd} cmend
        FULL OUTER JOIN ${clds.achieveTableNameSta} cmsta
        ON cmend.pk_member = cmsta.pk_member
        WHERE cmend.pk_member IN
        <foreach item="memberId" collection="member.addRegionList" open="(" separator="," close=")">
            #{memberId}
        </foreach>

        UNION ALL

        <!-- 子查询3 -->
        SELECT
        0 AS add_AMOUNT,
        0 AS add_PV,
        0 AS SUM_AMOUNT,
        0 AS SUM_PV,
        NVL(SUM(berend.SUM_AMOUNT + berend.TEAM_SUM_AMOUNT) - SUM(bersta.SUM_AMOUNT + bersta.TEAM_SUM_AMOUNT), 0) AS ber_sum_AMOUNT,
        NVL(SUM(berend.SUM_PV + berend.TEAM_SUM_PV) - SUM(bersta.SUM_PV + bersta.TEAM_SUM_PV), 0) AS ber_sum_PV
        FROM ${clds.achieveTableNameBeforeEnd} berend
        FULL OUTER JOIN ${clds.achieveTableNameBeforeSta} bersta
        ON berend.pk_member = bersta.pk_member
        WHERE berend.pk_member IN
        <foreach item="memberId" collection="member.addRegionList" open="(" separator="," close=")">
            #{memberId}
        </foreach>
        ) combined_data
        ) group1
        CROSS JOIN
        (
        SELECT
        SUM(add_AMOUNT) AS add_AMOUNT,
        SUM(add_PV) AS add_PV,
        SUM(SUM_AMOUNT) AS SUM_AMOUNT,
        SUM(SUM_PV) AS SUM_PV,
        SUM(ber_sum_AMOUNT) AS ber_sum_AMOUNT,
        SUM(ber_sum_PV) AS ber_sum_PV
        FROM (
        <!-- 子查询1 -->
        SELECT
        NVL(SUM(cmend.SUM_AMOUNT + cmend.TEAM_SUM_AMOUNT) - SUM(cmber.SUM_AMOUNT + cmber.TEAM_SUM_AMOUNT), 0) AS add_AMOUNT,
        NVL(SUM(cmend.SUM_PV + cmend.TEAM_SUM_PV) - SUM(cmber.SUM_PV + cmber.TEAM_SUM_PV), 0) AS add_PV,
        0 AS SUM_AMOUNT,
        0 AS SUM_PV,
        0 AS ber_sum_AMOUNT,
        0 AS ber_sum_PV
        FROM ${clds.achieveTableNameEnd} cmend
        FULL OUTER JOIN ${clds.achieveTableNameBef} cmber
        ON cmend.pk_member = cmber.pk_member
            <choose>
                <when test="member.reduceRegionList!=null and member.reduceRegionList.size>0">
                    WHERE cmend.pk_member IN
                    <foreach item="memberId" collection="member.reduceRegionList" open="(" separator="," close=")">
                        #{memberId}
                    </foreach>
                </when>
                <otherwise>
                    WHERE cmend.pk_member is null
                </otherwise>
            </choose>

        UNION ALL

        <!-- 子查询2 -->
        SELECT
        0 AS add_AMOUNT,
        0 AS add_PV,
        NVL(SUM(cmend.SUM_AMOUNT + cmend.TEAM_SUM_AMOUNT) - SUM(cmsta.SUM_AMOUNT + cmsta.TEAM_SUM_AMOUNT), 0) AS SUM_AMOUNT,
        NVL(SUM(cmend.SUM_PV + cmend.TEAM_SUM_PV) - SUM(cmsta.SUM_PV + cmsta.TEAM_SUM_PV), 0) AS SUM_PV,
        0 AS ber_sum_AMOUNT,
        0 AS ber_sum_PV
        FROM ${clds.achieveTableNameEnd} cmend
        FULL OUTER JOIN ${clds.achieveTableNameSta} cmsta
        ON cmend.pk_member = cmsta.pk_member
            <choose>
                <when test="member.reduceRegionList!=null and member.reduceRegionList.size>0">
                    WHERE cmend.pk_member IN
                    <foreach item="memberId" collection="member.reduceRegionList" open="(" separator="," close=")">
                        #{memberId}
                    </foreach>
                </when>
                <otherwise>
                    WHERE cmend.pk_member is null
                </otherwise>
            </choose>

        UNION ALL

        <!-- 子查询3 -->
        SELECT
        0 AS add_AMOUNT,
        0 AS add_PV,
        0 AS SUM_AMOUNT,
        0 AS SUM_PV,
        NVL(SUM(berend.SUM_AMOUNT + berend.TEAM_SUM_AMOUNT) - SUM(bersta.SUM_AMOUNT + bersta.TEAM_SUM_AMOUNT), 0) AS ber_sum_AMOUNT,
        NVL(SUM(berend.SUM_PV + berend.TEAM_SUM_PV) - SUM(bersta.SUM_PV + bersta.TEAM_SUM_PV), 0) AS ber_sum_PV
        FROM ${clds.achieveTableNameBeforeEnd} berend
        FULL OUTER JOIN ${clds.achieveTableNameBeforeSta} bersta
        ON berend.pk_member = bersta.pk_member
            <choose>
                <when test="member.reduceRegionList!=null and member.reduceRegionList.size>0">
                    WHERE berend.pk_member IN
                    <foreach item="memberId" collection="member.reduceRegionList" open="(" separator="," close=")">
                        #{memberId}
                    </foreach>
                </when>
                <otherwise>
                    WHERE berend.pk_member is null
                </otherwise>
            </choose>
        ) combined_data
        ) group2
    </select>

</mapper>
