<?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.kaohe.mapper.KhTemplateMapper">

    <resultMap type="KhTemplate" id="KhTemplateResult">
        <result property="id" column="id"/>
        <result property="temName" column="tem_name"/>
        <collection property="items" ofType="KhItems " javaType="ArrayList" select="getItems" column="id"/>
    </resultMap>

    <resultMap id="KhItemsResult" type="KhItems">
        <result property="id" column="itemId"/>
        <result property="itemName" column="khitemName"/>
        <result property="type" column="type"/>
        <result property="typeId" column="type_id"/>
    </resultMap>

    <sql id="selectKhTemplateVo">
        select id, tem_name, khitem_id, item_name
        from kh_template
    </sql>

    <select id="selectKhTemplateList" parameterType="KhTemplate" resultMap="KhTemplateResult">
        /*select t.*,ti.khitem_id itemId ,ti.item_name khitemName from kh_template t left join kh_tem_item ti on
        t.id=ti.tem_id*/
        <include refid="selectKhTemplateVo"/>
        <where>
            <if test="temName != null  and temName != ''">and tem_name like concat('%', #{temName}, '%')</if>
        </where>
    </select>

    <select id="getItems" resultMap="KhItemsResult">
        select ti.khitem_id itemId ,ti.item_name khitemName ,type,type_id from kh_tem_item ti
        <where>
            <if test="id != null ">ti.tem_id = #{id}</if>
        </where>
    </select>

    <select id="selectKhTemplateById" parameterType="Long" resultMap="KhTemplateResult">
        <include refid="selectKhTemplateVo"/>
        where id = #{id}
    </select>

    <insert id="insertKhTemplate" parameterType="KhTemplate" useGeneratedKeys="true" keyProperty="id">
        insert into kh_template
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="temName != null and temName != ''">tem_name,</if>
            <if test="khitemId != null">khitem_id,</if>
            <if test="itemName != null and itemName != ''">item_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="temName != null and temName != ''">#{temName},</if>
            <if test="khitemId != null">#{khitemId},</if>
            <if test="itemName != null and itemName != ''">#{itemName},</if>
        </trim>
    </insert>

    <update id="updateKhTemplate" parameterType="KhTemplate">
        update kh_template
        <trim prefix="SET" suffixOverrides=",">
            <if test="temName != null and temName != ''">tem_name = #{temName},</if>
            <if test="khitemId != null">khitem_id = #{khitemId},</if>
            <if test="itemName != null and itemName != ''">item_name = #{itemName},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteKhTemplateById" parameterType="Long">
        delete
        from kh_template
        where id = #{id}
    </delete>

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