<?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="progress_id" property="progressId" jdbcType="INTEGER"/>
        <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>

    <resultMap id="ProgressDTOMap" type="com.pcloud.book.personalstage.dto.PersonalStageProgressDTO">
        <association property="startText" column="startMessageId"  javaType="string" select="getContentById"/>
        <association property="endText" column="endMessageId"  javaType="string" select="getContentById"/>
    </resultMap>
    
    <sql id="Base_Column_List">
        id, progress_id,content, minutes, create_time, update_time,type
    </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
        where progress_id=#{progressId}
        ORDER BY type
    </select>

    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO personal_stage_progress_message(
            progress_id,
            content, 
            minutes,
            type,
            create_time, 
            update_time
        ) VALUES (
           #{progressId, jdbcType=INTEGER},
            #{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 (
        progress_id,
            content, 
            minutes,
            type,
            create_time, 
            update_time
        ) VALUES 
        <foreach collection="list" item="item" separator=",">
        (
            #{item.progressId, jdbcType=INTEGER},
            #{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="progressId != null">
                progress_id = #{progressId},
            </if>
            <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="map" resultType="com.pcloud.book.personalstage.entity.PersonalStageProgressMessage">
        select
        id,
        progress_id,
        content,
        minutes,
        type
        from
        personal_stage_progress_message
        where
        type = 1
        and progress_id = #{progressId}
        order by id asc
    </select>

    <select id="getWaitClickProgress" resultMap="BaseResultMap" parameterType="integer">
        select
        <include refid="Base_Column_List"/>
        from
        personal_stage_progress_message
        where type = 2
        and progress_id = #{progressId}
        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" parameterType="long">
        select count(id) from personal_stage_progress_message
        where
        type = 1
        and progress_id = #{progressId}
    </select>

    <select id="getProgressList" resultMap="ProgressDTOMap">
        select
        p.id progressId,
        p.NAME progressName,
        min(pm.id) startMessageId,
        max(pm.id) endMessageId,
        p.special_symbol specialSymbol
        from
        personal_stage_progress p
        left join personal_stage_progress_message pm
        on p.id = pm.progress_id
        where type = 1
        GROUP BY p.id
    </select>

    <select id="getContentById" resultType="string">
        select
        content
        from
        personal_stage_progress_message
        where
        id = #{_parameter}
    </select>

    <select id="getNameById" resultType="string">
        select
        name
        from
        personal_stage_progress
        where
        id = #{_parameter}
    </select>

    <delete id="deleteByProgressId" parameterType="integer">
        delete
        from
        personal_stage_progress_message
        where
        progress_id = #{progressId}
    </delete>


</mapper>