<?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.yunfeiyun.agmp.common.web.system.mapper.SysRoleMapper">

	<resultMap type="SysRole" id="SysRoleResult">
		<id     property="roleId"             column="roleId"               />
		<result property="roleName"           column="roleName"             />
		<result property="roleKey"            column="roleKey"              />
		<result property="roleSeq"           column="roleSeq"             />
		<result property="roleDatascope"          column="roleDatascope"            />
		<result property="roleMenucheckstrictly"  column="roleMenucheckstrictly"   />
		<result property="roleDeptcheckstrictly"  column="roleDeptcheckstrictly"   />
		<result property="roleAvlstatus"             column="roleAvlstatus"                />
		<result property="roleDelstatus"            column="roleDelstatus"              />
		<result property="roleCreator"           column="roleCreator"             />
		<result property="roleCreateddate"         column="roleCreateddate"           />
		<result property="roleModifier"           column="roleModifier"             />
		<result property="roleModifieddate"         column="roleModifieddate"           />
		<result property="roleRemark"             column="roleRemark"                />
	</resultMap>
	
	<sql id="selectRoleVo">
	    select distinct r.roleId, r.roleName, r.roleKey, r.roleSeq, r.roleDatascope, r.roleMenucheckstrictly, r.roleDeptcheckstrictly,
            r.roleAvlstatus, r.roleDelstatus, r.roleCreateddate, r.roleRemark
        from SysRole r
	        left join SysUserrole ur on ur.roleId = r.roleId
	        left join SysUser u on u.userId = ur.userId
	        left join SysDept d on u.deptId = d.deptId
    </sql>
    
    <select id="selectRoleList" parameterType="SysRole" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		where r.roleDelstatus = '0'
		<if test="roleId != null and roleId != ''">
			AND r.roleId = #{roleId}
		</if>
		<if test="roleName != null and roleName != ''">
			AND r.roleName like concat('%', #{roleName}, '%')
		</if>
		<if test="roleAvlstatus != null and roleAvlstatus != ''">
			AND r.roleAvlstatus = #{roleAvlstatus}
		</if>
		<if test="roleKey != null and roleKey != ''">
			AND r.roleKey like concat('%', #{roleKey}, '%')
		</if>
		<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
			and date_format(r.roleCreateddate,'%y%m%d') &gt;= date_format(#{params.beginTime},'%y%m%d')
		</if>
		<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
			and date_format(r.roleCreateddate,'%y%m%d') &lt;= date_format(#{params.endTime},'%y%m%d')
		</if>
		<!-- 数据范围过滤 -->
		${params.dataScope}
		order by r.roleSeq
	</select>
    
	<select id="selectRolePermissionByUserId" parameterType="String" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.roleDelstatus = '0' and ur.userId = #{userId}
	</select>
	
	<select id="selectRoleAll" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
	</select>
	
	<select id="selectRoleListByUserId" parameterType="String" resultType="Long">
		select r.roleId
        from SysRole r
	        left join SysUserrole ur on ur.roleId = r.roleId
	        left join SysUser u on u.userId = ur.userId
	    where u.userId = #{userId}
	</select>
	
	<select id="selectRoleById" parameterType="String" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		where r.roleId = #{roleId}
	</select>
	
	<select id="selectRolesByUserName" parameterType="String" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		WHERE r.roleDelstatus = '0' and u.userLoginname = #{userName}
	</select>
	
	<select id="checkRoleNameUnique" parameterType="String" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		 where r.roleName=#{roleName} and r.roleDelstatus = '0' limit 1
	</select>
	
	<select id="checkRoleKeyUnique" parameterType="String" resultMap="SysRoleResult">
		<include refid="selectRoleVo"/>
		 where r.roleKey=#{roleKey} and r.roleDelstatus = '0' limit 1
	</select>
	
 	<insert id="insertRole" parameterType="SysRole" useGeneratedKeys="true" keyProperty="roleId">
 		insert into SysRole(
 			<if test="roleId != null and roleId != ''">roleId,</if>
 			<if test="roleName != null and roleName != ''">roleName,</if>
 			<if test="roleKey != null and roleKey != ''">roleKey,</if>
 			<if test="roleSeq != null">roleSeq,</if>
 			<if test="roleDatascope != null and roleDatascope != ''">roleDatascope,</if>
 			<if test="roleMenucheckstrictly != null">roleMenucheckstrictly,</if>
 			<if test="roleDeptcheckstrictly != null">roleDeptcheckstrictly,</if>
 			<if test="roleAvlstatus != null and roleAvlstatus != ''">roleAvlstatus,</if>
 			<if test="roleRemark != null and roleRemark != ''">roleRemark,</if>
 			<if test="roleCreator != null and roleCreator != ''">roleCreator,</if>
		roleCreateddate
 		)values(
 			<if test="roleId != null and roleId != ''">#{roleId},</if>
 			<if test="roleName != null and roleName != ''">#{roleName},</if>
 			<if test="roleKey != null and roleKey != ''">#{roleKey},</if>
 			<if test="roleSeq != null">#{roleSeq},</if>
 			<if test="roleDatascope != null and roleDatascope != ''">#{roleDatascope},</if>
 			<if test="roleMenucheckstrictly != null">#{roleMenucheckstrictly},</if>
 			<if test="roleDeptcheckstrictly != null">#{roleDeptcheckstrictly},</if>
 			<if test="roleAvlstatus != null and roleAvlstatus != ''">#{roleAvlstatus},</if>
 			<if test="roleRemark != null and roleRemark != ''">#{roleRemark},</if>
 			<if test="roleCreator != null and roleCreator != ''">#{roleCreator},</if>
		date_format(sysdate(),'%Y-%m-%d %H:%i:%s')
 		)
	</insert>
	
	<update id="updateRole" parameterType="SysRole">
 		update SysRole
 		<set>
 			<if test="roleName != null and roleName != ''">roleName = #{roleName},</if>
 			<if test="roleKey != null and roleKey != ''">roleKey = #{roleKey},</if>
 			<if test="roleSeq != null">roleSeq = #{roleSeq},</if>
 			<if test="roleDatascope != null and roleDatascope != ''">roleDatascope = #{roleDatascope},</if>
 			<if test="roleMenucheckstrictly != null">roleMenucheckstrictly = #{roleMenucheckstrictly},</if>
 			<if test="roleDeptcheckstrictly != null">roleDeptcheckstrictly = #{roleDeptcheckstrictly},</if>
 			<if test="roleAvlstatus != null and roleAvlstatus != ''">roleAvlstatus = #{roleAvlstatus},</if>
 			<if test="roleRemark != null">roleRemark = #{roleRemark},</if>
 			<if test="roleModifier != null and roleModifier != ''">roleModifier = #{roleModifier},</if>
			roleModifieddate = date_format(sysdate(),'%Y-%m-%d %H:%i:%s')
 		</set>
 		where roleId = #{roleId}
	</update>
	
	<delete id="deleteRoleById" parameterType="String">
 		update SysRole set roleDelstatus = '2' where roleId = #{roleId}
 	</delete>
 	
 	<delete id="deleteRoleByIds" parameterType="String">
 	    update SysRole set roleDelstatus = '2' where roleId in
 		<foreach collection="array" item="roleId" open="(" separator="," close=")">
 			#{roleId}
        </foreach> 
 	</delete>
 	
</mapper> 