<?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.bonus.member.mapper.CuMemberActivityMapper">


    <select id="queryCuMemberActivityVO" resultType="com.hzs.bonus.member.vo.MemberActivityVO">
        select ct.pk_member,bc.name country_name,ct.member_code,ct.member_name,bg.grade_name,ba.awards_name,
               '' pk_Rec_Grade,0 rec_num,0 rec_achieve,cm.pay_time from ${settleTable} ct
       inner join cu_member cm
                  on cm.pk_id=ct.pk_member
       inner join bd_grade bg
                  on bg.pk_id = ct.pk_grade
       inner join bd_awards ba
                  on ba.pk_id=ct.pk_awards
       inner join bd_country bc
                  on bc.pk_id=ct.pk_settle_country
        where bg.del_flag=0 and ba.del_flag=0 and bc.del_flag=0
          and bg.grade_value >= #{memberActivityParam.pkGrade}
          and ba.awards_value >= #{memberActivityParam.pkAwards}
          <if test="memberActivityParam.pkCountry != null">
              and ct.pk_settle_country=#{memberActivityParam.pkCountry}
          </if>
    </select>
    <select id="queryRecCuMemberActivityVO" resultType="com.hzs.bonus.member.vo.MemberActivityVO">
        select ct.pk_member,bc.name country_name,ct.member_code,ct.member_name,bg.grade_name,ba.awards_name,
        rg.grade_name pk_rec_grade,b.rec_num rec_num,b.order_achieve rec_achieve,cm.pay_time from ${settleTable} ct
        inner join(
        select pk_reference,min(bg.pk_id) pk_rec_grade,sum(order_achieve) order_achieve,count(ct.pk_member) rec_num from sa_order so
        inner join ${settleTable} ct
        on so.pk_member=ct.pk_member
        inner join bd_grade bg
        on bg.pk_id = ct.pk_grade
        where so.del_flag=0 and bg.del_flag=0 and so.order_status = 1
        and so.pay_time >= #{memberActivityParam.startDate, jdbcType=DATE} and so.pay_time &lt;#{memberActivityParam.endDate, jdbcType=DATE}
        and bg.grade_value >=#{memberActivityParam.pkRecGrade}
        <if test="memberActivityParam.pkCountry != null">
            and so.pk_country=#{memberActivityParam.pkCountry}
        </if>
        group by pk_reference
        having sum(order_achieve)>= #{memberActivityParam.recAchieve}
           and count(ct.pk_member)>= #{memberActivityParam.recNum}
        ) b
        on ct.pk_member=b.pk_reference
        inner join cu_member cm
        on cm.pk_id=ct.pk_member
        inner join bd_grade bg
        on bg.pk_id = ct.pk_grade
        inner join bd_awards ba
        on ba.pk_id=ct.pk_awards
        inner join bd_country bc
        on bc.pk_id=ct.pk_settle_country
        left join bd_grade rg
        on rg.pk_id=b.pk_rec_grade and rg.del_flag=0
        where bg.del_flag=0 and ba.del_flag=0 and bc.del_flag=0
        and bg.grade_value >= #{memberActivityParam.pkGrade}
        and ba.awards_value >= #{memberActivityParam.pkAwards}
        <if test="memberActivityParam.pkCountry != null">
            and ct.pk_settle_country=#{memberActivityParam.pkCountry}
        </if>
    </select>
    <select id="queryCuMemberActivityDetailVO" resultType="com.hzs.bonus.member.vo.MemberActivityDetailVO">
        select * from(
        select cd.pk_member,cd.place_dept,(cd.sum_pv-nvl(cb.sum_pv,0)) sum_pv from ${settleTable} cd
        <if test="memberActivityParam.placeDeptNum != null and memberActivityParam.placeDeptNum>0">
            inner join(select cd.pk_member from ${settleTable} cd
            left join ${beforeSettleTable} cb
            on cd.pk_member=cb.pk_member and cd.place_dept=cb.place_dept
            group by cd.pk_member
            having count(cd.place_dept) >= #{memberActivityParam.placeDeptNum}
            and max(cd.sum_pv-nvl(cb.sum_pv,0)) >=#{memberActivityParam.maxPlaceDeptAchieve}) b
            on cd.pk_member=b.pk_member
        </if>
        left join ${beforeSettleTable} cb
        on cd.pk_member=cb.pk_member and cd.place_dept=cb.place_dept
        where cd.pk_member in
        <foreach item="item" collection="memberActivityVoList" open="(" separator="," close=")">
            #{item.pkMember}
        </foreach>)
        order by pk_member,sum_pv
    </select>
    <select id="queryCouCuMemberActivityDetailVO" resultType="com.hzs.bonus.member.vo.MemberActivityDetailVO">
        select * from(
        select cd.pk_member,cd.place_dept,(cd.sum_pv-nvl(cb.sum_pv,0)) sum_pv from ${settleTable} cd
        <if test="memberActivityParam.placeDeptNum != null and memberActivityParam.placeDeptNum>0">
            inner join(select cd.pk_member from ${settleTable} cd
            left join ${beforeSettleTable} cb
            on cd.pk_member=cb.pk_member and cd.place_dept=cb.place_dept and cd.pk_country=cb.pk_country
            where cd.pk_country=#{memberActivityParam.pkCountry}
            group by cd.pk_member
            having count(cd.place_dept) >= #{memberActivityParam.placeDeptNum}
            and max(cd.sum_pv-nvl(cb.sum_pv,0)) >=#{memberActivityParam.maxPlaceDeptAchieve}) b
            on cd.pk_member=b.pk_member
        </if>
        left join ${beforeSettleTable} cb
        on cd.pk_member=cb.pk_member and cd.place_dept=cb.place_dept and cd.pk_country=cb.pk_country
        where cd.pk_country=#{memberActivityParam.pkCountry} and cd.pk_member in
        <foreach item="item" collection="memberActivityVoList" open="(" separator="," close=")">
            #{item.pkMember}
        </foreach>)
        order by pk_member,sum_pv
    </select>
</mapper>
