<?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.izouma.awesomeadmin.dao.SysRoleMapper" >
	<resultMap id="BaseResultMap" type="com.izouma.awesomeadmin.model.SysRole" >
		<id column="id" property="id" jdbcType="INTEGER" />
		<result column="office_id" property="officeId" jdbcType="VARCHAR" />
		<result column="name" property="name" jdbcType="VARCHAR" />
		<result column="enname" property="enname" jdbcType="VARCHAR" />
		<result column="role_type" property="roleType" jdbcType="VARCHAR" />
		<result column="is_sys" property="isSys" jdbcType="VARCHAR" />
		<result column="useable" property="useable" jdbcType="VARCHAR" />
		<result column="create_by" property="createBy" jdbcType="VARCHAR" />
		<result column="create_date" property="createDate" jdbcType="TIMESTAMP" />
		<result column="update_by" property="updateBy" jdbcType="VARCHAR" />
		<result column="update_date" property="updateDate" jdbcType="TIMESTAMP" />
		<result column="remarks" property="remarks" jdbcType="VARCHAR" />
		<result column="del_flag" property="delFlag" jdbcType="CHAR" />
		<result column="menu_ids" property="menuIds" jdbcType="VARCHAR" />
		<result column="permission" property="permission" jdbcType="VARCHAR" />
	</resultMap>
	<sql id="Base_Column_List" >
		 id, office_id, name, enname, role_type, is_sys, useable, create_by, create_date, update_by, update_date, remarks, del_flag,menu_ids,permission
	</sql>
	<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
		select
		<include refid="Base_Column_List" />
		from sys_role
		where id = #{id,jdbcType=INTEGER}
	</select>
	<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
		delete from sys_role
		where id = #{id,jdbcType=INTEGER}
	</delete>
	<insert id="insertSelective" parameterType="com.izouma.awesomeadmin.model.SysRole" useGeneratedKeys="true" keyProperty="id">
		insert into sys_role
		<trim prefix="(" suffix=")" suffixOverrides="," >
			<if test="id != null" >
				id,	
			</if>
			<if test="officeId != null" >
				office_id,	
			</if>
			<if test="name != null" >
				name,	
			</if>
			<if test="enname != null" >
				enname,	
			</if>
			<if test="roleType != null" >
				role_type,	
			</if>
			<if test="isSys != null" >
				is_sys,	
			</if>
			<if test="useable != null" >
				useable,	
			</if>
			<if test="createBy != null" >
				create_by,	
			</if>
			<if test="createDate != null" >
				create_date,	
			</if>
			<if test="updateBy != null" >
				update_by,	
			</if>
			<if test="updateDate != null" >
				update_date,	
			</if>
			<if test="remarks != null" >
				remarks,	
			</if>
			<if test="menuIds != null" >
				menu_ids,
			</if>
			<if test="permission != null" >
				permission,
			</if>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides="," >
			<if test="id != null" >
				#{id,jdbcType=INTEGER},	
			</if>
			<if test="officeId != null" >
				#{officeId,jdbcType=VARCHAR},	
			</if>
			<if test="name != null" >
				#{name,jdbcType=VARCHAR},	
			</if>
			<if test="enname != null" >
				#{enname,jdbcType=VARCHAR},	
			</if>
			<if test="roleType != null" >
				#{roleType,jdbcType=VARCHAR},	
			</if>
			<if test="isSys != null" >
				#{isSys,jdbcType=VARCHAR},	
			</if>
			<if test="useable != null" >
				#{useable,jdbcType=VARCHAR},	
			</if>
			<if test="createBy != null" >
				#{createBy,jdbcType=VARCHAR},	
			</if>
			<if test="createDate != null" >
				#{createDate,jdbcType=TIMESTAMP},	
			</if>
			<if test="updateBy != null" >
				#{updateBy,jdbcType=VARCHAR},	
			</if>
			<if test="updateDate != null" >
				#{updateDate,jdbcType=TIMESTAMP},	
			</if>
			<if test="remarks != null" >
				#{remarks,jdbcType=VARCHAR},	
			</if>
			<if test="menuIds != null" >
				#{menuIds,jdbcType=VARCHAR},
			</if>
			<if test="permission != null" >
				#{permission,jdbcType=VARCHAR},
			</if>
		</trim>
	</insert>
	<update id="updateByPrimaryKeySelective" parameterType="com.izouma.awesomeadmin.model.SysRole" >
		update sys_role
		<set >
			<if test="officeId != null" >
				office_id = #{officeId,jdbcType=VARCHAR},	
			</if>
			<if test="name != null" >
				name = #{name,jdbcType=VARCHAR},	
			</if>
			<if test="enname != null" >
				enname = #{enname,jdbcType=VARCHAR},	
			</if>
			<if test="roleType != null" >
				role_type = #{roleType,jdbcType=VARCHAR},	
			</if>
			<if test="isSys != null" >
				is_sys = #{isSys,jdbcType=VARCHAR},	
			</if>
			<if test="useable != null" >
				useable = #{useable,jdbcType=VARCHAR},	
			</if>
			<if test="createBy != null" >
				create_by = #{createBy,jdbcType=VARCHAR},	
			</if>
			<if test="createDate != null" >
				create_date = #{createDate,jdbcType=TIMESTAMP},	
			</if>
			<if test="updateBy != null" >
				update_by = #{updateBy,jdbcType=VARCHAR},	
			</if>
			<if test="updateDate != null" >
				update_date = #{updateDate,jdbcType=TIMESTAMP},	
			</if>
			<if test="remarks != null" >
				remarks = #{remarks,jdbcType=VARCHAR},	
			</if>
			<if test="delFlag != null" >
				del_flag = #{delFlag,jdbcType=CHAR},	
			</if>
			<if test="menuIds != null" >
				menu_ids = #{menuIds,jdbcType=VARCHAR},
			</if>
			<if test="permission != null" >
				permission = #{permission,jdbcType=VARCHAR},
			</if>
		</set>
		where id = #{id,jdbcType=INTEGER}
	</update>
	<select id="querySysRolesByPage" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.SysRole">
		select <include refid="Base_Column_List"/> from sys_role
		<where>
			and del_flag = 'N'
			<if test="record.id != null and !&quot;&quot;.equals(record.id)">
				and id = #{record.id}	
			</if>
			<if test="record.officeId != null and !&quot;&quot;.equals(record.officeId)">
				and office_id = #{record.officeId}	
			</if>
			<if test="record.name != null and !&quot;&quot;.equals(record.name)">
				and name = #{record.name}	
			</if>
			<if test="record.enname != null and !&quot;&quot;.equals(record.enname)">
				and enname = #{record.enname}	
			</if>
			<if test="record.roleType != null and !&quot;&quot;.equals(record.roleType)">
				and role_type = #{record.roleType}	
			</if>
			<if test="record.isSys != null and !&quot;&quot;.equals(record.isSys)">
				and is_sys = #{record.isSys}	
			</if>
			<if test="record.useable != null and !&quot;&quot;.equals(record.useable)">
				and useable = #{record.useable}	
			</if>
			<if test="record.createBy != null and !&quot;&quot;.equals(record.createBy)">
				and create_by = #{record.createBy}	
			</if>
			<if test="record.createDate != null and !&quot;&quot;.equals(record.createDate)">
				and create_date = #{record.createDate}	
			</if>
			<if test="record.updateBy != null and !&quot;&quot;.equals(record.updateBy)">
				and update_by = #{record.updateBy}	
			</if>
			<if test="record.updateDate != null and !&quot;&quot;.equals(record.updateDate)">
				and update_date = #{record.updateDate}	
			</if>
			<if test="record.remarks != null and !&quot;&quot;.equals(record.remarks)">
				and remarks = #{record.remarks}	
			</if>
			<if test="record.delFlag != null and !&quot;&quot;.equals(record.delFlag)">
				and del_flag = #{record.delFlag}	
			</if>
			<if test="record.menuIds != null and !&quot;&quot;.equals(record.menuIds)">
				and menu_ids = #{record.menuIds}
			</if>
			<if test="record.permission != null and !&quot;&quot;.equals(record.permission)">
				and permission = #{record.permission}
			</if>
		</where>
		order by id desc
	</select>
	<select id="queryAllSysRole" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.SysRole">
		select <include refid="Base_Column_List"/> from sys_role
		<where>
			and del_flag = 'N'
			<if test="id != null and !&quot;&quot;.equals(id)">
				and id = #{id}	
			</if>
			<if test="officeId != null and !&quot;&quot;.equals(officeId)">
				and office_id = #{officeId}	
			</if>
			<if test="name != null and !&quot;&quot;.equals(name)">
				and name = #{name}	
			</if>
			<if test="enname != null and !&quot;&quot;.equals(enname)">
				and enname = #{enname}	
			</if>
			<if test="roleType != null and !&quot;&quot;.equals(roleType)">
				and role_type = #{roleType}	
			</if>
			<if test="isSys != null and !&quot;&quot;.equals(isSys)">
				and is_sys = #{isSys}	
			</if>
			<if test="useable != null and !&quot;&quot;.equals(useable)">
				and useable = #{useable}	
			</if>
			<if test="createBy != null and !&quot;&quot;.equals(createBy)">
				and create_by = #{createBy}	
			</if>
			<if test="createDate != null and !&quot;&quot;.equals(createDate)">
				and create_date = #{createDate}	
			</if>
			<if test="updateBy != null and !&quot;&quot;.equals(updateBy)">
				and update_by = #{updateBy}	
			</if>
			<if test="updateDate != null and !&quot;&quot;.equals(updateDate)">
				and update_date = #{updateDate}	
			</if>
			<if test="remarks != null and !&quot;&quot;.equals(remarks)">
				and remarks = #{remarks}	
			</if>
			<if test="delFlag != null and !&quot;&quot;.equals(delFlag)">
				and del_flag = #{delFlag}	
			</if>
			<if test="idStr != null and !&quot;&quot;.equals(idStr)">
				and id in  (${idStr})
			</if>
			<if test="menuIds != null and !&quot;&quot;.equals(menuIds)">
				and menu_ids = #{menuIds}
			</if>
			<if test="permission != null and !&quot;&quot;.equals(permission)">
				and permission = #{permission}
			</if>
		</where>
		order by id desc
	</select>
	<select id="querySysRole" parameterType="java.util.Map" resultType="com.izouma.awesomeadmin.model.SysRole">
		select <include refid="Base_Column_List"/> from sys_role
		<where>
			and del_flag = 'N'
			<if test="id != null and !&quot;&quot;.equals(id)">
				and id = #{id}	
			</if>
			<if test="officeId != null and !&quot;&quot;.equals(officeId)">
				and office_id = #{officeId}	
			</if>
			<if test="name != null and !&quot;&quot;.equals(name)">
				and name = #{name}	
			</if>
			<if test="enname != null and !&quot;&quot;.equals(enname)">
				and enname = #{enname}	
			</if>
			<if test="roleType != null and !&quot;&quot;.equals(roleType)">
				and role_type = #{roleType}	
			</if>
			<if test="isSys != null and !&quot;&quot;.equals(isSys)">
				and is_sys = #{isSys}	
			</if>
			<if test="useable != null and !&quot;&quot;.equals(useable)">
				and useable = #{useable}	
			</if>
			<if test="createBy != null and !&quot;&quot;.equals(createBy)">
				and create_by = #{createBy}	
			</if>
			<if test="createDate != null and !&quot;&quot;.equals(createDate)">
				and create_date = #{createDate}	
			</if>
			<if test="updateBy != null and !&quot;&quot;.equals(updateBy)">
				and update_by = #{updateBy}	
			</if>
			<if test="updateDate != null and !&quot;&quot;.equals(updateDate)">
				and update_date = #{updateDate}	
			</if>
			<if test="remarks != null and !&quot;&quot;.equals(remarks)">
				and remarks = #{remarks}	
			</if>
			<if test="delFlag != null and !&quot;&quot;.equals(delFlag)">
				and del_flag = #{delFlag}	
			</if>
			<if test="menuIds != null and !&quot;&quot;.equals(menuIds)">
				and menu_ids = #{menuIds}
			</if>
			<if test="permission != null and !&quot;&quot;.equals(permission)">
				and permission = #{permission}
			</if>
		</where>
		LIMIT 1
	</select>
    <select id="selectByPrimaryKeys" resultType="com.izouma.awesomeadmin.model.SysRole">
		select
		<include refid="Base_Column_List" />
		from sys_role
		where id in (${ids})
		and del_flag = 'N'
	</select>
    <select id="getUserRoles" resultType="com.izouma.awesomeadmin.model.SysRole">
		select
		<include refid="Base_Column_List" />
		from sys_role
		where id in (select role_id from sys_user_role where user_id = #{userId})
		and del_flag = 'N'
	</select>
    <update id="delete">
		UPDATE sys_role SET del_flag = 'Y'
		<where>
			AND id = #{id}
		</where>
	</update>
	<insert id="setUserRoles">
		insert into sys_user_role (user_id, role_id)
		values
		<foreach collection="roles" item="roleId" separator=",">
			(#{userId}, #{roleId})
		</foreach>
	</insert>
    <delete id="clearUserRoles">
		delete from sys_user_role where user_id = #{userId}
	</delete>
	<insert id="addUserRole">
		insert into sys_user_role (user_id, role_id) values (#{userId}, #{roleId})
	</insert>
	<delete id="removeUserRole">
		delete from sys_user_role when user_id = #{userId} and role_id = #{roleId}
	</delete>
	<select id="getUserRoleId" resultType="java.lang.String">
		SELECT group_concat(role_id) FROM sys_user_role WHERE user_id = #{id}
	</select>
</mapper>

