package com.qqchen.deploy.backend.system.repository;

import com.qqchen.deploy.backend.system.entity.Menu;
import com.qqchen.deploy.backend.framework.repository.IBaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface IMenuRepository extends IBaseRepository<Menu, Long> {

    /**
     * 查询所有未删除的菜单，按排序字段排序
     */
    List<Menu> findByDeletedFalseOrderBySort();

    /**
     * 根据角色ID列表查询对应的菜单
     */
    @Query(value = "SELECT DISTINCT m.* FROM sys_menu m " +
           "INNER JOIN sys_role_menu rm ON m.id = rm.menu_id " +
           "WHERE rm.role_id IN :roleIds " +
           "AND m.enabled = true " +
           "AND m.deleted = false " +
           "ORDER BY m.sort", nativeQuery = true)
    List<Menu> findByRoleIdsAndEnabledTrue(@Param("roleIds") List<Long> roleIds);

    /**
     * 检查菜单编码是否存在
     */
    boolean existsByPermissionAndDeletedFalse(String permission);

    /**
     * 检查同级菜单下是否存在相同名称的菜单
     */
    boolean existsByNameAndParentIdAndDeletedFalse(String name, Long parentId);
}