<?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.web.modules.pay.mapper.BizRefundRecordMapper">

    <resultMap id="BizRefundRecordResult" type="com.ruoyi.web.modules.pay.domain.BizRefundRecord">
        <id property="refundId" column="refund_id"/>
        <result property="aftersaleId" column="aftersale_id"/>
        <result property="aftersaleNo" column="aftersale_no"/>
        <result property="orderId" column="order_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="payId" column="pay_id"/>
        <result property="memberId" column="member_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="outRefundNo" column="out_refund_no"/>
        <result property="wxRefundId" column="wx_refund_id"/>
        <result property="refundAmount" column="refund_amount"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="failReason" column="fail_reason"/>
        <result property="notifyTime" column="notify_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select refund_id, aftersale_id, aftersale_no, order_id, order_no, pay_id, member_id, shop_id,
               out_refund_no, wx_refund_id, refund_amount, refund_status, fail_reason, notify_time,
               create_time, update_time
        from biz_refund_record
    </sql>

    <select id="selectByAftersaleId" resultMap="BizRefundRecordResult">
        <include refid="selectVo"/>
        where aftersale_id = #{aftersaleId}
    </select>

    <select id="sumSuccessRefundAmountByOrderId" resultType="java.math.BigDecimal">
        select coalesce(sum(refund_amount), 0)
        from biz_refund_record
        where order_id = #{orderId}
          and refund_status in ('1', '3')
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.modules.pay.domain.BizRefundRecord" useGeneratedKeys="true" keyProperty="refundId">
        insert into biz_refund_record (
            aftersale_id, aftersale_no, order_id, order_no, pay_id, member_id, shop_id,
            out_refund_no, refund_amount, refund_status, create_time, update_time
        ) values (
            #{aftersaleId}, #{aftersaleNo}, #{orderId}, #{orderNo}, #{payId}, #{memberId}, #{shopId},
            #{outRefundNo}, #{refundAmount}, #{refundStatus}, #{createTime}, #{updateTime}
        )
    </insert>

    <update id="updateForSuccess">
        update biz_refund_record
        set wx_refund_id = #{wxRefundId},
            refund_status = #{refundStatus},
            notify_time = #{notifyTime},
            update_time = #{notifyTime}
        where refund_id = #{refundId}
          and refund_status = '0'
    </update>

    <update id="updateForFail">
        update biz_refund_record
        set refund_status = '2',
            fail_reason = #{failReason},
            update_time = now()
        where refund_id = #{refundId}
          and refund_status = '0'
    </update>
</mapper>
