<?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="cn.iocoder.mall.pay.biz.dao.PayRefundMapper">

    <sql id="FIELDS">
        id, transaction_id, refund_code, app_id, create_ip, order_id,
        order_description, price, status,
        finish_time, notify_url, extension_data, refund_channel, refund_time, notify_time,
        trade_no, create_time
    </sql>

    <insert id="insert" parameterType="PayRefundDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO refund (
            transaction_id, refund_code, app_id, create_ip, order_id,
            order_description, price, status,
            finish_time, notify_url, extension_data, refund_channel, refund_time, notify_time,
            trade_no, create_time
        ) VALUES (
            #{transactionId}, #{refundCode}, #{appId}, #{createIp}, #{orderId},
            #{orderDescription}, #{price}, #{status},
            #{finishTime}, #{notifyUrl}, #{extensionData}, #{refundChannel}, #{refundTime}, #{notifyTime},
            #{tradeNo}, #{createTime}
        )
    </insert>

    <update id="update">
        UPDATE refund
        <set>
            <if test="entity.status != null">
                , status = #{entity.status}
            </if>
            <if test="entity.finishTime != null">
                , finish_time = #{entity.finishTime}
            </if>
            <if test="entity.extensionData != null">
                , extension_data = #{entity.extensionData}
            </if>
            <if test="entity.refundTime != null">
                , refund_time = #{entity.refundTime}
            </if>
            <if test="entity.notifyTime != null">
                , notify_time = #{entity.notifyTime}
            </if>
            <if test="entity.tradeNo != null">
                , trade_no = #{entity.tradeNo}
            </if>
        </set>
        WHERE id = #{entity.id}
        <if test="whereStatus != null">
            AND status = #{whereStatus}
        </if>
    </update>

    <select id="selectByRefundCode" parameterType="String" resultType="PayRefundDO">
        SELECT
            <include refid="FIELDS"/>
        FROM refund
        WHERE refund_code = #{refundCode}
        LIMIT 1
    </select>

    <select id="selectById" parameterType="Integer" resultType="PayRefundDO">
        SELECT
            <include refid="FIELDS"/>
        FROM refund
        WHERE id = #{id}
    </select>

    <select id="selectListByPage" resultType="PayRefundDO">
        SELECT
            <include refid="FIELDS"/>
        FROM refund
        <where>
            <if test="createBeginTime != null">
                AND create_time >= #{createBeginTime}
            </if>
            <if test="createEndTime != null">
                AND #{createEndTime} >= create_time
            </if>
            <if test="finishBeginTime != null">
                AND finish_time >= #{finishBeginTime}
            </if>
            <if test="finishEndTime != null">
                AND #{finishEndTime} >= finish_time
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="payChannel != null">
                AND pay_channel = #{payChannel}
            </if>
        </where>
        LIMIT #{offset}, #{limit}
    </select>

    <select id="selectCountByPage" resultType="Integer">
        SELECT
            COUNT(1)
        FROM refund
        <where>
            <if test="createBeginTime != null">
                AND create_time >= #{createBeginTime}
            </if>
            <if test="createEndTime != null">
                AND #{createEndTime} >= create_time
            </if>
            <if test="finishBeginTime != null">
                AND finish_time >= #{finishBeginTime}
            </if>
            <if test="finishEndTime != null">
                AND #{finishEndTime} >= finish_time
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="payChannel != null">
                AND pay_channel = #{payChannel}
            </if>
        </where>
    </select>

</mapper>
