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.
456/EmployeeSystem/EmployeeHistory.java

251 lines
9.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
* 员工历史记录类 - 负责记录员工的历史变更信息
* 遵循单一职责原则:只负责员工变更历史的记录和管理
*/
public class EmployeeHistory {
private final String employeeId;
private final List<ChangeRecord> historyRecords;
/**
* 构造函数
* @param employeeId 员工ID
*/
public EmployeeHistory(String employeeId) {
this.employeeId = employeeId;
this.historyRecords = new ArrayList<>();
}
/**
* 记录薪资调整历史
* @param oldSalary 调整前薪资
* @param newSalary 调整后薪资
* @param reason 调整原因
* @param changedBy 操作人
*/
public void recordSalaryChange(double oldSalary, double newSalary, String reason, String changedBy) {
String description = String.format("薪资从 %.2f 调整为 %.2f", oldSalary, newSalary);
ChangeRecord record = new ChangeRecord(ChangeType.SALARY_ADJUSTMENT,
description, reason, changedBy);
historyRecords.add(record);
}
/**
* 记录职位变更历史
* @param oldPosition 原职位
* @param newPosition 新职位
* @param reason 变更原因
* @param changedBy 操作人
*/
public void recordPositionChange(String oldPosition, String newPosition, String reason, String changedBy) {
String description = String.format("职位从 '%s' 变更为 '%s'", oldPosition, newPosition);
ChangeRecord record = new ChangeRecord(ChangeType.POSITION_CHANGE,
description, reason, changedBy);
historyRecords.add(record);
}
/**
* 记录部门变更历史
* @param oldDepartment 原部门
* @param newDepartment 新部门
* @param reason 变更原因
* @param changedBy 操作人
*/
public void recordDepartmentChange(String oldDepartment, String newDepartment, String reason, String changedBy) {
String description = String.format("部门从 '%s' 变更为 '%s'", oldDepartment, newDepartment);
ChangeRecord record = new ChangeRecord(ChangeType.DEPARTMENT_CHANGE,
description, reason, changedBy);
historyRecords.add(record);
}
/**
* 记录员工状态变更历史(在职/离职)
* @param newStatus 新状态
* @param reason 变更原因
* @param changedBy 操作人
*/
public void recordStatusChange(boolean newStatus, String reason, String changedBy) {
String description = "状态变更为 " + (newStatus ? "在职" : "离职");
ChangeRecord record = new ChangeRecord(ChangeType.STATUS_CHANGE,
description, reason, changedBy);
historyRecords.add(record);
}
/**
* 记录员工基本信息变更历史
* @param fieldName 变更的字段名
* @param oldValue 旧值
* @param newValue 新值
* @param changedBy 操作人
*/
public void recordInfoChange(String fieldName, String oldValue, String newValue, String changedBy) {
String description = String.format("%s 从 '%s' 变更为 '%s'", fieldName, oldValue, newValue);
ChangeRecord record = new ChangeRecord(ChangeType.INFO_CHANGE,
description, "信息更新", changedBy);
historyRecords.add(record);
}
/**
* 记录培训完成历史
* @param courseName 课程名称
* @param certificateId 证书ID
* @param score 成绩
* @param changedBy 操作人
*/
public void recordTrainingCompletion(String courseName, String certificateId, double score, String changedBy) {
String description = String.format("完成课程 '%s'证书ID: %s成绩: %.2f",
courseName, certificateId, score);
ChangeRecord record = new ChangeRecord(ChangeType.TRAINING_COMPLETION,
description, "培训完成", changedBy);
historyRecords.add(record);
}
/**
* 获取所有历史记录
* @return 按时间倒序排列的历史记录列表
*/
public List<ChangeRecord> getAllHistory() {
List<ChangeRecord> sortedRecords = new ArrayList<>(historyRecords);
sortedRecords.sort(Comparator.comparing(ChangeRecord::getChangeTime).reversed());
return sortedRecords;
}
/**
* 按变更类型获取历史记录
* @param changeType 变更类型
* @return 指定类型的历史记录列表
*/
public List<ChangeRecord> getHistoryByType(ChangeType changeType) {
return historyRecords.stream()
.filter(record -> record.getChangeType() == changeType)
.sorted(Comparator.comparing(ChangeRecord::getChangeTime).reversed())
.collect(Collectors.toList());
}
/**
* 获取特定时间段内的历史记录
* @param startTime 开始时间
* @param endTime 结束时间
* @return 时间段内的历史记录列表
*/
public List<ChangeRecord> getHistoryByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
return historyRecords.stream()
.filter(record -> !record.getChangeTime().isBefore(startTime) &&
!record.getChangeTime().isAfter(endTime))
.sorted(Comparator.comparing(ChangeRecord::getChangeTime).reversed())
.collect(Collectors.toList());
}
/**
* 获取最后N条历史记录
* @param n 记录数量
* @return 最近的N条历史记录
*/
public List<ChangeRecord> getLatestHistory(int n) {
List<ChangeRecord> sortedRecords = new ArrayList<>(historyRecords);
sortedRecords.sort(Comparator.comparing(ChangeRecord::getChangeTime).reversed());
return sortedRecords.stream().limit(n).collect(Collectors.toList());
}
/**
* 获取薪资变更次数
* @return 薪资变更次数
*/
public int getSalaryChangeCount() {
return (int) historyRecords.stream()
.filter(record -> record.getChangeType() == ChangeType.SALARY_ADJUSTMENT)
.count();
}
/**
* 获取职位变更次数
* @return 职位变更次数
*/
public int getPositionChangeCount() {
return (int) historyRecords.stream()
.filter(record -> record.getChangeType() == ChangeType.POSITION_CHANGE)
.count();
}
/**
* 获取部门变更次数
* @return 部门变更次数
*/
public int getDepartmentChangeCount() {
return (int) historyRecords.stream()
.filter(record -> record.getChangeType() == ChangeType.DEPARTMENT_CHANGE)
.count();
}
/**
* 获取培训完成次数
* @return 培训完成次数
*/
public int getTrainingCompletionCount() {
return (int) historyRecords.stream()
.filter(record -> record.getChangeType() == ChangeType.TRAINING_COMPLETION)
.count();
}
/**
* 获取员工ID
* @return 员工ID
*/
public String getEmployeeId() {
return employeeId;
}
/**
* 获取历史记录总数
* @return 历史记录总数
*/
public int getTotalRecordCount() {
return historyRecords.size();
}
/**
* 变更类型枚举
*/
public enum ChangeType {
SALARY_ADJUSTMENT, // 薪资调整
POSITION_CHANGE, // 职位变更
DEPARTMENT_CHANGE, // 部门变更
STATUS_CHANGE, // 状态变更
INFO_CHANGE, // 信息变更
TRAINING_COMPLETION // 培训完成
}
/**
* 变更记录内部类
*/
public static class ChangeRecord {
private final ChangeType changeType;
private final String description;
private final String reason;
private final String changedBy;
private final LocalDateTime changeTime;
public ChangeRecord(ChangeType changeType, String description, String reason, String changedBy) {
this.changeType = changeType;
this.description = description;
this.reason = reason;
this.changedBy = changedBy;
this.changeTime = LocalDateTime.now();
}
public ChangeType getChangeType() { return changeType; }
public String getDescription() { return description; }
public String getReason() { return reason; }
public String getChangedBy() { return changedBy; }
public LocalDateTime getChangeTime() { return changeTime; }
@Override
public String toString() {
return String.format("[%s] %s | 原因: %s | 操作人: %s | 时间: %s",
changeType.name(), description, reason, changedBy, changeTime);
}
}
}