<?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.bootdo.system.dao.RoleDao">

	<select id="get" resultType="com.bootdo.system.domain.RoleDO">
		select
			`role_id`,`role_name`,`role_sign`,`remark`,`role_type`,`user_id_create`,`gmt_create`,`gmt_modified`
		from sys_role where role_id = #{value}
	</select>

	<select id="getExamineRole" resultType="com.bootdo.system.domain.RoleDO">
		select distinct
		sys_role.`role_id`,sys_role.`role_name`,sys_role.`role_sign`,sys_role.`remark`,sys_role.`role_type`,
		sys_role.`user_id_create`,sys_role.`gmt_create`,sys_role.`gmt_modified`
		from sys_user
		left join sys_user_role on sys_user_role.user_id = sys_user.user_id
		left join sys_role on sys_role.role_id = sys_user_role.role_id
		where sys_role.role_type = 1 and sys_user.user_id = #{userId} limit 1
	</select>

	<select id="getExamineList" resultType="com.bootdo.system.domain.RoleDO">
		select `role_id`,`role_name`,`role_sign`,`remark`,`role_type`,`user_id_create`,`gmt_create`,`gmt_modified`
		from sys_role
		where role_type = 1
	</select>

	<select id="list" resultType="com.bootdo.system.domain.RoleDO">
		select
		`role_id`,`role_name`,`role_sign`,`remark`,`role_type`,`user_id_create`,`gmt_create`,`gmt_modified`
		from sys_role
		<where>
			<if test="roleId != null and roleId != ''"> and role_id = #{roleId} </if>
			<if test="roleName != null and roleName != ''"> and role_name = #{roleName} </if>
			<if test="roleSign != null and roleSign != ''"> and role_sign = #{roleSign} </if>
			<if test="remark != null and remark != ''"> and remark = #{remark} </if>
			<if test="roleType != null and roleType != ''"> and role_type = #{roleType} </if>
			<if test="userIdCreate != null and userIdCreate != ''"> and user_id_create = #{userIdCreate} </if>
			<if test="gmtCreate != null and gmtCreate != ''"> and gmt_create = #{gmtCreate} </if>
			<if test="gmtModified != null and gmtModified != ''"> and gmt_modified = #{gmtModified} </if>
		</where>
		<choose>
			<when test="sort != null and sort.trim() != ''">
				order by ${sort} ${order}
			</when>
			<otherwise>
				order by role_id desc
			</otherwise>
		</choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>

	<select id="count" resultType="int">
		select count(*) from sys_role
		<where>
			<if test="roleId != null and roleId != ''"> and role_id = #{roleId} </if>
			<if test="roleName != null and roleName != ''"> and role_name = #{roleName} </if>
			<if test="roleSign != null and roleSign != ''"> and role_sign = #{roleSign} </if>
			<if test="remark != null and remark != ''"> and remark = #{remark} </if>
			<if test="roleType != null and roleType != ''"> and role_type = #{roleType} </if>
			<if test="userIdCreate != null and userIdCreate != ''"> and user_id_create = #{userIdCreate} </if>
			<if test="gmtCreate != null and gmtCreate != ''"> and gmt_create = #{gmtCreate} </if>
			<if test="gmtModified != null and gmtModified != ''"> and gmt_modified = #{gmtModified} </if>
		</where>
	</select>

	<insert id="save" parameterType="com.bootdo.system.domain.RoleDO"
		useGeneratedKeys="true" keyProperty="roleId">
		insert into sys_role
		(
		`role_name`,
		`role_sign`,
		`remark`,
		`role_type`,
		`user_id_create`,
		`gmt_create`,
		`gmt_modified`
		)
		values
		(
		#{roleName},
		#{roleSign},
		#{remark},
		#{roleType},
		#{userIdCreate},
		#{gmtCreate},
		#{gmtModified}
		)
	</insert>

	<update id="update" parameterType="com.bootdo.system.domain.RoleDO">
		update sys_role
		<set>
			<if test="roleName != null">`role_name` = #{roleName}, </if>
			<if test="roleSign != null">`role_sign` = #{roleSign}, </if>
			<if test="remark != null">`remark` = #{remark}, </if>
			<if test="roleType != null">`role_type` = #{roleType}, </if>
			<if test="userIdCreate != null">`user_id_create` = #{userIdCreate}, </if>
			<if test="gmtCreate != null">`gmt_create` = #{gmtCreate}, </if>
			<if test="gmtModified != null">`gmt_modified` = #{gmtModified}</if>
		</set>
		where role_id = #{roleId}
	</update>

	<delete id="remove">
		delete from sys_role where role_id = #{value}
	</delete>

	<delete id="batchRemove">
		delete from sys_role where role_id in
		<foreach item="roleId" collection="array" open="(" separator=","
			close=")">
			#{roleId}
		</foreach>
	</delete>

</mapper>