<?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.ruoyi.ie.mapper.KnowledgeTreeMapper">

    <resultMap type="KnowledgeTree" id="KnowledgeTreeResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="pid"    column="pid"    />
        <result property="subjectId"    column="subjectId"    />
        <result property="pharseId"    column="pharseId"    />
        <result property="sort"    column="sort"    />
        <result property="oldId"    column="oldId"    />
        <result property="level"    column="level"    />
        <result property="uniqueCode"    column="unique_code"    />
        <result property="videoKnowledgeId"    column="videoKnowledgeId"    />
        <result property="questionsCount"    column="questionsCount"    />
    </resultMap>

    <sql id="selectKnowledgeTreeVo">
        select id, name, pid, subjectId, pharseId, sort, oldId, level, unique_code,videoKnowledgeId,questionsCount from knowledge_tree
    </sql>

    <select id="selectKnowledgeTreeList" parameterType="KnowledgeTree" resultMap="KnowledgeTreeResult">
        select t1.id, t1.name, t1.pid, t1.subjectId, t1.pharseId, t1.sort, t1.oldId, t1.level, t1.unique_code,t1.videoKnowledgeId,t1.questionsCount from knowledge_tree t1
        <where>
            <if test="name != null  and name != ''"> and t1.name like concat('%', #{name}, '%')</if>
            <if test="pid != null "> and t1.pid = #{pid}</if>
            <if test="subjectId != null "> and t1.subjectId = #{subjectId}</if>
            <if test="pharseId != null "> and t1.pharseId = #{pharseId}</if>
            <if test="sort != null "> and t1.sort = #{sort}</if>
            <if test="oldId != null  and oldId != ''"> and t1.oldId = #{oldId}</if>
            <if test="level != null "> and t1.level = #{level}</if>
            <if test="uniqueCode != null  and uniqueCode != ''"> and t1.unique_code = #{uniqueCode}</if>
            <if test="location != null  and location != ''"> and (`location` IS NULL OR FIND_IN_SET(#{location}, `location`) &gt; 0)</if>
            <if test="examineeType != null  and examineeType != ''"> and (`examineeType` IS NULL OR FIND_IN_SET(#{examineeType}, `examineeType`) &gt; 0)</if>
        </where>
        order by sort asc,id asc
    </select>

    <select id="selectKnowledgeTreeById" parameterType="Long" resultMap="KnowledgeTreeResult">
        <include refid="selectKnowledgeTreeVo"/>
        where id = #{id}
    </select>

    <insert id="insertKnowledgeTree" parameterType="KnowledgeTree" useGeneratedKeys="true" keyProperty="id">
        insert into knowledge_tree
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="pid != null">pid,</if>
            <if test="subjectId != null">subjectId,</if>
            <if test="pharseId != null">pharseId,</if>
            <if test="sort != null">sort,</if>
            <if test="oldId != null">oldId,</if>
            <if test="level != null">level,</if>
            <if test="uniqueCode != null">unique_code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="pid != null">#{pid},</if>
            <if test="subjectId != null">#{subjectId},</if>
            <if test="pharseId != null">#{pharseId},</if>
            <if test="sort != null">#{sort},</if>
            <if test="oldId != null">#{oldId},</if>
            <if test="level != null">#{level},</if>
            <if test="uniqueCode != null">#{uniqueCode},</if>
         </trim>
    </insert>

    <update id="updateKnowledgeTree" parameterType="KnowledgeTree">
        update knowledge_tree
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="pid != null">pid = #{pid},</if>
            <if test="subjectId != null">subjectId = #{subjectId},</if>
            <if test="pharseId != null">pharseId = #{pharseId},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="oldId != null">oldId = #{oldId},</if>
            <if test="level != null">level = #{level},</if>
            <if test="uniqueCode != null">unique_code = #{uniqueCode},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKnowledgeTreeById" parameterType="Long">
        delete from knowledge_tree where id = #{id}
    </delete>

    <delete id="deleteKnowledgeTreeByIds" parameterType="String">
        delete from knowledge_tree where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
