<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.pcloud.book.personalstage.dao.impl.PersonalStageReplyDaoImpl">

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageReply">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="personalStageId" column="personal_stage_id" jdbcType="BIGINT"/>
        <result property="relevanceId" column="relevance_id" jdbcType="BIGINT"/>
        <result property="relevanceType" column="relevance_type" jdbcType="INTEGER"/>
        <result property="paperId" column="paper_id" jdbcType="BIGINT"/>
        <result property="paperTitle" column="paper_title" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="progressId" column="progress_id" jdbcType="TIMESTAMP"/>
    </resultMap>

    <resultMap  id="PersonalStageReplyMap"  extends="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageReply">
       <association property="progressName" column="progress_id" javaType="string"
                    select="com.pcloud.book.personalstage.dao.impl.PersonalStageProgressMessageDaoImpl.getNameById"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, name, personal_stage_id, relevance_id, relevance_type, paper_id, paper_title, create_time,progress_id
    </sql>

    <select id="getById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column_List"/>
        from personal_stage_reply
        where id = #{id,jdbcType=BIGINT}
    </select>

    <delete id="deleteById" parameterType="long">
        delete from personal_stage_reply
        where id = #{id,jdbcType=BIGINT}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStageReply" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_reply
        <trim prefix="(" suffix=")" suffixOverrides=",">
            name,
            personal_stage_id,
            relevance_id,
            relevance_type,
            paper_id,
            paper_title,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{name,jdbcType=VARCHAR},
            #{personalStageId,jdbcType=BIGINT},
            #{relevanceId,jdbcType=BIGINT},
            #{relevanceType,jdbcType=INTEGER},
            #{paperId},
            #{paperTitle},
            NOW()
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.personalstage.entity.PersonalStageReply" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_reply (
        name,
        personal_stage_id,
        relevance_id,
        relevance_type,
        paper_id,
        paper_title,
        create_time,
        progress_id
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.name,jdbcType=VARCHAR},
            #{item.personalStageId,jdbcType=BIGINT},
            #{item.relevanceId,jdbcType=BIGINT},
            #{item.relevanceType,jdbcType=INTEGER},
            #{item.paperId},
            #{item.paperTitle},
            NOW(),
            #{item.progressId}
            )
        </foreach>
    </insert>

    <select id="getIdsByPersonalStageId" parameterType="long" resultType="long">
        select id from personal_stage_reply
        where personal_stage_id=#{personalStageId}
    </select>

    <delete id="deleteByIds" parameterType="long">
        delete from personal_stage_reply
        where id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <select id="getListByPersonalStageId" parameterType="long" resultMap="PersonalStageReplyMap">
        select <include refid="Base_Column_List"/>
        from personal_stage_reply
        where personal_stage_id=#{personalStageId}
    </select>

    <select id="getIdsByRelevance" parameterType="map" resultType="long">
        select id from personal_stage_reply
        where relevance_id=#{relevanceId}
        and relevance_type=#{relevanceType}
    </select>


    <select id="personalStageReply4Order" parameterType="map" resultMap="BaseResultMap">
        select  <include refid="Base_Column_List"/>
        from personal_stage_reply
        where relevance_id=#{relevanceId}
        and relevance_type=#{relevanceType}
        order by create_time asc
        limit #{count},1
    </select>
</mapper>