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

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageWakeup">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="personalStageId" column="personal_stage_id" jdbcType="BIGINT"/>
        <result property="toStageStartTime" column="to_stage_start_time" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, personal_stage_id, to_stage_start_time, create_time
    </sql>

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

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

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStageWakeup" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_wakeup
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_id,
            to_stage_start_time,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{personalStageId,jdbcType=BIGINT},
            #{toStageStartTime,jdbcType=INTEGER},
            NOW()
        </trim>
    </insert>

    <insert id="batchInsert"  parameterType="com.pcloud.book.personalstage.entity.PersonalStageWakeup" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_wakeup (
        personal_stage_id,
        to_stage_start_time,
        create_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.personalStageId,jdbcType=BIGINT},
            #{item.toStageStartTime,jdbcType=INTEGER},
            NOW()
            )
        </foreach>
    </insert>

    <delete id="deleteByPersonalStageId" parameterType="long">
        delete from personal_stage_wakeup
        where personal_stage_id =#{personalStageId}
    </delete>

    <select id="getListByPersonalStageId" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from personal_stage_wakeup
        where personal_stage_id=#{personalStageId}
        order by to_stage_start_time
    </select>

    <select id="getFirstWakeup" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from personal_stage_wakeup
        where personal_stage_id=#{personalStageId}
        order by to_stage_start_time
        limit 1
    </select>

</mapper>