<?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.system.board.mapper.BoardMapper">

    <!-- 查询会员汇总数据 -->
    <select id="getMemberBoard" resultType="com.hzs.system.board.vo.BoardMemberVO">
        select *
        from (select count(1) member_total
              from cu_member cm
              where cm.del_flag = 0
                and cm.pay_status = 1
                and cm.category = 0
                and cm.customer_type != 1) a
                 left join (
            select count(1) member_today
            from cu_member cm
            where cm.del_flag = 0
              and cm.pay_status = 1
              and cm.category = 0
              and to_char(cm.pay_time, 'yyyy-mm-dd') =
                  to_char(sysdate, 'yyyy-mm-dd')
              and cm.customer_type != 1
        ) b on 1 = 1
                 left join (select count(1) member_yesterday
                            from cu_member cm
                            where cm.del_flag = 0
                              and cm.pay_status = 1
                              and cm.category = 0
                              and to_char(cm.pay_time, 'yyyy-mm-dd') =
                                  to_char(sysdate - 1, 'yyyy-mm-dd')
                              and cm.customer_type != 1
        ) c
                           on 1 = 1
                 left join (select count(1) member_month
                            from cu_member cm
                            where cm.del_flag = 0
                              and cm.pay_status = 1
                              and cm.category = 0
                              and to_char(cm.pay_time, 'yyyy-mm') =
                                  to_char(sysdate, 'yyyy-mm')
                              and cm.customer_type != 1
        ) d on 1 = 1
    </select>

    <!-- 会员各等级数据 -->
    <select id="listMemberLevelBoard" resultType="com.hzs.system.board.vo.BoardMemberLevelVO">
        select bg.grade_name,
               bg.grade_value,
               nvl(a.member_total, 0) member_total,
               0                      sort
        from (select cm.PK_REGISTER_GRADE, count(1) member_total
              from cu_member cm
              where cm.del_flag = 0
                and cm.pay_status = 1
                and cm.category = 0
                and to_char(cm.pay_time, 'yyyy-mm-dd') =
                    to_char(sysdate, 'yyyy-mm-dd')
                and cm.customer_type != 1
              group by cm.PK_REGISTER_GRADE
              order by cm.PK_REGISTER_GRADE) a
                 right join bd_grade bg
                            on bg.pk_id = a.PK_REGISTER_GRADE
        where bg.del_flag = 0
        union all
        select bg.grade_name,
               bg.grade_value,
               nvl(a.member_total, 0) member_total,
               1                      sort
        from (select cm.PK_REGISTER_GRADE, count(1) member_total
              from cu_member cm
              where cm.del_flag = 0
                and cm.pay_status = 1
                and cm.category = 0
                and to_char(cm.pay_time, 'yyyy-mm-dd') =
                    to_char(sysdate - 1, 'yyyy-mm-dd')
                and cm.customer_type != 1
              group by cm.PK_REGISTER_GRADE
              order by cm.PK_REGISTER_GRADE) a
                 right join bd_grade bg
                            on bg.pk_id = a.PK_REGISTER_GRADE
        where bg.del_flag = 0
        union all
        select bg.grade_name,
               bg.grade_value,
               nvl(a.member_total, 0) member_total,
               2                      sort
        from (select cm.PK_REGISTER_GRADE, count(1) member_total
              from cu_member cm
              where cm.del_flag = 0
                and cm.pay_status = 1
                and cm.category = 0
                and to_char(cm.pay_time, 'yyyy-mm') = to_char(sysdate, 'yyyy-mm')
                and cm.customer_type != 1
              group by cm.PK_REGISTER_GRADE
              order by cm.PK_REGISTER_GRADE) a
                 right join bd_grade bg
                            on bg.pk_id = a.PK_REGISTER_GRADE
        where bg.del_flag = 0
        order by sort, grade_value
    </select>

    <!-- 查询日业绩汇总数据 -->
    <select id="getDayAchieve" resultType="com.hzs.system.board.vo.BoardAchieveVO">
        select a.*, b.*, 0 sort
        from (
                 select nvl(sum(so.order_achieve), 0) first_achieve,
                        nvl(sum(so.order_amount), 0)  first_amount
                 from sa_order so
                 where so.del_flag = 0
                   and so.order_status = 1
                   and so.order_type in (41, 42)
                   and to_char(so.pay_time, 'yyyy-mm-dd') =
                       to_char(sysdate, 'yyyy-mm-dd')
             ) a
                 left join (
            select nvl(sum(so.order_achieve), 0) rep_achieve,
                   nvl(sum(so.order_amount), 0)  rep_amount
            from sa_order so
            where so.del_flag = 0
              and so.order_status = 1
              and so.order_type in (43, 44)
              and to_char(so.pay_time, 'yyyy-mm-dd') =
                  to_char(sysdate, 'yyyy-mm-dd')
        ) b on 1 = 1
        union all
        select a.*, b.*, 1 sort
        from (
                 select nvl(sum(so.order_achieve), 0) first_achieve,
                        nvl(sum(so.order_amount), 0)  first_amount
                 from sa_order so
                 where so.del_flag = 0
                   and so.order_status = 1
                   and so.order_type in (41, 42)
                   and to_char(so.pay_time, 'yyyy-mm-dd') =
                       to_char(sysdate - 1, 'yyyy-mm-dd')
             ) a
                 left join (
            select nvl(sum(so.order_achieve), 0) rep_achieve,
                   nvl(sum(so.order_amount), 0)  rep_amount
            from sa_order so
            where so.del_flag = 0
              and so.order_status = 1
              and so.order_type in (43, 44)
              and to_char(so.pay_time, 'yyyy-mm-dd') =
                  to_char(sysdate - 1, 'yyyy-mm-dd')
        ) b on 1 = 1
        order by sort
    </select>

    <!-- 查询月业绩汇总数据 -->
    <select id="getMonthAchieve" resultType="com.hzs.system.board.vo.BoardAchieveVO">
        select a.*, b.*
        from (select nvl(sum(so.order_achieve), 0) first_achieve,
                     nvl(sum(so.order_amount), 0)  first_amount
              from sa_order so
              where so.del_flag = 0
                and so.order_status = 1
                and so.order_type in (41, 42)
                and to_char(so.pay_time, 'yyyy-mm') = to_char(sysdate, 'yyyy-mm')
             ) a
                 left join (
            select nvl(sum(so.order_achieve), 0)                            rep_achieve,
                   nvl(sum(so.order_amount - nvl(so.CONSUME_AMOUNT, 0)), 0) rep_amount
            from sa_order so
            where so.del_flag = 0
              and so.order_status = 1
              and so.order_type in (43, 44)
              and to_char(so.pay_time, 'yyyy-mm') =
                  to_char(sysdate, 'yyyy-mm')
        ) b on 1 = 1
    </select>

    <!-- 各币种充值数据 -->
    <select id="listAccountRechargeBoard" resultType="com.hzs.system.board.vo.BoardRechargeAccountVO">
        select ba.account_name, nvl(a.recharge_amount, 0) recharge_amount, 0 sort, ba.field_value
        from (select t.pk_account, sum(t.recharge_amount) recharge_amount
              from CU_MEMBER_RECHARGE t
              where t.del_flag = 0
                and t.recharge_amount != 0
           and t.pk_account in
               (select t.pk_id
                  from bd_account t
                 where t.del_flag = 0
                   and t.enable_state = 0
                   and t.pk_country = 1
                   and t.field_value in (1, 2, 3, 4))
           and ((t.recharge_source = 1 and t.approve_state = 2) or
               (t.recharge_source = 0 and t.approve_state = 2))
           and to_char(t.modified_time, 'yyyy-mm-dd') =
               to_char(sysdate, 'yyyy-mm-dd')
              group by t.pk_account) a
                 right join bd_account ba
                            on ba.pk_id = a.pk_account
                                and ba.del_flag = 0
        where ba.field_value in (1, 2, 3, 4)
        union all
        select ba.account_name, nvl(a.recharge_amount, 0) recharge_amount, 1 sort, ba.field_value
        from (select t.pk_account, sum(t.recharge_amount) recharge_amount
              from CU_MEMBER_RECHARGE t
              where t.del_flag = 0
                and t.recharge_amount != 0
           and t.pk_account in
               (select t.pk_id
                  from bd_account t
                 where t.del_flag = 0
                   and t.enable_state = 0
                   and t.pk_country = 1
                   and t.field_value in (1, 2, 3, 4))
           and ((t.recharge_source = 1 and t.approve_state = 2) or
               (t.recharge_source = 0 and t.approve_state = 2))
           and to_char(t.modified_time, 'yyyy-mm-dd') =
               to_char(sysdate - 1, 'yyyy-mm-dd')
              group by t.pk_account) a
                 right join bd_account ba
                            on ba.pk_id = a.pk_account
                                and ba.del_flag = 0
        where ba.field_value in (1, 2, 3, 4)
        union all
        select ba.account_name, nvl(a.recharge_amount, 0) recharge_amount, 2 sort, ba.field_value
        from (select t.pk_account, sum(t.recharge_amount) recharge_amount
              from CU_MEMBER_RECHARGE t
              where t.del_flag = 0
                and t.recharge_amount != 0
           and t.pk_account in
               (select t.pk_id
                  from bd_account t
                 where t.del_flag = 0
                   and t.enable_state = 0
                   and t.pk_country = 1
                   and t.field_value in (1, 2, 3, 4))
           and ((t.recharge_source = 1 and t.approve_state = 2) or
               (t.recharge_source = 0 and t.approve_state = 2))
           and to_char(t.modified_time, 'yyyy-mm') =
               to_char(sysdate - 1, 'yyyy-mm')
              group by t.pk_account) a
                 right join bd_account ba
                            on ba.pk_id = a.pk_account
                                and ba.del_flag = 0
        where ba.field_value in (1, 2, 3, 4)
        order by sort, field_value
    </select>

</mapper>
