<?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.pcloudkeyword.dao.impl.PcloudKeywordNameDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.pcloudkeyword.entity.PcloudKeywordName">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="pcloud_keyword_id" property="pcloudKeywordId" jdbcType="BIGINT"/>
        <result column="name" property="name" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, pcloud_keyword_id, name, create_time
    </sql>

    <delete id="deleteById" parameterType="long">
        delete from pcloud_keyword_name where id=#{id}
    </delete>

    <insert id="insert" parameterType="com.pcloud.book.pcloudkeyword.entity.PcloudKeywordName" useGeneratedKeys="true"
            keyProperty="id">
        insert into pcloud_keyword_name
        <trim prefix="(" suffix=")" suffixOverrides=",">
            pcloud_keyword_id,
            name,
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{pcloudKeywordId,jdbcType=BIGINT},
            #{name,jdbcType=VARCHAR},
            NOW()
        </trim>
    </insert>

    <!--批量插入-->
    <insert id="batchInsert"  parameterType="com.pcloud.book.pcloudkeyword.entity.PcloudKeywordName" useGeneratedKeys="true" keyProperty="id">
        insert into pcloud_keyword_name (
        pcloud_keyword_id,
        name,
        create_time
        ) values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.pcloudKeywordId,jdbcType=BIGINT},
            #{item.name,jdbcType=VARCHAR},
            NOW()
            )
        </foreach>
    </insert>

    <select id="getById" parameterType="long" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from pcloud_keyword_name
        where id=#{id}
    </select>

    <delete id="deleteByPcloudKeywordId" parameterType="long">
        delete from pcloud_keyword_name
        where pcloud_keyword_id=#{pcloudKeywordId}
    </delete>

    <select id="getByPcloudKeywordId" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from pcloud_keyword_name
        where pcloud_keyword_id=#{pcloudKeywordId}
    </select>

    <select id="getByPcloudKeywordIds" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from pcloud_keyword_name
        where pcloud_keyword_id in
        <foreach collection="pcloudKeywordIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </select>

    <delete id="deleteByKeywordIds" parameterType="map">
        delete FROM
        pcloud_keyword_name
        where pcloud_keyword_id in
        <foreach collection="pcloudKeywordIds" item="item" open="(" separator="," close=")">
            ${item}
        </foreach>
    </delete>

    <select id="getNameListByClassifyId" parameterType="map" resultType="string">
        SELECT
        n.`name`
        FROM
        pcloud_keyword_name n
        LEFT JOIN pcloud_keyword k ON n.pcloud_keyword_id = k.id
        WHERE
        k.keyword_classify_id = #{keywordClassifyId}
        <if test=" keywordId != null">
            AND k.id != #{keywordId}
        </if>
    </select>

    <select id="getByKeywordName" parameterType="map" resultMap="BaseResultMap">
        select <include refid="Base_Column_List"/>
        from pcloud_keyword_name
        where name = #{keywordName}
        limit 1
    </select>
</mapper>