<?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.retail.member.mapper.CuMemberRetailRangeMapper">
    <select id="selectDirectPushByPkMember" resultType="long">
        SELECT m.pk_id
        from cu_member m
        WHERE m.del_flag = 0
          AND m.category = 0
          AND m.system_type = #{systemType}
          AND m.pk_parent = #{pkMember}
    </select>

    <sql id="selectSmallAreaAchieve">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT SUM(t1.arealPv) arealPv ,#{pkMember} pkMember
            FROM (
            <if test="isFirst != null and isFirst == 0">
                SELECT NVL(mr.MONTH_CONSUME_PV + mr.TEAM_MONTH_PV,0) arealPv
                FROM ${memberRetailTable} mr
                WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
                UNION ALL
            </if>
            SELECT NVL(SUM(so.order_achieve), 0) arealPv
            FROM sa_order so
            WHERE so.del_flag = 0
            AND so.order_status = 1
            AND so.order_type in (41, 42, 43, 44)
            AND so.system_type = #{systemType}
            AND so.pay_time >= TRUNC(SYSDATE)
            AND so.pk_member IN (
            SELECT t0.pk_id
            FROM (SELECT m.pk_id,
            m.del_flag,
            m.category,
            m.pay_status,
            m.system_type
            FROM cu_member m
            START WITH m.pk_id = #{pkMember}
            CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
            WHERE t0.del_flag = 0
            AND t0.system_type = #{systemType}
            AND t0.category = 0)) t1
        </foreach>
    </sql>

    <select id="selectMemberOrderByPkMember" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT t0.payTime
        FROM (SELECT so.pay_time payTime
              FROM sa_order so
              WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type = 40
                AND so.pay_time >= TRUNC(SYSDATE)
                AND so.pk_member = #{pkMember}
              ORDER BY so.pay_time) t0
        WHERE ROWNUM = 1
    </select>


    <select id="selectMemberParentByPkMember" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT SUM(t1.arealPv) arealPv , SUM(t1.welfarelPv) welfarelPv,#{pkMember} pkMember
            FROM (
            SELECT NVL(SUM(so.order_achieve) + SUM(so.order_ass_achieve), 0) arealPv,
            NVL(SUM(so.order_achieve) + SUM(so.order_ass_achieve), 0) welfarelPv
            FROM sa_order so
            WHERE so.del_flag = 0
            AND so.order_status = 1
            AND so.order_type =40
            AND so.pay_time >= #{payTime}
            AND so.pk_member IN (
            SELECT t0.pk_id
            FROM (SELECT m.pk_id,
            m.del_flag,
            m.category,
            m.pay_status,
            m.system_type
            FROM cu_member m
            START WITH m.pk_id = #{pkMember}
            CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
            WHERE t0.del_flag = 0
            AND t0.category = 0)
            ) t1
        </foreach>
    </select>

    <select id="selectTableNameIsExist" resultType="string">
        SELECT table_name FROM user_tables WHERE table_name in
        <foreach item="table" collection="tableNames" open="(" separator="," close=")">
            #{table}
        </foreach>
    </select>


    <select id="selectMemberAchieve" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        <include refid="selectSmallAreaAchieve"/>
    </select>

    <select id="selectMemberAchieveDetail_bak" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT mr.member_code memberCode,
        mr.member_name memberName,
        mr.new_consume_pv + mr.team_new_pv yesterdayPv,
        mr.consume_pv + mr.team_consume_pv sumPv,
        mr.month_consume_pv + mr.team_month_pv monthPv
        FROM ${memberRetailTable} mr
        WHERE mr.category = 0
        AND mr.pay_status = 1
        AND mr.pk_member = #{pkMember}
        <if test="pkMemberList != null and pkMemberList.size > 0">
            union all
            SELECT mr.member_code memberCode,
            mr.member_name memberName,
            mr.new_consume_pv + mr.team_new_pv yesterdayPv,
            mr.consume_pv + mr.team_consume_pv sumPv,
            mr.month_consume_pv + mr.team_month_pv monthPv
            FROM ${memberRetailTable} mr
            WHERE mr.category = 0
            AND mr.pay_status = 1
            <if test="memberName != null and memberName != ''">
                and (mr.member_code like #{memberName} || '%' or mr.member_name like #{memberName} || '%')
            </if>
            AND mr.pk_member in
            <foreach collection="pkMemberList" item="item" index="index" open="(" close=")">
                <if test="index != 0">
                    <choose>
                        <when test="index % 999 == 0">) or mr.pk_member IN (</when>
                        <otherwise>,</otherwise>
                    </choose>
                </if>
                #{item}
            </foreach>
            ORDER BY sumPv desc
        </if>
    </select>


    <select id="selectMemberAchieveDetail" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT cm.member_code memberCode, cm.member_name memberName,cm.nick_name nickName, t2.yesterdayPv, t2.sumPv,
        t2.monthPv,t2.realTimePv, t2.upMonthPv,bg.grade_name grade, ba.awards_name award
        FROM (
        SELECT pkMember,
        NVL(sum(t9.yesterdaypv), 0) yesterdayPv,
        NVL(sum(t9.sumPv), 0) sumPv,
        NVL(sum(t9.monthPv), 0) monthPv,
        NVL(sum(t9.realTimePv), 0) realTimePv,
        NVL(sum(t9.upMonthPv), 0) upMonthPv
        FROM (
        <include refid="selectPushAchieveList"/>
        <if test="pkMemberList != null and pkMemberList.size > 0">
            union all
            <include refid="selectUpMonthAchieveList"/>
        </if>
        ) t9
        group by pkMember
        ) t2
        left join cu_member cm
        on cm.pk_id = t2.pkmember
        left join bd_grade bg
        on cm.pk_settle_grade = bg.pk_id
        left join bd_awards ba
        on cm.pk_awards = ba.pk_id
        where cm.del_flag =0
        <if test="memberName != null and memberName != ''">
            and (cm.member_code like #{memberName} || '%' or cm.member_name like #{memberName} || '%')
        </if>
        ORDER BY t2.monthPv desc
    </select>


    <select id="selectMemberBigAchieve" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT cm.member_code, cm.member_name,cm.nick_name, t3.yesterdayPv, t3.sumPv, t3.monthPv,t3.realTimePv,cm.pk_id
        pkBigMember,bg.grade_name grade, ba.awards_name award
        FROM (SELECT *
        FROM (
        <include refid="selectPushAchieveList"/>
        ORDER BY monthPv desc) t2
        where rownum = 1) t3
        left join cu_member cm
        on cm.pk_id = t3.pkmember
        left join bd_grade bg
        on cm.pk_settle_grade = bg.pk_id
        left join bd_awards ba
        on cm.pk_awards = ba.pk_id
    </select>

    <!--查询上月业绩-->
    <sql id="selectUpMonthAchieveList">
        SELECT mr.pk_member,
        0 yesterdayPv,
        0 sumPv,
        0 monthPv,
        0 realTimePv,
        mr.month_consume_pv + mr.team_month_pv upMonthPv
        FROM ${retailUpMonthTable} mr
        WHERE mr.category = 0
        AND mr.pay_status = 1
        AND mr.pk_member in
        <foreach collection="pkMemberList" item="item" index="index" open="(" close=")">
            <if test="index != 0">
                <choose>
                    <when test="index % 999 == 0">) or mr.pk_member IN (</when>
                    <otherwise>,</otherwise>
                </choose>
            </if>
            #{item}
        </foreach>
    </sql>


    <select id="selectMemberOrderAchieveByPkMember" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT sum(arealPv) arealPv
        FROM (
        <if test="isFirst == 0">
            SELECT NVL(mr.MONTH_CONSUME_PV, 0) arealPv
            FROM ${memberRetailTable} mr
            WHERE mr.category = 0
            AND mr.pk_member = #{pkMember}
            union all
        </if>
        SELECT NVL(SUM(so.order_achieve), 0) arealPv
        FROM sa_order so
        WHERE so.del_flag = 0
        AND so.order_status = 1
        AND so.order_type in (41, 42, 43, 44)
        AND so.system_type = #{systemType}
        AND so.pay_time >= TRUNC(SYSDATE)
        AND so.pk_member = #{pkMember})
    </select>


    <sql id="selectPushAchieveList">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT pkMember,
            NVL(sum(t1.yesterdaypv), 0) yesterdayPv,
            NVL(sum(t1.sumPv), 0) sumPv,
            NVL(sum(t1.monthPv), 0) monthPv,
            NVL(sum(t1.realTimePv), 0) realTimePv,
            0 upMonthPv
            FROM (
            <if test="isFirst != null and isFirst == 0">
                SELECT mr.pk_member pkMember,
                mr.new_consume_pv + mr.team_new_pv yesterdayPv,
                mr.consume_pv + mr.team_consume_pv sumPv,
                mr.month_consume_pv + mr.team_month_pv monthPv,
                0 realTimePv
                FROM ${memberRetailTable} mr
                WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
                UNION ALL
            </if>
            SELECT #{pkMember} pkMember,
            0 yesterdayPv,
            NVL(SUM(so.order_achieve), 0) sumPv,
            NVL(SUM(so.order_achieve), 0) monthPv,
            NVL(SUM(so.order_achieve), 0) realTimePv
            FROM sa_order so
            WHERE so.del_flag = 0
            AND so.order_status = 1
            AND so.system_type = #{systemType}
            AND so.order_type in (41, 42, 43, 44)
            AND so.pay_time >= TRUNC(SYSDATE)
            AND so.pk_member IN (SELECT t0.pk_id
            FROM (SELECT m.pk_id,
            m.del_flag,
            m.category,
            m.pay_status,
            m.system_type
            FROM cu_member m
            START WITH m.pk_id = #{pkMember}
            CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
            WHERE t0.del_flag = 0
            AND t0.system_type = #{systemType}
            AND t0.category = 0)) t1
            group by pkMember
        </foreach>
    </sql>


    <select id="selectUpMonthAchieveByPkMember" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT mr.month_consume_pv + mr.team_month_pv upMonthPv
        FROM ${retailUpMonthTable} mr
        WHERE mr.category = 0
          AND mr.pay_status = 1
          AND mr.pk_member = #{pkMember}
    </select>


    <select id="selectExcludeWaterSmallAchieve" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT nvl(sum(arealPv),0) arealPv, nvl(sum(smallAreaPv),0) smallAreaPv
        FROM (
        SELECT sum(arealPv) arealPv, sum(arealPv) - max(arealPv) smallAreaPv
        from
        (
        <include refid="selectSmallAreaAchieve"/>
        )
        union all
        SELECT t5.total_team_pv * -1 arealPv,
        t5.small_area_pv * -1 smallAreaPv
        FROM (SELECT sr.total_team_pv, sr.small_area_pv, sr.period_date
        FROM cu_member_spe_retail sr
        where sr.del_flag = 0
        and sr.pk_country = 1
        and sr.period_date &lt; TRUNC(SYSDATE)
        and sr.member_code = #{memberCode}
        ORDER BY sr.period_date desc) t5
        where rownum = 1) t6

    </select>


    <!--市场动态新需求-->
    <sql id="selectSelfOrderAchieveByPkMember">
        SELECT #{pkMember}      pkMember,
               sum(yesterdayPv) yesterdayPv,
               sum(sumPv)       sumPv,
               sum(monthPv)     monthPv,
               0                realTimePv,
               0                upMonthPv
        FROM (SELECT NVL(mr.new_consume_pv, 0)   yesterdayPv,
                     NVL(mr.CONSUME_PV, 0)       sumPv,
                     NVL(mr.month_consume_pv, 0) monthPv
              FROM ${memberRetailTable} mr
              WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
              union all
              SELECT 0                             yesterdayPv,
                     NVL(SUM(so.order_achieve), 0) sumPv,
                     NVL(SUM(so.order_achieve), 0) monthPv
              FROM sa_order so
              WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                AND so.system_type = #{systemType}
                AND so.pay_time >= TRUNC(SYSDATE)
                AND so.pk_member = #{pkMember})
    </sql>


    <select id="selectMemberBigSmallAchieveDetail" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT nvl(sum(yesterdayPv),0) totalYesterdayPv, nvl(sum(yesterdayPv),0) - nvl(max(yesterdayPv),0)
        smallYesterdayPv,
        nvl(sum(sumPv),0) totalSumPv, nvl(sum(sumPv),0) - nvl(max(sumPv),0) smallSumPv,
        nvl(sum(monthPv),0) totalMonthPv, nvl(sum(monthPv),0) - nvl(max(monthPv),0) smallMonthPv
        from
        (
        <include refid="selectPushAchieveList"/>
        <if test="pkMemberList != null and pkMemberList.size > 0">
            union all
        </if>
        <include refid="selectSelfOrderAchieveByPkMember"/>
        )
    </select>

    <select id="selectMemberActiveState" resultType="integer">
        SELECT min(t0.enable_status) enable_status
        FROM (SELECT mr.enable_status
              FROM ${memberRetailTable} mr
              where mr.pk_member = #{pkMember}
              union all
              SELECT mrs.enable_status
              FROM ${memberRetailSTable} mrs
              where mrs.pk_member = #{pkMember}) t0
    </select>

    <!--开始大小区业绩 以累计为准 -->
    <select id="selectMemberOrderSumAchieveByPkMember" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        SELECT sum(arealPv) arealPv
        FROM (SELECT (NVL(mr.CONSUME_PV, 0)  + NVL( mr.TEAM_CONSUME_PV, 0 )) arealPv
              FROM ${memberRetailTable} mr
              WHERE mr.category = 0
                AND mr.pk_member = #{pkMember}
              union all
              SELECT NVL(SUM(so.order_achieve), 0) arealPv
              FROM sa_order so
              WHERE so.del_flag = 0
                AND so.order_status = 1
                AND so.order_type in (41, 42, 43, 44)
                AND so.system_type = #{systemType}
                AND so.pay_time >= TRUNC(SYSDATE)
                AND so.pk_member = #{pkMember})
    </select>


    <sql id="selectSmallAreaSumAchieveBak">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT SUM(t1.arealPv) arealPv ,#{pkMember} pkMember
            FROM (
            SELECT NVL(mr.CONSUME_PV + mr.TEAM_CONSUME_PV,0) arealPv
            FROM ${memberRetailTable} mr
            WHERE mr.category = 0
            AND mr.pk_member = #{pkMember}
            UNION ALL
            SELECT NVL(SUM(so.order_achieve), 0) arealPv
            FROM sa_order so
            WHERE so.del_flag = 0
            AND so.order_status = 1
            AND so.order_type in (41, 42, 43, 44)
            AND so.system_type = #{systemType}
            AND so.pay_time >= TRUNC(SYSDATE)
            AND so.pk_member IN (
            SELECT t0.pk_id
            FROM (SELECT m.pk_id,
            m.del_flag,
            m.category,
            m.pay_status,
            m.system_type
            FROM cu_member m
            START WITH m.pk_id = #{pkMember}
            CONNECT BY PRIOR m.pk_id = m.pk_parent) t0
            WHERE t0.del_flag = 0
            AND t0.system_type = #{systemType}
            AND t0.category = 0)) t1
        </foreach>
    </sql>
    <sql id="selectSmallAreaSumAchieve">
        <foreach collection="pkMemberList" item="pkMember" separator="union all">
            SELECT SUM(t1.arealPv) arealPv ,#{pkMember} pkMember
            FROM (
            SELECT NVL(mr.CONSUME_PV + mr.ALL_TEAM_CONSUME_PV,0) arealPv
            FROM ${memberRetailTable} mr
            WHERE mr.category = 0
            AND mr.pk_member = #{pkMember}
            UNION ALL
            SELECT
            NVL( mrs.NEW_CONSUME_PV + mrs.ALL_TEAM_NEW_PV, 0 ) arealPv
            FROM
            ${memberRetailSTable} mrs
            WHERE mrs.pk_member = #{pkMember}
            ) t1
        </foreach>
    </sql>

    <select id="selectMemberSumAchieve" resultType="com.hzs.retail.member.vo.CuMemberRetailAchieveVO">
        <include refid="selectSmallAreaSumAchieve"/>
    </select>


    <select id="selectSubMemberIdByRootMember" resultType="java.lang.Long">
        select cm.pk_id, cm.MEMBER_NAME
        from cu_member cm
        where cm.SYSTEM_TYPE = #{systemType}
            start with cm.pk_id = #{pkMember}
        connect by prior cm.pk_id = cm.pk_parent
        order by level
    </select>

    <select id="getRetailDataStatisticsDetail"
            resultType="com.hzs.retail.member.vo.CuMemberRetailDataStatisticsDetailVO">
        SELECT
        cu.pk_id,
        cu.MEMBER_CODE,
        mr.ENABLE_STATUS,

        NVL( mr.CONSUME_PV, 0 ) consumePv,
        0 newConsumePv,

        NVL( mr.TEAM_CONSUME_PV, 0 ) teamConsumePv,
        0 teamNewPv,

        0 sNewConsumePv,
        0 sTeamNewPv
        FROM ${memberRetailTable} mr
        left join cu_member cu on cu.PK_ID = mr.PK_MEMBER and mr.CATEGORY = 0
        <where>
            <if test="pkMemberList != null and pkMemberList.size > 0">
                cu.pk_id in
                <foreach collection="pkMemberList" item="pkMember" separator="," open="(" close=")">
                    #{pkMember}
                </foreach>
            </if>
        </where>

<!-- 市场动态不需要显示自己，不需要显示秒结
        SELECT
            cu.pk_id,
            cu.MEMBER_CODE,
            mr.ENABLE_STATUS,

            NVL( mr.CONSUME_PV, 0 ) consumePv,
            NVL( mr.REG_CONSUME_NEW_PV + mr.REP_CONSUME_NEW_PV, 0 ) newConsumePv,

            NVL( mr.TEAM_CONSUME_PV, 0 ) teamConsumePv,
            NVL( mr.REG_TEAM_NEW_PV + mr.REP_TEAM_NEW_PV, 0 ) teamNewPv,

            NVL( mrs.ALL_CONSUME_NEW_PV, 0 ) sNewConsumePv,
            NVL( mrs.ALL_TEAM_NEW_PV, 0 ) sTeamNewPv
        FROM
            cu_member cu
                left join ${memberRetailTable} mr on cu.PK_ID = mr.PK_MEMBER and mr.CATEGORY = 0
                left JOIN ${memberRetailSTable} mrs on cu.PK_ID = mrs.PK_MEMBER
        <where>
            <if test="pkMemberList != null and pkMemberList.size > 0">
                cu.pk_id in
                <foreach collection="pkMemberList" item="pkMember" separator="," open="(" close=")">
                    #{pkMember}
                </foreach>
            </if>
        </where>
        -->
    </select>

    <select id="getMarketDynamicsDetail" resultType="com.hzs.retail.member.vo.MarketDynamicsDetailVO">
        SELECT
            cu.pk_id,
            cu.MEMBER_CODE,
            cu.MEMBER_NAME,
            cu.PK_SETTLE_GRADE,
            grade.GRADE_NAME,
            cu.PK_AWARDS,
            awards.AWARDS_NAME,
            currentMonth.ENABLE_STATUS,
            NVL( NVL( currentDay.ALL_NEW_CONSUME_PV, 0 ) + NVL( currentDay.ALL_TEAM_NEW_PV, 0 ), 0 ) todayPv,
            NVL( NVL( currentMonth.NEW_CONSUME_PV, 0 ) + NVL( currentMonth.TEAM_NEW_PV, 0 ), 0 ) yesterdayPv,
            NVL( NVL( currentMonth.MONTH_CONSUME_PV, 0 ) + NVL( currentMonth.TEAM_MONTH_PV, 0 ) + NVL( currentDay.ALL_NEW_CONSUME_PV, 0 ) + NVL( currentDay.ALL_TEAM_NEW_PV, 0 ), 0 ) currentMonthPv,
            NVL( lastMonth.MONTH_CONSUME_PV, 0 ) + NVL( lastMonth.TEAM_MONTH_PV, 0 ) lastMonthPv
        FROM
            cu_member cu
                LEFT JOIN ${param.currentDayTableName} currentDay ON cu.PK_ID = currentDay.PK_MEMBER
                LEFT JOIN ${param.currentMonthTableName} currentMonth ON cu.PK_ID = currentMonth.PK_MEMBER AND cu.CATEGORY = 0
                LEFT JOIN ${param.lastMonthTableName} lastMonth ON cu.PK_ID = lastMonth.PK_MEMBER AND cu.CATEGORY = 0
                LEFT JOIN BD_GRADE grade on grade.PK_ID = cu.PK_SETTLE_GRADE
                LEFT JOIN BD_AWARDS awards on awards.PK_ID = cu.PK_AWARDS
        <where>
            <if test="param.pkIdList != null and param.pkIdList.size > 0">
                cu.pk_id in
                <foreach collection="param.pkIdList" item="pkMember" separator="," open="(" close=")">
                    #{pkMember}
                </foreach>
            </if>
        </where>
        order by totalBoxNum desc, todayPv desc, cu.CREATION_TIME desc, cu.pk_id asc
    </select>
</mapper>
