<?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.iemes.mapper.ResourcesMapper">
	<!--mybatis ehcache缓存配置 -->
	<!-- 以下两个<cache>标签二选一,第一个可以输出日志,第二个不输出日志 <cache type="org.mybatis.caches.ehcache.LoggingEhcache" 
		/> -->
	<!-- <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> -->

	<sql id="selectId">
		id,
		name,
		parentId,
		(select r.name from ly_resources r where r.id = ly_resources.parentId )
		parentName,
		resKey,
		ishide,
		type,
		resUrl,
		level,
		icon,
		description
	</sql>
	<select id="findChildlists" resultType="com.iemes.entity.ResFormMap">
		SELECT
		<include refid="selectId" />
		from ly_resources
		where parentId = #{id}
	</select>
	<!-- 获取该用户的权限 -->
	<select id="findRes" parameterType="String" resultType="com.iemes.entity.ResFormMap">
		SELECT
		<include refid="selectId" />
		from ly_resources where id in(
			select resId from
			ly_res_role where 1=1 and roleId in (SELECT roleId from ly_user_role where userId = #{userId})
		)
		<if test="parentId != null and parentId != '' and parentId != 0">
			and parentId = #{parentId}
		</if>
		order by level asc
	</select>

	<!-- 根据账号Id获取该用户的权限 -->
	<select id="findUserResourcess" parameterType="String"
		resultType="com.iemes.entity.ResFormMap">
		SELECT
		<include refid="selectId" />
		from ly_resources where id in(
		select resId from ly_res_role
		where roleId in (select roleId from ly_user_role where userId=#{userId}))
	</select>
	<update id="updateSortOrder" parameterType="java.util.List">
          <foreach collection="list" item="item" index="index" open="" close="" separator=";">
                update ly_resources
                <set>
                  level = #{item.level}
                </set>
                where id = #{item.id}
         </foreach>
	</update>
	<!-- 删 -->
	<delete id="deleteResourcesRole" parameterType="String">
		delete from
		ly_res_roles
		where role_id=#{id}
	</delete>

	<insert id="addRoleRes">
		insert into ly_res_roles (role_id,resc_id) value (#{roleId},#{resId})
	</insert>

</mapper>