package com.qqchen.deploy.backend.system.repository;

import com.qqchen.deploy.backend.framework.repository.IBaseRepository;
import com.qqchen.deploy.backend.system.entity.Department;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface IDepartmentRepository extends IBaseRepository<Department, Long> {
    
    List<Department> findByParentIdAndDeletedFalseOrderBySort(Long parentId);
    
    List<Department> findByDeletedFalseOrderBySort();
    
    boolean existsByCodeAndDeletedFalse(String code);
    
    boolean existsByNameAndDeletedFalse(String name);

    @Query("SELECT COALESCE(MAX(d.sort), 0) FROM Department d WHERE d.parentId = :parentId AND d.deleted = false")
    Integer findMaxSortByParentId(@Param("parentId") Long parentId);
} 