<?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.diagbot.mapper.SysRoleMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.SysRole">
        <id column="id" property="id" />
        <result column="is_deleted" property="isDeleted" />
        <result column="gmt_create" property="gmtCreate" />
        <result column="gmt_modified" property="gmtModified" />
        <result column="creator" property="creator" />
        <result column="modifier" property="modifier" />
        <result column="name" property="name" />
        <result column="roleLevel" property="roleLevel" />
        <result column="descritpion" property="descritpion" />
        <result column="menuItems" property="menuItems" />
        <result column="remark" property="remark" />
    </resultMap>

    <select id="getMenuPermission" parameterType="com.diagbot.vo.SysRoleMenuQueryVO" resultType="com.diagbot.dto.SysMenuPermissionDTO">
        SELECT DISTINCT
            t4.menuId AS menuId,
            t4.menuName AS menuName,
            t4.parentId AS parentId,
            t4.permissionId AS permissionId,
            t4.permissionName AS permissionName,
            t4.orderNo AS orderNo,
            IF (IFNULL(t5.id, 0) = 0, 0, 1) AS haveMenu,
            IF (IFNULL(t6.id, 0) = 0, 0, 1) AS havePermission
        FROM
            (
                SELECT
                    t2.id AS menuId,
                    t2.`name` AS menuName,
                    t2.parent_id AS parentId,
                    t3.id AS permissionId,
                    t3.`name` AS permissionName,
                    t2.order_no AS orderNo,
                    t1.order_nu AS orderNoPermissionId,
                    t1.id AS menuPermissionId
                FROM
                    sys_menu t2
                LEFT JOIN sys_menu_permission t1 ON t1.is_deleted = 'N'
                AND t1.menu_id = t2.id
                LEFT JOIN sys_permission t3 ON t3.is_deleted = 'N'
                AND t1.permission_id = t3.id
                WHERE
                    t2.is_deleted = 'N'
                AND t2.maintain_status = 1
            ) t4
        LEFT JOIN sys_role_menu t5 ON t5.is_deleted = 'N'
        AND t5.menu_id = t4.menuId
        AND t5.role_id = #{roleId}
        LEFT JOIN sys_role_permission t6 ON t6.is_deleted = 'N'
        AND t6.permission_id = t4.permissionId
        AND t6.role_id = #{roleId}
        ORDER BY
            orderNo ASC,menuId ASC,orderNoPermissionId IS NULL ASC,orderNoPermissionId ASC, menuPermissionId ASC
     </select>
</mapper>
