<?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.member.statis.mapper.MemberStatisticsMapper">

    <select id="getDirectStatisticsNumberOfPeople"
            resultType="com.hzs.common.domain.member.statis.DirectStatisticsTop30VO">
        SELECT
            *
        FROM
            (
                SELECT
                    PK_ID memberId,
                    MEMBER_CODE memberCode,
                    member_name memberName,
                    numberOfPeople,
                    numberOfAmount
                FROM
                    (
                        SELECT
                            o.PK_REFERENCE,
                            count( DISTINCT o.PK_MEMBER ) numberOfPeople,
                            sum( order_amount ) numberOfAmount
                        FROM
                            sa_order o
                                RIGHT JOIN cu_member m ON m.PK_ID = o.pk_member
                                AND m.DEL_FLAG = 0
                                AND m.CATEGORY = 0
                        WHERE
                            o.del_flag = 0
                          AND order_status = 1
                          AND ( order_type IN ( 1, 2 ) OR ( order_Type = 3 AND ORDER_TYPE_EXTEND = 1 ) )
                          and o.pay_time between #{param.startDate} and #{param.endDate}

                        GROUP BY PK_REFERENCE
                        ORDER BY numberOfAmount DESC
                    ) r
                        LEFT JOIN cu_member cm ON r.PK_REFERENCE = cm.pk_id
                WHERE  cm.PK_SETTLE_GRADE IN ( SELECT pk_id FROM BD_GRADE WHERE ( grade_value >= 30 ) )
                ORDER BY numberOfPeople DESC
            )
        WHERE ROWNUM <![CDATA[ <=]]> 30
    </select>
    <select id="getDirectStatisticsNumberOfAmount"
            resultType="com.hzs.common.domain.member.statis.DirectStatisticsTop30VO">
        SELECT
        *
        FROM
        (
            SELECT
                PK_ID memberId,
                MEMBER_CODE memberCode,
                member_name memberName,
                numberOfPeople,
                numberOfAmount
            FROM
            (
                SELECT
                o.PK_REFERENCE,
                count( DISTINCT o.PK_MEMBER ) numberOfPeople,
                sum( order_amount ) numberOfAmount
                FROM
                sa_order o
                RIGHT JOIN cu_member m ON m.PK_ID = o.pk_member
                AND m.DEL_FLAG = 0
                AND m.CATEGORY = 0
                WHERE
                o.del_flag = 0
                AND order_status = 1
                AND ( order_type IN ( 1, 2 ) OR ( order_Type = 3 AND ORDER_TYPE_EXTEND = 1 ) )
                and o.pay_time between #{param.startDate} and #{param.endDate}

                GROUP BY PK_REFERENCE
                ORDER BY numberOfAmount DESC
            ) r
            LEFT JOIN cu_member cm ON r.PK_REFERENCE = cm.pk_id
            WHERE  cm.PK_SETTLE_GRADE IN ( SELECT pk_id FROM BD_GRADE WHERE ( grade_value >= 30 ) )
            ORDER BY numberOfAmount DESC
        )
        WHERE ROWNUM <![CDATA[ <=]]> 30
    </select>
</mapper>
