package com.ywt.mg.domain.entities;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface MgMenusRepository extends PagingAndSortingRepository<MgMenus, Integer> {

    /**
     * 判断表中是否有引用mg_sys_module对应的id
     *
     * @param id
     * @return
     */
    @Query(value = " SELECT count(1) FROM mg_menus WHERE sys_module_id = ? ", nativeQuery = true)
    int checkSysModuleCount(int id);

    /**
     * 判断表中是否有引用mg_sys_module对应的id
     *
     * @param id
     * @return
     */
    @Query(value = " SELECT count(1) FROM mg_menus WHERE parent_id = ? ", nativeQuery = true)
    int checkSunCount(int id);


    /**
     * 根据sys_module_id得到所有符合条件的mg_menus对象
     *
     * @param sysModuleId
     * @return
     */
    @Query(value = " SELECT * FROM mg_menus WHERE sys_module_id = ?  ", nativeQuery = true)
    List<MgMenus> findAllMenusBySysModuleId(int sysModuleId);


    /**
     * 所有mg_menus对象
     *
     * @return
     */
    @Query(value = " SELECT * FROM mg_menus ", nativeQuery = true)
    List<MgMenus> findAllMenus();

    /**
     * 根据父类得到所有的子菜单
     *
     * @param id
     * @return
     */
    @Query(value = " SELECT * FROM mg_menus WHERE parent_id = ? ", nativeQuery = true)
    List<MgMenus> findAllMenusByParentId(int id);

    /**
     * 根据菜单code得到菜单记录
     *
     * @param code
     * @return
     */
    @Query(value = " SELECT * FROM mg_menus WHERE code = ? limit 1; ", nativeQuery = true)
    MgMenus getMgMenusByCode(String code);

}
