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


<!--    真实业绩新增奖衔查询-->
    <select id="selectCuMemberAwardsList"
            resultType="com.hzs.report.member.vo.CuMemberAwardsStatVO">
        SELECT
        cm.pk_id AS pkMember,
        cm.MEMBER_CODE AS memberCode,
        cm.MEMBER_NAME AS memberName,
        cm.PHONE AS phone,
        aw.PK_TRANSACTION AS pkAwards,
        ver.VERTEX_NAME AS vertexName,
        ct.TEAM_NAME AS teamName,
        cma.CREATION_TIME AS creationTime,
        bc.SHORT_NAME AS shortName,
        bc.NATIONAL_FLAG1 AS nationalFlag1
        FROM
        CU_MEMBER cm
        LEFT JOIN CU_MEMBER_AWARDS cma ON cm.pk_ID = cma.PK_MEMBER
        LEFT JOIN BD_AWARDS aw ON aw.pk_ID = cm.PK_AWARDS
        LEFT JOIN bd_vertex ver ON cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN BD_COUNTRY bc ON bc.PK_ID = cm.PK_SETTLE_COUNTRY
        INNER JOIN (
        SELECT PK_MEMBER, MAX(CREATION_TIME) AS CREATION_TIME
        FROM CU_MEMBER_AWARDS
        GROUP BY PK_MEMBER
        ) latestAward ON cm.pk_ID = latestAward.PK_MEMBER AND cma.CREATION_TIME = latestAward.CREATION_TIME
        where cma.DEL_FLAG=0 and
        cm.DEL_FLAG=0 and
        aw.DEL_FLAG=0 and
        bc.DEL_FLAG=0
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>
        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="startDate != null ">
            and cma.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cma.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="pkCountry != null">
            AND cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        ORDER BY cma.CREATION_TIME DESC
    </select>

    <!--查询左右区新增业绩-->
    <select id="selectNewPvList"
            resultType="com.hzs.report.member.vo.CuMemberAwardsStatVO">
            <foreach collection="casList" item="item" separator="UNION">
                select
                (A_NEW_PV+REP_A_NEW_PV) aNewPv,
                (B_NEW_PV+REP_B_NEW_PV) bNewPv,
                PK_MEMBER pkMember,
                period
                from ${item.achieveTableName}
                <where>
                    <if test="item.period!= null">
                        PERIOD =#{item.period}
                    </if>
                    <if test="item.pkMemberList != null and item.pkMemberList.size >0">
                      and  PK_MEMBER in
                        <foreach collection="item.pkMemberList" item="pkMember" open="(" separator="," close=")">
                            #{pkMember}
                        </foreach>
                    </if>
                </where>
            </foreach>
    </select>


    <!--    真实奖衔分析-->
    <select id="selectCuMemberAwardsAnalysisList"
            resultType="com.hzs.report.member.vo.CuMemberAwardsAnalysisVO">
        select
        cm.pk_id pkMember,
        cma.period,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        aw.PK_TRANSACTION as pkAwards,
        bg.PK_TRANSACTION as pkRegisterGrade,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        cma.CREATION_TIME creationTime,
        bc.SHORT_NAME shortName,
        bc.NATIONAL_FLAG1 nationalFlag1
        FROM CU_MEMBER_AWARDS cma
        LEFT JOIN CU_MEMBER cm on cm.pk_ID=cma.PK_MEMBER
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN BD_COUNTRY bc on bc.PK_ID=cm.PK_SETTLE_COUNTRY
        where cma.DEL_FLAG=0 and
        cm.DEL_FLAG=0 and
        aw.DEL_FLAG=0 and
        bc.DEL_FLAG=0 and
        bg.DEL_FLAG=0
        <if test="pkCountry != null">
            AND cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>
        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="startDate != null ">
            and cma.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cma.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="pkGrade != null">
            AND cm.PK_REGISTER_GRADE=#{pkGrade}
        </if>

        ORDER BY cma.CREATION_TIME DESC
    </select>
