<?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.ruoyi.platform.mapper.saas.SaaSMenuMapper">

	<resultMap type="com.ruoyi.common.core.domain.platform.vo.SysMenuVO" id="SysMenuResult">
		<id     property="id"         column="id"        />
		<result property="menuName"       column="menu_name"      />
		<result property="parentName"     column="parent_name"    />
		<result property="parentId"       column="parent_id"      />
		<result property="orderNum"       column="order_num"      />
		<result property="url"            column="url"            />
		<result property="target"         column="target"         />
		<result property="menuType"       column="menu_type"      />
		<result property="visible"        column="visible"        />
		<result property="isRefresh"      column="is_refresh"     />
		<result property="perms"          column="perms"          />
		<result property="icon"           column="icon"           />
		<result property="createBy"       column="create_by"      />
		<result property="createTime"     column="create_time"    />
		<result property="updateTime"     column="update_time"    />
		<result property="updateBy"       column="update_by"      />
		<result property="remark"         column="remark"         />
	</resultMap>

	<sql id="selectMenuVo">
        select id, menu_name, parent_id, order_num, url, target, menu_type, visible, is_refresh, ifnull(perms,'') as perms, icon, create_by, create_time
		from sys_menu
    </sql>

	<select id="selectMenuAll" resultMap="SysMenuResult">
		<include refid="selectMenuVo"/>
		order by parent_id, order_num
	</select>

	<select id="selectMenuList" parameterType="com.ruoyi.common.core.domain.platform.dto.SysMenuDTO" resultMap="SysMenuResult">
		<include refid="selectMenuVo"/>
		<where>
			<if test="menuName != null and menuName != ''">
				AND menu_name like concat('%', #{menuName}, '%')
			</if>
			<if test="visible != null and visible != ''">
				AND visible = #{visible}
			</if>
		</where>
		order by parent_id, order_num
	</select>

	<delete id="deleteMenuById" parameterType="String">
	    delete from sys_menu where id = #{menuId} or parent_id = #{menuId}
	</delete>

	<select id="selectMenuById" parameterType="String" resultMap="SysMenuResult">
		SELECT t.id, t.parent_id, t.menu_name, t.order_num, t.url, t.target, t.menu_type, t.visible, t.is_refresh, t.perms, t.icon, t.remark,
			(SELECT menu_name FROM sys_menu WHERE id = t.parent_id) parent_name
		FROM sys_menu t
		where t.id = #{menuId}
	</select>

	<select id="selectMenuTree" parameterType="String" resultType="String">
		select concat(m.id, ifnull(m.perms,'')) as perms
		from sys_menu m
				 left join sys_role_menu rm on m.id = rm.menu_id
		where rm.role_id = #{roleId} and m.menu_c_type = '00'
		order by m.parent_id, m.order_num
	</select>

	<!--租户角色选择菜单-->
	<select id="selectMenuTenantAll" resultMap="SysMenuResult">
		SELECT
			m.id,
			m.menu_name,
			m.parent_id,
			m.order_num,
			m.url,
			m.target,
			m.menu_type,
			m.visible,
			m.is_refresh,
			ifnull( m.perms, '' ) AS perms,
			m.icon,
			m.create_by,
			m.create_time
		FROM
			sys_menu m
				left join sys_role_menu rm on m.id = rm.menu_id
				LEFT JOIN trs_sys_role sr on rm.role_id = sr.id
		WHERE
			sr.role_key = 'saas_admin' AND sr.tenant_id = 'platform_saas'
		ORDER BY
			parent_id,
			order_num
	</select>

	<!--租户超管角色选择菜单-->
	<select id="selectMenuTenantAdminAll" resultMap="SysMenuResult">
		<include refid="selectMenuVo"/>
		WHERE
			menu_c_type = '00'
		ORDER BY
			parent_id,
			order_num
	</select>

	<!--租户用户-->
	<select id="selectMenusByUserId" parameterType="String" resultMap="SysMenuResult">
		select distinct m.id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
		from sys_menu m
				 left join sys_role_menu rm on m.id = rm.menu_id
				 left join sys_user_role ur on rm.role_id = ur.role_id
				 LEFT JOIN trs_sys_role ro on ur.role_id = ro.id
		where ur.user_id = #{userId} and m.menu_type in ('M', 'C') and m.menu_c_type = '00' and m.visible = 0  AND ro.status = 0
		order by m.parent_id, m.order_num
	</select>

	<select id="selectPermsByUserId" parameterType="String" resultType="String">
		select distinct m.perms
		from sys_menu m
				 left join sys_role_menu rm on m.id = rm.menu_id
				 left join sys_user_role ur on rm.role_id = ur.role_id
				 left join trs_sys_role r on r.id = ur.role_id
		where m.visible = '0' and m.menu_c_type = '00' and r.status = '0' and ur.user_id = #{userId}
	</select>

	<!--租户用户-->
	<select id="selectMenuAllByUserId" parameterType="String" resultMap="SysMenuResult">
		select distinct m.id, m.parent_id, m.menu_name, m.url, m.visible, m.is_refresh, ifnull(m.perms,'') as perms, m.target, m.menu_type, m.icon, m.order_num, m.create_time
		from sys_menu m
				 left join sys_role_menu rm on m.id = rm.menu_id
				 left join sys_user_role ur on rm.role_id = ur.role_id
				 LEFT JOIN trs_sys_role ro on ur.role_id = ro.id
		where ur.user_id = #{userId} and m.menu_c_type = '00'
		order by m.parent_id, m.order_num
	</select>

</mapper> 