<?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.PowerMapper">
    <resultMap id="BaseResultMap" type="com.manage.vo.Power_UserAndRoleTree">
        <result column="level" jdbcType="INTEGER" property="level"/>
        <result column="parent_id" jdbcType="INTEGER" property="parentId"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="new_name" jdbcType="VARCHAR" property="newName"/>
        <result column="sys_flag" jdbcType="VARCHAR" property="sysFlag"/>
        <result column="dept_id" jdbcType="VARCHAR" property="deptId"/>
        <result column="self_id" jdbcType="INTEGER" property="selfId"/>
        <result column="tel" jdbcType="VARCHAR" property="tel"/>
    </resultMap>
    <select id="getUserTree" resultMap="BaseResultMap">
        select 1 as level,0 as parent_id,power_sys_dict.hospital_name name,power_sys_dict.hospital_name new_name,null as
        sys_flag,null as dept_id,dict_id self_id,null as tel from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict
            where power_user_dict.user_id = #{userId}) and
        </if>
        <if test="key != null and key != '' and userId == null">
            dict_id in (select dict_id from power_user_dict
            INNER JOIN power_user ON power_user.effective = 1 AND power_user_dict.user_id = power_user.user_id AND (
            power_user.user_name LIKE '%${key}%' OR power_user.user_id LIKE '%${key}%' OR power_user.user_tel LIKE
            '%${key}%'
            )) AND
        </if>
        sys_name is null and dict_status = 1
        union ALL
        select 2 as level,power_sys_dict.parent_id,power_sys_dict.sys_name name,power_sys_dict.sys_name
        new_name,sys_flag,null as dept_id,null as self_id,null as tel from power_sys_dict where sys_flag = 'power' and
        parent_id in (select power_sys_dict.dict_id from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict
            where user_id = #{userId}) and
        </if>
        sys_name is null and dict_status = 1) and dict_status = 1
        UNION ALL
        select 3 as level,power_dept.dict_id parent_id,dept_name name,dept_name new_name,null
        sys_flag,power_dept.dept_id,null as self_id,null as tel from power_dept
        <if test="key != null and key != ''">
            INNER JOIN power_user on FIND_IN_SET(power_dept.dept_id,power_user.dept_id) and power_user.effective = 1 and
            (power_user.user_id LIKE '%${key}%' OR power_user.user_name LIKE '%${key}%' OR power_user.user_tel LIKE
            '%${key}%')
            <if test="userId != null">
                AND power_user.user_id != #{userId}
            </if>
        </if>
        where power_dept.dict_id in (select power_sys_dict.dict_id from power_sys_dict where
        <if test="userId != null">
            power_sys_dict.dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId})
            and
        </if>
        sys_name is null and dict_status = 1) and power_dept.effective = 1
        union ALL
        select 4 as level,power_sys_dict.parent_id parent_id,sys_name name,sys_name
        new_name,sys_flag,power_sys_dict.dept_id,power_sys_dict.dict_id as self_id,null as tel from power_sys_dict where
        power_sys_dict.dict_status = 1 and power_sys_dict.sys_flag != 'power' and power_sys_dict.dept_id in (select
        power_dept.dept_id from power_dept
        <if test="key != null and key != ''">
            INNER JOIN power_user on FIND_IN_SET(power_dept.dept_id,power_user.dept_id) and power_user.effective = 1 and
            (power_user.user_id LIKE '%${key}%' OR power_user.user_name LIKE '%${key}%' OR power_user.user_tel LIKE
            '%${key}%')
            <if test="userId != null">
                AND power_user.user_id != #{userId}
            </if>
        </if>
        where power_dept.effective = 1 and power_dept.dict_id in (select power_sys_dict.dict_id from power_sys_dict
        where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId}) and
        </if>
        sys_name is null and dict_status = 1 ))
        union ALL
        select 5 as level,power_user_dict.dict_id as parent_id,power_user.user_name name,power_user.user_name
        new_name,null as sys_flag,power_user.dept_id,power_user.user_id self_id,power_user.user_tel from power_user
        INNER JOIN (select power_dept.dept_id from power_dept where power_dept.effective = 1 and power_dept.dict_id in
        (select power_sys_dict.dict_id from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId}) and
        </if>
        dict_status = 1 and sys_name is null)) a
        on a.dept_id in (power_user.dept_id)
        LEFT JOIN power_user_dict on power_user.user_id = power_user_dict.user_id
        where power_user.effective = 1 and power_user.role_id != 0 and power_user.role_id != -100
        <if test="userId != null">
            AND power_user.user_id != #{userId}
        </if>
        <if test="key != null and key != ''">
            and (power_user.user_id LIKE '%${key}%' OR power_user.user_name LIKE '%${key}%' OR power_user.user_tel LIKE
            '%${key}%')
        </if>
        group by self_id
    </select>
    <select id="getRoleTree" resultMap="BaseResultMap">
        select 1 as level,0 as parent_id,power_sys_dict.hospital_name name,power_sys_dict.hospital_name new_name,null as
        sys_flag,null as dept_id,dict_id self_id,null as tel from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict
            where power_user_dict.user_id = #{userId}) and
        </if>
        <if test="key != null and key != '' and userId == null">
            dict_id in (select dict_id from power_user_dict
            INNER JOIN power_user ON power_user.effective = 1 AND power_user_dict.user_id = power_user.user_id
            INNER JOIN power_role ON power_user.role_id = power_role.role_id AND power_role.effective AND
            power_role.role_name LIKE '%${key}%'
            ) AND
        </if>
        sys_name is null and dict_status = 1
        union ALL
        select 2 as level,power_sys_dict.parent_id,power_sys_dict.sys_name name,power_sys_dict.sys_name
        new_name,sys_flag,null as dept_id,null as self_id,null as tel from power_sys_dict
        where sys_flag = 'power' and parent_id in (select power_sys_dict.dict_id from power_sys_dict
        where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId}) and
        </if>
        sys_name is null and dict_status = 1 ) and dict_status = 1
        UNION ALL
        select 3 as level,power_dept.dict_id parent_id,dept_name name,dept_name new_name,null
        sys_flag,power_dept.dept_id,null as self_id,null as tel from power_dept
        <if test="key != null and key != ''">
            INNER JOIN power_user on FIND_IN_SET(power_dept.dept_id,power_user.dept_id) and power_user.effective = 1
            INNER JOIN power_role on power_user.role_id = power_role.role_id and power_role.effective = 1 and
            power_role.role_name LIKE '%${key}%'
        </if>
        where power_dept.dict_id in (select power_sys_dict.dict_id from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId}) and
        </if>
        sys_name is null and dict_status = 1 ) and power_dept.effective = 1
        union ALL
        select 4 as level,power_sys_dict.parent_id parent_id,sys_name name,sys_name
        new_name,sys_flag,power_sys_dict.dept_id,power_sys_dict.dict_id as self_id,null as tel from power_sys_dict
        <if test="key != null and key != ''">
            INNER JOIN power_user on FIND_IN_SET(power_sys_dict.dept_id,power_user.dept_id) and power_user.effective = 1
            INNER JOIN power_role on power_user.role_id = power_role.role_id and power_role.effective = 1 and
            power_role.role_name LIKE '%${key}%'
        </if>
        where power_sys_dict.dict_status = 1 and power_sys_dict.sys_flag != 'power' and power_sys_dict.dept_id in
        (select power_dept.dept_id from power_dept where power_dept.effective = 1 and power_dept.dict_id in (select
        power_sys_dict.dict_id from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId}) and
        </if>
        sys_name is null and dict_status = 1 ))
        union ALL
        select 5 as level,power_user_dict.dict_id as parent_id,power_role.role_name name,power_role.role_name
        new_name,null as sys_flag,power_user.dept_id,power_user.role_id self_id,power_user.user_tel from power_user
        INNER JOIN (select power_dept.dept_id from power_dept where power_dept.effective = 1 and power_dept.dict_id in
        (select power_sys_dict.dict_id from power_sys_dict where
        <if test="userId != null">
            dict_id in (select dict_id from power_user_dict where power_user_dict.user_id = #{userId}) and
        </if>
        dict_status = 1 and sys_name is null)) a
        on a.dept_id in (power_user.dept_id)
        LEFT JOIN power_user_dict on power_user.user_id = power_user_dict.user_id
        LEFT JOIN power_role on power_role.effective = 1 AND power_user.role_id = power_role.role_id
        where power_user.effective = 1 and power_user.role_id != 0 and power_user.role_id != -100
        <if test="roleId != null">
            AND power_role.role_id != #{roleId}
        </if>
        <if test="key != null and key != ''">
            AND power_role.role_name LIKE '%${key}%'
        </if>
    </select>
</mapper>