<?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.generator.mapper.SysProjectStructureMapper">
    
    <resultMap type="SysProjectStructure" id="SysProjectStructureResult">
        <result property="nodeId"    column="node_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="nodeName"    column="node_name"    />
        <result property="nodeType"    column="node_type"    />
        <result property="module"    column="module"    />
        <result property="templateId"    column="template_id"    />
        <result property="tableId"    column="table_id"    />
        <result property="category"    column="category"    />
        <result property="sortOrder"    column="sort_order"    />
        <result property="status"    column="status"    />
        <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="selectSysProjectStructureVo">
        select node_id, parent_id, node_name, node_type, module, template_id, table_id, category, sort_order, status, create_by, create_time, update_by, update_time from sys_project_structure
    </sql>

    <select id="selectSysProjectStructureList" parameterType="SysProjectStructure" resultMap="SysProjectStructureResult">
        <include refid="selectSysProjectStructureVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="nodeName != null  and nodeName != ''"> and node_name like concat('%', #{nodeName}, '%')</if>
            <if test="nodeType != null  and nodeType != ''"> and node_type = #{nodeType}</if>
            <if test="module != null  and module != ''"> and module = #{module}</if>
            <if test="templateId != null "> and template_id = #{templateId}</if>
            <if test="tableId != null "> and table_id = #{tableId}</if>
            <if test="category != null  and category != ''"> and category = #{category}</if>
            <if test="sortOrder != null "> and sort_order = #{sortOrder}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
        order by sort_order
    </select>
    
    <select id="selectSysProjectStructureByNodeId" parameterType="Long" resultMap="SysProjectStructureResult">
        <include refid="selectSysProjectStructureVo"/>
        where node_id = #{nodeId}
    </select>

    <select id="selectProjectStructureListByModule" resultMap="SysProjectStructureResult">
        select s.node_id, s.parent_id, s.node_name, s.node_type, s.module, s.template_id, s.table_id, 
               s.category, s.sort_order, s.status, s.create_by, s.create_time, s.update_by, s.update_time
        from sys_project_structure s
        left join sys_project_module pm on s.module = pm.module_id and pm.project_id = #{projectId}
        where (s.module is null or s.module = '' or pm.module_id is not null)
        and s.template_id = #{templateId}
        order by s.sort_order
    </select>

    <insert id="insertSysProjectStructure" parameterType="SysProjectStructure" useGeneratedKeys="true" keyProperty="nodeId">
        insert into sys_project_structure
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="nodeName != null">node_name,</if>
            <if test="nodeType != null">node_type,</if>
            <if test="module != null">module,</if>
            <if test="templateId != null">template_id,</if>
            <if test="tableId != null">table_id,</if>
            <if test="category != null">category,</if>
            <if test="sortOrder != null">sort_order,</if>
            <if test="status != null">status,</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="parentId != null">#{parentId},</if>
            <if test="nodeName != null">#{nodeName},</if>
            <if test="nodeType != null">#{nodeType},</if>
            <if test="module != null">#{module},</if>
            <if test="templateId != null">#{templateId},</if>
            <if test="tableId != null">#{tableId},</if>
            <if test="category != null">#{category},</if>
            <if test="sortOrder != null">#{sortOrder},</if>
            <if test="status != null">#{status},</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="updateSysProjectStructure" parameterType="SysProjectStructure">
        update sys_project_structure
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="nodeName != null">node_name = #{nodeName},</if>
            <if test="nodeType != null">node_type = #{nodeType},</if>
            <if test="module != null">module = #{module},</if>
            <if test="templateId != null">template_id = #{templateId},</if>
            <if test="tableId != null">table_id = #{tableId},</if>
            <if test="category != null">category = #{category},</if>
            <if test="sortOrder != null">sort_order = #{sortOrder},</if>
            <if test="status != null">status = #{status},</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 node_id = #{nodeId}
    </update>

    <delete id="deleteSysProjectStructureByNodeId" parameterType="Long">
        delete from sys_project_structure where node_id = #{nodeId}
    </delete>

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