Skip to content

Commit

Permalink
Merge pull request #50 from Kusitms-28th-Kukathon-F/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
OJOJIN authored Oct 7, 2023
2 parents 8ebbb86 + 988f1ab commit 0864df3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,4 +32,10 @@ public ResponseEntity<SuccessResponse<List<HistoryQuarterDetailResponseDto>>> fi
List<HistoryQuarterDetailResponseDto> request = tumblerHistoryService.findDetailByQuarter(startPeriod, endPeriod, userId);
return SuccessResponse.of(SuccessCode.OK, request);
}

@GetMapping("/rank/{userId}")
public ResponseEntity<SuccessResponse<List<HistoryRankResponseDto>>> findHistoryRank(@RequestParam String period, @PathVariable Long userId) {
List<HistoryRankResponseDto> request = tumblerHistoryService.findHistoryRank(period, userId);
return SuccessResponse.of(SuccessCode.OK, request);
}
}
Original file line number Diff line number Diff line change
@@ -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<TumblerHistory> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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> findAllByCreatedAtBetweenOrderByTumblerCountDesc(LocalDateTime searchStartDate, LocalDateTime searchEndDate);

List<TumblerHistory> findAllByCreatedAtBetween(LocalDateTime searchStartDate, LocalDateTime searchEndDate);
List<TumblerHistory> findAllByDepartmentOrderByCreatedAtDesc(Department departcafement);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -66,13 +67,32 @@ public List<HistoryQuarterDetailResponseDto> 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<HistoryRankResponseDto> findHistoryRank(String period, Long userId) {
User finduser = getUserById(userId);
Department userDepartment = getDepartmentByUser(finduser);
Company company = userDepartment.getCompany();
List<Department> departments = company.getDepartments();
List<HistoryRankResponseDto> 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";
Expand Down Expand Up @@ -102,9 +122,14 @@ private TumblerHistory findHistoryInMonth(LocalDateTime searchStartDate, LocalDa
return tumblerHistory;
}

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

private List<TumblerHistory> findAllHistoryInPeriod(LocalDateTime searchStartDate, LocalDateTime searchEndDate) {
List<TumblerHistory> tumblerHistories = tumblerHistoryRepository.findAllByCreatedAtBetweenOrderByTumblerCountDesc(searchStartDate, searchEndDate);
return tumblerHistories;
}

}
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.HistoryQuarterDetailResponseDto;
import kusitms.server.domain.tumbler.history.entity.TumblerHistory;

import java.util.Comparator;

public class ListComparatorTumblerHistory implements Comparator<TumblerHistory> {

@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;
}
}
}

0 comments on commit 0864df3

Please sign in to comment.