<!--     中国查询真实奖衔分析-->
    <select id="selectCuMemberAwardsAnalysisListCn"
            resultType="com.hzs.report.member.vo.CuMemberAwardsAnalysisVO">
        select
        cm.pk_id pkMember,
        cma.period,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        aw.PK_TRANSACTION as pkAwards,
        bg.PK_TRANSACTION as pkRegisterGrade,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        cma.CREATION_TIME creationTime,
        bc.SHORT_NAME shortName,
        bc.NATIONAL_FLAG1 nationalFlag1
        FROM CU_MEMBER_AWARDS cma
        LEFT JOIN CU_MEMBER cm on cm.pk_ID=cma.PK_MEMBER
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN BD_COUNTRY bc on bc.PK_ID=cm.PK_SETTLE_COUNTRY
        where cma.DEL_FLAG=0 and
        cm.DEL_FLAG=0 and
        aw.DEL_FLAG=0 and
        bc.DEL_FLAG=0 and
        bg.DEL_FLAG=0
        <if test="pkCountry != null">
            AND cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>
        <if test="startDate != null ">
            and cma.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cma.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="pkAwards != null">
            AND aw.AWARDS_VALUE=#{pkAwards}
        </if>
        <if test="pkGrade != null">
            AND bg.GRADE_VALUE=#{pkGrade}
        </if>
        ORDER BY cma.CREATION_TIME DESC
    </select>
    <!--    真实奖衔分析-部门下会员累计业绩 -->
    <select id="selectPlaceDeptMemberAnalysisList"
            resultType="com.hzs.report.member.vo.PlaceDeptMemberVO">
            select
            cmat.PK_MEMBER pkMember,
            cmat.PLACE_DEPT placeDept,
            cmat.SUM_PV sumPv,
            cmat.NEW_PV newPv
            from  ${achieveTableName} cmat
            where cmat.PK_MEMBER=#{pkMember}
    </select>

<!--     真实奖衔分析详情（穿透页面接口）-->
    <select id="selectCuMemberAwardsAnalysisDetailsList"
            resultType="com.hzs.report.member.vo.CuMemberAwardsAnalysisVO">
        select
        cm.pk_id pkMember,
        cma.period,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        aw.PK_TRANSACTION as pkAwards,
        bg.PK_TRANSACTION as pkRegisterGrade,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        cma.CREATION_TIME creationTime,
        bc.SHORT_NAME shortName,
        bc.NATIONAL_FLAG1 nationalFlag1
        FROM CU_MEMBER_AWARDS cma
        LEFT JOIN CU_MEMBER cm on cm.pk_ID=cma.PK_MEMBER
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN BD_COUNTRY bc on bc.PK_ID=cm.PK_SETTLE_COUNTRY
        where cma.DEL_FLAG=0 and
        cm.DEL_FLAG=0 and
        aw.DEL_FLAG=0 and
        bc.DEL_FLAG=0 and
        bg.DEL_FLAG=0
        <if test="pkMember != null and placeDept !=null ">
            AND cm.pk_id in (
            SELECT pk_id FROM cu_member
            start with pk_place_parent = #{pkMember} and place_dept = #{placeDept}
            CONNECT BY pk_place_parent = PRIOR pk_id
            )
        </if>
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>
        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="startDate != null ">
            and cma.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cma.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="pkGrade != null">
            AND cm.PK_REGISTER_GRADE=#{pkGrade}
        </if>
        ORDER BY aw.AWARDS_VALUE DESC
    </select>

<!--    收入明细-->
    <select id="selectRevenueDetailsList"
            resultType="com.hzs.report.member.vo.RevenueDetailsVO">
        SELECT
        LPAD(row_number() over (ORDER BY realIncomeTotal DESC), 2, '0') AS rowNumber,
        purRealSubtotal,
        repurRealSubtotal,
        realIncomeTotal,
        pkMember,
        memberCode,
        memberName,
        phone,
        pkAwards,
        pkRegisterGrade,
        vertexName,
        teamName
        FROM (
        SELECT
        sum(cmb.PUR_REAL_SUBTOTAL) AS purRealSubtotal,
        sum(cmb.REPUR_REAL_SUBTOTAL) AS repurRealSubtotal,
        sum(cmb.REAL_INCOME_TOTAL) AS realIncomeTotal,
        cm.pk_id AS pkMember,
        cm.MEMBER_CODE AS memberCode,
        cm.MEMBER_NAME AS memberName,
        cm.PHONE AS phone,
        aw.PK_TRANSACTION AS pkAwards,
        bg.PK_TRANSACTION AS pkRegisterGrade,
        ver.VERTEX_NAME AS vertexName,
        ct.TEAM_NAME AS teamName
        FROM
        CU_MEMBER_BONUS cmb
        inner join cu_member_settle_period cmsp on cmb.PERIOD=cmsp.PK_ID
        LEFT JOIN CU_MEMBER cm ON cm.pk_ID = cmb.PK_MEMBER
        LEFT JOIN BD_AWARDS aw ON aw.pk_ID = cm.PK_AWARDS
        LEFT JOIN BD_GRADE bg ON bg.pk_id = cm.PK_REGISTER_GRADE
        LEFT JOIN bd_vertex ver ON cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        WHERE
        cmb.DEL_FLAG = 0
        AND cm.DEL_FLAG = 0
        AND aw.DEL_FLAG = 0
        AND bg.DEL_FLAG = 0
        AND cmb.GRANT_STATUS >=2
        and cmb.INCOME_STATUS=0
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>
        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="pkRegisterGrade != null">
            AND cm.PK_REGISTER_GRADE=#{pkRegisterGrade}
        </if>
        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="startDate != null ">
            and cmsp.settle_date &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cmsp.settle_date &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkCountry != null">
            AND cmb.PK_COUNTRY=#{pkCountry}
        </if>

        GROUP BY
        cm.pk_id,
        cm.MEMBER_CODE,
        cm.MEMBER_NAME,
        cm.PHONE,
        aw.PK_TRANSACTION,
        bg.PK_TRANSACTION,
        ver.VERTEX_NAME,
        ct.TEAM_NAME
        )
        ORDER BY realIncomeTotal DESC
    </select>

