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

    <resultMap  id="BaseResultMap" type="com.pcloud.book.personalstage.entity.PersonalStageJumpKeyword">
        <id property="id" column="id" jdbcType="BIGINT"/>
        <result property="personalStageId" column="personal_stage_id" jdbcType="BIGINT"/>
        <result property="personalStageJumpId" column="personal_stage_jump_id" jdbcType="BIGINT"/>
        <result property="keyword" column="keyword" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
    </resultMap>

    <sql id="Base_Column_List">
        id, personal_stage_id, personal_stage_jump_id, keyword, create_time
    </sql>

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

    <select id="listBy" resultMap="BaseResultMap" parameterType="map">
        select
        <include refid="Base_Column_List"/>
        from personal_stage_jump_keyword
        where personal_stage_jump_id IN
        <foreach collection="jumpIds" item="jumpId" separator="," open="(" close=")">
            ${jumpId}
        </foreach>
    </select>

    <select id="getByKeyword" resultType="com.pcloud.book.personalstage.dto.PersonalStageJumpKeywordDto" parameterType="map">
        SELECT
        a.id, a.personal_stage_id personalStageId, a.personal_stage_jump_id personalStageJumpId,
        a.keyword, b.jump_type jumpType, b.after_personal_stage_id afterPersonalStageId
        FROM personal_stage_jump_keyword a,personal_stage_jump b
        WHERE a.personal_stage_jump_id = b.id AND a.keyword = #{keyword,jdbcType=VARCHAR}
        AND a.personal_stage_id = #{personalStageId,jdbcType=BIGINT}
        AND b.jump_type = #{jumpType,jdbcType=INTEGER}
        LIMIT 0,1
    </select>

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

    <delete id="deleteByJumpId" parameterType="long">
        delete from personal_stage_jump_keyword
        where personal_stage_jump_id = #{jumpId,jdbcType=BIGINT}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.personalstage.entity.PersonalStageJumpKeyword" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_jump_keyword
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_id,
            personal_stage_jump_id,
            keyword,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{personalStageId, jdbcType=BIGINT},
            #{personalStageJumpId,jdbcType=BIGINT},
            #{keyword,jdbcType=VARCHAR},
            NOW()
        </trim>
    </insert>

    <insert id="batchInsert" parameterType="list" useGeneratedKeys="true" keyProperty="id">
        insert into personal_stage_jump_keyword
        <trim prefix="(" suffix=")" suffixOverrides=",">
            personal_stage_id,
            personal_stage_jump_id,
            keyword,
            create_time
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                #{item.personalStageId, jdbcType=BIGINT},
                #{item.personalStageJumpId,jdbcType=BIGINT},
                #{item.keyword,jdbcType=VARCHAR},
                NOW()
            </trim>
        </foreach>
    </insert>

    <select id="getStageKeywordByKeywords" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/> from personal_stage_jump_keyword
        where personal_stage_id = #{personalStageId}
        and keyword in
        <foreach collection="keywords" item="item" open="(" close=")" separator=",">
            '${item}'
        </foreach>
        limit 1
    </select>

    <select id="getFirstKeywordByPersonalStageId" parameterType="map" resultType="string">
        select keyword from personal_stage_jump_keyword where
        personal_stage_id=#{personalStageId}
        order by id asc
        limit 1
    </select>
</mapper>