<?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.manage.dao.User_Dept_MenuMapper" >
    <resultMap id="BaseResultMap" type="com.manage.vo.User_Dept_Menu" >
        <result column="sys_id" property="sysId" jdbcType="INTEGER" />
        <result column="hospital_name" property="hospitalName" jdbcType="VARCHAR" />
        <result column="dept_id" property="deptId" jdbcType="VARCHAR" />
        <result column="dept_name" property="deptName" jdbcType="VARCHAR" />
        <result column="dept_code" property="deptCode" jdbcType="VARCHAR" />
        <result column="sys_flag" property="sysFlag" jdbcType="VARCHAR" />
        <result column="sys_name" property="sysName" jdbcType="VARCHAR" />
        <result column="hospital_id" property="hospitalId" jdbcType="INTEGER" />
        <result column="sys_type" property="sysType" jdbcType="VARCHAR" />
        <result column="sort" property="sort" jdbcType="INTEGER" />
        <result column="user_id" property="userId" jdbcType="INTEGER" />
        <result column="user_name" property="userName" jdbcType="VARCHAR" />
        <result column="user_tel" property="userTel" jdbcType="VARCHAR" />
        <result column="user_email" property="userEmail" jdbcType="VARCHAR" />
        <result column="role_id" property="roleId" jdbcType="INTEGER" />
        <result column="role_name" property="roleName" jdbcType="VARCHAR" />
        <result column="menu_id" property="menuId" jdbcType="INTEGER" />
        <result column="menu_name" property="menuName" jdbcType="VARCHAR" />
        <result column="menu_icon" property="menuIcon" jdbcType="VARCHAR" />
        <result column="menu_url" property="menuUrl" jdbcType="VARCHAR" />
        <result column="method" property="method" jdbcType="VARCHAR" />
        <result column="method_type" property="methodType" jdbcType="VARCHAR" />
        <result column="method_parent" property="methodParent" jdbcType="INTEGER" />
        <result column="method_sort" property="methodSort" jdbcType="INTEGER" />
    </resultMap>
    <select id="selectAll" resultMap="BaseResultMap">
        <!--查询非管理员的非权限系统的权限菜单  开始-->
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据  开始-->
        select user_menu.* from (SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        power_dept.dept_id,
        dept_name,
        power_dept.dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_role_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_user
        LEFT JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        LEFT JOIN power_sys_dict ON power_dept.dept_id = power_sys_dict.dept_id
        LEFT JOIN power_sys_dict hospital ON power_dept.dict_id = hospital.dict_id
        INNER JOIN power_role_menu ON power_role.role_id = power_role_menu.role_id
        LEFT JOIN power_menu ON power_menu.menu_id = power_role_menu.menu_id
        AND power_sys_dict.sys_flag = power_menu.sys_flag
        LEFT JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_menu.menu_id = power_user_menu.menu_id
        WHERE
        <if test="userName != null and userName != ''">
            power_user.user_name = #{userName} AND
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            power_menu.sys_flag = #{sysFlag} AND
        </if>
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="roleId != null">
            power_role.role_id = #{roleId} AND
        </if>
        <if test="sysId != null">
            power_sys_dict.dict_id = #{sysId} AND
        </if>
        <if test="hospitalId != null">
            hospital.dict_id = #{hospitalId} AND
        </if>
        power_menu.sys_flag != 'power'
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND power_user_menu.flag IS NULL
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_dept.effective = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据  结束-->
        <!--拼接非管理员的非权限系统power_user_menu的flag为1的菜单数据  开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        power_dept.dept_id,
        dept_name,
        power_dept.dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_user
        INNER JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        INNER JOIN power_sys_dict hospital ON power_dept.dict_id = hospital.dict_id
        INNER JOIN power_sys_dict ON power_dept.dept_id = power_sys_dict.dept_id
        INNER JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_user_menu.flag = 1
        INNER JOIN power_menu ON power_menu.menu_id = power_user_menu.menu_id
        AND power_menu.sys_flag = power_sys_dict.sys_flag
        WHERE
        <if test="userName != null and userName != ''">
            power_user.user_name = #{userName} AND
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            power_menu.sys_flag = #{sysFlag} AND
        </if>
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="roleId != null">
            power_role.role_id = #{roleId} AND
        </if>
        <if test="sysId != null">
            power_sys_dict.dict_id = #{sysId} AND
        </if>
        <if test="hospitalId != null">
            hospital.dict_id = #{hospitalId} AND
        </if>
        power_menu.sys_flag != 'power'
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_dept.effective = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--拼接非管理员的非权限系统power_user_menu的flag为1的菜单数据  开始-->
        <!--查询非管理员的非权限系统的权限菜单  结束-->

        <!--查询非管理员的权限系统的权限菜单  开始-->
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据   开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_sys_dict
        LEFT JOIN power_sys_dict hospital ON power_sys_dict.parent_id = hospital.dict_id,
        power_user
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_role_menu ON power_role.role_id = power_role_menu.role_id
        LEFT JOIN power_menu ON power_menu.menu_id = power_role_menu.menu_id
        LEFT JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_menu.menu_id = power_user_menu.menu_id
        WHERE
        power_user.user_name = @userName
        AND power_menu.sys_flag = @sysFlag
        AND power_sys_dict.sys_flag = 'power'
        AND power_menu.sys_flag = 'power'
        AND power_user_menu.flag IS NULL
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据  结束-->

        <!--拼接非管理员的权限系统power_user_menu的flag为1的菜单数据  开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_sys_dict
        INNER JOIN power_sys_dict hospital ON power_sys_dict.parent_id = hospital.dict_id,
        power_user
        INNER JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        INNER JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_user_menu.flag = 1
        INNER JOIN power_menu ON power_menu.menu_id = power_user_menu.menu_id
        WHERE
        <if test="userName != null and userName != ''">
            power_user.user_name = #{userName} AND
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            power_menu.sys_flag = #{sysFlag} AND
        </if>
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="roleId != null">
            power_role.role_id = #{roleId} AND
        </if>
        <if test="sysId != null">
            power_sys_dict.dict_id = #{sysId} AND
        </if>
        <if test="hospitalId != null">
            hospital.dict_id = #{hospitalId} AND
        </if>
        power_sys_dict.sys_flag = 'power'
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND power_menu.sys_flag = 'power'
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--拼接非管理员的权限系统power_user_menu的flag为1的菜单数据  结束-->
        <!--查询非管理员的权限系统的权限菜单  结束-->

        <!--查询医院管理员的非权限系统的权限菜单开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        power_dept.dept_id,
        dept_name,
        power_dept.dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_user
        LEFT JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        LEFT JOIN power_sys_dict ON power_dept.dept_id = power_sys_dict.dept_id
        LEFT JOIN power_sys_dict hospital ON power_dept.dict_id = hospital.dict_id
        INNER JOIN power_menu ON power_menu.sys_flag IN (
        SELECT
        power_sys_dict.sys_flag
        FROM
        power_dept
        INNER JOIN power_user ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_sys_dict ON power_dept.dict_id = power_sys_dict.parent_id
        <where>
            <if test="userId != null">
                AND power_user.user_id = #{userId}
            </if>
            <if test="userName != null and userName != ''">
                AND power_user.user_name = #{userName}
            </if>
            <if test="roleId != null">
                AND power_user.role_id = #{roleId}
            </if>
        </where>
        GROUP BY
        power_sys_dict.sys_flag
        )
        WHERE
        <if test="userName != null and userName != ''">
            power_user.user_name = #{userName} AND
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            power_menu.sys_flag = #{sysFlag} AND
            <if test="roleId != null and  roleId == -100">
                power_sys_dict.sys_flag = #{sysFlag} AND
            </if>
        </if>
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="roleId != null">
            power_role.role_id = #{roleId} AND
        </if>
        <if test="sysId != null">
            power_sys_dict.dict_id = #{sysId} AND
        </if>
        <if test="hospitalId != null">
            hospital.dict_id = #{hospitalId} AND
        </if>
        power_menu.sys_flag != 'power'
        AND power_user.role_id = - 100
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_dept.effective = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询医院管理员的非权限系统的权限菜单  结束-->

        <!--查询医院管理员的权限系统的权限菜单  开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_sys_dict
        INNER JOIN power_sys_dict hospital ON power_sys_dict.parent_id = hospital.dict_id,
        power_user
        INNER JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_role ON power_user.role_id = power_role.role_id,
        power_menu
        WHERE
        <if test="userName != null and userName != ''">
            power_user.user_name = #{userName} AND
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            power_menu.sys_flag = #{sysFlag} AND
        </if>
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="roleId != null">
            power_role.role_id = #{roleId} AND
        </if>
        <if test="sysId != null">
            power_sys_dict.dict_id = #{sysId} AND
        </if>
        <if test="hospitalId != null">
            hospital.dict_id = #{hospitalId} AND
        </if>
        power_sys_dict.sys_flag = 'power'
        AND power_user.role_id = - 100
        AND power_menu.sys_flag = 'power'
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询医院管理员的权限系统的权限菜单  结束-->

        <!--查询系统管理员所有菜单  开始-->
        UNION ALL
        SELECT
        NULL AS sys_id,
        NULL AS hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_menu.sys_flag,
        NULL AS sys_name,
        NULL AS hospital_id,
        NULL AS sys_type,
        NULL AS sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        null as hospital_sort
        FROM
        power_user
        INNER JOIN power_role ON power_user.role_id = power_role.role_id,
        power_menu
        WHERE
        <if test="userName != null and userName != ''">
            power_user.user_name = #{userName} AND
        </if>
        <if test="sysFlag != null and sysFlag != ''">
            power_menu.sys_flag = #{sysFlag} AND
        </if>
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="roleId != null">
            power_role.role_id = #{roleId} AND
        </if>
        power_user.role_id = 0
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询系统管理员所有菜单  结束-->

        <!--补充未创建科室的医院数据-->
        <if test="userId == null and userName == null and roleId == null and (sysId != null || sysFlag != null || hospitalId != null)">
            UNION ALL
            SELECT
            NULL AS sys_id,
            hospital.hospital_name,
            NULL AS dept_id,
            NULL AS dept_name,
            NULL AS dept_code,
            NULL AS sys_flag,
            NULL AS sys_name,
            hospital.dict_id hospital_id,
            NULL AS sys_type,
            NULL AS sort,
            NULL AS user_id,
            NULL AS user_name,
            NULL AS user_tel,
            NULL AS user_email,
            NULL AS role_id,
            NULL AS role_name,
            NULL AS menu_id,
            NULL AS menu_name,
            NULL AS menu_icon,
            NULL AS menu_url,
            NULL AS method,
            NULL AS method_type,
            NULL AS method_parent,
            NULL AS method_sort,
            hospital.sort hospital_sort
            FROM
            power_sys_dict hospital
            WHERE
            hospital.dict_id NOT IN (
            SELECT
            dict_id
            FROM
            power_dept
            GROUP BY
            dict_id
            )
            AND hospital.hospital_name IS NOT NULL AND hospital.dict_status = 1
            UNION ALL
            SELECT
            power_sys_dict.dict_id sys_id,
            hospital.hospital_name,
            NULL AS dept_id,
            NULL AS dept_name,
            NULL AS dept_code,
            power_sys_dict.sys_flag,
            power_sys_dict.sys_name,
            hospital.dict_id hospital_id,
            power_sys_dict.sys_type,
            power_sys_dict.sort,
            NULL AS user_id,
            NULL AS user_name,
            NULL AS user_tel,
            NULL AS user_email,
            NULL AS role_id,
            NULL AS role_name,
            NULL AS menu_id,
            NULL AS menu_name,
            NULL AS menu_icon,
            NULL AS menu_url,
            NULL AS method,
            NULL AS method_type,
            NULL AS method_parent,
            NULL AS method_sort,
            hospital.sort hospital_sort
            FROM
            power_sys_dict hospital
            LEFT JOIN power_sys_dict ON hospital.dict_id = power_sys_dict.parent_id
            WHERE
            hospital.dict_id NOT IN (
            SELECT
            dict_id
            FROM
            power_dept
            GROUP BY
            dict_id
            )
            AND power_sys_dict.sys_flag = 'power' AND hospital.dict_status = 1 AND power_sys_dict.dict_status = 1
        </if>
        ) user_menu
        ORDER BY user_menu.hospital_sort,user_menu.sort,user_menu.method_sort
    </select>

    <select id="validUserRoleMenu" resultMap="BaseResultMap">
        <!--查询非管理员的非权限系统的权限菜单  开始-->
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据  开始-->
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        power_dept.dept_id,
        dept_name,
        power_dept.dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_role_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_user
        LEFT JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        LEFT JOIN power_sys_dict ON power_dept.dept_id = power_sys_dict.dept_id
        LEFT JOIN power_sys_dict hospital ON power_dept.dict_id = hospital.dict_id
        INNER JOIN power_role_menu ON power_role.role_id = power_role_menu.role_id
        LEFT JOIN power_menu ON power_menu.menu_id = power_role_menu.menu_id
        AND power_sys_dict.sys_flag = power_menu.sys_flag
        LEFT JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_menu.menu_id = power_user_menu.menu_id
        WHERE
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="url != null and url != ''">
            power_menu.menu_url = #{url} AND
        </if>
        power_menu.sys_flag != 'power'
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND power_user_menu.flag IS NULL
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_dept.effective = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据  结束-->
        <!--拼接非管理员的非权限系统power_user_menu的flag为1的菜单数据  开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        power_dept.dept_id,
        dept_name,
        power_dept.dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_user
        INNER JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        INNER JOIN power_sys_dict hospital ON power_dept.dict_id = hospital.dict_id
        INNER JOIN power_sys_dict ON power_dept.dept_id = power_sys_dict.dept_id
        INNER JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_user_menu.flag = 1
        INNER JOIN power_menu ON power_menu.menu_id = power_user_menu.menu_id
        AND power_menu.sys_flag = power_sys_dict.sys_flag
        WHERE
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="url != null and url != ''">
            power_menu.menu_url = #{url} AND
        </if>
        power_menu.sys_flag != 'power'
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_dept.effective = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--拼接非管理员的非权限系统power_user_menu的flag为1的菜单数据  开始-->
        <!--查询非管理员的非权限系统的权限菜单  结束-->

        <!--查询非管理员的权限系统的权限菜单  开始-->
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据   开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_sys_dict
        LEFT JOIN power_sys_dict hospital ON power_sys_dict.parent_id = hospital.dict_id,
        power_user
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_role_menu ON power_role.role_id = power_role_menu.role_id
        LEFT JOIN power_menu ON power_menu.menu_id = power_role_menu.menu_id
        LEFT JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_menu.menu_id = power_user_menu.menu_id
        WHERE
        power_user.user_name = @userName
        AND power_menu.sys_flag = @sysFlag
        AND power_sys_dict.sys_flag = 'power'
        AND power_menu.sys_flag = 'power'
        AND power_user_menu.flag IS NULL
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询角色权限菜单去除用户权限菜单勾选掉即power_user_menu的flag为0的数据  结束-->

        <!--拼接非管理员的权限系统power_user_menu的flag为1的菜单数据  开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_sys_dict
        INNER JOIN power_sys_dict hospital ON power_sys_dict.parent_id = hospital.dict_id,
        power_user
        INNER JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        INNER JOIN power_user_menu ON power_user.user_id = power_user_menu.user_id
        AND power_user_menu.flag = 1
        INNER JOIN power_menu ON power_menu.menu_id = power_user_menu.menu_id
        WHERE
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="url != null and url != ''">
            power_menu.menu_url = #{url} AND
        </if>
        power_sys_dict.sys_flag = 'power'
        AND power_user.role_id != 0
        AND power_user.role_id != - 100
        AND power_menu.sys_flag = 'power'
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--拼接非管理员的权限系统power_user_menu的flag为1的菜单数据  结束-->
        <!--查询非管理员的权限系统的权限菜单  结束-->

        <!--查询医院管理员的非权限系统的权限菜单开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        power_dept.dept_id,
        dept_name,
        power_dept.dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_user
        LEFT JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        LEFT JOIN power_sys_dict ON power_dept.dept_id = power_sys_dict.dept_id
        LEFT JOIN power_sys_dict hospital ON power_dept.dict_id = hospital.dict_id
        INNER JOIN power_menu ON power_menu.sys_flag IN (
        SELECT
        power_sys_dict.sys_flag
        FROM
        power_dept
        INNER JOIN power_user ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_sys_dict ON power_dept.dict_id = power_sys_dict.parent_id
        <where>
            <if test="userId != null">
                AND power_user.user_id = #{userId}
            </if>
            <if test="userName != null and userName != ''">
                AND power_user.user_name = #{userName}
            </if>
            <if test="roleId != null">
                AND power_user.role_id = #{roleId}
            </if>
        </where>
        GROUP BY
        power_sys_dict.sys_flag
        )
        WHERE
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="url != null and url != ''">
            power_menu.menu_url = #{url} AND
        </if>
        power_menu.sys_flag != 'power'
        AND power_user.role_id = - 100
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_dept.effective = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询医院管理员的非权限系统的权限菜单  结束-->

        <!--查询医院管理员的权限系统的权限菜单  开始-->
        UNION ALL
        SELECT
        power_sys_dict.dict_id sys_id,
        hospital.hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_sys_dict.sys_flag,
        power_sys_dict.sys_name,
        hospital.dict_id hospital_id,
        power_sys_dict.sys_type,
        power_sys_dict.sort sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        hospital.sort hospital_sort
        FROM
        power_sys_dict
        INNER JOIN power_sys_dict hospital ON power_sys_dict.parent_id = hospital.dict_id,
        power_user
        INNER JOIN power_dept ON FIND_IN_SET(
        power_dept.dept_id,
        power_user.dept_id
        )
        INNER JOIN power_role ON power_user.role_id = power_role.role_id,
        power_menu
        WHERE
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="url != null and url != ''">
            power_menu.menu_url = #{url} AND
        </if>
        power_sys_dict.sys_flag = 'power'
        AND power_user.role_id = - 100
        AND power_menu.sys_flag = 'power'
        AND hospital.dict_status = 1
        AND power_sys_dict.dict_status = 1
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询医院管理员的权限系统的权限菜单  结束-->

        <!--查询系统管理员所有菜单  开始-->
        UNION ALL
        SELECT
        NULL AS sys_id,
        NULL AS hospital_name,
        NULL AS dept_id,
        NULL AS dept_name,
        NULL AS dept_code,
        power_menu.sys_flag,
        NULL AS sys_name,
        NULL AS hospital_id,
        NULL AS sys_type,
        NULL AS sort,
        power_user.user_id,
        power_user.user_name,
        power_user.user_tel,
        power_user.user_email,
        power_user.role_id,
        power_role.role_name,
        power_menu.menu_id,
        power_menu.menu_name,
        power_menu.menu_icon,
        power_menu.menu_url,
        power_menu.method,
        power_menu.function_type method_type,
        power_menu.parent_id method_parent,
        power_menu.sort method_sort,
        null as hospital_sort
        FROM
        power_user
        INNER JOIN power_role ON power_user.role_id = power_role.role_id,
        power_menu
        WHERE
        <if test="userId != null">
            power_user.user_id = #{userId} AND
        </if>
        <if test="url != null and url != ''">
            power_menu.menu_url = #{url} AND
        </if>
        power_user.role_id = 0
        AND power_user.effective = 1
        AND power_role.effective = 1
        AND power_menu.effective = 1
        <!--查询系统管理员所有菜单  结束-->
    </select>
    <select id="selectAllMenusByUserIdAndSysFlag" resultMap="BaseResultMap">
        SELECT DISTINCT
        user_dept_menu.sys_flag,
        user_dept_menu.menu_id,
        user_dept_menu.menu_name,
        user_dept_menu.menu_icon,
        user_dept_menu.menu_url,
        user_dept_menu.method,
        user_dept_menu.method_type,
        user_dept_menu.method_parent,
        user_dept_menu.method_sort
        FROM
        user_dept_menu
        WHERE
        user_id = ${userId}
        <if test="sysFlag != null and sysFlag != ''">
            AND sys_flag = #{sysFlag}
        </if>
    </select>
    <!--针对roleId=-100的医院管理员用户名查询的菜单权限-->
    <select id="selectMenuListByRoleId" resultMap="BaseResultMap">
        SELECT
            sys.dict_id sys_id,
            hospital.hospital_name,
            power_dept.dept_id,
            power_dept.dept_name,
            power_dept.dept_code,
            sys.sys_flag,
            sys.sys_name,
            hospital.dict_id hospital_id,
            sys.sys_type,
            sys.sort,
            power_user.user_id,
            power_user.user_name,
            power_user.user_tel,
            power_user.user_email,
            power_user.role_id,
            power_role.role_name,
            power_menu.menu_id,
            power_menu.menu_name,
            power_menu.menu_icon,
            power_menu.menu_url,
            power_menu.method,
            power_menu.function_type method_type,
            power_menu.parent_id method_parent,
            power_menu.sort method_sort
        FROM
            power_user
        LEFT JOIN power_user_dict ON power_user.user_id = power_user_dict.user_id
        INNER JOIN power_role ON power_user.role_id = power_role.role_id
        LEFT JOIN power_sys_dict sys ON power_user_dict.dict_id = sys.parent_id
        AND sys.dict_status = 1
        LEFT JOIN power_sys_dict hospital ON power_user_dict.dict_id = hospital.dict_id
        INNER JOIN power_menu ON sys.sys_flag = power_menu.sys_flag
        AND power_menu.effective = 1
        LEFT JOIN power_dept ON FIND_IN_SET(
            power_dept.dept_id,
            power_user.dept_id
        )
        AND power_dept.effective = 1
        WHERE
            user_name = #{userName}
        AND power_menu.sys_flag = #{sysFlag}
    </select>
</mapper>