<!--    查询左右区累计业绩-->
    <select id="selectAddUpPvList"
            resultType="com.hzs.report.member.vo.CuMemberAwardsAnalysisVO">
        <foreach collection="casList" item="item" separator="UNION">
            select
            A_SUM_REAL_PV aSumRealPv,
            B_SUM_REAL_PV bSumRealPv,
            A_SUM_PV aSumPv,
            B_SUM_PV bSumPv,
            REP_A_SUM_PV repASumPv,
            REP_B_SUM_PV repBSumPv,
            period,
            PK_MEMBER pkMember
            from ${item.achieveTableName}
            <where>
                <if test="item.period!= null">
                    PERIOD =#{item.period}
                </if>
                <if test="item.pkMemberList != null and item.pkMemberList.size >0">
                    and  PK_MEMBER in
                    <foreach collection="item.pkMemberList" item="pkMember" open="(" separator="," close=")">
                        #{pkMember}
                    </foreach>
                </if>
            </where>
        </foreach>
    </select>


    <!--      复购bv统计 -->
    <select id="selectRepurchasePVStatList"
            resultType="com.hzs.report.member.vo.RepurchasePVStatVO">
        select
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        aw.PK_TRANSACTION as pkAwards,
        cma.REPURCHASE_NEW_PV repurchaseNewPv,
        (cma.REP_A_SUM_PV+cma.REP_B_SUM_PV) as historyPv ,
        cmas.ASSESS_TARGET assessTarget,
        cmas.MONTH_SURPLUS monthSurplus,
        cmas.ASSESS_STATUS assessStatus,
        cmas.IS_EXAMINE isExamine,
        cmas.CREATION_TIME creationTime
        from ${achieveTableName}  cma
        LEFT JOIN CU_MEMBER cm on cma.PK_MEMBER=cm.pk_id
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN  ${assessTableName} cmas on cma.PK_MEMBER=cmas.PK_MEMBER
        where
        cm.DEL_FLAG=0
        AND cmas.DEL_FLAG = 0 and cmas.ASSESS_TYPE=0
        and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        <if test="pkMember != null ">
            AND cm.pk_id = #{pkMember}
        </if>
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>

        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="period != null">
            AND cma.PERIOD=#{period}
        </if>
        <if test="period != null">
            AND cmas.PERIOD=#{period}
        </if>

        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="assessStatus != null">
            AND cmas.ASSESS_STATUS=#{assessStatus}
        </if>
        <if test="isExamine != null">
            AND cmas.IS_EXAMINE=#{isExamine}
        </if>
        <if test="startDate != null ">
            and cmas.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cmas.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>

        ORDER BY cmas.CREATION_TIME DESC
    </select>

