<?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.xinelu.manage.mapper.termbank.TermBankMapper">

    <resultMap type="TermBank" id="TermBankResult">
        <result property="id" column="id"/>
        <result property="termCode" column="term_code"/>
        <result property="termName" column="term_name"/>
        <result property="termContent" column="term_content"/>
        <result property="parentTermCode" column="parent_term_code"/>
        <result property="termLevel" column="term_level"/>
        <result property="termSort" column="term_sort"/>
        <result property="termRemark" column="term_remark"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectTermBankVo">
        select id,
               term_code,
               term_name,
               term_content,
               parent_term_code,
               term_level,
               term_sort,
               term_remark,
               create_by,
               create_time,
               update_by,
               update_time
        from term_bank
    </sql>

    <select id="selectTermBankList" parameterType="TermBank" resultMap="TermBankResult">
        <include refid="selectTermBankVo"/>
        <where>
            <if test="termCode != null  and termCode != ''">
                and term_code = #{termCode}
            </if>
            <if test="termName != null  and termName != ''">
                and term_name like concat('%', #{termName}, '%')
            </if>
            <if test="termContent != null  and termContent != ''">
                and term_content like concat('%', #{termContent}, '%')
            </if>
            <if test="parentTermCode != null  and parentTermCode != ''">
                and parent_term_code = #{parentTermCode}
            </if>
            <if test="termLevel != null ">
                and term_level = #{termLevel}
            </if>
            <if test="termSort != null ">
                and term_sort = #{termSort}
            </if>
            <if test="termRemark != null  and termRemark != ''">
                and term_remark = #{termRemark}
            </if>
        </where>
        order by term_sort
    </select>

    <select id="selectTermBankTree" resultType="com.xinelu.manage.vo.termbank.TermBankVO">
        select id, term_code, term_content, parent_term_code, term_level, term_sort
        from term_bank
        <where>
            <if test="level != null ">
                term_level &lt;= #{level}
            </if>
        </where>
        order by term_level,term_sort
    </select>

    <select id="selectTermBankById" parameterType="Long"
            resultMap="TermBankResult">
        <include refid="selectTermBankVo"/>
        where id = #{id}
    </select>

    <insert id="insertTermBank" parameterType="TermBank" useGeneratedKeys="true"
            keyProperty="id">
        insert into term_bank
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="termCode != null and termCode != ''">term_code,
            </if>
            <if test="termName != null">term_name,
            </if>
            <if test="termContent != null">term_content,
            </if>
            <if test="parentTermCode != null">parent_term_code,
            </if>
            <if test="termLevel != null">term_level,
            </if>
            <if test="termSort != null">term_sort,
            </if>
            <if test="termRemark != null">term_remark,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="termCode != null and termCode != ''">#{termCode},
            </if>
            <if test="termName != null">#{termName},
            </if>
            <if test="termContent != null">#{termContent},
            </if>
            <if test="parentTermCode != null">#{parentTermCode},
            </if>
            <if test="termLevel != null">#{termLevel},
            </if>
            <if test="termSort != null">#{termSort},
            </if>
            <if test="termRemark != null">#{termRemark},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateTermBank" parameterType="TermBank">
        update term_bank
        <trim prefix="SET" suffixOverrides=",">
            <if test="termCode != null and termCode != ''">term_code =
                #{termCode},
            </if>
            <if test="termName != null">term_name =
                #{termName},
            </if>
            <if test="termContent != null">term_content =
                #{termContent},
            </if>
            <if test="parentTermCode != null">parent_term_code =
                #{parentTermCode},
            </if>
            <if test="termLevel != null">term_level =
                #{termLevel},
            </if>
            <if test="termSort != null">term_sort =
                #{termSort},
            </if>
            <if test="termRemark != null">term_remark =
                #{termRemark},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTermBankById" parameterType="Long">
        delete
        from term_bank
        where id = #{id}
    </delete>

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

    <select id="selectTermBankByParentTermCode" resultType="com.xinelu.manage.domain.termbank.TermBank">
        <include refid="selectTermBankVo"/>
        <where>
            <if test="parentTermCode != null  and parentTermCode != ''">
                and term_code = #{parentTermCode}
            </if>
        </where>
    </select>
</mapper>