Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #46

Merged
merged 4 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kusitms.server.domain.common.dto.SuccessResponse;
import kusitms.server.domain.common.dto.code.SuccessCode;
import kusitms.server.domain.tumbler.history.dto.response.HistoryMonthDetailResponseDto;
import kusitms.server.domain.tumbler.history.dto.response.HistoryQuarterDetailResponseDto;
import kusitms.server.domain.tumbler.history.service.TumblerHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,9 +20,15 @@ public class TumblerHistoryController {
private final TumblerHistoryService tumblerHistoryService;

// ex. 202310 이정도로 달까지만 주세요
@GetMapping("/month")
public ResponseEntity<SuccessResponse<List<HistoryMonthDetailResponseDto>>> findDetailByMonth(@RequestParam String period, @RequestParam Long userId) {
@GetMapping("/month/{userId}")
public ResponseEntity<SuccessResponse<List<HistoryMonthDetailResponseDto>>> findDetailByMonth(@RequestParam String period, @PathVariable Long userId) {
List<HistoryMonthDetailResponseDto> request = tumblerHistoryService.findDetailByMonth(period, userId);
return SuccessResponse.of(SuccessCode.OK, request);
}

@GetMapping("/quarter/{userId}")
public ResponseEntity<SuccessResponse<List<HistoryQuarterDetailResponseDto>>> findDetailByQuarter(@RequestParam String startPeriod, @RequestParam String endPeriod, @PathVariable Long userId) {
List<HistoryQuarterDetailResponseDto> request = tumblerHistoryService.findDetailByQuarter(startPeriod, endPeriod, userId);
return SuccessResponse.of(SuccessCode.OK, request);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kusitms.server.domain.tumbler.history.dto.response;

import kusitms.server.domain.tumbler.history.entity.TumblerHistory;
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Builder
@Getter
public class HistoryQuarterDetailResponseDto {

private String tumblerName;

private String deptName;

private Integer tumblerGrade;

private Double tumblerPercent;

private Double tumblerAverage;

public static HistoryQuarterDetailResponseDto of(List<TumblerHistory> tumblerHistories) {
Double average = (double) 0;
for (TumblerHistory tumbler:tumblerHistories) {
average += tumbler.getTumblerCount();
}
average /= tumblerHistories.size();
return HistoryQuarterDetailResponseDto.builder()
.tumblerName(tumblerHistories.get(0).getTumblerName())
.deptName(tumblerHistories.get(0).getDepartment().getDeptName())
.tumblerGrade(tumblerHistories.get(0).getTumblerGrade())
.tumblerPercent(tumblerHistories.get(0).getTumblerPercent())
.tumblerAverage(average)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface TumblerHistoryRepository extends JpaRepository<TumblerHistory,

TumblerHistory findByCreatedAtBetweenAndDepartment(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department);

List<TumblerHistory> findAllByCreatedAtBetweenAndDepartment(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department);

List<TumblerHistory> findAllByCreatedAtBetween(LocalDateTime searchStartDate, LocalDateTime searchEndDate);
List<TumblerHistory> findAllByDepartmentOrderByCreatedAtDesc(Department departcafement);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package kusitms.server.domain.tumbler.history.service;

import kusitms.server.domain.common.error.ApplicationError;
import kusitms.server.domain.common.error.NotFoundException;
import kusitms.server.domain.company.entity.Company;
import kusitms.server.domain.company.repository.CompanyRepository;
import kusitms.server.domain.department.entity.Department;
import kusitms.server.domain.department.repository.DepartmentRepository;
import kusitms.server.domain.tumbler.history.dto.response.HistoryMonthDetailResponseDto;
import kusitms.server.domain.tumbler.history.dto.response.HistoryQuarterDetailResponseDto;
import kusitms.server.domain.tumbler.history.entity.TumblerHistory;
import kusitms.server.domain.tumbler.history.repository.TumblerHistoryRepository;
import kusitms.server.domain.tumbler.history.util.ListComparator;
import kusitms.server.domain.tumbler.history.util.ListComparatorMonth;
import kusitms.server.domain.tumbler.history.util.ListComparatorQuarter;
import kusitms.server.domain.user.entity.User;
import kusitms.server.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,7 +21,6 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static kusitms.server.domain.common.error.ApplicationError.DEPARTMENT_NOT_FOUND;
Expand All @@ -47,15 +47,32 @@ public List<HistoryMonthDetailResponseDto> findDetailByMonth(String period, Long
LocalDateTime endMonthDate = createEndMonthDate(period);
departments.forEach(department ->
response.add(
HistoryMonthDetailResponseDto.of(findHistoryInPeriod(startMonthDate, endMonthDate, department))
HistoryMonthDetailResponseDto.of(findHistoryInMonth(startMonthDate, endMonthDate, department))
)
);
response.sort(new ListComparator());
response.sort(new ListComparatorMonth());

return response;
}


public List<HistoryQuarterDetailResponseDto> findDetailByQuarter(String startPeriod, String endPeriod, Long userId) {
User finduser = getUserById(userId);
Department userDepartment = getDepartmentByUser(finduser);
Company company = userDepartment.getCompany();
List<Department> departments = company.getDepartments();
List<HistoryQuarterDetailResponseDto> response = new ArrayList<>();
LocalDateTime startMonthDate = createStartMonthDate(startPeriod);
LocalDateTime endMonthDate = createEndMonthDate(endPeriod);
departments.forEach(department ->
response.add(
HistoryQuarterDetailResponseDto.of(findHistoryInQuarter(startMonthDate, endMonthDate, department))
)
);
response.sort(new ListComparatorQuarter());
return response;
}

// 월별 검색을 할때 1일 0시0분0초 값을 추가 해줘서 범위의 처음 값을 만듭니다.
private LocalDateTime createStartMonthDate(String period) {
period += "01";
Expand All @@ -80,9 +97,14 @@ private Department getDepartmentByUser(User user) {
.orElseThrow(() -> new NotFoundException(DEPARTMENT_NOT_FOUND));
}

private TumblerHistory findHistoryInPeriod(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department) {
private TumblerHistory findHistoryInMonth(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department) {
TumblerHistory tumblerHistory = tumblerHistoryRepository.findByCreatedAtBetweenAndDepartment(searchStartDate, searchEndDate, department);
return tumblerHistory;
}

private List<TumblerHistory> findHistoryInQuarter(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department) {
List<TumblerHistory> tumblerHistories = tumblerHistoryRepository.findAllByCreatedAtBetweenAndDepartment(searchStartDate, searchEndDate, department);
return tumblerHistories;
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package kusitms.server.domain.tumbler.history.util;

import kusitms.server.domain.tumbler.history.dto.response.HistoryMonthDetailResponseDto;
import kusitms.server.domain.tumbler.history.dto.response.HistoryQuarterDetailResponseDto;

import java.util.Comparator;

public class ListComparator implements Comparator<HistoryMonthDetailResponseDto> {
public class ListComparatorMonth implements Comparator<HistoryMonthDetailResponseDto> {

@Override
public int compare(HistoryMonthDetailResponseDto o1, HistoryMonthDetailResponseDto o2) {
Expand All @@ -17,4 +18,5 @@ public int compare(HistoryMonthDetailResponseDto o1, HistoryMonthDetailResponseD
return 0;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package kusitms.server.domain.tumbler.history.util;

import kusitms.server.domain.tumbler.history.dto.response.HistoryMonthDetailResponseDto;
import kusitms.server.domain.tumbler.history.dto.response.HistoryQuarterDetailResponseDto;

import java.util.Comparator;

public class ListComparatorQuarter implements Comparator<HistoryQuarterDetailResponseDto> {

@Override
public int compare(HistoryQuarterDetailResponseDto o1, HistoryQuarterDetailResponseDto o2) {

if(o1.getTumblerAverage() > o2.getTumblerAverage()){
return -1;
}else if(o1.getTumblerAverage() < o2.getTumblerAverage()){
return 1;
}else{
return 0;
}
}
}