<?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.SysMenuMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.diagbot.entity.wrapper.SysMenuWrapper">
        <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="is_file" property="isFile" />
        <result column="parent_id" property="parentId" />
        <result column="code" property="code" />
        <result column="order_no" property="orderNo" />
        <result column="remark" property="remark" />
    </resultMap>

    <select id="getByRole" resultMap="BaseResultMap">
        select distinct a.* from sys_menu a, sys_role_menu b, sys_user_role c
        where a.id = b.menu_id and b.role_id and b.role_id = c.role_id and c.user_id = #{userId}
            <if test="roleSet != null and roleSet.size() > 0">
                and b.role_id in
                <foreach collection="roleSet" open="(" separator="," close=")" item="item">
                    '${item}'
                </foreach>
            </if>
        and a.is_deleted = 'N' and b.is_deleted = 'N' and c.is_deleted = 'N'
        ORDER BY a.parent_id , a.order_no
    </select>

    <select id="getSubMenuById" resultMap="BaseResultMap">
        select a.* from sys_menu a where a.is_deleted = 'N' and parent_id = #{id} ORDER BY order_no
    </select>

    <select id="getByRolePermission" resultType="com.diagbot.dto.SysUserPermissionDTO">
        SELECT DISTINCT
            a.menu_id AS menuId,
            d.id AS permissionId,
            d.`name` AS NAME,
            d. CODE AS CODE
        FROM
            sys_menu_permission a,
            sys_role_permission b,
            sys_user_role c,
            sys_permission d
        WHERE
            a.permission_id = b.permission_id
        AND b.role_id = c.role_id
        AND d.id = b.permission_id
        AND c.user_id = #{userId}
        <if test="roleSet != null and roleSet.size() > 0">
            and b.role_id in
            <foreach collection="roleSet" open="(" separator="," close=")" item="item">
                '${item}'
            </foreach>
        </if>
        AND a.is_deleted = 'N'
        AND b.is_deleted = 'N'
        AND c.is_deleted = 'N'
        AND d.is_deleted = 'N'
    </select>

</mapper>
