Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.1 kB
2
Indexable
Never
package com.appcent.efortapi.report;

import com.appcent.efortapi.employee.Employee;
import com.appcent.efortapi.employee.EmployeeRepository;
import com.appcent.efortapi.project.Project;
import com.appcent.efortapi.project.ProjectRepository;
import com.appcent.efortapi.timetrack.TimeTrack;
import com.appcent.efortapi.timetrack.TimeTrackRepository;
import com.appcent.efortapi.user.User;
import com.appcent.efortapi.user.UserRepository;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@Service
public class MonthlyReportServiceImpl implements MonthlyReportService {

    private final TimeTrackRepository timeTrackRepository;
    private final EmployeeRepository employeeRepository;
    private final ProjectRepository projectRepository;
    private final UserRepository userRepository;

    public MonthlyReportServiceImpl(TimeTrackRepository timeTrackRepository, EmployeeRepository employeeRepository, 
                                    ProjectRepository projectRepository, UserRepository userRepository) {
        this.timeTrackRepository = timeTrackRepository;
        this.employeeRepository = employeeRepository;
        this.projectRepository = projectRepository;
        this.userRepository = userRepository;
    }

    @Override
    public MonthlyEmployeeReportDto getMonthlyReportByEmployee(Long employeeId, String year, String month) {

        User user = userRepository.findById(employeeId).orElseThrow(() -> new NoSuchElementException("Kullanıcı bulunamadı."));
        Employee employee = employeeRepository.findByEmail(user.getEmail());

        List<Project> projects = projectRepository.findByMembers_Email(user.getEmail());

        List<ProjectInfo> projectInfoList = projects.stream().map(project -> {
            List<TimeTrack> timeTrackList = timeTrackRepository
                    .findByPeriodYearAndPeriodMonthAndUser_EmailAndProject_Name(year, month, user.getEmail(), project.getName());

            List<TimeTrackInfo> timeTrackInfoList = timeTrackList.stream().map(timeTrack -> TimeTrackInfo.builder()
                            .date(timeTrack.getDate())
                            .description(timeTrack.getDescription())
                            .workHours(timeTrack.getWorkHours())
                            .overTime(timeTrack.getOverTime())
                            .build())
                    .collect(Collectors.toList());

            return ProjectInfo.builder()
                    .projectName(project.getName())
                    .timeTrackList(timeTrackInfoList)
                    .build();
        })
                .collect(Collectors.toList());

        return MonthlyEmployeeReportDto.builder()
                .employeeId(user.getId())
                .employeeName(employee.getFirstName())
                .employeeSurname(employee.getLastName())
                .projectInfoList(projectInfoList)
                .periodMonth(month)
                .periodYear(year)
                .build();
    }
}