<?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.ylcm.sys.mapper.PermissionMapper">
	
	<resultMap type="com.ylcm.sys.domain.Permission" id="permMap">
		<id property="permissionId" column="permission_id" />
		<result property="type" column="type" />
		<result property="pid" column="pid" />
		<result property="name" column="name" />
		<result property="code" column="code" />
		<result property="url" column="url" />
		<result property="rank" column="rank" />
		<result property="logo" column="logo" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
	</resultMap>
	
	<resultMap type="com.ylcm.sys.domain.Permission" id="permSubMap">
		<id property="permissionId" column="permission_id" />
		<result property="type" column="type" />
		<result property="pid" column="pid" />
		<result property="name" column="name" />
		<result property="code" column="code" />
		<result property="url" column="url" />
		<result property="rank" column="rank" />
		<result property="logo" column="logo" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
		<collection property="subPerms"
						ofType="com.ylcm.sys.domain.Permission"
						column="permission_id"
						select="getPermissionsByPid"/>
	</resultMap>
	
	<!-- 新增权限 -->
	<insert id="addPermission" parameterType="com.ylcm.sys.domain.Permission">
		INSERT INTO permission (
		<if test="type != null and type != ''">
			type,
		</if>
		<if test="pid > 0">
			pid,
		</if>
		<if test="name != null and name != ''">
			name,
		</if>
		<if test="code != null and code != ''">
			code,
		</if>
		<if test="url != null and url != ''">
			url,
		</if>
		<if test="rank > 0 ">
			rank,
		</if>
		<if test="crtTime != null">
			crt_time,
		</if>
		<if test="updTime != null">
			upd_time,
		</if>
		<if test="logo != null and logo != ''">
			logo
		</if>
		) VALUES (
		<if test="type != null and type != ''">
			#{type},
		</if>
		<if test="pid > 0">
			#{pid},
		</if>
		<if test="name != null and name != ''">
			#{name},
		</if>
		<if test="code != null and code != ''">
			#{code},
		</if>
		<if test="url != null and url != ''">
			#{url},
		</if>
		<if test="rank > 0">
			#{rank},
		</if>
		<if test="crtTime != null">
			#{crtTime},
		</if>
		<if test="updTime != null">
			#{updTime},
		</if>
		<if test="logo != null and logo != ''">
			#{logo}
		</if>
		)
	</insert>
	
	<!-- 更新权限 -->
	<update id="updPermission" parameterType="com.ylcm.sys.domain.Permission">
		UPDATE permission  
		SET 
		<if test="type != null and type != ''">
			type = #{type},
		</if>
		<if test="pid > 0">
			pid = #{pid}  ,
		</if>
		<if test="name != null and name != ''">
			name = #{name}  ,
		</if>
		<if test="code != null and code != ''">
			code = #{code} ,
		</if>
	  	<if test="url != null and url != ''">
			url = #{url} ,
	  	</if>
	  	<if test="rank > 0">
			rank = #{rank}  ,
	  	</if>
	  	<if test="updTime != null">
			upd_time = #{updTime} ,
	  	</if>
	  	<if test="logo != null and logo != ''">
			logo = #{logo}
	  	</if>
		WHERE permission_id = #{permissionId}
	</update>
	
	<!-- 删除权限 -->
	<delete id="delPermission">
		DELETE FROM permission
		WHERE permission_id = #{permissionId}
	</delete>
	
	<!-- 查询父级权限列表 -->
	<select id="getMenuList" resultMap="permMap">
		SELECT permission_id, type, pid, name, code, url, rank, logo, crt_time ,upd_time
		FROM permission
		WHERE type = 'menu'
		ORDER BY rank ASC
	</select>
	
	<!-- 获取父级菜单权限信息(含子菜单) -->
	<select id="getAllMenus" resultMap="permSubMap">
		SELECT p.permission_id, p.type, p.pid, p.name, p.code, p.url, p.rank,p.logo 
		FROM permission p
		WHERE type = 'menu'
		ORDER BY p.rank ASC
	</select>
	
	<!-- 获取所有功能菜单 -->
	<select id="getAllFunctions" resultMap="permMap">
		SELECT *
		FROM permission p
		WHERE type = 'func'
		ORDER BY p.rank ASC
	</select>
	
	<!-- 根据权限标识获取权限信息 -->
	<select id="getPermissionById" resultMap="permMap">
		SELECT * 
		FROM permission
		WHERE permission_id = #{permissionId}
	</select>
	
	<!-- 根据角色获取父级菜单权限信息 (含子菜单)-->
	<select id="getMenusByRoleId" resultMap="permSubMap">
		SELECT p.permission_id, p.type, p.pid, p.name, p.code, p.url, p.rank,p.logo 
		FROM permission p
		JOIN role_permission rp ON p.permission_id = rp.permission_id
		WHERE type = 'menu'
		AND rp.role_id = #{roleId}
		ORDER BY p.rank ASC
	</select>
	
	<!-- 根据角色获取所有权限信息 -->
	<select id="getPermissionsByRoleId" resultMap="permMap">
		SELECT p.permission_id, p.type, p.pid, p.name, p.code, p.url, p.rank,p.logo 
		FROM permission p
		JOIN role_permission rp ON p.permission_id = rp.permission_id
		WHERE rp.role_id = #{roleId}
		ORDER BY p.rank ASC
	</select>
	
	<!-- 根据角色获取所有权限信息 (登录)-->
	<select id="getByRoleIdForLogin" resultMap="permMap">
		SELECT p.permission_id, p.type, p.pid, p.name, p.code, p.url, p.rank,p.logo
		FROM permission p
		JOIN role_permission rp ON p.permission_id = rp.permission_id
		WHERE rp.role_id = #{roleId} and p.type in ('menu','submenu')
		ORDER BY p.rank ASC
	</select>
	
	<!-- 根据角色获取所有功能权限信息 (登录)-->
	<select id="getFuncsByRoleIdForLogin" resultMap="permMap">
		SELECT p.permission_id, p.type, p.pid, p.name, p.code, p.url, p.rank,p.logo
		FROM permission p
		JOIN role_permission rp ON p.permission_id = rp.permission_id
		WHERE rp.role_id = #{roleId} and p.type = 'func'
		ORDER BY p.rank ASC
	</select>
	
	<!-- 根据条件查询权限总数 -->
	<select id="getRowCount" parameterType="java.util.Map" resultType="Integer">
		SELECT count(permission_id) FROM permission
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			<if test="name != null">
				AND name like CONCAT('%',#{name},'%')
			</if>
			<if test="code != null and code != ''">
				AND code = #{code}
			</if>
			<if test="type != null and type != ''">
				AND type = #{type}
			</if>
		</trim>
	</select>
	
	<!-- 条件查询权限列表 -->
	<select id="getPermissionList" resultMap="permSubMap">
		SELECT permission_id, type, pid, name, code, url, rank, crt_time, upd_time
		FROM permission
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			<if test="name != null">
				AND name like CONCAT('%',#{name},'%')
			</if>
			<if test="code != null and code != ''">
				AND code = #{code}
			</if>
			<if test="type != null and type != ''">
				AND type = #{type}
			</if>
		</trim>
		ORDER BY upd_time DESC
		<if test="pageIndex != null">
			LIMIT #{pageSize} OFFSET #{pageIndex}
		</if>
	</select>
	
	<!-- 查找当前权限是否关联角色 -->
	<select id="getRolePermById" resultMap="permMap">
		SELECT p.permission_id
		FROM role_permission rp
		JOIN permission p ON rp.permission_id = p.permission_id AND p.permission_id = #{permissionId}
	</select>
	
	<!-- 删除权限信息(只删除未关联角色的权限) -->
	<delete id="delPermissionsByIds">
		DELETE FROM permission
		WHERE permission_id IN
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
		AND NOT EXISTS (SELECT NULL FROM role_permission rp WHERE rp.permission_id = permission.permission_id)
	</delete>
		
	<!-- 唯一性判断 -->
	<select id="getCheckPermission" parameterType="java.util.Map" resultType="Integer">
		SELECT count(*) FROM permission WHERE  name = #{name} AND type = #{type}
		 <if test="permissionId != null">AND permission_id != #{permissionId}</if>
	</select>
	
	<!-- 根据父权限获取所有菜单子权限 -->
	<select id="getPermissionsByPid" resultMap="permMap">
		SELECT permission_id, type, pid, name, code, url, rank
		FROM permission
		WHERE pid = #{pid} 
		ORDER BY rank ASC
	</select>
	
	<!-- 删除角色权限关联（根据角色） -->
	<delete id="delRolePermissionsById">
		DELETE FROM role_permission
		WHERE role_id = #{roleId}
	</delete>
	
	<!-- 添加用户信息 -->
	<insert id="addRolePermission" parameterType="java.util.Map" >
		INSERT INTO role_permission ( 
		<if test="roleId != null and roleId != ''">
			role_id,
		</if>
		<if test="permissionId != null and permissionId != ''">
			permission_id,
		</if>
		<if test="crtTime != null">
			crt_time,
		</if>
		<if test="updTime != null">
			upd_time
		</if>
		) VALUES (
		<if test="roleId != null and roleId != ''">
			#{roleId},
		</if>
		<if test="permissionId != null and permissionId != ''">
			#{permissionId},
		</if>
		<if test="crtTime != null">
			#{crtTime},
		</if>
		<if test="updTime != null">
			#{updTime}
		</if>
		)
	</insert>
</mapper>