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

    <!-- 删除会员延缓发放主表 -->
    <delete id="deleteCuMemberBonusDelay">
        delete
        from CU_MEMBER_BONUS_DELAY
        where PK_BONUS in (
            select pk_id
            from cu_member_bonus
            where period = #{period}
              and grant_status &lt;= 2)
    </delete>

    <!-- 批量插入会员延缓发放主表 -->
    <insert id="batchInsertCuMemberBonusDelay">
        insert ALL
        <foreach item="item" index="index" collection="cuMemberBonusDelayList" separator=" ">
            into cu_member_bonus_delay (
            pk_member, period, pk_country, pk_creator,
            before_grant_income, grant_income, after_grant_income, pk_bonus
            )
            values (
            #{item.pkMember}, #{item.period}, #{item.pkCountry}, #{item.pkCreator},
            #{item.beforeGrantIncome}, #{item.grantIncome}, #{item.afterGrantIncome}, #{item.pkBonus}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <!-- 查询当天没有产生过奖金，但是还有延缓发放奖金的列表 -->
    <select id="listDelayNotBonus" resultType="com.hzs.common.domain.member.ext.CuMemberSettleExt">
        select cmbd.period, cmbd.pk_country,
        cmbd.before_grant_income, cmbd.grant_income, cmbd.after_grant_income,
        a.pk_member, a.member_code, a.member_name,
        a.pk_place_parent, a.place_dept, a.pk_grade, a.pk_awards,
        a.account_status, a.income_status, a.pk_settle_country,
        a.purchase_status, a.share_status, a.register_authority,
        a.a_balance, a.b_balance, a.r_a_balance, a.r_b_balance,
        a.rep_a_balance, a.rep_b_balance, a.r_rep_a_balance, a.r_rep_b_balance,
        a.register_amount, a.register_pv, a.upgrade_amount, a.upgrade_pv,
        a.repurchase_amount, a.repurchase_pv,
        a.hi_fun_amount, a.hi_fun_pv,
        a.mall_amount, a.mall_pv,
        a.pk_center_code, a.pk_vertex, a.category,
        a.min_achieve, a.round, a.second,
        a.store_level, a.expire_status,
        a.pk_rate
        from (
        select cmbd.pk_member,
        cmbd.period,
        cmbd.pk_creator,
        cmbd.pk_country,
        cmbd.before_grant_income,
        cmbd.grant_income,
        cmbd.after_grant_income
        from cu_member_bonus_delay cmbd
        left join cu_member cm
        on cm.pk_id = cmbd.pk_member
        where cmbd.del_flag = 0
        and cm.del_flag = 0
        and cm.category = 0
        and cmbd.period = #{yesterdayPeriod}
        and cmbd.after_grant_income > 0
        <if test="pkMemberSet.size > 0">
            and cmbd.pk_member not in
            <foreach collection="pkMemberSet" item="item" open="(" close=")" separator=" union ">
                select #{item} from dual
            </foreach>
        </if>
        ) cmbd
        left join ${currentSettleTableName} a
        on a.pk_member = cmbd.pk_member
    </select>

    <!-- 查询延缓奖金列表 -->
    <select id="listDelay" resultType="com.hzs.common.domain.member.bonus.ext.CuMemberBonusDelayExt">
        select cmbd.*, cm.member_code, cm.member_name, cmsp.settle_date
        from CU_MEMBER_BONUS_DELAY cmbd
        left join cu_member cm
        on cm.pk_id = cmbd.pk_member
        left join cu_member_settle_period cmsp
        on cmsp.pk_id = cmbd.period
        where cmbd.del_flag = 0
        <if test="param.memberCode != null and param.memberCode != ''">
            and cm.member_code like #{param.memberCode} || '%'
        </if>
        <if test="param.startDate != null">
            and cmsp.settle_date >= #{param.startDate}
        </if>
        <if test="param.endDate != null">
            and #{param.endDate} >= cmsp.settle_date
        </if>
        order by cmbd.period desc
    </select>

</mapper>
