<?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.fs.system.mapper.SysKeywordMapper">

    <resultMap type="SysKeyword" id="SysKeywordResult">
        <result property="keywordId"    column="keyword_id"    />
        <result property="keyword"    column="keyword"    />
        <result property="companyId"    column="company_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectSysKeyword">
        select keyword_id, keyword, company_id, create_time, update_time from sys_keyword
    </sql>

    <sql id="selectSysKeywordVo">
        SELECT
            keyword_id,
            keyword,
            sys_keyword.company_id,
            sys_keyword.create_time,
            sys_keyword.update_time,
            company.company_name
        FROM
            sys_keyword
                LEFT JOIN company ON company.company_id = sys_keyword.company_id
    </sql>

    <select id="selectSysKeywordList" parameterType="SysKeyword" resultMap="SysKeywordResult">
        <include refid="selectSysKeywordVo"/>
        <where>
            <if test="keyword != null  and keyword != ''"> and keyword like concat('%', #{keyword}, '%')</if>
            <if test="companyId != null "> and sys_keyword.company_id = #{companyId}</if>
        </where>
    </select>

    <select id="selectSysKeywordById" parameterType="Long" resultMap="SysKeywordResult">
        <include refid="selectSysKeywordVo"/>
        where keyword_id = #{keywordId}
    </select>

    <insert id="insertSysKeyword" parameterType="SysKeyword" useGeneratedKeys="true" keyProperty="keywordId">
        insert into sys_keyword
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="keyword != null">keyword,</if>
            <if test="companyId != null">company_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="keyword != null">#{keyword},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateSysKeyword" parameterType="SysKeyword">
        update sys_keyword
        <trim prefix="SET" suffixOverrides=",">
            <if test="keyword != null">keyword = #{keyword},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where keyword_id = #{keywordId}
    </update>

    <delete id="deleteSysKeywordById" parameterType="Long">
        delete from sys_keyword where keyword_id = #{keywordId}
    </delete>

    <delete id="deleteSysKeywordByIds" parameterType="String">
        delete from sys_keyword where keyword_id in
        <foreach item="keywordId" collection="keywordIds" open="(" separator="," close=")">
            #{keywordId}
        </foreach>
    </delete>


    <select id="selectSysKeywordByIds" parameterType="Long" resultMap="SysKeywordResult">
        <include refid="selectSysKeyword"/>
        where keyword_id in
        <foreach item="keywordId" collection="keywordIds" open="(" separator="," close=")">
            #{keywordId}
        </foreach>
    </select>

</mapper>
