package com.hertz.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hertz.modules.system.entity.SysMenu;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {
  @Select("""
      SELECT DISTINCT m.*
      FROM sys_menu m
      INNER JOIN sys_role_menu rm ON rm.menu_id = m.id
      INNER JOIN sys_user_role ur ON ur.role_id = rm.role_id
      WHERE ur.user_id = #{userId}
        AND m.status = 1
        AND m.visible = 1
        AND m.type IN ('D','M','B')
      ORDER BY m.sort ASC, m.id ASC
      """)
  List<SysMenu> selectMenusByUserId(@Param("userId") long userId);

  @Select("""
      SELECT m.*
      FROM sys_menu m
      WHERE m.status = 1
        AND m.visible = 1
        AND m.type IN ('D','M','B')
      ORDER BY m.sort ASC, m.id ASC
      """)
  List<SysMenu> selectAllVisibleMenus();

  @Select("""
      SELECT DISTINCT m.perms
      FROM sys_menu m
      INNER JOIN sys_role_menu rm ON rm.menu_id = m.id
      INNER JOIN sys_user_role ur ON ur.role_id = rm.role_id
      WHERE ur.user_id = #{userId}
        AND m.status = 1
        AND m.perms IS NOT NULL
        AND m.perms <> ''
      """)
  List<String> selectPermsByUserId(@Param("userId") long userId);

  @Select("""
      SELECT DISTINCT m.perms
      FROM sys_menu m
      WHERE m.status = 1
        AND m.perms IS NOT NULL
        AND m.perms <> ''
      """)
  List<String> selectAllPerms();
}

