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

    <sql id="FIELDS">
        id, transaction_id, transaction_extension_id, app_id, order_id,
        status, next_notify_time, last_execute_time, notify_times, max_notify_times,
        create_time
    </sql>

    <insert id="insert" parameterType="PayTransactionNotifyTaskDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO transaction_notify_task (
            transaction_id, transaction_extension_id, app_id, order_id,
            status, next_notify_time, notify_times, max_notify_times
        ) VALUES (
            #{transactionId}, #{transactionExtensionId}, #{appId}, #{orderId},
            #{status}, #{nextNotifyTime}, #{notifyTimes}, #{maxNotifyTimes}
        )
    </insert>

    <update id="update" parameterType="PayTransactionNotifyTaskDO">
        UPDATE transaction_notify_task
        <set>
            <if test="status != null">
                , status = #{status}
            </if>
            <if test="nextNotifyTime != null">
                , next_notify_time = #{nextNotifyTime}
            </if>
            <if test="lastExecuteTime != null">
                , last_execute_time = #{lastExecuteTime}
            </if>
            <if test="notifyTimes != null">
                , notify_times = #{notifyTimes}
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <select id="selectByNotify" resultType="PayTransactionNotifyTaskDO">
        SELECT
            <include refid="FIELDS"/>
        FROM transaction_notify_task
        WHERE status IN (1, 3, 4, 5)
        AND next_notify_time <![CDATA[ <= ]]> NOW()
        AND last_execute_time > next_notify_time
    </select>

</mapper>