<!--     商城BV统计-->
    <select id="selectMallPVStatList"
            resultType="com.hzs.report.member.vo.MallPVStatVO">
        select
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        aw.PK_TRANSACTION as pkAwards,
        cmas.ASSESS_TARGET assessTarget,
        cmas.MONTH_SURPLUS monthSurplus,
        cmas.ASSESS_STATUS assessStatus,
        cmas.IS_EXAMINE isExamine,
        cmas.CREATION_TIME creationTime
        from ${achieveTableName}  cma
        LEFT JOIN CU_MEMBER cm on cma.PK_MEMBER=cm.pk_id
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN  ${assessTableName} cmas on cma.PK_MEMBER=cmas.PK_MEMBER
        where
        cm.DEL_FLAG=0
        AND cmas.DEL_FLAG = 0 and cmas.ASSESS_TYPE=1
        and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        <if test="pkMember != null ">
            AND cm.pk_id = #{pkMember}
        </if>
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>

        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="period != null">
            AND cma.PERIOD=#{period}
        </if>

        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="assessStatus != null">
            AND cmas.ASSESS_STATUS=#{assessStatus}
        </if>
        <if test="isExamine != null">
            AND cmas.IS_EXAMINE=#{isExamine}
        </if>
        <if test="startDate != null ">
            and cmas.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cmas.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        ORDER BY cmas.CREATION_TIME DESC
    </select>

<!--     查询个人统计-->
    <select id="selectPersonStatList"
            resultType="com.hzs.report.member.vo.PersonStatVO">
        select
        cm.pk_id pkMember,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        aw.PK_TRANSACTION as pkAwards,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        bg.PK_TRANSACTION as pkRegisterGrade
        FROM CU_MEMBER cm
        LEFT JOIN BD_AWARDS aw on aw.pk_ID=cm.PK_AWARDS
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        LEFT JOIN BD_GRADE bg on bg.pk_id=cm.PK_REGISTER_GRADE
        where
        cm.DEL_FLAG=0 and
        aw.DEL_FLAG=0 and
        bg.DEL_FLAG=0
        <if test="memberCode != null and memberCode !='' ">
            AND cm.MEMBER_CODE like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName !='' ">
            AND cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="phone != null and phone !='' ">
            AND cm.phone like #{phone} || '%'
        </if>
        <if test="pkAwards != null">
            AND cm.PK_AWARDS=#{pkAwards}
        </if>
        <if test="startDate != null ">
            and cm.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and cm.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        <if test="pkCountry != null">
            AND cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="pkRegisterGrade != null">
            AND cm.PK_REGISTER_GRADE=#{pkRegisterGrade}
        </if>
        ORDER BY cm.CREATION_TIME DESC
    </select>

<!--    个人统计业绩查询-->
    <select id="selectPersonPvList"
            resultType="com.hzs.report.member.vo.PersonStatVO">
        <foreach collection="perList" item="item" separator="UNION">
            select
            REGISTER_PV registerPv,
            REPURCHASE_PV repurchasePv,
            period,
            PK_MEMBER pkMember
            from ${item.achieveTableName}
            <where>
                <if test="item.period!= null">
                    PERIOD =#{item.period}
                </if>
                <if test="item.pkMemberList != null and item.pkMemberList.size >0">
                    and  PK_MEMBER in
                    <foreach collection="item.pkMemberList" item="pkMember" open="(" separator="," close=")">
                        #{pkMember}
                    </foreach>
                </if>
            </where>
        </foreach>
    </select>

