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


    <!-- 查询联创数据报表-->
    <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(repurchaseOrderMo), 0) repurchaseOrderMo,
        nvl(sum(repurchaseOrderPv), 0) repurchaseOrderPv,
        nvl(sum(repurchaseOrderSumMo), 0) repurchaseOrderSumMo,
        nvl(sum(repurchaseOrderSumPv), 0) repurchaseOrderSumPv,
        nvl(sum(repurchaseMo), 0) repurchaseMo,
        nvl(sum(repurchasePv), 0) repurchasePv,
        nvl(sum(repurchaseSumMo), 0) repurchaseSumMo,
        nvl(sum(repurchaseSumPv), 0) repurchaseSumPv
        from(
        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,
        0 repurchaseOrderMo,
        0 repurchaseOrderPv,
        0 repurchaseOrderSumMo,
        0 repurchaseOrderSumPv,
        0 repurchaseMo,
        0 repurchasePv,
        0 repurchaseSumMo,
        0 repurchaseSumPv
        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(1, 2, 24, 25, 20)
        <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,
        CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_AMOUNT, 0)) else 0 end repurchaseOrderMo,
        CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_ACHIEVE, 0)) else 0 end repurchaseOrderPv,
        CASE WHEN sum(nvl(so.ORDER_AMOUNT, 0)) is null THEN 0 ELSE sum(so.ORDER_AMOUNT) end repurchaseOrderSumMo,
        CASE WHEN sum(nvl(so.ORDER_ACHIEVE, 0)) is null THEN 0 ELSE sum(so.ORDER_ACHIEVE) end repurchaseOrderSumPv,
        0 repurchaseMo,
        0 repurchasePv,
        0 repurchaseSumMo,
        0 repurchaseSumPv
        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(3,12,13,14,18,19,26,22,28)
        <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,
        0 repurchaseOrderMo,
        0 repurchaseOrderPv,
        0 repurchaseOrderSumMo,
        0 repurchaseOrderSumPv,
        CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_AMOUNT, 0)-nvl(so.CONSUME_AMOUNT, 0)) else 0 end
        repurchaseMo,
        CASE when to_char(so.PAY_TIME, 'yyyy-mm-dd') = #{lcDate.endDate} then sum(nvl(so.ORDER_ACHIEVE, 0)) else 0 end repurchasePv,
        CASE WHEN sum(nvl(so.ORDER_AMOUNT, 0)) is null THEN 0 ELSE sum(so.ORDER_AMOUNT-nvl(so.CONSUME_AMOUNT, 0)) end repurchaseSumMo,
        CASE WHEN sum(nvl(so.ORDER_ACHIEVE, 0)) is null THEN 0 ELSE sum(so.ORDER_ACHIEVE) end repurchaseSumPv
        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(3,10,26,22,14,12,18,19,7,28,13,27,31,40)
        <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')
        )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>

</mapper>
