<?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.RoleMapper">

	<resultMap type="com.ylcm.sys.domain.Role" id="roleMap">
		<id property="roleId" column="role_id" />
		<result property="name" column="name" />
		<result property="code" column="code" />
		<result property="info" column="info" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
	</resultMap>
	
	<resultMap type="com.ylcm.sys.domain.Role" id="rolePermMap">
		<id property="roleId" column="role_id" />
		<result property="name" column="name" />
		<result property="code" column="code" />
		<result property="info" column="info" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
		<collection property="perms" ofType="com.ylcm.sys.domain.Permission" column="role_id" javaType="java.util.List"
			select="com.ylcm.sys.mapper.PermissionMapper.getMenusByRoleId"/>
	</resultMap>
	
	<resultMap type="com.ylcm.sys.domain.Role" id="rolePermLoginMap">
		<id property="roleId" column="role_id" />
		<result property="name" column="name" />
		<result property="code" column="code" />
		<result property="info" column="info" />
		<result property="crtTime" column="crt_time" />
		<result property="updTime" column="upd_time" />
		<collection property="perms" ofType="com.ylcm.sys.domain.Permission" column="role_id" javaType="java.util.List"
			select="com.ylcm.sys.mapper.PermissionMapper.getByRoleIdForLogin"/>
		<collection property="funcs" ofType="com.ylcm.sys.domain.Permission" column="role_id" javaType="java.util.List"
			select="com.ylcm.sys.mapper.PermissionMapper.getFuncsByRoleIdForLogin"/>
	</resultMap>
	<!-- 新增角色 -->
	<insert id="addRole" parameterType="com.ylcm.sys.domain.Role" useGeneratedKeys="true" keyProperty="roleId">
		INSERT INTO role (
		<if test="name != null and name != ''">
			name,
		</if>
		<if test="code != null and code != ''">
			code,
		</if>
		<if test="info != null and info != ''">
			info,
		</if>
		<if test="crtTime != null">
			crt_time,
		</if>
		<if test="updTime != null">
			upd_time
		</if>
		) VALUES(
		<if test="name != null and name != ''">
			#{name},
	 	</if>
	 	<if test="code != null and code != ''">
			#{code},
	 	</if>
	 	<if test="info != null and info != ''">
			#{info},
	 	</if>
	 	<if test="crtTime != null">
			#{crtTime},
	 	</if>
	 	<if test="updTime != null">
			#{updTime}
		</if>
		)
	</insert>
	
	<!-- 更新角色 -->
	<update id="updRole" parameterType="com.ylcm.sys.domain.Role" >
		UPDATE role  
		SET 
		<if test="name != null and name != ''">
			name = #{name},
	  	</if>
	  	<if test="code != null and code != ''">
			code = #{code},
	  	</if>
	  	<if test="info != null and info != ''">
			info = #{info},
	  	</if>
	  	<if test="updTime != null">
			upd_time = #{updTime}
	  	</if>
		WHERE role_id = #{roleId}
	</update>
	
	<!-- 删除角色 -->
	<delete id="delRole" >
		DELETE FROM role
		WHERE role_id = #{roleId}
	</delete>
	
	<!-- 根据条件查询角色总数 -->
	<select id="getRowCount" parameterType="java.util.Map" resultType="int">
		SELECT count(role_id) 
		FROM role 
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			<if test="name != null and name != ''">
				AND name LIKE CONCAT('%',#{name},'%')
			</if>
			<if test="code != null and code != ''">
				AND code = #{code}
			</if>
		</trim>
	</select>
	
	<!-- 根据条件查询角色列表 -->
	<select id="getRoleList" resultMap="roleMap">
		SELECT role_id, name, code, info, crt_time, upd_time
		FROM role 
		<trim prefix="WHERE" prefixOverrides="AND |OR ">
			<if test="name != null and name != ''">
				AND name LIKE CONCAT('%',#{name},'%')
			</if>
			<if test="code != null and code != ''">
				AND code = #{code}
			</if>
		</trim>
		ORDER BY upd_time DESC
		<if test="pageIndex != null">
			LIMIT #{pageSize} OFFSET #{pageIndex}
		</if>
	</select>
	
	<!-- 查询角色列表-->
	<select id="getRolesList" resultMap="roleMap">
		SELECT role_id, name
		FROM role 
		ORDER BY crt_time ASC
	</select>
	
	<!-- 根据角色标识获取角色信息 -->
	<select id="getRoleById" resultMap="rolePermLoginMap">
		SELECT role_id, name, code, info
		FROM role
		WHERE role_id = #{roleId}
	</select>
	
	<!-- 根据角色编码获取角色信息 -->
	<select id="getRoleByCode" resultMap="roleMap">
		SELECT role_id, name, code, info
		FROM role
		WHERE code = #{code}
	</select>
	
	<!-- 根据用户标识获取用户角色 -->
	<select id="getRolesByUserId" resultMap="rolePermMap">
		SELECT r.role_id, r.name, r.code, r.info
		FROM role r
		JOIN user_role ur 
		ON ur.role_id = r.role_id
		WHERE ur.user_id = #{userId}
	</select>
	
	<!-- 根据用户标识获取用户角色(登录) -->
	<select id="getByUserIdForLogin" resultMap="rolePermLoginMap">
		SELECT r.role_id, r.name, r.code, r.info
		FROM role r
		JOIN user_role ur 
		ON ur.role_id = r.role_id
		WHERE ur.user_id = #{userId}
	</select>
	
	<!-- 删除角色信息(只删除未关联用户的权限) -->
	<delete id="delRolesByIds">
		DELETE FROM role
		WHERE role_id IN
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
		AND NOT EXISTS (SELECT NULL FROM user_role ur WHERE ur.role_id = role.role_id)
	</delete>
	
	<!-- 根据用户标识删除用户角色 -->
	<delete id="delUserRolesByUserId">
		DELETE FROM user_role
		WHERE user_id = #{userId}
	</delete>
	
	<!-- 新增用户角色关联信息-->
	<insert id="addUserRole" parameterType="java.util.Map">
		INSERT INTO user_role (
		   	<if test="userId != null and userId != ''">
				user_id,
		   	</if>
		   	<if test="roleId != null and roleId != ''">
				role_id,
		   	</if>
		   	<if test="crtTime != null">
				crt_time,
		   	</if>
		   	<if test="updTime != null">
				upd_time
		   	</if>
		) VALUES (
		   	<if test="userId != null and userId != ''">
				#{userId},
		   	</if>
		   	<if test="roleId != null and roleId != ''">
				#{roleId},
		   	</if>
		   	<if test="crtTime != null">
				#{crtTime},
		   	</if>
		   	<if test="updTime != null">
				#{updTime}
			</if>
		)
	</insert>
	
	<!-- 根据角色标识删除角色权限 -->
	<delete id="delRolePermsByRoleId">
		DELETE FROM role_permission
		WHERE role_id = #{roleId}
	</delete>
	
	<!-- 新增角色权限 -->
	<insert id="addRolePerm" 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>