package com.izouma.jmrh.repo;

import com.izouma.jmrh.domain.DataCenterMenu;
import com.izouma.jmrh.domain.Menu;
import org.hibernate.annotations.Where;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface DataCenterMenuRepo extends JpaRepository<DataCenterMenu, Long>, JpaSpecificationExecutor<DataCenterMenu> {
    List<DataCenterMenu> findByRootTrue();

    @Where(clause = "enabled = 1")
    List<DataCenterMenu> findByNameAndRootTrue(String name);

    List<DataCenterMenu> findByEnabledTrue();

    @Query(nativeQuery = true, value = "SELECT ifnull(max(sort + 1),1) FROM data_center_menu")
    int nextSort();

    @Query(value = "SELECT * FROM data_center_menu " +
            "WHERE id in " +
            "  (SELECT id FROM data_center_menu " +
            "   WHERE FIND_IN_SET ( id, ?1 )  " +
            "   AND enabled = 1 " +
            "   UNION " +
            "   SELECT DISTINCT (parent) id  FROM data_center_menu " +
            "   WHERE FIND_IN_SET ( id, ?1 )  AND enabled = 1) AND enabled = 1 ", nativeQuery = true)
    List<DataCenterMenu> findByInId(String Ids);
}