<!--     直推统计-->
    <select id="selectDirectPushStatList" resultType="com.hzs.report.member.vo.DirectPushVO">
        SELECT
        #{orderType} orderType,
        cm.PK_ID pkMember,
        cm.member_code memberCode,
        cm.member_name memberName,
        cm.phone,
        gr.PK_TRANSACTION AS pkRegisterGrade,
        awa.PK_TRANSACTION AS pkAwards,
        par.member_code parMemberCode,
        par.member_name parMemberName,
        place.member_code placeMemberCode,
        place.member_name placeMemberName,
        cm.CREATION_TIME creationTime,
        ver.VERTEX_NAME AS vertexName,
        ct.TEAM_NAME AS teamName,
        so.ORDER_ACHIEVE orderAchieve,
        bc.SHORT_NAME shortName,
        bct.SHORT_NAME settleCountryName
        FROM
        cu_member cm
        LEFT JOIN bd_vertex ver ON cm.pk_vertex = ver.pk_id
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        left join BD_COUNTRY bc on cm.PK_COUNTRY=bc.pk_id
        left join BD_COUNTRY bct on cm.PK_SETTLE_COUNTRY=bct.pk_id
        inner JOIN ( select * from (
        SELECT
        sum(so.ORDER_ACHIEVE) orderAchieve,
        so.PK_REFERENCE pkMember
        FROM
        SA_ORDER so
        WHERE
        so.DEL_FLAG = 0
        AND so.order_status = 1
        <if test="pkCountry != null">
            and so.pk_Country=#{pkCountry}
        </if>
        <if test="orderTypeList != null">
            and  so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startDate != null ">
            and so.PAY_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        GROUP BY
        so.PK_REFERENCE
        )
        <if test="orderAchieve != null">
            where  orderAchieve &gt;= #{orderAchieve}
        </if>
        ) subq ON cm.PK_PARENT = subq.pkMember
        INNER JOIN cu_member par ON par.pk_id = cm.pk_parent
        INNER JOIN cu_member place ON place.pk_id = cm.pk_place_parent
        LEFT JOIN bd_awards awa ON awa.pk_id = cm.PK_AWARDS
        LEFT JOIN bd_grade gr ON gr.pk_id = cm.PK_REGISTER_GRADE
        INNER JOIN SA_ORDER so ON so.PK_MEMBER = cm.PK_ID
        and so.DEL_FLAG = 0 and so.ORDER_STATUS=1
        <if test="orderTypeList != null">
            and  so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        where cm.del_flag = 0
        and par.del_flag = 0
        and place.del_flag = 0
        and awa.del_flag = 0
        and gr.del_flag = 0
        and so.DEL_FLAG = 0
        <if test="pkCountry != null ">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
        <if test="startDate != null and startDate !='' ">
            and so.PAY_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate !=''">
            and so.PAY_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkCountry != null">
            and so.pk_Country=#{pkCountry}
        </if>

        <if test="pkRegisterGrade != null">
            and  cm.PK_REGISTER_GRADE = #{pkRegisterGrade}
        </if>
        <if test="pkAwards != null">
            and  cm.PK_AWARDS = #{pkAwards}
        </if>
        <if test="pkTeamCode != null">
            and  cm.pk_team_code = #{pkTeamCode}
        </if>
        <if test="pkVertex != null">
            and  cm.pk_vertex = #{pkVertex}
        </if>
        <if test="memberName != null and memberName!=''">
            and  cm.member_name like #{memberName} || '%'
        </if>
        <if test="parMemberCode != null and parMemberCode!=''">
            and  par.member_code like #{parMemberCode} || '%'
        </if>
        <if test="parMemberName != null and parMemberName!=''">
            and  par.member_name like #{parMemberName} || '%'
        </if>

        <if test="phone != null and phone!=''">
            and  cm.phone like #{phone} || '%'
        </if>
        <if test="orderAchieve != null">
            and subq.orderAchieve &gt;= #{orderAchieve}
        </if>
        ORDER BY so.ORDER_ACHIEVE DESC
    </select>
