<?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.payBill.mapper.PayBillMapper">

    <resultMap type="PayBillVO" id="PayBillResult" autoMapping="true">
        <result property="refundAmount" column="refund_amount" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
        <result property="refundingAmount" column="refunding_amount" typeHandler="com.ruoyi.system.mapper.typehandler.NonNullDecimalTypeHandler"/>
    </resultMap>

    <sql id="selectPayBillVo">
        select
            spb.pay_id,
            spb.pay_no,
            spb.order_no,
            spb.amount,
            spb.channel_id,
            spb.status,
            spb.pay_time,
            spb.create_time,
            spb.description,
            spb.account,
            spb.channel_cost,
            spb.refund_amount,
            spb.refunding_amount
        from ss_pay_bill spb
    </sql>

    <sql id="searchCondition">
        <if test="query.payId != null "> and spb.pay_id = #{query.payId}</if>
        <if test="query.payNo != null  and query.payNo != ''"> and spb.pay_no = #{query.payNo}</if>
        <if test="query.orderNo != null "> and spb.order_no = #{query.orderNo}</if>
        <if test="query.channelId != null "> and spb.channel_id = #{query.channelId}</if>
        <if test="query.payDate != null">and date(spb.pay_time) = date(#{query.payDate})</if>
        <if test="query.status != null  and query.status != ''"> and spb.status = #{query.status}</if>
        <if test="query.startCreateTime != null">and spb.create_time >= #{query.startCreateTime}</if>
        <if test="query.endCreateTime != null">and spb.create_time &lt;= #{query.endCreateTime}</if>
        <if test="query.orderNos != null and query.orderNos.size() > 0">
            and spb.order_no in
            <foreach item="item" index="index" collection="query.orderNos" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.payIds != null and query.payIds.size() > 0">
            and spb.pay_id in
            <foreach item="item" index="index" collection="query.payIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.statusList != null and query.statusList.size() > 0">
            and spb.status in
            <foreach item="item" index="index" collection="query.statusList" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectPayBillList" parameterType="PayBillQuery" resultMap="PayBillResult">
        <include refid="selectPayBillVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectPayBillByPayId" parameterType="Long" resultMap="PayBillResult">
        <include refid="selectPayBillVo"/>
        where spb.pay_id = #{payId}
    </select>

    <select id="selectByorderNo" resultMap="PayBillResult">
        <include refid="selectPayBillVo"/>
        where spb.order_no = #{orderNo}
    </select>

    <select id="selectByPayNo" resultMap="PayBillResult">
        <include refid="selectPayBillVo"/>
        where spb.pay_no = #{payNo}
    </select>

    <insert id="insertPayBill" parameterType="PayBill" useGeneratedKeys="true" keyProperty="payId">
        insert into ss_pay_bill
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="payNo != null and payNo != ''">pay_no,</if>
            <if test="orderNo != null">order_no,</if>
            <if test="amount != null">amount,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="description != null">`description`,</if>
            <if test="account != null">`account`,</if>
            <if test="channelCost != null">channel_cost,</if>
            <if test="refundAmount != null">refund_amount,</if>
            <if test="refundingAmount != null">refunding_amount,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="payNo != null and payNo != ''">#{payNo},</if>
            <if test="orderNo != null">#{orderNo},</if>
            <if test="amount != null">#{amount},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="description != null">#{description},</if>
            <if test="account != null">#{account},</if>
            <if test="channelCost != null">#{channelCost},</if>
            <if test="refundAmount != null">#{refundAmount},</if>
            <if test="refundingAmount != null">#{refundingAmount},</if>
         </trim>
    </insert>

    <insert id="addRefundingAmount">
        update ss_pay_bill
        set refunding_amount = refunding_amount + #{amount}
        where pay_id = #{payId} and amount >= refunding_amount + refund_amount + #{amount}
    </insert>

    <update id="recordRefundAmount">
        update ss_pay_bill
        set refunding_amount = refunding_amount - #{amount},
            refund_amount = refund_amount + #{amount}
        where pay_id = #{payId} and refunding_amount - #{amount} >= 0
    </update>

    <!-- selectSumOfChannelCost -->

    <select id="selectSumOfChannelCost" resultType="BigDecimal">
        select sum(spb.channel_cost) from ss_pay_bill spb
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <update id="updatePayBill" parameterType="PayBill">
        update ss_pay_bill
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where pay_id = #{data.payId}
    </update>

    <sql id="updateColumns">
        <if test="data.payNo != null and data.payNo != ''">pay_no = #{data.payNo},</if>
        <if test="data.orderNo != null">order_no = #{data.orderNo},</if>
        <if test="data.amount != null">amount = #{data.amount},</if>
        <if test="data.channelId != null">channel_id = #{data.channelId},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.payTime != null">pay_time = #{data.payTime},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.description != null">`description` = #{data.description},</if>
        <if test="data.account != null">`account` = #{data.account},</if>
        <if test="data.channelCost != null">channel_cost = #{data.channelCost},</if>
        <if test="data.refundAmount != null">refund_amount = #{data.refundAmount},</if>
        <if test="data.refundingAmount != null">refunding_amount = #{data.refundingAmount},</if>
    </sql>

    <update id="updatePayQuery">
        update ss_pay_bill spb
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <update id="updateRefundAmountByQuery">
        update ss_pay_bill spb set
        refund_amount = refund_amount + #{data.refundAmount},
        refunding_amount = refunding_amount - #{data.refundAmount},
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <delete id="deletePayBillByPayId" parameterType="Long">
        delete from ss_pay_bill where pay_id = #{payId}
    </delete>

    <delete id="deletePayBillByPayIds" parameterType="String">
        delete from ss_pay_bill where pay_id in
        <foreach item="payId" collection="array" open="(" separator="," close=")">
            #{payId}
        </foreach>
    </delete>
</mapper>
