<?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.keao.edu.auth.dal.dao.SysUserRoleDao">

    <resultMap type="com.keao.edu.auth.api.entity.SysUserRole" id="SysUserRole">
        <result column="user_id_" property="userId"/>
        <result column="role_id_" property="roleId"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="SysUserRole" parameterType="java.util.Map">
		SELECT * FROM sys_user_role WHERE user_id_ = #{userId} AND role_id_ = #{roleId}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="SysUserRole">
		SELECT * FROM sys_user_role ORDER BY user_id_,role_id_
	</select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.keao.edu.auth.api.entity.SysUserRole" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        <!--
        <selectKey resultClass="int" keyProperty="id" >
        SELECT SEQ_WSDEFINITION_ID.nextval AS ID FROM DUAL
        </selectKey>
        -->
        INSERT INTO sys_user_role (user_id_,role_id_) VALUES(#{userId},#{roleId})
    </insert>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete" parameterType="java.util.Map">
		DELETE FROM sys_user_role WHERE user_id = #{userId} AND role_id = #{roleId} 
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="SysUserRole" parameterType="map">
        SELECT * FROM sys_user_role ORDER BY user_id_,role_id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
		SELECT COUNT(*) FROM sys_user_role
	</select>

    <select id="queryRoleIdListByUserId" resultType="int" parameterType="int">
		SELECT ur.role_id_ FROM sys_user_role ur left join sys_role r on ur.role_id_ = r.id_ where ur.user_id_ = #{userId} and r.del_flag_ = 0
	</select>

    <select id="queryRoleCodeListByUserId" resultType="string" parameterType="int">
		SELECT r.role_code_ FROM sys_user_role ur left join sys_role r on ur.role_id_ = r.id_ where ur.user_id_ = #{userId} and r.del_flag_ = 0
	</select>
    <select id="countRoleUser" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM sys_user_role WHERE role_id_ = #{id}
    </select>
</mapper>
