<?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="edu.whut.infrastructure.dao.INotifyTaskDao">

    <resultMap id="dataMap" type="edu.whut.infrastructure.dao.po.NotifyTask">
        <id column="id" property="id"/>
        <result column="activity_id" property="activityId"/>
        <result column="team_id" property="teamId"/>
        <result column="notify_url" property="notifyUrl"/>
        <result column="notify_count" property="notifyCount"/>
        <result column="notify_status" property="notifyStatus"/>
        <result column="parameter_json" property="parameterJson"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <insert id="insert" parameterType="edu.whut.infrastructure.dao.po.NotifyTask">
        insert into notify_task(activity_id, team_id, notify_url, notify_count, notify_status, parameter_json,
        create_time, update_time)
        values(#{activityId}, #{teamId}, #{notifyUrl}, #{notifyCount}, #{notifyStatus}, #{parameterJson}, now(),now())
    </insert>

    <select id="queryUnExecutedNotifyTaskList" resultMap="dataMap">
        select activity_id, team_id, notify_url, notify_count, notify_status, parameter_json
        from notify_task
        where notify_status in (0, 2)
            limit 50
    </select>

    <select id="queryUnExecutedNotifyTaskByTeamId" resultMap="dataMap">
        select activity_id, team_id, notify_url, notify_count, notify_status, parameter_json
        from notify_task
        where team_id = #{teamId} and notify_status in (0, 2)
    </select>

    <update id="updateNotifyTaskStatusSuccess" parameterType="java.lang.String">
        update notify_task
        set notify_count = notify_count + 1, notify_status = 1, update_time = now()
        where team_id = #{teamId}
    </update>

    <update id="updateNotifyTaskStatusError" parameterType="java.lang.String">
        update notify_task
        set notify_count = notify_count + 1, notify_status = 3, update_time = now()
        where team_id = #{teamId}
    </update>

    <update id="updateNotifyTaskStatusRetry" parameterType="java.lang.String">
        update notify_task
        set notify_count = notify_count + 1, notify_status = 2, update_time = now()
        where team_id = #{teamId}
    </update>

</mapper>
