diff --git a/src/main/java/kusitms/server/domain/tumbler/history/controller/TumblerHistoryController.java b/src/main/java/kusitms/server/domain/tumbler/history/controller/TumblerHistoryController.java index ccae362..420ff62 100644 --- a/src/main/java/kusitms/server/domain/tumbler/history/controller/TumblerHistoryController.java +++ b/src/main/java/kusitms/server/domain/tumbler/history/controller/TumblerHistoryController.java @@ -5,6 +5,7 @@ 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.dto.response.HistoryRankResponseDto; import kusitms.server.domain.tumbler.history.service.TumblerHistoryService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; @@ -31,4 +32,10 @@ public ResponseEntity>> fi List request = tumblerHistoryService.findDetailByQuarter(startPeriod, endPeriod, userId); return SuccessResponse.of(SuccessCode.OK, request); } + + @GetMapping("/rank/{userId}") + public ResponseEntity>> findHistoryRank(@RequestParam String period, @PathVariable Long userId) { + List request = tumblerHistoryService.findHistoryRank(period, userId); + return SuccessResponse.of(SuccessCode.OK, request); + } } diff --git a/src/main/java/kusitms/server/domain/tumbler/history/dto/response/HistoryRankResponseDto.java b/src/main/java/kusitms/server/domain/tumbler/history/dto/response/HistoryRankResponseDto.java new file mode 100644 index 0000000..53b18b7 --- /dev/null +++ b/src/main/java/kusitms/server/domain/tumbler/history/dto/response/HistoryRankResponseDto.java @@ -0,0 +1,36 @@ +package kusitms.server.domain.tumbler.history.dto.response; + +import kusitms.server.domain.department.entity.Department; +import kusitms.server.domain.tumbler.history.entity.TumblerHistory; +import kusitms.server.domain.tumbler.history.util.ListComparatorQuarter; +import kusitms.server.domain.tumbler.history.util.ListComparatorTumblerHistory; +import lombok.Builder; +import lombok.Getter; + +import java.util.List; + +@Builder +@Getter +public class HistoryRankResponseDto { + + private Integer rank; + + private Integer tumblerCount; + + + public static HistoryRankResponseDto of(List tumblerHistories, String deptName) { + tumblerHistories.sort(new ListComparatorTumblerHistory()); + Integer rank = 0; + + for (int i = 0; i < tumblerHistories.size(); i++) { + if(deptName.equals(tumblerHistories.get(i).getDepartment().getDeptName())) { + rank = i; + } + } + + return HistoryRankResponseDto.builder() + .rank(rank + 1) + .tumblerCount(tumblerHistories.get(rank).getTumblerCount()) + .build(); + } +} diff --git a/src/main/java/kusitms/server/domain/tumbler/history/repository/TumblerHistoryRepository.java b/src/main/java/kusitms/server/domain/tumbler/history/repository/TumblerHistoryRepository.java index 5cf8959..3a9a3c0 100644 --- a/src/main/java/kusitms/server/domain/tumbler/history/repository/TumblerHistoryRepository.java +++ b/src/main/java/kusitms/server/domain/tumbler/history/repository/TumblerHistoryRepository.java @@ -12,6 +12,7 @@ public interface TumblerHistoryRepository extends JpaRepository findAllByCreatedAtBetweenAndDepartment(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department); + List findAllByCreatedAtBetweenOrderByTumblerCountDesc(LocalDateTime searchStartDate, LocalDateTime searchEndDate); List findAllByCreatedAtBetween(LocalDateTime searchStartDate, LocalDateTime searchEndDate); List findAllByDepartmentOrderByCreatedAtDesc(Department departcafement); diff --git a/src/main/java/kusitms/server/domain/tumbler/history/service/TumblerHistoryService.java b/src/main/java/kusitms/server/domain/tumbler/history/service/TumblerHistoryService.java index 7a0ff91..964c7ff 100644 --- a/src/main/java/kusitms/server/domain/tumbler/history/service/TumblerHistoryService.java +++ b/src/main/java/kusitms/server/domain/tumbler/history/service/TumblerHistoryService.java @@ -7,6 +7,7 @@ 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.dto.response.HistoryRankResponseDto; import kusitms.server.domain.tumbler.history.entity.TumblerHistory; import kusitms.server.domain.tumbler.history.repository.TumblerHistoryRepository; import kusitms.server.domain.tumbler.history.util.ListComparatorMonth; @@ -66,13 +67,32 @@ public List findDetailByQuarter(String startPer LocalDateTime endMonthDate = createEndMonthDate(endPeriod); departments.forEach(department -> response.add( - HistoryQuarterDetailResponseDto.of(findHistoryInQuarter(startMonthDate, endMonthDate, department)) + HistoryQuarterDetailResponseDto.of(findHistoryInPeriod(startMonthDate, endMonthDate, department)) ) ); response.sort(new ListComparatorQuarter()); return response; } + + + // 근 3달간의 랭킹을 봐봅시다.. + public List findHistoryRank(String period, Long userId) { + User finduser = getUserById(userId); + Department userDepartment = getDepartmentByUser(finduser); + Company company = userDepartment.getCompany(); + List departments = company.getDepartments(); + List response = new ArrayList<>(); + LocalDateTime startMonthDate = createStartMonthDate(period); + LocalDateTime endMonthDate = createEndMonthDate(period); + for (int i = 0; i < 3; i++) { + response.add( + HistoryRankResponseDto.of(findAllHistoryInPeriod(startMonthDate.minusMonths(i), endMonthDate.minusMonths(i)), userDepartment.getDeptName()) + ); + } + return response; + } + // 월별 검색을 할때 1일 0시0분0초 값을 추가 해줘서 범위의 처음 값을 만듭니다. public LocalDateTime createStartMonthDate(String period) { period += "01"; @@ -102,9 +122,14 @@ private TumblerHistory findHistoryInMonth(LocalDateTime searchStartDate, LocalDa return tumblerHistory; } - private List findHistoryInQuarter(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department) { + private List findHistoryInPeriod(LocalDateTime searchStartDate, LocalDateTime searchEndDate, Department department) { List tumblerHistories = tumblerHistoryRepository.findAllByCreatedAtBetweenAndDepartment(searchStartDate, searchEndDate, department); return tumblerHistories; } + private List findAllHistoryInPeriod(LocalDateTime searchStartDate, LocalDateTime searchEndDate) { + List tumblerHistories = tumblerHistoryRepository.findAllByCreatedAtBetweenOrderByTumblerCountDesc(searchStartDate, searchEndDate); + return tumblerHistories; + } + } diff --git a/src/main/java/kusitms/server/domain/tumbler/history/util/ListComparatorTumblerHistory.java b/src/main/java/kusitms/server/domain/tumbler/history/util/ListComparatorTumblerHistory.java new file mode 100644 index 0000000..f7f8ffa --- /dev/null +++ b/src/main/java/kusitms/server/domain/tumbler/history/util/ListComparatorTumblerHistory.java @@ -0,0 +1,21 @@ +package kusitms.server.domain.tumbler.history.util; + +import kusitms.server.domain.tumbler.history.dto.response.HistoryQuarterDetailResponseDto; +import kusitms.server.domain.tumbler.history.entity.TumblerHistory; + +import java.util.Comparator; + +public class ListComparatorTumblerHistory implements Comparator { + + @Override + public int compare(TumblerHistory o1, TumblerHistory o2) { + + if(o1.getTumblerCount() > o2.getTumblerCount()){ + return -1; + }else if(o1.getTumblerCount() < o2.getTumblerCount()){ + return 1; + }else{ + return 0; + } + } +}