<?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.aidex.system.mapper.SysRoleMapper">


	<sql id="roleColumns">
		t.id as "id",
		t.role_name AS "roleName",
		t.role_key AS "roleKey",
		t.sort AS "sort",
		t.data_scope  AS "dataScope",
		t.menu_check_strictly  AS "menuCheckStrictly",
		t.dept_check_strictly  AS "deptCheckStrictly",
		t.status AS "status",
		t.create_by AS "createBy",
		t.create_dept AS "createDept",
		t.create_time AS "createTime",
		t.update_by AS "updateBy",
		t.update_time AS "updateTime",
		t.update_ip AS "updateIp",
		t.remark AS "remark",
		t.version AS "version",
		t.del_flag AS "delFlag"
	</sql>

	<sql id="selectRoleVo">
	    select distinct <include refid="roleColumns"/>
        from sys_role t
	        left join sys_user_role ur on ur.role_id = t.id
	        left join sys_user u on u.id = ur.user_id
	        left join sys_dept d on u.dept_id = d.id
    </sql>

    <select id="findList" resultType="SysRole">
		<include refid="selectRoleVo"/>
		where t.del_flag = #{DEL_FLAG_NORMAL}
		<if test="roleName != null and roleName != ''">
			AND t.role_name like concat('%', #{roleName}, '%')
		</if>
		<if test="status != null and status != ''">
			AND t.status = #{status}
		</if>
		<if test="roleKey != null and roleKey != ''">
			AND t.role_key like concat('%', #{roleKey}, '%')
		</if>
		<if test="codeOrName != null and codeOrName != ''">
			AND (t.role_name like concat('%', #{codeOrName}, '%') or t.role_key like concat('%', #{codeOrName}, '%'))
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			and date_format(t.create_time,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			and date_format(t.create_time,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		order by t.sort, t.create_time
	</select>

	<select id="selectRolePermissionByUserId" parameterType="String" resultType="SysRole">
		<include refid="selectRoleVo"/>
		WHERE t.del_flag = #{DEL_FLAG_NORMAL} and ur.user_id = #{userId}
	</select>

	<select id="selectRoleAll" resultType="SysRole">
		<include refid="selectRoleVo"/>
	</select>

	<select id="selectRoleListByUserId" parameterType="String" resultType="String">
		select t.id
        from sys_role t
	        left join sys_user_role ur on ur.role_id = t.id
	        left join sys_user u on u.id = ur.user_id
	    where u.id = #{userId}
	</select>

	<select id="get" resultType="SysRole">
		<include refid="selectRoleVo"/>
		where t.id =  #{id}
	</select>

	<select id="selectRolesByUserName" resultType="SysRole">
		<include refid="selectRoleVo"/>
		WHERE t.del_flag = #{DEL_FLAG_NORMAL} and u.user_name = #{userName}
	</select>

	<select id="findListWithUnique" resultType="SysRole">
		SELECT
			1
		FROM sys_role t
		where t.del_flag = #{DEL_FLAG_NORMAL}
		<if test="roleName != null and roleName != ''">
			AND t.role_name = #{roleName}
		</if>
		<if test="roleKey != null and roleKey != ''">
			AND t.role_key = #{roleKey}
		</if>
		<if test="notEqualId != null and notEqualId != ''">
			AND t.id != #{notEqualId}
		</if>
	</select>

	<insert id="insert" >
		insert into sys_role(
		id,
		role_name,
		role_key,
		sort,
		data_scope,
		menu_check_strictly,
		dept_check_strictly,
		status,
		create_by,
		create_dept,
		create_time,
		update_by,
		update_time,
		update_ip,
		remark,
		version,
		del_flag
		)values(
			#{id},
			#{roleName},
			#{roleKey},
			#{sort},
			#{dataScope},
			#{menuCheckStrictly},
			#{deptCheckStrictly},
			#{status},
			#{createBy},
			#{createDept},
			#{createTime},
			#{updateBy},
			#{updateTime},
			#{updateIp},
			#{remark},
			1,
			#{DEL_FLAG_NORMAL}
		)
	</insert>
	<update id="update">
		update sys_role
		<set>
			<if test="roleName != null and roleName != ''">	role_name = #{roleName}, </if>
			<if test="roleKey != null and roleKey != ''"> role_key = #{roleKey}, </if>
			<if test="sort != null and sort != ''"> sort = #{sort}, </if>
			<if test="dataScope != null and dataScope != ''"> data_scope = #{dataScope}, </if>
			<if test="menuCheckStrictly != null">menu_check_strictly = #{menuCheckStrictly},</if>
			<if test="deptCheckStrictly != null">dept_check_strictly = #{deptCheckStrictly},</if>
			<if test="status != null and status != ''">status = #{status}, </if>
			<if test="updateBy != null and updateBy != ''">update_by = #{updateBy}, </if>
			<if test="updateTime != null">update_time = #{updateTime}, </if>
			<if test="updateIp != null and updateIp != ''">update_ip = #{updateIp}, </if>
			<if test="remark != null and remark != ''">remark = #{remark}, </if>
			version = version + 1
		</set>
		where id = #{id}  and version = #{version}
	</update>

	<update id="updateRoleStatus" parameterType="SysRole">
 		update sys_user set status = #{status} where id = #{userId}
	</update>


	<delete id="delete">
		UPDATE sys_role SET
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</delete>

 	<delete id="deleteRoleByIds" parameterType="String">
 	    update sys_role set del_flag = '1' where id in
 		<foreach collection="array" item="roleId" open="(" separator="," close=")">
 			#{roleId}
        </foreach>
 	</delete>

	<select id="findMaxSort" resultType="Integer">
		SELECT max(a.sort)
		FROM sys_role a
		WHERE del_flag = #{DEL_FLAG_NORMAL}
	</select>

	<delete id="deleteRoleById" parameterType="String">
		update sys_role set del_flag = '1' where id = #{roleId}
	</delete>
</mapper>
