<?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="cn.wisenergy.mapper.WorkUserRoleMapper">
    <insert id="insertBatch">
        INSERT INTO work_user_role
        (user_id,role_id)
        VALUES
        <foreach collection ="roleIds" item="roleId" separator =",">
            ( #{userId}, #{roleId})
        </foreach >


    </insert>
    <delete id="deleteByUserId">
        DELETE
        FROM work_user_role
        WHERE user_id = #{userId}
    </delete>


    <select id="getRole" resultType="java.lang.Integer">
        SELECT role_id
        FROM work_user_role
        WHERE user_id = #{userId}
    </select>

    <select id="getRoleUserLevel" resultType="cn.wisenergy.model.dto.UserRoleLevelDto">
select ur.user_id as 'user_id',r.id as 'role_id',l.name as 'level_name',l.rank as 'rank'
from work_user_role ur join work_role r on ur.role_id = r.id
join work_role_level rl on rl.role_id = r.id JOIN work_level l on l.id = rl.level_id
WHERE ur.user_id = #{userId}
ORDER BY rank desc


    </select>
    <select id="getUserRoleDeptDtoByRoleId" resultType="cn.wisenergy.model.app.WorkUser">
        select u.id,u.name,u.dept_id
        from work_user_role r left JOIN work_user u on r.user_id = u.id
        <where>
            u.status = 1
            <if test="null != roleId">
            AND  r.role_id = #{roleId}
            </if>
            group by r.user_id
        </where>
        ORDER BY CONVERT( u.name USING GBK ) ASC
    </select>
</mapper>