package com.izouma.jmrh.repo;

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.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.Transient;
import java.util.List;

public interface MenuRepo extends JpaRepository<Menu, Long>, JpaSpecificationExecutor<Menu> {
    List<Menu> findByRootTrue();
    @Where(clause = "enabled = 1")
    List<Menu> findByNameAndRootTrue(String name);

    List<Menu> findByEnabledTrue();
    @Query(nativeQuery = true, value = "SELECT ifnull(max(sort + 1),1) FROM menu")
    int nextSort();
    @Query(value = "SELECT\n" +
            "\t*\n" +
            "FROM\n" +
            "\tmenu \n" +
            "WHERE\n" +
            "\tid in (SELECT\n" +
            "\tid\n" +
            "FROM\n" +
            "\tmenu \n" +
            "WHERE\n" +
            "\tFIND_IN_SET ( id, ?1 ) \n" +
            "\tAND enabled = 1 UNION\n" +
            "SELECT DISTINCT\n" +
            "\t (parent) id \n" +
            "FROM\n" +
            "\tmenu \n" +
            "WHERE\n" +
            "\tFIND_IN_SET ( id, ?1 ) \n" +
            "\tAND enabled = 1) \n" +
            "\tAND enabled = 1\n" +
            "\n ",nativeQuery = true)
   List<Menu> findByInId(String Ids);
}
