/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.system.repository;

import me.zhengjie.modules.dm.system.domain.DmSystem;
import me.zhengjie.modules.dm.system.service.dto.DmSystemDto;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
* @website https://el-admin.vip
* @author Li Rui
* @date 2021-11-30
**/
public interface DmSystemRepository extends JpaRepository<DmSystem, Integer>, JpaSpecificationExecutor<DmSystem> {

    /**
     * 根据角色查询
     *
     * @param roleIds
     * @return
     */
    @Query(value = "select s.* from dm_system as s left join dm_role_system as rs on s.id = rs.system_id left join sys_role as r on rs.role_id = r.role_id where r.role_id in (:roleIds) and if(:isMenu IS NULL,1=1,s.is_menu = :isMenu) GROUP BY id", nativeQuery = true)
    List<DmSystem> findByRoleId(@Param("roleIds") List<Long> roleIds, @Param("isMenu") Long isMenu);

    /**
     * 返回全部的子系统
     * @return
     */
    @Query(value = "select s.* from dm_system as s", nativeQuery = true)
    List<DmSystem> getSystems();

}