<?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.PcloudKeywordDaoImpl">
    <resultMap id="BaseResultMap" type="com.pcloud.book.pcloudkeyword.entity.PcloudKeyword">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="keyword_classify_id" property="keywordClassifyId" jdbcType="BIGINT"/>
        <result column="type" property="type" jdbcType="INTEGER"/>
        <result column="method" property="method" jdbcType="INTEGER"/>
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
        <result column="skill_ids" property="skillIds" jdbcType="VARCHAR"/>
    </resultMap>


    <sql id="Base_Column_List">
        id, keyword_classify_id, type,method, create_time, skill_ids
    </sql>

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

    <insert id="insert" parameterType="com.pcloud.book.pcloudkeyword.entity.PcloudKeyword" useGeneratedKeys="true"
            keyProperty="id">
        insert into pcloud_keyword
        <trim prefix="(" suffix=")" suffixOverrides=",">
            keyword_classify_id,
            type,
            method,
            create_time,
            skill_ids
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            #{keywordClassifyId,jdbcType=BIGINT},
            #{type,jdbcType=INTEGER},
            #{method,jdbcType=INTEGER},
            NOW(),
            #{skillIds}
        </trim>
    </insert>

    <update id="update" parameterType="com.pcloud.book.pcloudkeyword.entity.PcloudKeyword">
        update pcloud_keyword
        <set>
            <if test="keywordClassifyId != null">
                keyword_classify_id = #{keywordClassifyId,jdbcType=BIGINT},
            </if>
            <if test="type != null">
                type = #{type,jdbcType=INTEGER},
            </if>
            <if test="method != null">
                method = #{method,jdbcType=INTEGER},
            </if>
            skill_ids = #{skillIds},
        </set>
        where id = #{id,jdbcType=BIGINT}
    </update>

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

    <select id="getPcloudKeywordList" parameterType="map" resultMap="BaseResultMap">
        select
        t1.id, t1.keyword_classify_id, t1.type,t1.method, t1.create_time, t1.skill_ids
        from pcloud_keyword t1
        inner join pcloud_keyword_name t2 on t1.id=t2.pcloud_keyword_id
        where t1.keyword_classify_id = #{keywordClassifyId,jdbcType=BIGINT}
        <if test="name!=null">
            and t2.name like concat('%', #{name}, '%')
        </if>
        group by t1.id
    </select>

    <select id="getIdsByClassifyId" parameterType="long" resultType="long">
        SELECT
        id
        FROM
        pcloud_keyword
        WHERE
        keyword_classify_id = #{keywordClassifyId,jdbcType=BIGINT}
    </select>

    <delete id="deleteByClassifyId" parameterType="long">
        delete FROM
        pcloud_keyword
        WHERE
        keyword_classify_id = #{keywordClassifyId,jdbcType=BIGINT}
    </delete>

    <select id="getByClassifyIdAndName" parameterType="map" resultMap="BaseResultMap">
        select  t1.id, t1.keyword_classify_id, t1.type,t1.method, t1.create_time, t1.skill_ids
        from pcloud_keyword t1
        inner join pcloud_keyword_name t2 on t1.id=t2.pcloud_keyword_id
        where t1.keyword_classify_id=#{pcloudClassifyId}
        and t1.type=#{type}
        <if test="type==1">
            and t2.name=#{name}
        </if>
        <if test="type==2">
            and t2.name like concat('%', #{name}, '%')
        </if>
        group by t1.id
        limit 1
    </select>

    <select id="listAllBlurryKeyword" parameterType="long" resultMap="BaseResultMap">
        select  t1.id, t1.keyword_classify_id, t1.type,t1.method, t1.create_time, t1.skill_ids
        from pcloud_keyword t1
        inner join pcloud_keyword_name t2 on t1.id=t2.pcloud_keyword_id
        where t1.keyword_classify_id=#{pcloudClassifyId}
        and t1.type=2
        group by t1.id
    </select>

    <select id="skillExist" resultType="Boolean" parameterType="map">
        SELECT
        EXISTS (
        SELECT
        id
        FROM
        pcloud_keyword
        WHERE
        skill_ids = #{skillId}
        )
    </select>
</mapper>