<!--     会员订单分布统计-->
    <select id="memberOrderScatter" resultType="com.hzs.report.member.vo.OrderScatterVO">
       select  *  from (
        SELECT
        (sum(so.ORDER_ACHIEVE)) orderAchieve,
        (sum(ORDER_AMOUNT)) orderAmount,
        #{orderType} orderType,
        <if test="areaType == 1">
            pr.name prName
        </if>
        <if test="areaType == 2">
            pr.name prName,
            ct.name ctName
        </if>
        <if test="areaType == 3">
            pr.name prName,
            ct.name ctName,
            co.name coName
        </if>
        from SA_ORDER so
        left join BD_AREA pr on pr.pk_id=so.REC_PROVINCE
        left join BD_AREA ct on ct.pk_id=so.REC_CITY
        left join BD_AREA co on co.pk_id=so.REC_COUNTY
        where
        pr.del_flag = 0
        and ct.del_flag = 0
        and co.del_flag = 0
        and so.del_flag = 0
        and so.pk_Country=#{pkCountry}
        and so.ORDER_STATUS=1
        <if test="startDate != null ">
            and so.CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null">
            and so.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null">
            and  so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY
        <if test="areaType == 1">
            pr.name
        </if>
        <if test="areaType == 2">
            pr.name,
            ct.name
        </if>
        <if test="areaType == 3">
            pr.name,
            ct.name,
            co.name
        </if>
        ) ORDER BY orderAchieve DESC


    </select>

    <!--    安置业绩统计 查询会员-->
    <select id="positStatList" resultType="com.hzs.report.member.vo.PositStatVO">
        select  cm.pk_id pkMember,
        cm.member_code memberCode,
        cm.member_name memberName,
        gr.PK_TRANSACTION AS pkSettleGrade,
        awa.PK_TRANSACTION AS pkAwards,
        bc.SHORT_NAME shortName,
        bc.NATIONAL_FLAG1 nationalFlag1
        from  CU_MEMBER  cm
        INNER JOIN CU_MEMBER pa on cm.PK_ID=pa.PK_PLACE_PARENT
        LEFT JOIN bd_awards awa ON awa.pk_id = cm.PK_AWARDS
        LEFT JOIN bd_grade gr ON gr.pk_id = cm.PK_SETTLE_GRADE
        LEFT JOIN BD_COUNTRY bc on bc.pk_id=cm.PK_SETTLE_COUNTRY
        where cm.del_flag = 0
        and cm.CATEGORY=0
        and awa.del_flag = 0
        and gr.del_flag = 0
        and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        GROUP BY cm.pk_id ,
        cm.member_code ,
        cm.member_name ,
        gr.PK_TRANSACTION ,
        awa.PK_TRANSACTION ,
        bc.SHORT_NAME ,
        bc.NATIONAL_FLAG1

    </select>


    <!--    安置业绩统计 查询部门业绩-->
    <select id="positStatAchieve" resultType="com.hzs.report.member.vo.PositDeptAchieveVO">
        select * from (
        <foreach collection="psVO.placeDeptList" item="item" separator="UNION ALL">
            SELECT
            #{psVO.pkMember} pkMember,
            #{item} placeDept ,
            nvl( sum( so.ORDER_ACHIEVE ), 0 ) achieve,
            nvl( sum( so.ORDER_AMOUNT/#{psVO.rate} ), 0 ) amount
            FROM
            SA_ORDER so
            INNER JOIN (
            SELECT
            a.pk_id
            FROM
            ( SELECT pk_id, del_flag, CATEGORY FROM cu_member WHERE 1 = 1
            START WITH pk_place_parent = #{psVO.pkMember} AND place_dept = #{item} CONNECT BY pk_place_parent = PRIOR pk_id ) a
            WHERE
            a.del_flag = 0 and a.CATEGORY=0
            ) a ON so.pk_member = a.pk_id
            WHERE
            so.del_flag = 0
            AND so.ORDER_STATUS = 1
            <if test="psVO.startDate != null and psVO.startDate!='' ">
                and so.PAY_TIME &gt;= to_date(#{psVO.startDate}, 'yyyy-mm-dd')
            </if>
            <if test="psVO.endDate != null and psVO.endDate!='' ">
                and so.PAY_TIME &lt;= to_date(#{psVO.endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="psVO.orderTypeList != null">
                and  so.ORDER_TYPE in
                <foreach collection="psVO.orderTypeList" item="ortyitem" open="(" close=")" separator=",">
                    #{ortyitem}
                </foreach>
            </if>
        </foreach>
        ) rig where  1=1
        <if test="psVO.achieve != null ">
            and rig.achieve &gt;= #{psVO.achieve}
        </if>
        ORDER BY rig.placeDept ASC

    </select>


    <select id="achieveSummaryList" resultType="com.hzs.report.member.vo.AchieveSummaryVO">
        select  sum(cms.REGISTER_NEW_AMOUNT) orderAmount,
        sum(cms.REGISTER_AMOUNT) orderAmountSum,
        sum(cms.REGISTER_NEW_PV) orderAchieve ,
        sum(cms.REGISTER_PV) orderAchieveSum,
        cms.PK_SETTLE_COUNTRY pkSettleCountry,
        bc.SHORT_NAME shortName
        from ${settleTableName} cms
        left join BD_COUNTRY bc on bc.pk_id=cms.PK_SETTLE_COUNTRY
        where 1=1
        <if test="countryIdList != null">
            and  PK_SETTLE_COUNTRY in
            <foreach collection="countryIdList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY cms.PK_SETTLE_COUNTRY,bc.SHORT_NAME
        ORDER BY cms.PK_SETTLE_COUNTRY
    </select>

    <!--   查询奖金扣项配置参数-->
    <select id="bonusDeduct" parameterType="com.hzs.report.member.vo.AchieveSummaryVO"
            resultType="com.hzs.report.member.vo.AchieveSummaryVO">
        select b.PK_COUNTRY pkSettleCountry,
        (a.BACK_RATIO+a.PLATFORM_RATIO)/100 backRatio
        from BD_BONUS_ITEMS b
        left JOIN BD_BONUS_DEDUCT a on a.PK_BONUS_ITEMS=b.PK_ID
        where
        b.DEL_FLAG =0
        and a.BACK_RATIO>0
        and a.PLATFORM_RATIO>0
        <if test="pkSettleCountry != null ">
            AND b.PK_COUNTRY = #{pkSettleCountry}
        </if>
        ORDER BY a.BACK_RATIO,a.PLATFORM_RATIO DESC
    </select>

    <!--     查询国家首购奖金拨比-->
    <select id="queryForeignCountryCuBonusStatisSum" parameterType="com.hzs.report.member.vo.AchieveSummaryVO"
            resultType="java.math.BigDecimal">
        select  nvl(mainBonusRate,0)  from  (
                SELECT
                CASE WHEN sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT) = 0
                THEN 0 ELSE ROUND (#{perCent}*#{backRatio}*sum(cbs.INTRODUCE_BONUS + cbs.ORG_BONUS+cbs.LEADER_BONUS
                +cbs.SEE_POINT_INCOME+cbs.SHARE_INCOME)
                / sum(cbs.MAIN_REG_AMOUNT+cbs.MAIN_UP_AMOUNT),4)  end  AS mainBonusRate
                FROM
                CU_BONUS_STATIS cbs
                left join bd_vertex ver on cbs.pk_bd_vertex = ver.pk_id
                LEFT JOIN cu_member_team ct ON ct.pk_id = cbs.pk_team_code
                WHERE 1=1

                <if test="pkSettleCountry != null">
                    and cbs.PK_COUNTRY=#{pkSettleCountry}
                </if>
                <if test="startDate!=null and startDate!='' ">
                    and cbs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
                </if>
                <if test="endDate!=null and endDate!='' ">
                    and cbs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                </if>
                )


    </select>

<!--    查询开通店铺会员的推荐-->
    <select id="queryReferenceList" resultType="com.hzs.report.member.vo.QueryParentStoreVO">
        select
        distinct
        cm.PK_ID pkMember,
        cm.PK_PARENT pkParent,
        cs.CREATION_TIME creationTime,
        bsl.LEVEL_VALUE levelValue
        from  CU_STORE cs
        INNER JOIN CU_MEMBER cm on cs.PK_MEMBER=cm.PK_ID and cm.CATEGORY = 0
        INNER JOIN CU_MEMBER pat on pat.PK_ID=cm.PK_PARENT and pat.CATEGORY = 0 and pat.DEL_FLAG=0
        INNER JOIN BD_COUNTRY bc on bc.pk_id=pat.PK_SETTLE_COUNTRY
        INNER JOIN BD_STORE_LEVEL bsl on bsl.PK_ID=cs.PK_STORE_LEVEL and bsl.DEL_FLAG=0
        where cs.DEL_FLAG=0
        <if test="pkSettleCountry != null">
            and pat.PK_SETTLE_COUNTRY=#{pkSettleCountry}
        </if>
        <if test="levelValue != null">
            and bsl.LEVEL_VALUE=#{levelValue}
        </if>
        <if test="startDate!=null and startDate!='' ">
            and cs.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cs.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
    </select>
    <!--    统计推荐人安置部门下开通店铺-->
    <select id="queryParentStoreList" resultType="com.hzs.report.member.vo.StoreNumberVO">
        select
         cm.pk_place_parent pkParent,
         cm.place_dept placeDept,
         count(1) storeNumber
          from cu_member cm
         where cm.pk_place_parent = #{pkParent}
         start with cm.pk_id = #{pkMember}
        connect by cm.pk_id = prior cm.pk_place_parent
        GROUP BY  cm.pk_place_parent, cm.place_dept
    </select>

<!--     查询推荐人安置伞下开通店铺会员-->
    <select id="parentStoreDetailsId" resultType="com.hzs.report.member.vo.ParentStoreDetailsVO">
                select
                cm.PK_ID pkId,
				cs.creation_time creationTime
        from (
        SELECT
        cm.PK_ID,
        cm.PK_PARENT
        FROM CU_MEMBER cm
        WHERE cm.CATEGORY = 0 and cm.DEL_FLAG = 0
            START WITH cm.pk_place_parent=#{pkId}
            CONNECT BY cm.pk_place_parent = PRIOR cm.PK_ID
        ) cm
        INNER JOIN CU_STORE cs on cs.PK_MEMBER=cm.PK_ID
        INNER JOIN BD_STORE_LEVEL bsl on bsl.PK_ID=cs.PK_STORE_LEVEL and bsl.DEL_FLAG=0
        <if test="levelValue != null">
            and bsl.LEVEL_VALUE  &gt;= #{levelValue}
        </if>
        where cs.DEL_FLAG=0 and  cm.PK_PARENT=#{pkId}
    </select>

    <select id="parentStoreDetails" resultType="com.hzs.report.member.vo.ParentStoreDetailsVO">
                select
				cs.store_code storeCode,
				cs.store_name storeName,
				bsl.LEVEL_VALUE levelValue,
				cm.PK_ID pkId,
                cmm.member_code memberCode,
                cmm.member_name memberName,
                cm.PLACE_DEPT placeDept,
				cs.creation_time creationTime
                    from (
                    SELECT
                    #{pkId} PK_ID,
                    cm.place_dept
                    FROM
                    cu_member cm
                    WHERE cm.CATEGORY = 0 and cm.DEL_FLAG = 0
                    and cm.pk_place_parent = #{pkParent} START WITH cm.pk_id = #{pkId} CONNECT BY cm.pk_id = PRIOR cm.pk_place_parent
                    ) cm
                    INNER JOIN CU_STORE cs on cs.PK_MEMBER=cm.PK_ID
                    INNER JOIN cu_member cmm on cs.PK_MEMBER=cmm.PK_ID
                    INNER JOIN BD_STORE_LEVEL bsl on bsl.PK_ID=cs.PK_STORE_LEVEL and bsl.DEL_FLAG=0
                    where cs.DEL_FLAG=0
                    order by cm.PLACE_DEPT
    </select>


<!--     推荐店铺查询-->
    <select id="getParentStoreList" resultType="com.hzs.report.member.vo.QueryParentStoreVO">
        select
        tmp.PK_PARENT pkId,
        tmp.PK_PARENT pkParent,
        tmp.placeDeptStr,
        tmp.storeNumberStr,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        bc.SHORT_NAME shortName
            from (select tmp.PK_PARENT,
            LISTAGG(tmp.PLACE_DEPT, ',') WITHIN GROUP(ORDER BY STORE_NUMBER desc) AS placeDeptStr,
            LISTAGG(tmp.STORE_NUMBER, ',') WITHIN GROUP(ORDER BY STORE_NUMBER desc) AS storeNumberStr
            from (SELECT PK_PARENT,
            PLACE_DEPT,
            COUNT(STORE_NUMBER) AS STORE_NUMBER
            FROM CU_PARENT_STORE
            where 1=1
            <if test="levelValue!=null">
                and LEVEL_VALUE &gt;= #{levelValue}
            </if>
            <if test="startDate != null and startDate!='' ">
                and CREATION_TIME &gt;= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate!=''">
                and CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
            GROUP BY PK_PARENT,  PLACE_DEPT
            order by PK_PARENT, STORE_NUMBER desc) tmp
            group by tmp.PK_PARENT) tmp
            INNER JOIN CU_MEMBER cm on cm.PK_ID=tmp.PK_PARENT and cm.DEL_FLAG = 0 and cm.CATEGORY = 0
            INNER JOIN BD_COUNTRY bc on bc.pk_id=cm.PK_SETTLE_COUNTRY and bc.DEL_FLAG = 0
            where
            <if test="storeNumber1>0">
                (case
                when REGEXP_SUBSTR(tmp.storeNumberStr, '[^,]+', 1, 1) >= #{storeNumber1} then
                1
                else
                0
                end)
            </if>
        <if test="storeNumber2>0">
            +(case
            when REGEXP_SUBSTR(tmp.storeNumberStr, '[^,]+', 1, 2) >= #{storeNumber2} then
            1
            else
            0
            end)
        </if>
        <if test="storeNumber3>0">
            +(case
            when REGEXP_SUBSTR(tmp.storeNumberStr, '[^,]+', 1, 3) >= #{storeNumber3} then
            1
            else
            0
            end)
        </if>
        <if test="storeNumber4>0">
            +(case
            when REGEXP_SUBSTR(tmp.storeNumberStr, '[^,]+', 1, 4) >= #{storeNumber4} then
            1
            else
            0
            end)
        </if>
        <if test="storeNumber5>0">
            +(case
            when REGEXP_SUBSTR(tmp.storeNumberStr, '[^,]+', 1, 5) >= #{storeNumber5} then
            1
            else
            0
            end)
        </if>
              >= #{storeSize}
        <if test="pkCountry!=null">
            and cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </if>
    </select>
<!--    查询表是否存在-->
    <select id="queryExistOracleTable" resultType="java.lang.Integer">
        select 1
        from user_tables
        where table_name = upper(#{tableName})
    </select>




</mapper>
