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

    <!-- 查询伞下直推新增业绩 -会员 -->
    <select id="directPushNewList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        bc.NATIONAL_FLAG1 nationalFlag1,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode ,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        cm.PAY_TIME payTime ,
        ba.AWARDS_NAME awardsName,
        bv.VERTEX_NAME vertexName,
        cmt.team_name teamName
        from cu_member cm
        left join BD_COUNTRY bc on bc.PK_ID = cm.PK_SETTLE_COUNTRY
        left join bd_awards ba on ba.pk_id = cm.PK_AWARDS
        left join bd_vertex bv on bv.PK_ID = cm.PK_VERTEX
        left join cu_member_team cmt on cm.pk_team_code = cmt.pk_id
        where cm.del_flag = 0 and cm.pk_id = #{pkMember}
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME = #{memberName}
        </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>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY = #{pkCountry}
        </if>
    </select>

    <!-- 查询伞下直推新增业绩 -业绩-->
    <select id="directPushNewAchieList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select
        sum(orderAchieve) orderAchieve,
        sum(orderAmount) orderAmount,
        sum(orderAssAchieve) orderAssAchieve
        from (
        select
        CASE
        WHEN sum( so.ORDER_ACHIEVE ) is null THEN 0
        ELSE sum( so.ORDER_ACHIEVE )
        end orderAchieve,
        CASE
        WHEN sum( so.ORDER_AMOUNT - nvl(so.consume_amount, 0) ) is null THEN 0
        ELSE sum( so.ORDER_AMOUNT - nvl(so.consume_amount, 0) )
        end orderAmount,
        CASE
        WHEN sum(so.order_ass_achieve) is null THEN 0
        ELSE sum(so.order_ass_achieve)
        end orderAssAchieve
        from SA_ORDER so
        where so.DEL_FLAG = 0
        and so.ORDER_STATUS = 1
        and so.PK_COUNTRY = #{pkCountry}
        and so.pk_member in (
        SELECT cm.pk_id
        FROM cu_member cm
        WHERE cm.del_flag = 0
        START WITH cm.pk_parent = #{pkMember} CONNECT BY cm.pk_parent = PRIOR cm.pk_id )
        <if test="orderTypeList != null">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startPayTime != null and endPayTime != null">
            and so.PAY_TIME between #{startPayTime, jdbcType=TIMESTAMP} and #{endPayTime, jdbcType=TIMESTAMP}
        </if>
        )
    </select>

    <!-- 查询伞下直推新增业绩 -业绩-->
    <select id="directPushNewAchieUpList" resultType="com.hzs.report.member.vo.DirectPushNewVo">
        select (endm.orderAchieve - stam.orderAchieve) orderAchieve,
               (endm.orderAmount - stam.orderAmount)   orderAmount,
               endm.pkMember
        FROM (
                 SELECT nvl(sum(cma.REGISTER_PV + cma.UPGRADE_PV), 0)         AS orderAchieve,
                        nvl(sum(cma.REGISTER_AMOUNT + cma.UPGRADE_AMOUNT), 0) AS orderAmount,
                        #{pkMember}                                           AS pkMember
                 FROM ${dpo.achieveTableNameEnd} cma
                 WHERE 1 = 1
                   AND cma.PK_COUNTRY = #{dpo.pkCountry}
                   AND cma.PERIOD = #{dpo.periodEnd}
                   AND cma.PK_MEMBER IN (
                     select cm.PK_MEMBER
                     from (
                              SELECT *
                              FROM ${dpo.settleTableNameEnd} cm
                              WHERE cm.pk_settle_country = #{dpo.pkCountry}
                                and cm.PERIOD = #{dpo.periodEnd}
                          ) cm
                 START WITH cm.pk_parent = #{pkMember}
                 CONNECT BY cm.pk_parent = PRIOR cm.PK_MEMBER
             ) ) endm
             join (
        SELECT
        nvl(sum(cma.REGISTER_PV +cma.UPGRADE_PV), 0) AS orderAchieve,
        nvl(sum(cma.REGISTER_AMOUNT+cma.UPGRADE_AMOUNT), 0) AS orderAmount, #{pkMember} AS pkMember
            FROM ${dpo.achieveTableNameSta} cma
            WHERE 1 = 1
            AND cma.PK_COUNTRY = #{dpo.pkCountry}
            AND cma.PERIOD = #{dpo.periodSta}
            AND cma.PK_MEMBER IN (
            select cm.PK_MEMBER from (
            SELECT *
            FROM ${dpo.settleTableNameSta} cm
            WHERE cm.pk_settle_country = #{dpo.pkCountry}
            and cm.PERIOD =#{dpo.periodSta}
            ) cm
            START WITH cm.pk_parent = #{pkMember}
            CONNECT BY cm.pk_parent = PRIOR cm.PK_MEMBER
            )
            ) stam
        on endm.pkMember = stam.pkMember
    </select>

    <select id="memberConsumptionList" resultType="com.hzs.report.member.vo.MemberConsumptionVo">
        select cm.MEMBER_CODE memberCode ,cm.MEMBER_NAME memberName,nvl(a.orderAmount,0)orderAmount,nvl(a.orderAchieve,0)orderAchieve,cm.pk_id
        pkMember
        from cu_member cm
        left join (
        select nvl(sum(si.PRICE*si.QUANTITY),0) orderAmount, nvl(sum(si.ACHIEVEMENT*si.QUANTITY),0) orderAchieve,so.PK_CREATOR from SA_ORDER_ITEMS si
        left join SA_ORDER so on so.PK_ID = si.PK_ORDER
        where si.del_flag = 0 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkWares != null">
            and si.PK_WARES=#{pkWares}
        </if>
        group by so.PK_CREATOR
        )a on a.PK_CREATOR = cm.pk_id
        where cm.del_flag = 0
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME=#{memberName}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE=#{memberCode}
        </if>
    </select>

    <select id="memberResettleAchieve" resultType="com.hzs.report.member.vo.MemberResettleAchieveVo">
        select bc.NATIONAL_FLAG1 nationalFlag,
        e.achieve,
        e.amount,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        cm.PK_AWARDS,
        cm.PK_TEAM_CODE,
        cm.CONSUME_MONEY consumeMoney,
        cm.CONSUME_ACHIEVE consumeAchieve,
        cm.PAY_TIME payTime,
        ba.AWARDS_NAME awardsName,
        mt.TEAM_NAME teamCodeName,
        bv.VERTEX_NAME vertexName,
        cm.PAY_TIME orderTime,
        cm.pk_id pkMember,
        e.place_dept placeDept
        from cu_member cm
        left join BD_COUNTRY bc
        on bc.PK_ID = cm.PK_SETTLE_COUNTRY
        left join bd_awards ba
        on ba.pk_id = cm.PK_AWARDS
        left join cu_member_team mt
        on mt.pk_id = cm.PK_TEAM_CODE
        left join bd_vertex bv
        on bv.pk_id = cm.PK_VERTEX
        inner join (
        select #{pkMember} pkMember,
        1 place_dept,
        nvl(sum(so.ORDER_ACHIEVE), 0) achieve,
        nvl(sum(so.ORDER_AMOUNT - nvl(so.consume_amount, 0)), 0) amount
        from SA_ORDER so
        inner join (
        select a.pk_id from (
        SELECT pk_id,del_flag,CUSTOMER_TYPE
        FROM cu_member where 1 = 1
        <if test="pkAwards != null">
            and PK_AWARDS=#{pkAwards}
        </if>
        <if test="pkVertex != null">
            and PK_VERTEX=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and PK_TEAM_CODE=#{pkTeamCode}
        </if>
        START WITH pk_place_parent = #{pkMember} and place_dept = 1
        CONNECT BY pk_place_parent = PRIOR pk_id) a
        where a.del_flag = 0
        ) a
        on so.pk_member = a.pk_id
        where so.del_flag = 0
        and so.ORDER_STATUS = 1
        <if test="pkCountry != null">
            and pk_country = #{pkCountry}
        </if>
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        union all
        select #{pkMember} pkMember,
        2 place_dept,
        nvl(sum(so.ORDER_ACHIEVE), 0) achieve,
        nvl(sum(so.ORDER_AMOUNT - nvl(so.consume_amount, 0)), 0) amount
        from SA_ORDER so
        inner join (
        select a.pk_id from (
        SELECT pk_id,del_flag,CUSTOMER_TYPE
        FROM cu_member where 1 = 1
        <if test="pkAwards != null">
            and PK_AWARDS=#{pkAwards}
        </if>
        <if test="pkVertex != null">
            and PK_VERTEX=#{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and PK_TEAM_CODE=#{pkTeamCode}
        </if>
        START WITH pk_place_parent = #{pkMember} and place_dept = 2
        CONNECT BY pk_place_parent = PRIOR pk_id) a
        where a.del_flag = 0
        ) a
        on so.pk_member = a.pk_id
        where so.del_flag = 0
        and so.ORDER_STATUS = 1
        <if test="pkCountry != null">
            and pk_country=#{pkCountry}
        </if>
        <if test="startPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endPayTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="orderTypeList != null  and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ) e on e.pkMember = cm.pk_id
        where cm.del_flag = 0
        and cm.pk_id = #{pkMember}
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME = #{memberName}
        </if>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY = #{pkCountry}
        </if>
    </select>

    <!-- 查询伞下新增安置业绩-->
    <select id="selectBrollyNewABAchieve" resultType="com.hzs.report.member.vo.MemberResettleAchieveVo">
        select cm.PAY_TIME orderTime,
        cm.pk_id pkMember,
        bc.NATIONAL_FLAG1 nationalFlag,
        bc.SHORT_NAME shortName,
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE phone,
        ba.AWARDS_NAME awardsName,
        mt.TEAM_NAME teamCodeName,
        bv.VERTEX_NAME vertexName,
        nvl(cmpv.aSumAmount, 0) aSumAmount,
        nvl(cmpv.bSumAmount, 0) bSumAmount,
        nvl(cmpv.aSumPv, 0) aSumPv,
        nvl(cmpv.bSumPv, 0) bSumPv
        from cu_member cm
        left join BD_COUNTRY bc on bc.PK_ID = cm.PK_SETTLE_COUNTRY
        left join bd_awards ba on ba.pk_id = cm.PK_AWARDS
        left join cu_member_team mt on mt.pk_id = cm.PK_TEAM_CODE
        left join bd_vertex bv on bv.pk_id = cm.PK_VERTEX
        inner join (
        select
        #{pkMember} pkMember,
        sum(a_sum_amount) aSumAmount,
        sum(b_sum_amount) bSumAmount,
        sum(a_sum_pv) aSumPv,
        sum(b_sum_pv) bSumPv
        from ${achieveTableName} where PERIOD = #{period} and PK_COUNTRY = #{pkCountry} and PK_MEMBER = #{pkMember}
        ) cmpv on cm.pk_id = cmpv.pkMember
        where cm.del_flag = 0
        <if test="pkAwards != null">
            and PK_AWARDS = #{pkAwards}
        </if>
        <if test="pkVertex != null">
            and PK_VERTEX = #{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and PK_TEAM_CODE = #{pkTeamCode}
        </if>
    </select>

    <select id="getOrderCurrencyList" resultType="com.hzs.report.member.vo.OrderCurrencyVo">
        select cm.MEMBER_CODE memberCode,cm.MEMBER_NAME memberName,so.ORDER_CODE orderCode,so.ORDER_TYPE orderType,so.ORDER_AMOUNT
        orderAmount,so.POSTAGE postage
        from sa_order so
        left join CU_MEMBER cm on cm.pk_id = so.PK_CREATOR
        where 1 = 1 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="orderCode != null and orderCode != ''">
            and so.ORDER_CODE like #{orderCode} || '%'
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE like #{orderType} || '%'
        </if>
    </select>

    <select id="getBySourceCodeMemberTradeList" resultType="com.hzs.report.member.vo.MemberTradeVo">
        select mt.* from cu_member_trade mt where 1 = 1
        <if test="orderCodeList != null and orderCodeList.size > 0">
            and mt.SOURCE_CODE in
            <foreach collection="orderCodeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

    <select id="getByProvinceOrderDistribution" resultType="com.hzs.report.member.vo.OrderDistribution">
        select nvl(sum(sa.ORDER_ACHIEVE),0) orderAchieve,ba.NAME province from sa_order sa
        left join bd_area ba on ba.pk_id = sa.REC_PROVINCE
        where sa.del_flag = 0 and sa.ORDER_STATUS = 1 and sa.PK_COUNTRY = #{pkCountry}
        <if test="startTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by sa.REC_PROVINCE ,ba.NAME
    </select>

    <select id="getByCityOrderDistribution" resultType="com.hzs.report.member.vo.OrderDistribution">
        select nvl(sum(sa.ORDER_ACHIEVE),0) orderAchieve,ba.NAME province,ci.NAME city from sa_order sa
        left join bd_area ba on ba.pk_id = sa.REC_PROVINCE
        left join bd_area ci on ci.pk_id = sa.REC_CITY
        where sa.del_flag = 0 and sa.ORDER_STATUS = 1 and sa.PK_COUNTRY = #{pkCountry}
        <if test="startTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by sa.REC_PROVINCE ,ci.NAME,ba.NAME
    </select>

    <select id="getByDistrictOrderDistribution" resultType="com.hzs.report.member.vo.OrderDistribution">
        select nvl(sum(sa.ORDER_ACHIEVE),0) orderAchieve,ba.NAME province,ci.NAME city,ar.NAME district from sa_order sa
        left join bd_area ba on ba.pk_id = sa.REC_PROVINCE
        left join bd_area ci on ci.pk_id = sa.REC_CITY
        left join bd_area ar on ar.pk_id = sa.REC_COUNTY
        where sa.del_flag = 0 and sa.ORDER_STATUS = 1 and sa.PK_COUNTRY = #{pkCountry}
        <if test="startTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(sa.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endPayTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by sa.REC_PROVINCE ,ar.NAME , ci.NAME,ba.NAME
    </select>

    <select id="getProductOrderInfo" resultType="com.hzs.report.member.vo.ProductOrderInfoVo">
        select oi.PK_PRODUCT pkProduct,
        so.ORDER_TYPE orderType ,
        sum(oi.QUANTITY)quantity,
        min(a.productCode) productCode,
        min(a.productName) productName ,
        min(a.classifyName) classifyName,
        sum(oi.price * oi.QUANTITY) productPrice,
        min(a.productUnit) productUnit,
        min(a.pkClassify) pkClassify
        from SA_ORDER_ITEMS oi
        left join sa_order so on oi.PK_ORDER = so.pk_id
        left join (
        select bp.PRODUCT_CODE productCode,bp.PRODUCT_NAME productName,bc.NAME classifyName,bc.pk_id pkClassify,bp.pk_id pkProduct,bu.NAME productUnit
        from BD_PRODUCT bp
        left join bd_classify bc on bc.PK_ID = bp.PK_CLASSIFY
        left join bd_unit bu on bu.PK_ID = bp.UNIT
        where bp.del_flag = 0
        ) a on a.pkProduct = oi.PK_PRODUCT
        where a.productCode is not null
        and oi.del_flag = 0 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="productName != null and productName !=''">
            and a.productName = #{productName}
        </if>
        <if test="pkClassify != null">
            and a.pkClassify = #{pkClassify}
        </if>
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        group by oi.PK_PRODUCT , so.ORDER_TYPE
    </select>

    <select id="reportSummaryList" resultType="com.hzs.report.member.vo.ReportSummaryVo">
        select cm.MEMBER_CODE memberCode,cm.MEMBER_NAME memberName,bv.VERTEX_NAME vertexName,mt.TEAM_NAME teamName,
        cm.REGISTER_AUTHORITY,cm.REGISTER_AUTHORITY registerAuthority,a.orderAmount consumeMoney ,a.orderAchieve consumeAchieve
        from CU_MEMBER cm
        left join bd_vertex bv on bv.PK_ID = cm.PK_VERTEX
        left join cu_member_team mt on mt.PK_ID = cm.PK_TEAM_CODE
        left join (
        select nvl(sum(so.ORDER_AMOUNT),0) orderAmount ,nvl(sum(ORDER_ACHIEVE),0) orderAchieve,so.pk_creator pkMember from SA_ORDER so
        where so.ORDER_TYPE in (1, 2, 4, 5)
        and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="startCreateTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startCreateTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endCreateTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endCreateTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        group by so.pk_creator
        )a on a.pkMember = cm.PK_ID
        where 1 =1 and cm.REGISTER_AUTHORITY in (1,2) and cm.del_flag = 0 and cm.PAY_STATUS = 1
        and a.orderAmount > 0
        <if test="memberCode != null and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.MEMBER_NAME = #{memberName}
        </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="registerAuthority != null">
            and cm.REGISTER_AUTHORITY = #{registerAuthority}
        </if>
    </select>

    <select id="productShippingDetailsList" resultType="com.hzs.report.member.vo.ProductShippingDetailsInfoVo">
        select bp.PRODUCT_CODE productCode,bp.PRODUCT_NAME productName,bc.NAME classifyName,oi.PK_AWARDS pkAwards,oi.QUANTITY quantity,
        oi.PRICE price,bw.AWARDS_NAME awardsName
        from sa_order_items oi
        left join SA_ORDER so on so.PK_ID = oi.PK_ORDER
        left join BD_PRODUCT bp on bp.pk_id = oi.PK_PRODUCT
        left join bd_classify bc on bc.PK_ID = bp.PK_CLASSIFY
        left join bd_awards bw on bw.pk_id = oi.PK_AWARDS
        where 1 = 1
        and oi.del_flag = 0
        and so.del_flag = 0
        and so.ORDER_STATUS = 1
        <if test="pkCountry != null">
            and so.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="startTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="pkProduct != null">
            and oi.PK_PRODUCT = #{pkProduct}
        </if>
        <if test="pkClassify != null">
            and bc.pk_id = #{pkClassify}
        </if>
        <if test="pkAwards != null">
            and bw.pk_id = #{pkAwards}
        </if>
        <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="productName != null and productName != ''">
            and bp.PRODUCT_NAME like #{productName} || '%'
        </if>
    </select>

    <select id="productDetailQueryList" resultType="com.hzs.report.member.vo.ProductDetailQueryVo">
        select
        bp.PRODUCT_CODE productCode,
        bp.PRODUCT_NAME productName,
        so.ORDER_CODE orderCode,
        so.ORDER_TYPE orderType,
        so.CREATION_TIME createTime,
        cme.MEMBER_NAME memberName,
        cme.MEMBER_CODE memberCode,
        cme.PHONE phone,
        bv.VERTEX_NAME vertexName,
        mt.TEAM_NAME teamName,
        so.REC_NAME recName,
        so.REC_PHONE recPhone,
        cm.MEMBER_CODE purchaseCode,
        cm.MEMBER_NAME purchaseName,
        so.REC_PROVINCE recProvince,
        so.REC_CITY recCity,
        so.REC_COUNTY recCounty,
        so.REC_ADDRESS recAddress,
        so.ORDER_AMOUNT orderAmount,
        so.ORDER_ACHIEVE orderAchieve,
        so.POSTAGE postage,
        so.DELIVERY_WAY deliveryWay,
        so.PAY_TIME payTime,
        oi.PRICE price,
        oi.QUANTITY quantity,
        bw.WARES_CODE waresCode,
        bw.WARES_NAME waresName
        from SA_ORDER_ITEMS oi
        inner join SA_ORDER so on so.PK_ID = oi.PK_ORDER
        left join bd_product bp on bp.PK_ID = oi.PK_PRODUCT
        left join cu_member cm on cm.PK_ID = so.PK_CREATOR
        left join cu_member cme on cme.PK_ID = so.PK_MEMBER
        left join bd_vertex bv on bv.PK_ID = cme.PK_VERTEX
        left join cu_member_team mt on mt.PK_ID = cm.PK_TEAM_CODE
        left join BD_WARES bw on bw.pk_id = oi.PK_WARES
        where oi.del_flag = 0 and so.del_flag = 0 and so.ORDER_STATUS = 1
        <if test="waresCode != null and waresCode != ''">
            and bw.WARES_CODE like #{waresCode} || '%'
        </if>
        <if test="waresName != null and waresName != ''">
            and bw.WARES_NAME like #{waresName} || '%'
        </if>
        <if test="productCode != null and productCode != ''">
            and bp.PRODUCT_CODE like #{productCode} || '%'
        </if>
        <if test="productName != null and productName != ''">
            and bp.PRODUCT_NAME like #{productName} || '%'
        </if>
        <if test="pkMember != null">
            and cm.pk_id in (
            SELECT m.pk_id
            FROM CU_MEMBER m
            start with m.PK_PARENT = #{pkMember}
            connect by m.PK_PARENT = prior m.pk_id
            )
        </if>
        <if test="memberName != null and memberName != ''">
            and cme.MEMBER_NAME like #{memberName} || '%'
        </if>
        <if test="pkVertex != null">
            and cm.PK_VERTEX = #{pkVertex}
        </if>
        <if test="pkCountry != null">
            and cm.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkTeamCode != null">
            and cm.PK_TEAM_CODE = #{pkTeamCode}
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cme.MEMBER_CODE = #{memberCode}
        </if>

        <if test="orderTypeList != null">
            and so.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="startPayTime != null and endPayTime != null">
            and so.PAY_TIME between #{startPayTime, jdbcType=TIMESTAMP} and #{endPayTime, jdbcType=TIMESTAMP}
        </if>
        order by oi.pk_id desc
    </select>

    <select id="selectSmallAreaNewAdd" resultType="com.hzs.report.member.vo.SmallAreaNewAddAchieveVO">
        SELECT m.member_code memberCode,
        m.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        t5.*
        FROM (SELECT *
        FROM (SELECT CASE
        WHEN t2.dept = 2 THEN
        t6.rightSumPv
        WHEN t2.dept = 1 THEN
        t6.leftSumPv
        END achieve,
        t6.rightSumPv,
        t6.leftSumPv,
        t2.dept,
        t2.a_sum_pv * 1 historyLeftSumPv,
        t2.b_sum_pv * 1 historyRightSumPv,
        t6.pk_member
        FROM (SELECT NVL(SUM(t1.a_new_pv) * 1, 0) leftSumPv,
        NVL(SUM(t1.b_new_pv) * 1, 0) rightSumPv,
        t1.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t1
        GROUP BY t1.pk_member) t6
        LEFT JOIN (SELECT ms.pk_member pkMember,
        ms.a_sum_pv,
        ms.b_sum_pv,
        CASE
        WHEN ms.a_sum_pv > ms.b_sum_pv THEN
        2
        WHEN ms.a_sum_pv &lt;= ms.b_sum_pv THEN
        1
        END dept
        FROM ${settleTable} ms
        WHERE ms.period = #{beforePeriod}) t2
        ON t6.pk_member = t2.pkmember) t3
        WHERE t3.achieve >= #{achieve}) t5
        LEFT JOIN cu_member m
        ON m.pk_id = t5.pk_member
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        LEFT JOIN bd_awards a
        ON a.pk_id = m.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = m.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
    </select>

    <select id="selectSmallAreaNewAddNemMember" resultType="com.hzs.report.member.vo.SmallAreaNewAddAchieveVO">
        SELECT t5.member_code memberCode,
        t5.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        NVL(t5.b_new_pv * 1, 0)
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        NVL(t5.a_new_pv * 1, 0)
        END achieve,
        t5.a_new_pv * 1 leftSumPv,
        t5.b_new_pv * 1 rightSumPv,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        2
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        1
        END dept,
        0 historyLeftSumPv,
        0 historyRightSumPv
        FROM (SELECT t1.pk_id,
        t1.member_code,
        t1.member_name,
        t1.pk_awards,
        t1.pk_vertex,
        t3.a_new_pv,
        t3.b_new_pv
        FROM (SELECT m.pk_id,
        m.member_code,
        m.member_name,
        m.pk_awards,
        m.pk_vertex
        FROM cu_member m
        WHERE m.creation_time >= to_date(#{startDate}, 'yyyy-mm-dd')
        AND m.creation_time &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        AND m.pk_settle_country= #{pkCountry}
        ) t1
        LEFT JOIN (SELECT NVL(SUM(t2.a_new_pv), 0) a_new_pv,
        NVL(SUM(t2.b_new_pv), 0) b_new_pv,
        t2.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t2
        GROUP BY t2.pk_member) t3
        ON t1.pk_id = t3.pk_member
        WHERE t3.a_new_pv * 1 >= #{achieve}
        AND t3.b_new_pv * 1 >= #{achieve}) t5
        LEFT JOIN bd_awards a
        ON a.pk_id = t5.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = t5.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
    </select>

    <select id="selectSmallAreaNewAddPage" resultType="com.hzs.report.member.vo.SmallAreaNewAddAchieveVO">
        SELECT m.member_code memberCode,
        m.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        t5.achieve,t5.leftsumpv,t5.rightsumpv,t5.dept,t5.historyleftsumpv,t5.historyrightsumpv
        FROM (SELECT *
        FROM (SELECT CASE
        WHEN t2.dept = 2 THEN
        t6.rightSumPv
        WHEN t2.dept = 1 THEN
        t6.leftSumPv
        END achieve,
        t6.rightSumPv,
        t6.leftSumPv,
        t2.dept,
        t2.a_sum_pv * 1 historyLeftSumPv,
        t2.b_sum_pv * 1 historyRightSumPv,
        t6.pk_member
        FROM (SELECT NVL(SUM(t1.a_new_pv) * 1, 0) leftSumPv,
        NVL(SUM(t1.b_new_pv) * 1, 0) rightSumPv,
        t1.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t1
        GROUP BY t1.pk_member) t6
        LEFT JOIN (SELECT ms.pk_member pkMember,
        ms.a_sum_pv,
        ms.b_sum_pv,
        CASE
        WHEN ms.a_sum_pv > ms.b_sum_pv THEN
        2
        WHEN ms.a_sum_pv &lt;= ms.b_sum_pv THEN
        1
        END dept
        FROM ${settleTable} ms
        WHERE ms.period = #{beforePeriod}) t2
        ON t6.pk_member = t2.pkmember) t3
        WHERE t3.achieve >= #{achieve}) t5
        LEFT JOIN cu_member m
        ON m.pk_id = t5.pk_member
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        LEFT JOIN bd_awards a
        ON a.pk_id = m.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = m.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
        UNION
        SELECT t5.member_code memberCode,
        t5.member_name memberName,
        a.awards_name awardsName,
        bv.vertex_name vertexName,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        NVL(t5.b_new_pv * 1, 0)
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        NVL(t5.a_new_pv * 1, 0)
        END achieve,
        t5.a_new_pv * 1 leftSumPv,
        t5.b_new_pv * 1 rightSumPv,
        CASE
        WHEN t5.a_new_pv > t5.b_new_pv THEN
        2
        WHEN t5.a_new_pv &lt;= t5.b_new_pv THEN
        1
        END dept,
        0 historyLeftSumPv,
        0 historyRightSumPv
        FROM (SELECT t1.pk_id,
        t1.member_code,
        t1.member_name,
        t1.pk_awards,
        t1.pk_vertex,
        t3.a_new_pv,
        t3.b_new_pv
        FROM (SELECT m.pk_id,
        m.member_code,
        m.member_name,
        m.pk_awards,
        m.pk_vertex
        FROM cu_member m
        WHERE m.creation_time >= to_date(#{startDate}, 'yyyy-mm-dd')
        AND m.creation_time &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        AND m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        AND m.pk_settle_country= #{pkCountry}
        ) t1
        LEFT JOIN (SELECT NVL(SUM(t2.a_new_pv), 0) a_new_pv,
        NVL(SUM(t2.b_new_pv), 0) b_new_pv,
        t2.pk_member
        FROM (
        <foreach collection="monthAchieveVOList" item="monthAchieve" open=" " separator=" union all " close=" ">
            SELECT NVL(SUM(a_new_pv), 0) a_new_pv,
            NVL(SUM(b_new_pv), 0) b_new_pv,
            pk_member
            FROM ${monthAchieve.achieveTable}
            WHERE pk_country = #{pkCountry}
            <if test="monthAchieve.startPeriod != null">
                AND period >= #{monthAchieve.startPeriod}
            </if>
            <if test="monthAchieve.endPeriod != null">
                AND period &lt;= #{monthAchieve.endPeriod}
            </if>
            GROUP BY pk_member
        </foreach>
        ) t2
        GROUP BY t2.pk_member) t3
        ON t1.pk_id = t3.pk_member
        WHERE t3.a_new_pv * 1 >= #{achieve}
        AND t3.b_new_pv * 1 >= #{achieve}) t5
        LEFT JOIN bd_awards a
        ON a.pk_id = t5.pk_awards
        AND a.pk_country = #{pkCountry}
        AND a.del_flag = 0
        LEFT JOIN bd_vertex bv
        ON bv.pk_id = t5.pk_vertex
        AND bv.pk_country = #{pkCountry}
        AND bv.del_flag = 0
    </select>

    <sql id="select_base">
        SELECT mt.*,
        t1.member_code AS memberCode,
        t1.member_name AS memberName,
        v.VERTEX_NAME AS vertexName,
        c.TEAM_NAME AS teamName
        FROM cu_member_trade mt
        INNER JOIN (SELECT m.pk_id,
        m.member_code,
        m.member_name,
        m.pk_vertex,
        m.pk_team_code
        FROM cu_member m
        WHERE m.del_flag = 0
        AND m.pay_status = 1
        AND m.category = 0
        AND m.pk_settle_country = #{pkCountry}
        START WITH m.member_code = #{memberCode}
        <if test='treeType == "2"'>
            CONNECT BY PRIOR m.pk_id = m.pk_place_parent
        </if>
        <if test='treeType == "1"'>
            CONNECT BY PRIOR m.pk_id = m.pk_parent
        </if>
        ) t1
        ON mt.pk_member = t1.pk_id
        LEFT JOIN cu_member_team c
        ON c.pk_id = t1.pk_team_code
        AND c.del_flag = 0
        LEFT JOIN bd_vertex v
        ON v.pk_id = t1.PK_VERTEX
        AND v.del_flag = 0
    </sql>

    <sql id="ids_where">
        <where>
            mt.del_flag='0'
            and t.pk_id in
            <foreach collection="pkIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </where>
    </sql>

    <sql id="list_where">
        <where>
            mt.del_flag = 0
            <if test="pkAccount != null">
                and mt.PK_ACCOUNT = #{pkAccount}
            </if>
            <if test="tradeType != null">
                and mt.TRADE_TYPE = #{tradeType}
            </if>
            <if test="sourceCode != null and sourceCode != ''">
                and mt.SOURCE_CODE = #{sourceCode}
            </if>
            <if test="pkCountry != null">
                and mt.pk_country = #{pkCountry}
            </if>
            <if test="startDate != null and startDate != ''">
                and mt.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and mt.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </sql>

    <select id="selectMemberUnderWalletList" resultType="com.hzs.report.member.vo.CuMemberWalletVO">
        <include refid="select_base"/>
        <choose>
            <when test="pkIds != null and pkIds.size >0">
                <include refid="ids_where"></include>
            </when>
            <otherwise>
                <include refid="list_where"></include>
            </otherwise>
        </choose>
        <!-- 2024.05.15 业务沟通，伞下钱包明细去掉已经退单的数据 -->
        and not exists (
        select 1
        from sa_order_charge_log socl
        where socl.del_flag = 0
        and socl.charge_type = 233
        and socl.approval_status = 2
        and instr(socl.after_data, mt.source_code) > 0
        )
        order by mt.creation_time Desc,mt.PK_ID Desc
    </select>

    <select id="productSaleStatList" resultType="com.hzs.report.member.vo.ProductSaleStatVo">
        select a.* from ( select min(oi.SKU_CODE) skuCode ,min(bp.PRODUCT_NAME) productName ,
        oi.PK_PRODUCT pkProduct, min(bu.NAME) unitStr, min(bw.WARES_NAME) waresName ,
        bw.pk_id pkWares, min(oi.SPECS_NAME) specsName,oi.SPECS_NAME_ID specsNameId,sum(oi.WARES_QUANTITY) waresQuantity,sum(oi.QUANTITY)
        productQuantity,
        min(bw.WARES_CODE) waresCode,sum(wd.QUANTITY) specsQuantity
        from SA_ORDER_ITEMS oi
        left join SA_ORDER so on so.pk_id = oi.PK_ORDER
        inner join bd_product bp on bp.pk_id = oi.PK_PRODUCT
        left join bd_wares bw on bw.pk_id = oi.PK_WARES and bw.del_flag = 0
        left join bd_wares_detail wd on wd.pk_id = oi.PK_WARES_DETAIL and wd.del_flag = 0
        left join bd_unit bu on bu.PK_ID = bp.UNIT
        where oi.del_flag = 0 and bp.del_flag = 0 and oi.pk_country = #{pkCountry} and bp.pk_country = #{pkCountry}
        <if test="skuCode != null and skuCode != ''">
            and oi.SKU_CODE like '%' || #{skuCode} || '%'
        </if>
        <if test="orderType != null">
            and so.ORDER_TYPE = #{orderType}
        </if>
        <if test="startTime != null">
            and so.PAY_TIME &gt;= #{startTime}
        </if>
        <if test="endTime != null">
            and so.PAY_TIME &lt;= #{endTime}
        </if>
        <if test="productName != null and productName != ''">
            and bp.PRODUCT_NAME like '%' || #{productName} || '%'
        </if>
        group by bw.pk_id, oi.PK_PRODUCT , oi.SPECS_NAME_ID ) a order by a.pkproduct desc
    </select>

    <select id="sumQuantity" resultType="integer">
        select
        sum(oi.QUANTITY)quantity
        from SA_ORDER_ITEMS oi
        left join sa_order so on oi.PK_ORDER = so.pk_id
        left join (
        select bp.PRODUCT_CODE productCode,bp.PRODUCT_NAME productName,bc.NAME classifyName,bc.pk_id pkClassify,bp.pk_id pkProduct,bu.NAME productUnit
        from BD_PRODUCT bp
        left join bd_classify bc on bc.PK_ID = bp.PK_CLASSIFY
        left join bd_unit bu on bu.PK_ID = bp.UNIT
        where bp.del_flag = 0
        ) a on a.pkProduct = oi.PK_PRODUCT
        where a.productCode is not null
        and oi.del_flag = 0 and so.del_flag = 0
        <if test="startTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &gt;= to_char(#{startTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="endTime != null">
            and to_char(so.PAY_TIME, 'yyyy-mm-dd hh24:mi:ss') &lt;= to_char(#{endTime}, 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="productName != null and productName !=''">
            and a.productName = #{productName}
        </if>
        <if test="pkClassify != null">
            and a.pkClassify = #{pkClassify}
        </if>
        <if test="pkCountry != null">
            and so.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="orderTypeList != null and orderTypeList.size > 0">
            and SO.ORDER_TYPE in
            <foreach collection="orderTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>
