<?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.keywords.dao.impl.KeywordDaoImpl">

    <insert id="insert" parameterType="keyword" useGeneratedKeys="true" keyProperty="id"
            flushCache="true">
        INSERT INTO keyword (
        guide,keywords,matching_rule, reply_type, content, description, link_url,pic_url, serve_id,
        serve_type, create_user, create_time
        )
        VALUES
        (
        #{guide, jdbcType=VARCHAR}, #{keywords, jdbcType=VARCHAR},
        #{matchingRule, jdbcType=TINYINT},#{replyType, jdbcType=TINYINT},#{content, jdbcType=VARCHAR},
        #{description, jdbcType=VARCHAR},
        #{linkUrl, jdbcType=VARCHAR},#{picUrl, jdbcType=VARCHAR} ,
        #{serveId, jdbcType=BIGINT},#{serveType, jdbcType=VARCHAR},#{createUser, jdbcType=BIGINT},now()
        )
    </insert>

    <update id="deleteById" parameterType="map">
        update keyword
        <set>
            is_delete= 1,
            update_user = #{updateUser},
            update_time = now()
        </set>
        where id = #{keywordId,jdbcType=BIGINT}
    </update>

    <update id="update" parameterType="keyword">
        update keyword
        <set>
            <if test="guide!=null">
                guide = #{guide},
            </if>
            <if test="keywords!=null">
                keywords = #{keywords},
            </if>
            <if test="matchingRule!=null">
                matching_rule = #{matchingRule},
            </if>
            <if test="replyType!=null">
                reply_type= #{replyType},
            </if>
            <if test="content!=null">
                content= #{content},
            </if>
            <if test="description!=null">
                description = #{description},
            </if>
            <if test="linkUrl!=null">
                link_url = #{linkUrl},
            </if>
            <if test="picUrl!=null">
                pic_url = #{picUrl},
            </if>
            <if test="serveId!=null">
                serve_id = #{serveId},
            </if>
            <if test="serveType!=null">
                serve_type = #{serveType},
            </if>
            <if test="updateUser!=null">
                update_user = #{updateUser},
            </if>
            update_time = now()
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

    <select id="getById" parameterType="Long" resultType="com.pcloud.book.keywords.entity.Keyword">
        select
        id ,
        guide guide,
        keywords keywords,
        matching_rule matchingRule,
        reply_type replyType,
        content content,
        description description,
        link_url linkUrl,
        pic_url picUrl,
        serve_id serveId,
        serve_type serveType,
        create_user createUser,
        create_time createTime
        from keyword
        where is_delete=0
        and id=#{id}
    </select>

    <select id="getByKeyword" resultType="com.pcloud.book.keywords.dto.ReplyKeywordDTO" parameterType="map">
        SELECT
            k.id keywordId,
            k.keywords,
            k.reply_type replyType,
            k.content,
            k.description,
            k.link_url linkUrl,
            k.pic_url picUrl,
            k.reply_type replyType
        FROM
            keyword k
        WHERE
            k.is_delete = 0
            and k.keywords = #{keyword}
        order BY id asc
        limit 1
    </select>

    <update id="updateServeInfo" parameterType="map">
        update
            keyword
        set
            content = #{title},
            pic_url = #{picUrl}
        where
            serve_id = #{serveId,jdbcType=BIGINT} and serve_type = #{serveType}
    </update>
</mapper>