You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.8 KiB

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;
/**
* 员工搜索类 - 负责复杂的员工搜索功能
* 遵循单一职责原则:只负责员工数据的搜索逻辑
*/
public class EmployeeSearch {
/**
* 按薪资范围搜索员工
* @param employees 员工列表
* @param minSalary 最低薪资
* @param maxSalary 最高薪资
* @return 符合条件的员工列表
*/
public List<Employee> searchBySalaryRange(List<Employee> employees, double minSalary, double maxSalary) {
if (minSalary > maxSalary) {
throw new IllegalArgumentException("最低薪资不能大于最高薪资");
}
return employees.stream()
.filter(e -> e.getSalary() >= minSalary && e.getSalary() <= maxSalary)
.collect(Collectors.toList());
}
/**
* 按入职日期范围搜索员工
* @param employees 员工列表
* @param startDate 开始日期
* @param endDate 结束日期
* @return 符合条件的员工列表
*/
public List<Employee> searchByHireDateRange(List<Employee> employees, LocalDate startDate, LocalDate endDate) {
if (startDate.isAfter(endDate)) {
throw new IllegalArgumentException("开始日期不能晚于结束日期");
}
return employees.stream()
.filter(e -> (!e.getHireDate().isBefore(startDate) && !e.getHireDate().isAfter(endDate)))
.collect(Collectors.toList());
}
/**
* 按部门搜索员工
* @param employees 员工列表
* @param department 部门名称
* @return 符合条件的员工列表
*/
public List<Employee> searchByDepartment(List<Employee> employees, String department) {
if (department == null || department.trim().isEmpty()) {
throw new IllegalArgumentException("部门名称不能为空");
}
return employees.stream()
.filter(e -> e.getDepartment().equals(department))
.collect(Collectors.toList());
}
/**
* 按职位搜索员工
* @param employees 员工列表
* @param position 职位名称
* @return 符合条件的员工列表
*/
public List<Employee> searchByPosition(List<Employee> employees, String position) {
if (position == null || position.trim().isEmpty()) {
throw new IllegalArgumentException("职位名称不能为空");
}
return employees.stream()
.filter(e -> e.getPosition().equals(position))
.collect(Collectors.toList());
}
/**
* 按年龄范围搜索员工
* @param employees 员工列表
* @param minAge 最小年龄
* @param maxAge 最大年龄
* @return 符合条件的员工列表
*/
public List<Employee> searchByAgeRange(List<Employee> employees, int minAge, int maxAge) {
if (minAge > maxAge) {
throw new IllegalArgumentException("最小年龄不能大于最大年龄");
}
if (minAge < 0 || maxAge < 0) {
throw new IllegalArgumentException("年龄不能为负数");
}
return employees.stream()
.filter(e -> e.getAge() >= minAge && e.getAge() <= maxAge)
.collect(Collectors.toList());
}
/**
* 按司龄搜索员工
* @param employees 员工列表
* @param minTenure 最小司龄(年)
* @param maxTenure 最大司龄(年)
* @return 符合条件的员工列表
*/
public List<Employee> searchByTenure(List<Employee> employees, int minTenure, int maxTenure) {
if (minTenure > maxTenure) {
throw new IllegalArgumentException("最小司龄不能大于最大司龄");
}
if (minTenure < 0 || maxTenure < 0) {
throw new IllegalArgumentException("司龄不能为负数");
}
return employees.stream()
.filter(e -> e.getTenure() >= minTenure && e.getTenure() <= maxTenure)
.collect(Collectors.toList());
}
/**
* 搜索在职员工
* @param employees 员工列表
* @return 在职员工列表
*/
public List<Employee> searchActiveEmployees(List<Employee> employees) {
return employees.stream()
.filter(Employee::isActive)
.collect(Collectors.toList());
}
/**
* 搜索离职员工
* @param employees 员工列表
* @return 离职员工列表
*/
public List<Employee> searchInactiveEmployees(List<Employee> employees) {
return employees.stream()
.filter(e -> !e.isActive())
.collect(Collectors.toList());
}
/**
* 按员工姓名关键字搜索
* @param employees 员工列表
* @param keyword 姓名关键字
* @return 符合条件的员工列表
*/
public List<Employee> searchByNameKeyword(List<Employee> employees, String keyword) {
if (keyword == null || keyword.trim().isEmpty()) {
throw new IllegalArgumentException("搜索关键字不能为空");
}
String lowerKeyword = keyword.toLowerCase();
return employees.stream()
.filter(e -> e.getName().toLowerCase().contains(lowerKeyword))
.collect(Collectors.toList());
}
/**
* 组合搜索:按部门和薪资范围
* @param employees 员工列表
* @param department 部门名称
* @param minSalary 最低薪资
* @param maxSalary 最高薪资
* @return 符合条件的员工列表
*/
public List<Employee> searchByDepartmentAndSalaryRange(List<Employee> employees,
String department,
double minSalary,
double maxSalary) {
return employees.stream()
.filter(e -> e.getDepartment().equals(department))
.filter(e -> e.getSalary() >= minSalary && e.getSalary() <= maxSalary)
.collect(Collectors.toList());
}
/**
* 组合搜索:按部门和职位
* @param employees 员工列表
* @param department 部门名称
* @param position 职位名称
* @return 符合条件的员工列表
*/
public List<Employee> searchByDepartmentAndPosition(List<Employee> employees,
String department,
String position) {
return employees.stream()
.filter(e -> e.getDepartment().equals(department))
.filter(e -> e.getPosition().equals(position))
.collect(Collectors.toList());
}
public class DepartmentSalaryStats {
}
}