<?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.yfd.platform.system.mapper.SysMenuMapper">
    <!-- 向上调整序号   根据父级id 序号小于原序号 并且 大于等于替换序号-->
    <update id="upMoveOrderno">
       update sys_menu set orderno=orderno+1 where parentid=#{parentid} and orderno &lt; #{Orderno} and orderno &gt;= #{upOrderno}
    </update>

    <!-- 向下调整序号    根据父级id 序号大于原序号 并且 小于等于替换序号-->
    <update id="downMoveOrderno">
        update sys_menu SET orderno=orderno-1 where parentid=#{parentid} and orderno &gt; #{Orderno} and orderno &lt;= #{downOrderno}
    </update>

    <select id="selectPermsByUserId" resultType="java.lang.String">
			SELECT
		permission
	FROM
		sys_role_users ru
		LEFT JOIN sys_role r ON ru.roleid = r.id
		LEFT JOIN sys_role_menu rm ON ru.roleid = rm.roleid
		LEFT JOIN sys_menu m ON rm.menuid = m.id
	WHERE
		r.isvaild = 1
		AND ru.userid = #{userId}
		AND permission IS NOT NULL
		AND permission != ''
		</select>

    <select id="selectMenuByUserId"
            resultType="Map">
        SELECT
        DISTINCT
        m.id,
        m.parentid,
        m.`name`,
        m.opturl,
        m.icon,
        m.orderno,
        m.type,
        m.isdisplay
        FROM
        sys_role_users ru
        JOIN sys_role r ON ru.roleid = r.id
        JOIN sys_role_menu rm ON ru.roleid = rm.roleid
        JOIN sys_menu m ON rm.menuid = m.id
        WHERE
        r.isvaild = 1
        AND ru.userid = #{userId}
        AND m.isdisplay=1
        AND m.type != 2
        ORDER BY
        m.orderno ASC
    </select>

    <select id="selectMenuByRoleId"
            resultType="java.lang.String">
		SELECT
			m.id
		FROM
		    sys_role_menu rm
			INNER JOIN sys_menu m ON rm.menuid = m.id
		WHERE
		    m.isdisplay = 1
		AND rm.roleid= #{id}
		ORDER BY
			m.orderno ASC
    </select>
    <select id="getSystemCodeById" resultType="java.lang.String">
		SELECT
		DISTINCT
			m.systemcode
		FROM
			sys_role_menu rm
			INNER JOIN sys_menu m ON rm.menuid = m.id
		WHERE
			rm.roleid = #{id}

	</select>

    <!--根据系统类型获取用户菜单-->
    <select id="selectMenuByType" resultType="java.util.Map">
         SELECT
        DISTINCT
        m.id,
        m.parentid,
        m.`name`,
        m.opturl,
        m.icon,
        m.orderno,
        m.type,
        m.isdisplay
        FROM
        sys_role_users ru
        JOIN sys_role r ON ru.roleid = r.id
        JOIN sys_role_menu rm ON ru.roleid = rm.roleid
        JOIN sys_menu m ON rm.menuid = m.id
        WHERE
        r.isvaild = 1
        AND ru.userid = #{id}
        AND m.isdisplay=1
        AND m.systemcode=#{systemCode}
        AND m.type != 2
        ORDER BY
        m.orderno ASC
    </select>
    <!--<select id="selectMenuByUserId"
            resultType="com.yfd.platform.system.domain.SysMenu">
        SELECT
            m.id,
            m.parentid,
            m.`name`,
            m.opturl,
            m.icon,
            m.orderno,
            m.type
        FROM
            sys_role_users ru
            JOIN sys_role r ON ru.roleid = r.id
            JOIN sys_role_menu rm ON ru.roleid = rm.roleid
            JOIN sys_menu m ON rm.menuid = m.id
        WHERE
            r.isvaild = 1
        AND	 ru.userid = #{userId}
            AND m.isdisplay = 1
        AND m.type != 2
        ORDER BY
            m.orderno ASC
    </select>-->
</mapper>
