<?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="SysRoleMapper">

	<!-- 新增用户信息-->
	<insert id="save">
		insert into sys_role(
			<if test="name != null and name != ''">
				name,
			</if>
			<if test="description != null and description != ''">
				description,
			</if>
			<if test="data_level != null and data_level != ''">
				data_level,
			</if>
			<if test="create_date != null and create_date != ''">
				create_date,
			</if>
			<if test="create_by != null and create_by != ''">
				create_by,
			</if>
			id
		) values (
			<if test="name != null and name != ''">
				#{name},
			</if>
			<if test="description != null and description != ''">
				#{description},
			</if>
			<if test="data_level != null and data_level != ''">
				#{data_level},
			</if>
			<if test="create_date != null and create_date != ''">
				#{create_date},
			</if>
			<if test="create_by != null and create_by != ''">
				#{create_by},
			</if>
			#{id}
		)
	</insert>
	
	<!-- 删除角色基本信息-->
	<delete id="delete">
		delete from sys_role
		where 
			id = #{id}
	</delete>
	<!-- 删除角色对应的菜单信息-->
	<delete id="deleteRoleMenu">
		delete from sys_role_menu
		where 
			role_id = #{id}
	</delete>
	
	
	<!-- 修改角色信息 -->
	<update id="edit">
		update  sys_role
			set 
			<if test="name != null and name != ''">
				name=#{name},
			</if>
			<if test="description != null and description != ''">
				description=#{description},
			</if>
			<if test="data_level != null and data_level != ''">
				data_level=#{data_level},
			</if>
			<if test="update_by != null and update_by != ''">
				update_by=#{update_by},
			</if>
				update_date=#{update_date}
			where 
				id = #{id}
	</update>
	
	<!-- 获取详情 -->
	<select id="findById" resultType="PageData">
		select
				a.id,
				a.name,
				a.data_level,
				a.description
		from 
				sys_role a 
		where 1=1
		   <if test="id != null and id != ''">
				and a.id=#{id}
		   </if>
		   <if test="name != null and name != ''">
			and a.name=#{name}
		   </if>
	</select>
	
	
	<!-- 列表 -->
	<select id="listPage" parameterType="Page" resultType="PageData">
		select
				a.id,
				a.name,
				a.description,
				a.query_flag,
				a.add_flag,
				a.update_flag,
				a.delete_flag,
				a.data_level,
				(select u.user_name from sys_user u where u.id=a.create_by) as create_by,
				date_format(a.create_date,'%Y-%c-%d %H:%i:%s') create_date,
				(select u.user_name from sys_user u where u.id=a.update_by) as update_by,
				date_format(a.update_date,'%Y-%c-%d %H:%i:%s') update_date
		from 
				sys_role a 
		where 1=1
		<if test="pd.name != null and pd.name != ''">
			and a.name LIKE  CONCAT(CONCAT('%', #{pd.name}), '%')
		</if>
		<if test="pd.DATA_LEVEL == 'personal'">
			and a.create_by=#{pd.USERID}
		</if> 
		order by a.create_date asc
	</select>
	
	<!-- 列表(全部) -->
	<select id="listAll" resultType="PageData">
		select a.id,a.name
		from sys_role a 
		where 1=1
	</select>
	
	<!-- 查询总数 -->
	<select id="findCount" resultType="Long">
		select count(a.id) 
		from sys_role a 
		where 1=1
		<if test="name != null and name != ''">
			and a.name LIKE  CONCAT(CONCAT('%', #{name}), '%')
		</if>
		<if test="DATA_LEVEL == 'personal'">
			and a.create_by=#{USERID}
		</if>
	</select>
	
	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		delete from user
		where 
			ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
	
	<!-- 先删除角色的所有菜单权限 -->
	<delete id="deleteRoleMenuById" parameterType="String">
		delete from sys_role_menu 
		where role_id=#{id}
	</delete>
	
	<!-- 新增角色菜单权限-->
	<insert id="saveRoleMenu">
		insert into sys_role_menu(
			role_id,
			menu_id,
			id
		) values (
			#{role_id},
			#{menu_id},
			#{id}
		)
	</insert>
	
	<update id="add_flag">
		update sys_role 
		set add_flag=#{value} 
		where id=#{id}
	</update>
	
	<update id="delete_flag">
		update sys_role 
		set delete_flag=#{value} 
		where id=#{id}
	</update>
	
	<update id="update_flag">
		update sys_role 
		set update_flag=#{value} 
		where id=#{id}
	</update>
	
	<update id="query_flag">
		update sys_role 
		set query_flag=#{value} 
		where id=#{id}
	</update>
	
	<select id="getRoleById" parameterType="String" resultType="SysRole">
		select * from sys_role 
		where id=#{roleId}
	</select>
</mapper>