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


    <!--    查询伞下账户余额-->
    <select id="selectUmbrellaLowerBalance"
            resultType="com.hzs.report.member.vo.CuMemberUmbrellaBalanceVO">
        select cm.pk_id pkId,
        cm.member_code memberCode,
        cm.member_name memberName,
        cm.phone,
        aw.PK_TRANSACTION as pkAwards,
        ver.VERTEX_NAME as vertexName,
        ct.TEAM_NAME as teamName,
        cmac.account4,
        cmac.LOCK_ACCOUNT4 lockAccount4,
        cmac.account1,
        cmac.account3,
        cmac.account13,
        cmac.account14
        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
        and ver.del_flag = 0
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        left join CU_MEMBER_ACCOUNT cmac on cmac.PK_MEMBER=cm.pk_id
        where cm.del_flag = 0 and cmac.del_flag = 0
        <if test="pkCountry != null">
            AND cm.PK_SETTLE_COUNTRY=#{pkCountry}
        </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="pkVertex != null">
            AND cm.pk_vertex=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            AND cm.pk_team_code=#{pkTeamCode}
        </if>
        start
        with cm.pk_id = #{pkId}
        connect by cm.PK_PARENT = prior cm.pk_id
        order by level

    </select>

    <select id="queryPlaceParentHisCuMemberAchieveExt" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select a.algebra, a.pk_member, a.pk_place_parent, a.place_dept,
        a.member_code, a.member_name, a.phone, bd.pk_transaction pk_grade,
        ba.pk_transaction pk_awards, bv.vertex_name,
        cmp.member_code parentCode, cmp.member_name parentName, cmpl.member_code placeCode, cmpl.member_name placeName,
        a.category, cm.pay_time, bdr.pk_transaction pk_register_grade
        from (
        <if test="param.placeDept == null">
            select pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
            member_code, member_name, phone, pk_grade, pk_awards, pk_vertex, pk_team_code,
            category, level algebra
            from ${tableName}
            start with member_code = #{param.memberCode}
        </if>
        <if test="param.placeDept != null">
            select pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
            member_code, member_name, phone, pk_grade, pk_awards, pk_vertex, pk_team_code,
            category, 1 algebra
            from ${tableName}
            where member_code = #{param.memberCode}
            union
            select pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
            member_code, member_name, phone, pk_grade, pk_awards, pk_vertex, pk_team_code,
            category, level + 1 algebra
            from ${tableName}
            start with member_code in (
            select member_code from cu_member cm
            where cm.pk_place_parent = (select pk_id from cu_member where member_code = #{param.memberCode})
            and cm.place_dept = #{param.placeDept}
            )
        </if>
        <choose>
            <when test="null != param.upDown and param.upDown == 1">
                connect by pk_member = prior pk_place_parent
            </when>
            <otherwise>
                connect by prior pk_member = pk_place_parent
            </otherwise>
        </choose>
        ) a
        left join cu_member cm
        on cm.pk_id = a.pk_member
        left join bd_grade bdr
        on cm.pk_register_grade = bdr.pk_id
        left join bd_grade bd
        on a.pk_grade = bd.pk_id
        left join bd_awards ba
        on a.pk_awards = ba.pk_id
        left join bd_vertex bv
        on a.pk_vertex = bv.pk_id
        left join cu_member cmp
        on a.pk_parent = cmp.pk_id
        left join cu_member cmpl
        on a.pk_place_parent = cmpl.pk_id
        where 1 = 1
        and cm.del_flag = 0
        <if test="param.pkRegisterGrade != null">
            and cm.pk_register_grade = #{param.pkRegisterGrade}
        </if>
        <if test="param.pkGrade != null">
            and a.pk_grade = #{param.pkGrade}
        </if>
        <if test="param.pkAwards != null">
            and a.pk_awards = #{param.pkAwards}
        </if>
        <if test="param.pkVertex != null">
            and a.pk_vertex = #{param.pkVertex}
        </if>
        order by a.algebra
    </select>

    <select id="queryPlaceParentCuMemberAchieveExt" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select a.algebra, a.pk_member, a.pk_place_parent, a.place_dept,
        a.member_code, a.member_name, a.phone, bdr.pk_transaction pk_register_grade, bd.pk_transaction pk_grade,
        ba.pk_transaction pk_awards, bv.vertex_name, a.pay_time, a.category,
        cmp.member_code parentCode, cmp.member_name parentName, cmpl.member_code placeCode, cmpl.member_name placeName
        from (
        select b.pk_member, b.pk_parent, b.pk_place_parent, b.place_dept,
        (case when b.purchase_status = 0 then 0 else a.purchase_status end) purchase_status,
        b.member_code, b.member_name, b.phone, b.pk_register_grade, b.pk_grade, b.pk_awards, b.pk_vertex, b.pk_team_code,
        b.pay_time, b.category, b.algebra
        from ${tableName} a
        inner join (
        <if test="param.placeDept == null">
            select pk_id pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
            member_code, member_name, phone, pk_register_grade, pk_settle_grade pk_grade, pk_awards,
            pk_vertex, pk_team_code, pay_time, category, level algebra
            from cu_member
            where del_flag = 0
            start with member_code = #{param.memberCode}
        </if>
        <if test="param.placeDept != null">
            select pk_id pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
            member_code, member_name, phone, pk_register_grade, pk_settle_grade pk_grade, pk_awards,
            pk_vertex, pk_team_code, pay_time, category, 1 algebra
            from cu_member
            where member_code = #{param.memberCode}
            union
            select pk_id pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
            member_code, member_name, phone, pk_register_grade, pk_settle_grade pk_grade, pk_awards,
            pk_vertex, pk_team_code, pay_time, category, level + 1 algebra
            from cu_member
            where del_flag = 0
            start with member_code in (
            select member_code from cu_member cm
            where cm.pk_place_parent = (select pk_id from cu_member where member_code = #{param.memberCode})
            and cm.place_dept = #{param.placeDept}
            )
        </if>
        <choose>
            <when test="null != param.upDown and param.upDown == 1">
                connect by pk_id = prior pk_place_parent
            </when>
            <otherwise>
                connect by prior pk_id = pk_place_parent
            </otherwise>
        </choose>
        ) b
        on a.pk_member = b.pk_member) a
        left join bd_grade bdr
        on a.pk_register_grade = bdr.pk_id
        left join bd_grade bd
        on a.pk_grade = bd.pk_id
        left join bd_awards ba
        on a.pk_awards = ba.pk_id
        left join bd_vertex bv
        on a.pk_vertex = bv.pk_id
        left join cu_member cmp
        on a.pk_parent = cmp.pk_id
        left join cu_member cmpl
        on a.pk_place_parent = cmpl.pk_id
        where 1 = 1
        <if test="param.pkRegisterGrade != null">
            and a.pk_register_grade = #{param.pkRegisterGrade}
        </if>
        <if test="param.pkGrade != null">
            and a.pk_grade = #{param.pkGrade}
        </if>
        <if test="param.pkAwards != null">
            and a.pk_awards = #{param.pkAwards}
        </if>
        <if test="param.pkVertex != null">
            and a.pk_vertex = #{param.pkVertex}
        </if>
        order by a.algebra
    </select>

    <select id="queryParentParentHisCuMemberAchieveExt" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select a.algebra, a.pk_member, a.pk_parent, a.pk_settle_country,
        a.member_code, a.member_name, a.phone, bd.pk_transaction pk_grade,
        ba.pk_transaction pk_awards, bv.vertex_name,
        cmp.member_code parentCode, cmp.member_name parentName, cmpl.member_code placeCode, cmpl.member_name placeName, a.place_dept placeDept,
        a.category, cm.pay_time, bdr.pk_transaction pk_register_grade
        from
        (
        select pk_member, pk_parent, purchase_status, pk_place_parent, place_dept,
        member_code, member_name, phone, pk_grade, pk_awards,
        pk_vertex, pk_team_code, pk_settle_country, category, level algebra
        from ${tableName}
        start with member_code = #{param.memberCode}
        <choose>
            <when test="null != param.upDown and param.upDown == 1">
                connect by pk_member = prior pk_parent
            </when>
            <otherwise>connect by prior pk_member = pk_parent</otherwise>
        </choose>
        ) a
        left join cu_member cm
        on cm.pk_id = a.pk_member
        left join bd_grade bdr
        on cm.pk_register_grade = bdr.pk_id
        left join bd_grade bd
        on a.pk_grade = bd.pk_id
        left join bd_awards ba
        on a.pk_awards = ba.pk_id
        left join bd_vertex bv
        on a.pk_vertex = bv.pk_id
        left join cu_member cmp
        on a.pk_parent = cmp.pk_id
        left join cu_member cmpl
        on a.pk_place_parent = cmpl.pk_id
        where 1 = 1
        and cm.del_flag = 0
        <if test="param.pkGrade != null">
            and a.pk_grade = #{param.pkGrade}
        </if>
        <if test="param.pkAwards != null">
            and a.pk_awards = #{param.pkAwards}
        </if>
        <if test="param.pkVertex != null">
            and a.pk_vertex = #{param.pkVertex}
        </if>
        order by a.algebra
    </select>

    <select id="queryParentParentCuMemberAchieveExt" resultType="com.hzs.common.domain.member.achieve.ext.CuMemberAchieveExt">
        select a.algebra, a.pk_member, a.pk_parent, a.pk_settle_country,
        a.member_code, a.member_name, a.phone, bdr.pk_transaction pk_register_grade, bd.pk_transaction pk_grade,
        ba.pk_transaction pk_awards, bv.vertex_name,
        cmp.member_code parentCode, cmp.member_name parentName, cmpl.member_code placeCode, cmpl.member_name placeName, a.place_dept placeDept,
        a.category, a.pay_time
        from
        (
        select pk_id pk_member, pk_parent, pk_place_parent, place_dept, purchase_status,
        member_code, member_name, phone, pk_register_grade, pk_settle_grade pk_grade, pk_awards,
        pk_vertex, pk_team_code, pk_settle_country, category, pay_time, level algebra
        from cu_member
        where del_flag = 0
        start with member_code = #{param.memberCode}
        <choose>
            <when test="null != param.upDown and param.upDown == 1">
                connect by pk_id = prior pk_parent
            </when>
            <otherwise>connect by prior pk_id = pk_parent</otherwise>
        </choose>
        ) a
        left join bd_grade bdr
        on a.pk_register_grade = bdr.pk_id
        left join bd_grade bd
        on a.pk_grade = bd.pk_id
        left join bd_awards ba
        on a.pk_awards = ba.pk_id
        left join bd_vertex bv
        on a.pk_vertex = bv.pk_id
        left join cu_member cmp
        on a.pk_parent = cmp.pk_id
        left join cu_member cmpl
        on a.pk_place_parent = cmpl.pk_id
        where 1 = 1
        <if test="param.pkRegisterGrade != null">
            and a.pk_register_grade = #{param.pkRegisterGrade}
        </if>
        <if test="param.pkGrade != null">
            and a.pk_grade = #{param.pkGrade}
        </if>
        <if test="param.pkAwards != null">
            and a.pk_awards = #{param.pkAwards}
        </if>
        <if test="param.pkVertex != null">
            and a.pk_vertex = #{param.pkVertex}
        </if>
        order by a.algebra
    </select>


    <!--统计分析-伞下直推-->
    <select id="queryUmbrellaBelowPush" resultType="com.hzs.report.member.vo.UmbrellaBelowPushVO">
        select
        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
        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
        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
        where cm.del_flag = 0 and ver.del_flag = 0 and par.del_flag = 0 and place.del_flag = 0
        and awa.del_flag = 0 and gr.del_flag = 0
        <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="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>
        start
        with cm.pk_id = #{pkMember}
        connect by cm.pk_parent = prior cm.pk_id
    </select>

    <!-- 查询会员订单消费业绩 -->
    <select id="queryOrderExpend" resultType="com.hzs.report.member.vo.UmbrellaBelowPushVO">
        select
        so.pk_member pkMember,
        so.ORDER_ACHIEVE orderAchieve
        from sa_order so
        inner join cu_member cm
        on cm.pk_id = so.pk_member
        where so.del_flag = 0 and cm.del_flag = 0
        <if test="pkMemberList != null and pkMemberList.size > 0">
            and so.pk_member in
            <foreach collection="pkMemberList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <!--    统计分析-伞下直推会员信息-->
    <select id="queryUmbrellaBelowInfo" parameterType="com.hzs.report.member.vo.UmbrellaBelowPushVO"
            resultType="com.hzs.report.member.vo.UmbrellaBelowPushVO">
        SELECT
        cm.PK_ID pkId,
        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
        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
        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
        WHERE
        cm.del_flag = 0
        <if test="pkId != null">
            and cm.pk_Id=#{pkId}
        </if>
    </select>

    <select id="selectByDeptNumberPlace" resultType="com.hzs.report.report.vo.DeptMemberNumberVO">
        select 1 placeDept, count(t.pk_id) quantity
        from CU_MEMBER t
        where t.del_flag = 0
          and t.category = 0
            start
        with t.PK_PLACE_PARENT = #{pkMember} and t.PLACE_DEPT = 1
        connect by t.PK_PLACE_PARENT = prior t.PK_ID
        union all
        select 2 placeDept, count(t.pk_id) quantity
        from CU_MEMBER t
        where t.del_flag = 0
          and t.category = 0
            start
        with t.PK_PLACE_PARENT = #{pkMember} and t.PLACE_DEPT = 2
        connect by t.PK_PLACE_PARENT = prior t.PK_ID
    </select>

</mapper>
