lrj
3 天以前 7ba080d35812e6db7bd5aa8f88161c02653eb6c1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.rongyichuang.employee.repository;
 
import com.rongyichuang.employee.entity.Employee;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
 
import java.util.List;
import java.util.Optional;
 
/**
 * 员工数据访问层
 */
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
 
    /**
     * 根据名称模糊查询员工列表
     */
    @Query("SELECT e FROM Employee e WHERE e.name LIKE %:name%")
    List<Employee> findByNameContaining(@Param("name") String name);
 
    /**
     * 分页查询员工列表,支持名称模糊搜索
     */
    @Query("SELECT e FROM Employee e WHERE (:name IS NULL OR e.name LIKE %:name%) ORDER BY e.createTime DESC")
    Page<Employee> findByNameContainingOrderByCreateTimeDesc(@Param("name") String name, Pageable pageable);
 
    /**
     * 根据手机号查询员工
     */
    Optional<Employee> findByPhone(String phone);
 
    /**
     * 检查手机号是否已存在
     */
    boolean existsByPhone(String phone);
 
    /**
     * 检查手机号是否已存在(排除指定ID)
     */
    @Query("SELECT COUNT(e) > 0 FROM Employee e WHERE e.phone = :phone AND e.id != :id")
    boolean existsByPhoneAndIdNot(@Param("phone") String phone, @Param("id") Long id);
 
    /**
     * 根据角色ID查询员工列表
     */
    List<Employee> findByRoleId(String roleId);
 
    /**
     * 根据用户ID查询员工
     */
    Optional<Employee> findByUserId(Long userId);
}