<?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.pcloud.book.personalstage.dao.impl.PersonalStageProgressMessageDaoImpl">

    <resultMap id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageProgressMessage">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="content" property="content" jdbcType="VARCHAR"/>
        <result column="minutes" property="minutes" jdbcType="INTEGER"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, content, minutes, type, create_time, update_time
    </sql>

    <select id="getById" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM personal_stage_progress_message
        WHERE id = #{id}
    </select>
    
    <select id="getProgressMessageList" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM personal_stage_progress_message
        ORDER BY type
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO personal_stage_progress_message(
            content, 
            minutes,
            type,
            create_time, 
            update_time
        ) VALUES (
            #{content, jdbcType=VARCHAR},
            #{minutes, jdbcType=INTEGER},
            #{type, jdbcType=INTEGER},
            NOW(),
            NOW()
        )
    </insert>
    
    <insert id="batchInsert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO personal_stage_progress_message (
            content, 
            minutes,
            type,
            create_time, 
            update_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.content, jdbcType=VARCHAR},
            #{item.minutes, jdbcType=INTEGER},
            #{item.type, jdbcType=INTEGER},
            NOW(),
            NOW()
        )
        </foreach>
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        UPDATE personal_stage_progress_message
        <set>
            <if test="content != null and content != ''">
                content = #{content},
            </if>
            <if test="minutes != null">
                minutes = #{minutes},
            </if>
            <if test="type != null">
                type = #{type},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        WHERE id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        DELETE FROM personal_stage_progress_message where id = #{id}
    </delete>

    <delete id="deleteAll">
        DELETE FROM personal_stage_progress_message
    </delete>

    <select id="getPersonalProgress" parameterType="date" resultType="com.pcloud.book.personalstage.entity.PersonalStageProgressMessage">
        select
        id,
        content,
        minutes,
        type
        from
        personal_stage_progress_message
        where
        TIMESTAMPDIFF(MINUTE,#{startTime}, now()) &gt;= minutes
        and type = 1
        order by id asc
    </select>

    <select id="getWaitClickProgress" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from
        personal_stage_progress_message
        where type = 2
        limit 1
    </select>

    <select id="selectAllProgressMessage" resultType="com.pcloud.book.personalstage.entity.PersonalStageProgressMessage">
        select
        id,
        content,
        minutes
        from
        personal_stage_progress_message
        order by id asc
    </select>

    <select id="countProgressMessage" resultType="int">
        select count(id) from personal_stage_progress_message
    </select>
</mapper>