<?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.ss.menu.mapper.MenuMapper">

    <resultMap type="MenuVO" id="MenuResult" autoMapping="true" />

    <sql id="selectSsMenuVo">
        select
            m.menu_id,
            m.menu_name,
            m.parent_id,
            m.order_num,
            m.visible,
            m.status,
            m.create_by,
            m.create_time,
            m.remark,
            m.icon,
            m.url
        from ss_menu m
    </sql>

    <sql id="searchCondition">
        <if test="query.menuName != null  and query.menuName != ''"> and menu_name like concat('%', #{query.menuName}, '%')</if>
        <if test="query.parentId != null "> and parent_id = #{query.parentId}</if>
        <if test="query.orderNum != null "> and order_num = #{query.orderNum}</if>
        <if test="query.visible != null  and query.visible != ''"> and visible = #{query.visible}</if>
        <if test="query.status != null  and query.status != ''"> and status = #{query.status}</if>
    </sql>

    <select id="selectSsMenuList" parameterType="MenuQuery" resultMap="MenuResult">
        <include refid="selectSsMenuVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectEtMenuListByRole" parameterType="string" resultMap="MenuResult">
        <include refid="selectSsMenuVo"/>
        where rm.role_id = #{role}
    </select>

    <select id="selectSsMenuByMenuId" parameterType="Long" resultMap="MenuResult">
        <include refid="selectSsMenuVo"/>
        where menu_id = #{menuId}
    </select>

    <insert id="insertSsMenu" parameterType="Menu" useGeneratedKeys="true" keyProperty="menuId">
        insert into ss_menu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">menu_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="visible != null">visible,</if>
            <if test="status != null">status,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="remark != null">remark,</if>
            <if test="icon != null and icon != ''">icon,</if>
            <if test="url != null and url != ''">url,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">#{menuName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="visible != null">#{visible},</if>
            <if test="status != null">#{status},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="icon != null and icon != ''">#{icon},</if>
            <if test="url != null and url != ''">#{url},</if>
         </trim>
    </insert>

    <update id="updateSsMenu" parameterType="Menu">
        update ss_menu
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where menu_id = #{data.menuId}
    </update>

    <sql id="updateColumns">
        <if test="data.menuName != null and data.menuName != ''">menu_name = #{data.menuName},</if>
        <if test="data.parentId != null">parent_id = #{data.parentId},</if>
        <if test="data.orderNum != null">order_num = #{data.orderNum},</if>
        <if test="data.visible != null">visible = #{data.visible},</if>
        <if test="data.status != null">status = #{data.status},</if>
        <if test="data.createBy != null">create_by = #{data.createBy},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.remark != null">remark = #{data.remark},</if>
        <if test="data.icon != null and data.icon != ''">icon = #{data.icon},</if>
        <if test="data.url != null and data.url != ''">url = #{data.url},</if>
    </sql>

    <delete id="deleteSsMenuByMenuId" parameterType="Long">
        delete from ss_menu where menu_id = #{menuId}
    </delete>

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