<?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.bonus.canteen.core.auth.user.mapper.MgrUserRoleMapper">
    <resultMap id="mgrUserRoleMap" type="com.bonus.canteen.core.auth.user.entity.MgrUserRole">
        <id property="userId" column="user_id"/>
        <result property="roleId" column="role_id"/>
    </resultMap>
    <select id="listRoles" resultType="java.lang.String">
        SELECT r.role_code
        FROM mgr_role r
        WHERE EXISTS(SELECT 1 FROM mgr_user_role u WHERE u.user_id = #{userId} AND r.role_id = u.role_id)
          AND r.del_flag = 2
    </select>

    <select id="getMgrUserRoleListByUserId" resultType="com.bonus.canteen.core.auth.user.vo.MgrUserRoleInfoVO">
        SELECT r.role_id,
               r.role_code,
               r.role_type,
               ur.if_default
        FROM mgr_role r
            JOIN mgr_user_role ur ON ur.role_id = r.role_id
        WHERE r.del_flag = 2 AND ur.user_id = #{userId}
    </select>

    <update id="updateDefaultByRoleCode">
        UPDATE mgr_user_role ur , mgr_role r
        SET ur.if_default = #{ifDefault}
        WHERE ur.user_id = #{userId}
          AND ur.role_id = r.role_id
          AND r.role_code = #{roleCode}
    </update>

    <select id="getUserRoleVOByUserIdList" resultType="com.bonus.canteen.core.auth.user.vo.MgrUserRolePageVO">
        SELECT mur.user_id,
               mr.role_id,
               mr.role_code,
               mr.role_name,
               mr.role_desc
        FROM mgr_user_role mur
                 LEFT JOIN mgr_role mr ON mur.role_id = mr.role_id
        where mur.user_id IN
        <foreach collection="userIdList" open="(" close=")" item="userId" separator=",">
            #{userId}
        </foreach>
    </select>
</mapper>
