<?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.BizPayRecordMapper">

    <resultMap id="BizPayRecordResult" type="com.ruoyi.web.modules.pay.domain.BizPayRecord">
        <id property="payId" column="pay_id"/>
        <result property="orderId" column="order_id"/>
        <result property="orderNo" column="order_no"/>
        <result property="memberId" column="member_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="payChannel" column="pay_channel"/>
        <result property="outTradeNo" column="out_trade_no"/>
        <result property="wxTransactionId" column="wx_transaction_id"/>
        <result property="prepayId" column="prepay_id"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="payStatus" column="pay_status"/>
        <result property="notifyTime" column="notify_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectVo">
        select pay_id, order_id, order_no, member_id, shop_id, pay_channel, out_trade_no,
               wx_transaction_id, prepay_id, pay_amount, pay_status, notify_time, create_time, update_time
        from biz_pay_record
    </sql>

    <select id="selectByOutTradeNo" resultMap="BizPayRecordResult">
        <include refid="selectVo"/>
        where out_trade_no = #{outTradeNo}
    </select>

    <select id="selectByWxTransactionId" resultMap="BizPayRecordResult">
        <include refid="selectVo"/>
        where wx_transaction_id = #{wxTransactionId}
    </select>

    <select id="selectLatestByOrderId" resultMap="BizPayRecordResult">
        <include refid="selectVo"/>
        where order_id = #{orderId}
        order by pay_id desc
        limit 1
    </select>

    <insert id="insert" parameterType="com.ruoyi.web.modules.pay.domain.BizPayRecord" useGeneratedKeys="true" keyProperty="payId">
        insert into biz_pay_record (
            order_id, order_no, member_id, shop_id, pay_channel, out_trade_no,
            pay_amount, pay_status, create_time, update_time
        ) values (
            #{orderId}, #{orderNo}, #{memberId}, #{shopId}, #{payChannel}, #{outTradeNo},
            #{payAmount}, #{payStatus}, #{createTime}, #{updateTime}
        )
    </insert>

    <update id="updateForPrepay" parameterType="com.ruoyi.web.modules.pay.domain.BizPayRecord">
        update biz_pay_record
        set prepay_id = #{prepayId},
            update_time = #{updateTime}
        where pay_id = #{payId}
    </update>

    <update id="updateForSuccess">
        update biz_pay_record
        set pay_status = '1',
            wx_transaction_id = #{wxTransactionId},
            notify_time = #{notifyTime},
            update_time = #{notifyTime}
        where pay_id = #{payId}
          and pay_status = '0'
    </update>
</mapper>
