<?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.refund.mapper.RefundMapper">

    <resultMap type="RefundVO" id="EtRefundResult" autoMapping="true" />

    <sql id="selectEtRefundVo">
        select
            r.id,
            r.refund_no,
            r.pay_id,
            r.user_id,
            r.user_name,
            r.amount,
            r.reason,
            r.status,
            r.create_time,
            r.refund_result,
            r.merchant_id,
            r.oper_time,
            p.amount as payFee
        from ss_refund r
        left join ss_pay_bill p on p.pay_id = r.pay_id
        left join ss_user m on m.user_id = r.merchant_id
    </sql>

    <sql id="searchCondition">
        <if test="refundNo != null  and refundNo != ''"> and r.refund_no = #{refundNo}</if>
        <if test="payId != null  and payId != ''"> and r.pay_id like concat('%', #{payId}, '%')</if>
        <if test="userId != null "> and r.user_id = #{userId}</if>
        <if test="amount != null "> and r.amount = #{amount}</if>
        <if test="refundResult != null "> and r.refund_result = #{refundResult}</if>
        <if test="status != null "> and r.status = #{status}</if>
        <if test="userName != null and userName != ''"> and r.user_name like concat('%', #{userName}, '%')</if>
        <if test="startTime != null "> and date(r.create_time) >= #{startTime}</if>
        <if test="endTime != null "> and date(r.create_time) &lt;= #{endTime}</if>
    </sql>

    <select id="selectEtRefundList" parameterType="Refund" resultMap="EtRefundResult">
        <include refid="selectEtRefundVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
        order by r.create_time desc
    </select>

    <select id="selectEtRefundById" parameterType="Long" resultMap="EtRefundResult">
        <include refid="selectEtRefundVo"/>
        where r.id = #{id}
    </select>

    <select id="selectEtRefundByPayId" resultMap="EtRefundResult">
        <include refid="selectEtRefundVo"/>
        where r.pay_id = #{payId}
    </select>

    <select id="selectEtRefundByRefundNo" resultMap="EtRefundResult">
        <include refid="selectEtRefundVo"/>
        where r.refund_no = #{refundNo}
    </select>

    <select id="getRefundedAmount" parameterType="refundQuery" resultType="java.math.BigDecimal">
        select COALESCE(SUM(amount), 0) amount from ss_refund r
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <insert id="insertEtRefund" parameterType="Refund">
        insert into ss_refund
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="refundNo != null">refund_no,</if>
            <if test="payId != null and payId != ''">pay_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="userType != null">user_type,</if>
            <if test="amount != null">amount,</if>
            <if test="reason != null">reason,</if>
            <if test="status != null">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="refundResult != null">refund_result,</if>
            <if test="merchantId != null">merchant_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="refundNo != null">#{refundNo},</if>
            <if test="payId != null and payId != ''">#{payId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="userType != null">#{userType},</if>
            <if test="amount != null">#{amount},</if>
            <if test="reason != null">#{reason},</if>
            <if test="status != null">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="refundResult != null">#{refund_result},</if>
            <if test="merchantId != null">#{merchantId},</if>
         </trim>
    </insert>

    <update id="updateEtRefund" parameterType="Refund">
        update ss_refund
        <trim prefix="SET" suffixOverrides=",">
            <if test="refundNo != null">refund_no = #{refundNo},</if>
            <if test="payId != null and payId != ''">pay_id = #{payId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="refundResult != null">refund_result = #{refundResult},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
        </trim>
        where id = #{id}
    </update>
    <update id="updateEtRefundByRefundNo">
        update ss_refund
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="amount != null">amount = #{amount},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="status != null">status = #{status},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="refundResult != null">refund_result = #{refundResult},</if>
            <if test="merchantId != null">merchant_id = #{merchantId},</if>
        </trim>
        where refund_no = #{refundNo}
    </update>

    <delete id="deleteEtRefundById" parameterType="Long">
        delete from ss_refund where id = #{id}
    </delete>

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

<!--    <select id="checkUserDeposit"  resultType="Order">-->
<!--        SELECT o.order_id,-->
<!--               o.area_id,-->
<!--               o.pay_id,-->
<!--               o.out_trade_no,-->
<!--               o.create_time,-->
<!--               o.user_id,-->
<!--               ref.id,-->
<!--               ref.refund_no-->
<!--        FROM ss_order o-->
<!--                 LEFT JOIN ss_refund ref ON ref.pay_id = o.pay_id-->
<!--        WHERE o.status = '4'-->
<!--          AND o.paid = '1'-->
<!--          AND o.type = 2  and o.user_id =  #{userId}-->
<!--          AND ref.id IS NULL;-->
<!--    </select>-->

<!--    <select id="getRefundedAmount" resultMap="EtRefundResult">-->
<!--        SELECT-->
<!--            COALESCE(SUM(amount), 0) AS total_refund_amount,-->
<!--            COALESCE(SUM(dispatch_fee), 0) AS total_dispatch_fee,-->
<!--            COALESCE(SUM(delivery_fee), 0) AS total_delivery_fee,-->
<!--            COALESCE(SUM(lease_fee), 0) AS total_lease_fee,-->
<!--            COALESCE(SUM(deposit), 0) AS total_deposit-->
<!--        FROM-->
<!--           ss_refund-->
<!--        WHERE-->
<!--            pay_id = #{payId};-->
<!--    </select>-->
</mapper>
