<?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.ruoyi.ss.dividendRefund.mapper.DividendRefundMapper">

    <resultMap type="DividendRefundVO" id="BonusRefundResult" autoMapping="true"/>

    <sql id="selectBonusRefundVo">
        select
            sbr.id,
            sbr.dividend_id,
            sbr.refund_amount,
            sbr.create_time,
            sb.arrival_type as bonus_arrival_type,
            sb.arrival_name as bonus_arrival_name
        from ss_bonus_refund sbr
            left join ss_bonus sb on sb.id = sbr.dividend_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and sbr.id = #{query.id}</if>
        <if test="query.dividendId != null "> and sbr.dividend_id = #{query.dividendId}</if>
        <if test="query.createDate != null "> and date(sbr.create_time) = #{query.createDate}</if>
        <if test="query.channelId != null "> and sb.channel_id = #{query.channelId}</if>
        <if test="query.bonusArrivalType != null and query.bonusArrivalType != '' "> and sb.arrival_type = #{query.bonusArrivalType}</if>
        <if test="query.bonusArrivalName != null and query.bonusArrivalName != '' "> and sb.arrival_name like concat('%', #{query.bonusArrivalName}, '%')</if>
        <if test="query.bonusArrivalTypes != null ">
            and sb.arrival_type in
            <foreach collection="query.bonusArrivalTypes" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${query.params.dataScope}
    </sql>

    <select id="selectBonusRefundList" parameterType="DividendRefundQuery" resultMap="BonusRefundResult">
        <include refid="selectBonusRefundVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectBonusRefundById" parameterType="Long" resultMap="BonusRefundResult">
        <include refid="selectBonusRefundVo"/>
        where sbr.id = #{id}
    </select>

    <insert id="insertBonusRefund" parameterType="DividendRefund" useGeneratedKeys="true" keyProperty="id">
        insert into ss_bonus_refund
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dividendId != null">dividend_id,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dividendId != null">#{dividendId},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <insert id="batchInsert" parameterType="DividendRefund" useGeneratedKeys="true" keyProperty="id">
        insert into ss_bonus_refund
        <trim prefix="(" suffix=")" suffixOverrides=",">
            dividend_id,
            refund_amount,
            create_time,
        </trim>
        values
        <foreach collection="list" item="i" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="i.dividendId != null ">#{i.dividendId},</if>
                <if test="i.dividendId == null ">default,</if>
                <if test="i.refundAmount != null ">#{i.refundAmount},</if>
                <if test="i.refundAmount == null ">default,</if>
                <if test="i.createTime != null ">#{i.createTime},</if>
                <if test="i.createTime == null ">default,</if>
            </trim>
        </foreach>
    </insert>

    <update id="batchUpdate">
        update ss_bonus_refund
        <trim prefix="SET" suffixOverrides=",">
            <foreach open="dividend_id = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.dividendId != null ">
                        WHEN #{item.id} THEN #{item.dividendId}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `dividend_id`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="refund_amount = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.refundAmount != null ">
                        WHEN #{item.id} THEN #{item.refundAmount}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `refund_amount`
                    </otherwise>
                </choose>
            </foreach>
            <foreach open="create_time = CASE id" collection="list" item="item" close="END,">
                <choose>
                    <when test="item.createTime != null ">
                        WHEN #{item.id} THEN #{item.createTime}
                    </when>
                    <otherwise>
                        WHEN #{item.id} THEN `create_time`
                    </otherwise>
                </choose>
            </foreach>
        </trim>
        where id in
        <foreach item="item" collection="list" open="(" separator="," close=")">
            #{item.id}
        </foreach>
    </update>

    <update id="updateBonusRefund" parameterType="DividendRefund">
        update ss_bonus_refund
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.dividendId != null">dividend_id = #{data.dividendId},</if>
        <if test="data.refundAmount != null">refund_amount = #{data.refundAmount},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
    </sql>

    <delete id="deleteBonusRefundById" parameterType="Long">
        delete from ss_bonus_refund where id = #{id}
    </delete>

    <delete id="deleteBonusRefundByIds" parameterType="String">
        delete from ss_bonus_refund where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <!-- selectSumOfAmount -->

    <select id="selectSumOfAmount" parameterType="DividendRefundQuery" resultType="BigDecimal">
        select sum(sbr.refund_amount)
        from ss_bonus_refund sbr
            left join ss_bonus sb on sb.id = sbr.dividend_id
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

</mapper>
