<?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.qmth.sop.business.mapper.SysUserRoleMapper">

    <select id="listRoleByUserId" resultType="com.qmth.sop.business.entity.SysRole">
        SELECT * FROM sys_role a
        WHERE EXISTS(SELECT 1 FROM sys_user_role b WHERE a.id = b.role_id AND b.user_id = #{userId})
    </select>

    <select id="listByRoleId" resultType="com.qmth.sop.business.entity.SysUserRole">
        select * from sys_user_role sur
        join sys_user su on su.id = sur.user_id
        <where>
            <if test="roleId != null and roleId != ''">
                and sur.role_id = #{roleId}
            </if>
        </where>
    </select>

    <select id="userListByRoleType" resultType="com.qmth.sop.business.bean.dto.UserDto">
        select su.*,so.name as orgName from sys_user su
        join sys_user_role sur on su.id = sur.user_id
        join sys_org so on so.id = su.org_id
        join sys_role sr on sr.id = sur.role_id
        <where> 1 = 1
            <if test="roleTypes != null and roleTypes != '' and roleTypes.size() > 0">
                and sr.type IN
                <foreach collection="roleTypes" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
