package com.jd.lightapplication.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jd.lightapplication.model.SysUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author WaiterXiaoYY
 * @since 2022-01-13
 */
@Repository
public interface SysUserMapper extends BaseMapper<SysUser> {

    @Select("<script>" +
            "SELECT DISTINCT rm.menu_id" +
            " FROM sys_user_role ur" +
            " LEFT JOIN sys_role_menu rm" +
            " ON ur.role_id = rm.role_id" +
            " WHERE 1 = 1" +
            "<if test=\"userId != null &amp;&amp; userId > 0\">" +
            " and ur.user_id = #{userId}" +
            "</if>" +
            "</script>")
    List<Long> getNavMenuIds(@Param("userId") Long userId);

    @Select("SELECT DISTINCT su.*" +
            " FROM sys_user_role ur" +
            " LEFT JOIN sys_role_menu rm" +
            " ON ur.role_id = rm.role_id" +
            " LEFT JOIN sys_user su ON ur.user_id = su.id" +
            " WHERE rm.menu_id = #{menuId}")
    List<SysUser> listByMenuId(@Param("menuId") Long menuId);

    @Select("SELECT su.* FROM sys_user su" +
            " INNER JOIN sys_user_role sur" +
            " ON su.id = sur.user_id" +
            " INNER JOIN sys_role sr" +
            " ON sur.role_id = sr.id" +
            " WHERE sr.code = 'GUEST'")
    List<SysUser> queryGuestUserAll();
}
