<?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.yushu.system.mapper.SysFolderMapper">
    
    <resultMap type="SysFolder" id="SysFolderResult">
        <result property="folderId"    column="folder_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="folderName"    column="folder_name"    />
        <result property="userId"    column="user_id"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectSysFolderVo">
        select folder_id, parent_id, folder_name, user_id, sort_order, create_by, create_time, update_by, update_time, remark from sys_folder
    </sql>

    <select id="selectSysFolderList" parameterType="SysFolder" resultMap="SysFolderResult">
        <include refid="selectSysFolderVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="folderName != null  and folderName != ''"> and folder_name like concat('%', #{folderName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
        </where>
        order by sort_order, create_time desc
    </select>
    
    <select id="selectSysFolderByFolderId" parameterType="Long" resultMap="SysFolderResult">
        <include refid="selectSysFolderVo"/>
        where folder_id = #{folderId}
    </select>
        
    <insert id="insertSysFolder" parameterType="SysFolder" useGeneratedKeys="true" keyProperty="folderId">
        insert into sys_folder
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="folderName != null and folderName != ''">folder_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="sortOrder != null">sort_order,</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>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},</if>
            <if test="folderName != null and folderName != ''">#{folderName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="sortOrder != null">#{sortOrder},</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>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateSysFolder" parameterType="SysFolder">
        update sys_folder
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="folderName != null and folderName != ''">folder_name = #{folderName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where folder_id = #{folderId}
    </update>

    <delete id="deleteSysFolderByFolderId" parameterType="Long">
        delete from sys_folder where folder_id = #{folderId}
    </delete>

    <delete id="deleteSysFolderByFolderIds" parameterType="String">
        delete from sys_folder where folder_id in 
        <foreach item="folderId" collection="array" open="(" separator="," close=")">
            #{folderId}
        </foreach>
    </delete>
    
    <select id="selectChildrenCountByFolderId" parameterType="Long" resultType="int">
        select count(*) from sys_folder where parent_id = #{folderId}
    </select>

</mapper>
