77 lines
2.8 KiB
Java
77 lines
2.8 KiB
Java
package com.srsystem.service;
|
|
|
|
import com.srsystem.dto.TrainingSummary;
|
|
import com.srsystem.dto.UpdateProgressRequest;
|
|
import com.srsystem.entity.Employee;
|
|
import com.srsystem.entity.TrainingProgress;
|
|
import com.srsystem.entity.enums.TrainingStatus;
|
|
import com.srsystem.exception.BusinessException;
|
|
import com.srsystem.exception.ErrorCode;
|
|
import com.srsystem.repository.EmployeeRepository;
|
|
import com.srsystem.repository.TrainingProgressRepository;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import java.util.List;
|
|
import java.util.Optional;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@Transactional(readOnly = true)
|
|
public class TrainingService {
|
|
|
|
private final TrainingProgressRepository trainingProgressRepository;
|
|
private final EmployeeRepository employeeRepository;
|
|
|
|
public List<TrainingProgress> getMyProgress(Long userId) {
|
|
return trainingProgressRepository.findByUser_EmpId(userId);
|
|
}
|
|
|
|
@Transactional
|
|
public TrainingProgress updateProgress(Long progressId, UpdateProgressRequest request) {
|
|
TrainingProgress progress = trainingProgressRepository.findById(progressId)
|
|
.orElseThrow(() -> new BusinessException(ErrorCode.REPORT_NOT_FOUND));
|
|
|
|
if (request.progressRate() != null) {
|
|
progress.setProgressRate(request.progressRate());
|
|
}
|
|
if (request.status() != null) {
|
|
progress.setStatus(request.status());
|
|
}
|
|
|
|
if (progress.getProgressRate() == 100) {
|
|
progress.setStatus(TrainingStatus.COMPLETED);
|
|
}
|
|
|
|
return trainingProgressRepository.save(progress);
|
|
}
|
|
|
|
public List<TrainingSummary> getSummary() {
|
|
List<Employee> employees = employeeRepository.findAll();
|
|
|
|
return employees.stream()
|
|
.map(emp -> {
|
|
List<TrainingProgress> progresses = trainingProgressRepository.findByUser_EmpId(emp.getEmpId());
|
|
long totalCourses = progresses.size();
|
|
long completedCount = progresses.stream()
|
|
.filter(p -> p.getStatus() == TrainingStatus.COMPLETED)
|
|
.count();
|
|
double avgProgressRate = progresses.stream()
|
|
.mapToInt(TrainingProgress::getProgressRate)
|
|
.average()
|
|
.orElse(0.0);
|
|
|
|
return new TrainingSummary(
|
|
emp.getEmpId(),
|
|
emp.getName(),
|
|
totalCourses,
|
|
completedCount,
|
|
avgProgressRate
|
|
);
|
|
})
|
|
.collect(Collectors.toList());
|
|
}
|
|
}
|