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.
110 lines
3.8 KiB
110 lines
3.8 KiB
/**
|
|
* 证书管理实现类
|
|
* 提供证书管理接口的具体实现
|
|
* 遵循单一职责原则,专注于证书的管理
|
|
*/
|
|
package com.employeetraining.certificate;
|
|
|
|
import java.time.LocalDate;
|
|
import java.util.*;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
public class CertificateManagerImpl implements CertificateManager {
|
|
// 使用线程安全的集合存储证书
|
|
private final Map<String, Certificate> certificateMap = new ConcurrentHashMap<>();
|
|
|
|
@Override
|
|
public void issueCertificate(Certificate certificate) {
|
|
if (certificate == null) {
|
|
throw new IllegalArgumentException("Certificate cannot be null");
|
|
}
|
|
if (certificate.getCertificateId() == null || certificate.getCertificateId().trim().isEmpty()) {
|
|
throw new IllegalArgumentException("Certificate ID cannot be empty");
|
|
}
|
|
certificateMap.put(certificate.getCertificateId(), certificate);
|
|
}
|
|
|
|
@Override
|
|
public boolean updateCertificateStatus(String certificateId, Certificate.CertificateStatus status) {
|
|
Certificate certificate = certificateMap.get(certificateId);
|
|
if (certificate != null) {
|
|
certificate.setStatus(status);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean revokeCertificate(String certificateId, String reason) {
|
|
Certificate certificate = certificateMap.get(certificateId);
|
|
if (certificate != null) {
|
|
certificate.setStatus(Certificate.CertificateStatus.REVOKED);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean renewCertificate(String certificateId, LocalDate newExpiryDate) {
|
|
Certificate certificate = certificateMap.get(certificateId);
|
|
if (certificate != null && newExpiryDate != null) {
|
|
certificate.setExpiryDate(newExpiryDate);
|
|
certificate.setStatus(Certificate.CertificateStatus.ACTIVE);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public Optional<Certificate> findCertificateById(String certificateId) {
|
|
if (certificateId == null) {
|
|
return Optional.empty();
|
|
}
|
|
return Optional.ofNullable(certificateMap.get(certificateId));
|
|
}
|
|
|
|
@Override
|
|
public List<Certificate> findCertificatesByEmployeeId(String employeeId) {
|
|
if (employeeId == null) {
|
|
return Collections.emptyList();
|
|
}
|
|
return certificateMap.values().stream()
|
|
.filter(certificate -> employeeId.equals(certificate.getEmployeeId()))
|
|
.toList();
|
|
}
|
|
|
|
@Override
|
|
public List<Certificate> findCertificatesByCourseId(String courseId) {
|
|
if (courseId == null) {
|
|
return Collections.emptyList();
|
|
}
|
|
return certificateMap.values().stream()
|
|
.filter(certificate -> courseId.equals(certificate.getCourseId()))
|
|
.toList();
|
|
}
|
|
|
|
@Override
|
|
public List<Certificate> findExpiredCertificates() {
|
|
return certificateMap.values().stream()
|
|
.filter(Certificate::isExpired)
|
|
.toList();
|
|
}
|
|
|
|
@Override
|
|
public List<Certificate> findCertificatesExpiringSoon(int days) {
|
|
if (days < 0) {
|
|
return Collections.emptyList();
|
|
}
|
|
LocalDate thresholdDate = LocalDate.now().plusDays(days);
|
|
return certificateMap.values().stream()
|
|
.filter(certificate -> certificate.getExpiryDate() != null &&
|
|
!certificate.isExpired() &&
|
|
!certificate.getExpiryDate().isAfter(thresholdDate))
|
|
.toList();
|
|
}
|
|
|
|
@Override
|
|
public List<Certificate> getAllCertificates() {
|
|
return new ArrayList<>(certificateMap.values());
|
|
}
|
|
} |