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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberBonus" type="com.hzs.common.domain.member.ext.CuMemberBonusExt">
        <id column="PK_ID" property="pkId" />
        <result column="DEL_FLAG" property="delFlag" />
        <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_CREATOR" property="pkCreator" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="PK_MODIFIED" property="pkModified" />
        <result column="MODIFIED_TIME" property="modifiedTime" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PERIOD" property="period" />
        <result column="PK_RATE" property="pkRate" />
        <result column="out_exchange_rate" property="outExchangeRate" />
        <result column="DIRECT_INCOME" property="directIncome" />
        <result column="EXPAND_INCOME" property="expandIncome" />
        <result column="EXPAND_NO_CAPPING" property="expandNoCapping" />
        <result column="COACH_INCOME" property="coachIncome" />
        <result column="SHARE_INCOME" property="shareIncome" />
        <result column="SERVICE_INCOME" property="serviceIncome" />
        <result column="BACK_POINTS" property="backPoints" />
        <result column="SERVICE_SPEND" property="serviceSpend" />
        <result column="PUR_REAL_SUBTOTAL" property="purRealSubtotal" />
        <result column="REPUR_RANGE_INCOME" property="repurRangeIncome" />
        <result column="SAME_LEVEL_INCOME" property="sameLevelINCOME" />
        <result column="POINT_SHARE_INCOME" property="pointShareIncome" />
        <result column="REPUR_REAL_SUBTOTAL" property="repurRealSubtotal" />
        <result column="GLOBAL_POINTS" property="globalPoints" />
        <result column="REAL_INCOME_TOTAL" property="realIncomeTotal" />
        <result column="INCOME_STATUS" property="incomeStatus" />
        <result column="GRANT_STATUS" property="grantStatus" />
        <result column="SETTLE_DATE" property="settleDate" />
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="VERTEX_NAME" property="vertexName" />
        <result column="TEAM_NAME" property="teamName" />
        <result column="STORE_INCOME" property="storeIncome" />
        <result column="PUR_REAL_SUBTOTAL" property="purRealSubtotal" />
        <result column="pk_vertex" property="pkVertex" />
        <result column="pk_team_code" property="pkTeamCode" />
        <result column="REPUR_REAL_SUBTOTAL" property="repurRealSubtotal" />

    </resultMap>

    <!--查询当天小区新增数据-->
    <select id="queryMemberBonusByTime" resultType="java.lang.Long">
        select cb.PK_MEMBER from cu_member_bonus cb
        where cb.del_flag=0 and cb.PERIOD=#{period}
    </select>
    <!--查询某一期奖金汇总数据-->
    <select id="queryMemberBonusByPeriod" resultMap="CuMemberBonus">
        select
        cm.pk_vertex,
        cm.pk_team_code,
        cb.PK_MEMBER,
        cb.PK_COUNTRY,
        cb.PERIOD,
        cb.REPUR_PUSH_INCOME,
        cb.DIRECT_INCOME,
        cb.EXPAND_INCOME,
        cb.COACH_INCOME,
        cb.SHARE_INCOME,
        cb.SERVICE_SPEND,
        cb.REPUR_RANGE_INCOME,
        cb.REPUR_EXPAND_INCOME,
        cb.CLOUD_DIRECT_INCOME,
        cb.HAI_FUN_INCOME,
        cb.HI_FUN_INCOME,
        cmsp.settle_date CREATION_TIME,
        cb.PUR_REAL_SUBTOTAL,
        cb.REPUR_REAL_SUBTOTAL,
        cb.MAKER_REAL_SUBTOTAL,
        cb.SERVICE_INCOME,
        cb.REPUR_COUPON_SUBTOTAL,
        cb.REAL_INCOME_TOTAL
        from cu_member_bonus cb
        inner join cu_member_settle_period cmsp on cb.PERIOD=cmsp.PK_ID
        inner join cu_member cm on cm.pk_id=cb.PK_MEMBER
        where cb.del_flag=0
        <if test="period!=null ">
            and cb.PERIOD=#{period}
        </if>
        <if test="pkCountry!=null ">
            and cb.PK_COUNTRY=#{pkCountry}
        </if>
        <if test="pkCountry==null ">
            and cb.PK_COUNTRY!=1
        </if>

        <if test="startDate!=null and startDate!='' ">
            and cmsp.settle_date >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate!=null and endDate!='' ">
            and cmsp.settle_date &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
    </select>

    <select id="queryWeekMemberBonus" resultMap="CuMemberBonus">
        select cb.pk_member,sum(expand_income) expand_income from cu_member_bonus cb
        inner join BD_SYSTEM_CONFIG sy
        on cb.pk_country = sy.pk_country
        where sy.key = 'EXPAND_CAP_METHOD1' and sy.value='2'
        and cb.period>= #{startPeriod} and cb.period &lt; #{endPeriod}
        group by cb.pk_member
    </select>

</mapper>
