package com.huimv.admin.repo.primary;

import com.huimv.admin.domain.primary.SysMenuEntity;
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 SysMenuEntityRepository extends JpaRepository<SysMenuEntity, Integer>, JpaSpecificationExecutor<SysMenuEntity> {

    @Query(nativeQuery = true , value = "SELECT * FROM sys_menu WHERE account_name=?1 AND password=?2")
    List<SysMenuEntity> findByAccountId(Integer accountId);

    @Query(nativeQuery = true , value = "SELECT * FROM sys_menu WHERE parent_id = ?1 ORDER BY sort ASC")
    List<SysMenuEntity> findAllByParentId(Integer id);

//    @Query(nativeQuery = true,value = "SELECT * FROM sys_menu WHERE id IN(SELECT DISTINCT(menu_id) FROM sys_group_menu WHERE group_id IN(SELECT group_id FROM sys_account_group WHERE account_id=(SELECT user1.id FROM sys_account account,sys_user user1 WHERE account.account_name=?1 AND account.user_id=user1.user_id  LIMIT 1)))")
    @Query(nativeQuery = true,value = "SELECT * FROM sys_menu WHERE id IN(SELECT DISTINCT(menu_id) FROM sys_group_menu WHERE group_id IN(SELECT group_id FROM sys_account_group WHERE account_id=(SELECT user1.id FROM sys_account account,sys_user user1 WHERE account.account_name=?1 AND account.user_id=user1.user_id))) ORDER BY sort ASC")
    List<SysMenuEntity> getMyMenu(String accountName);
}