<?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.saye.hospitalgd.mapper.system.RoleMapper">

    <resultMap id="BaseResultMap" type="com.saye.hospitalgd.model.Role" >
        <id column="role_id" property="roleId" jdbcType="VARCHAR" />
        <result column="role_name" property="roleName" jdbcType="VARCHAR" />
        <result column="remark" property="remark" jdbcType="VARCHAR" />
        <result column="modify_user_name" property="modifyUserName" jdbcType="VARCHAR" />
        <result column="modify_true_name" property="modifyTrueName" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="VARCHAR" />
        <result column="create_date" property="createDate" jdbcType="VARCHAR" />
    </resultMap>
    
    <select id="findRoleList" resultMap="BaseResultMap">
     SELECT ROLE_ID, ROLE_NAME from role
    </select>
    
    <select id="searchRole" resultMap="BaseResultMap">
     select ROLE_ID, ROLE_NAME,REMARK, MODIFY_USER_NAME, MODIFY_TRUE_NAME, substr(CREATE_TIME,0,19) as CREATE_TIME, CREATE_DATE
	FROM role
	 <where>
	 1 = 1
	 <if test="roleName!=null and roleName!=''">
	  and ROLE_NAME like concat('%',concat(#{roleName},'%'))
	 </if>
	 
	  <if test="date!= null  and  date!=''">
	   and CREATE_DATE = #{date}
	 </if>
	</where>
	 order by CREATE_TIME
    </select>
    
   	<!-- 添加角色 -->
    <insert id="addRole" parameterType="hashMap">
    	insert into role(ROLE_ID,ROLE_NAME,REMARK, MODIFY_USER_NAME, MODIFY_TRUE_NAME)
    	values(#{roleId},#{roleName},'',#{userName},#{userTrueName})
    </insert>
    
    <!--删除角色  -->
    <delete id="deleteRole">
    DELETE FROM role WHERE ROLE_ID =#{roleId}
    </delete>

	<!-- 根据条件查询人员是否在管理员角色下 -->
	<select id="findUserRoleListByParam" parameterType="hashMap" resultType="hashMap">
		select * from user_role t left join role r on t.role_id=r.role_id
		where t.user_id=#{userId}
        <if test="role_name!= null  and  role_name!=''">
		  and r.role_name=#{role_name}
        </if>
	</select>

    <update id="updateRole" parameterType="HashMap">
        update role
        set
            ROLE_NAME = #{roleName}
        where role_id = #{roleId}
    </update>

	<select id="findUserRoleListByUserId" parameterType="HashMap" resultType="HashMap">
		select r.* from user_role t left join role r on t.role_id=r.role_id
		where 1=1
		 <if test="userId!= null  and  userId!=''">
			and t.user_id=#{userId}
		 </if>
	</select>
</mapper>