init: SR System — Helpdesk & ITAM Portal
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
package com.srsystem.service;
|
||||
|
||||
import com.srsystem.dto.CreateDispatchRequest;
|
||||
import com.srsystem.entity.DispatchHistory;
|
||||
import com.srsystem.entity.Employee;
|
||||
import com.srsystem.entity.Project;
|
||||
import com.srsystem.exception.BusinessException;
|
||||
import com.srsystem.exception.ErrorCode;
|
||||
import com.srsystem.repository.DispatchHistoryRepository;
|
||||
import com.srsystem.repository.EmployeeRepository;
|
||||
import com.srsystem.repository.ProjectRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Transactional(readOnly = true)
|
||||
public class DispatchHistoryService {
|
||||
|
||||
private final DispatchHistoryRepository dispatchHistoryRepository;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final ProjectRepository projectRepository;
|
||||
|
||||
public List<DispatchHistory> findByEmployeeId(Long empId) {
|
||||
employeeRepository.findById(empId)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.EMPLOYEE_NOT_FOUND));
|
||||
return dispatchHistoryRepository.findByEmployee_EmpId(empId);
|
||||
}
|
||||
|
||||
public List<DispatchHistory> getActiveDispatches() {
|
||||
return dispatchHistoryRepository.findByEndDateIsNull();
|
||||
}
|
||||
|
||||
public Page<DispatchHistory> findAll(Pageable pageable, Long empId, Long projectId) {
|
||||
if (empId != null && projectId != null) {
|
||||
return dispatchHistoryRepository.findByEmployee_EmpIdAndProject_ProjectId(empId, projectId, pageable);
|
||||
}
|
||||
if (empId != null) {
|
||||
return dispatchHistoryRepository.findByEmployee_EmpId(empId, pageable);
|
||||
}
|
||||
if (projectId != null) {
|
||||
return dispatchHistoryRepository.findByProject_ProjectId(projectId, pageable);
|
||||
}
|
||||
return dispatchHistoryRepository.findAll(pageable);
|
||||
}
|
||||
|
||||
public DispatchHistory findById(Long dispatchId) {
|
||||
return dispatchHistoryRepository.findById(dispatchId)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.DISPATCH_NOT_FOUND));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void delete(Long dispatchId) {
|
||||
DispatchHistory dh = findById(dispatchId);
|
||||
dispatchHistoryRepository.delete(dh);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public DispatchHistory createDispatch(CreateDispatchRequest request) {
|
||||
Employee employee = employeeRepository.findById(request.employeeId())
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.EMPLOYEE_NOT_FOUND));
|
||||
Project project = projectRepository.findById(request.projectId())
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.PROJECT_NOT_FOUND));
|
||||
|
||||
DispatchHistory history = DispatchHistory.builder()
|
||||
.employee(employee)
|
||||
.project(project)
|
||||
.roleTitle(request.roleTitle())
|
||||
.contributionRate(request.contributionRate())
|
||||
.individualTech(request.individualTech())
|
||||
.performedTasks(request.performedTasks())
|
||||
.startDate(request.startDate() != null ? request.startDate() : LocalDate.now())
|
||||
.build();
|
||||
|
||||
return dispatchHistoryRepository.save(history);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public DispatchHistory endDispatch(Long dispatchId) {
|
||||
DispatchHistory history = dispatchHistoryRepository.findById(dispatchId)
|
||||
.orElseThrow(() -> new BusinessException(ErrorCode.DISPATCH_NOT_FOUND));
|
||||
history.setEndDate(LocalDate.now());
|
||||
return dispatchHistoryRepository.save(history);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user