From 313e1437aa1fb26d263bd596f831c91cb4d7ad36 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Mon, 27 Nov 2023 15:29:34 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[BE]=20HOTFIX=20:=20AVAILABLE=EC=9D=B8=20?= =?UTF-8?q?=EC=82=AC=EB=AC=BC=ED=95=A8=EB=93=A4=EB=8F=84=20=EB=B3=B4?= =?UTF-8?q?=EC=97=AC=EC=A3=BC=EB=8D=98=20=EB=B6=80=EB=B6=84=EC=9D=84=20?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=EC=97=90=EC=84=9C=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/CabinetFacadeServiceImpl.java | 970 +++++++++--------- 1 file changed, 475 insertions(+), 495 deletions(-) diff --git a/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java index 4c65c7c53..9a6ec8387 100644 --- a/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java +++ b/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java @@ -1,18 +1,5 @@ package org.ftclub.cabinet.cabinet.service; -import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.mapping; -import static org.ftclub.cabinet.cabinet.domain.CabinetStatus.AVAILABLE; -import static org.ftclub.cabinet.cabinet.domain.CabinetStatus.PENDING; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.IntStream; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.ftclub.cabinet.cabinet.domain.Cabinet; @@ -20,22 +7,7 @@ import org.ftclub.cabinet.cabinet.domain.Grid; import org.ftclub.cabinet.cabinet.domain.LentType; import org.ftclub.cabinet.cabinet.repository.CabinetOptionalFetcher; -import org.ftclub.cabinet.dto.ActiveCabinetInfoEntities; -import org.ftclub.cabinet.dto.BuildingFloorsDto; -import org.ftclub.cabinet.dto.CabinetClubStatusRequestDto; -import org.ftclub.cabinet.dto.CabinetDto; -import org.ftclub.cabinet.dto.CabinetInfoPaginationDto; -import org.ftclub.cabinet.dto.CabinetInfoResponseDto; -import org.ftclub.cabinet.dto.CabinetPaginationDto; -import org.ftclub.cabinet.dto.CabinetPendingResponseDto; -import org.ftclub.cabinet.dto.CabinetPreviewDto; -import org.ftclub.cabinet.dto.CabinetSimpleDto; -import org.ftclub.cabinet.dto.CabinetSimplePaginationDto; -import org.ftclub.cabinet.dto.CabinetStatusRequestDto; -import org.ftclub.cabinet.dto.CabinetsPerSectionResponseDto; -import org.ftclub.cabinet.dto.LentDto; -import org.ftclub.cabinet.dto.LentHistoryDto; -import org.ftclub.cabinet.dto.LentHistoryPaginationDto; +import org.ftclub.cabinet.dto.*; import org.ftclub.cabinet.lent.domain.LentHistory; import org.ftclub.cabinet.lent.repository.LentOptionalFetcher; import org.ftclub.cabinet.lent.repository.LentRedis; @@ -49,6 +21,14 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static java.util.stream.Collectors.groupingBy; +import static java.util.stream.Collectors.mapping; +import static org.ftclub.cabinet.cabinet.domain.CabinetStatus.PENDING; + @Service @RequiredArgsConstructor @Log4j2 @@ -63,348 +43,348 @@ public class CabinetFacadeServiceImpl implements CabinetFacadeService { private final LentRedis lentRedis; private final UserOptionalFetcher userOptionalFetcher; - /*-------------------------------------------READ-------------------------------------------*/ - - /** - * {@inheritDoc} - *

- * 존재하는 모든 건물들을 가져오고, 각 건물별 층 정보들을 가져옵니다. - */ - @Override - @Transactional(readOnly = true) - public List getBuildingFloorsResponse() { - log.debug("getBuildingFloorsResponse"); - return cabinetOptionalFetcher.findAllBuildings().stream() - .map(building -> { - List floors = cabinetOptionalFetcher.findAllFloorsByBuilding(building); - return cabinetMapper.toBuildingFloorsDto(building, floors); - }) - .collect(Collectors.toList()); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public CabinetInfoResponseDto getCabinetInfo(Long cabinetId) { - log.debug("getCabinetInfo"); - List lentDtos = new ArrayList<>(); - List lentHistories = lentOptionalFetcher.findAllActiveLentByCabinetId( - cabinetId); - if (lentHistories.isEmpty()) { + /*-------------------------------------------READ-------------------------------------------*/ + + /** + * {@inheritDoc} + *

+ * 존재하는 모든 건물들을 가져오고, 각 건물별 층 정보들을 가져옵니다. + */ + @Override + @Transactional(readOnly = true) + public List getBuildingFloorsResponse() { + log.debug("getBuildingFloorsResponse"); + return cabinetOptionalFetcher.findAllBuildings().stream() + .map(building -> { + List floors = cabinetOptionalFetcher.findAllFloorsByBuilding(building); + return cabinetMapper.toBuildingFloorsDto(building, floors); + }) + .collect(Collectors.toList()); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public CabinetInfoResponseDto getCabinetInfo(Long cabinetId) { + log.debug("getCabinetInfo"); + List lentDtos = new ArrayList<>(); + List lentHistories = lentOptionalFetcher.findAllActiveLentByCabinetId( + cabinetId); + if (lentHistories.isEmpty()) { // ArrayList users = ticketingSharedCabinet.findUsersInSessionByCabinetIdFromRedis( // cabinetId); - ArrayList users = lentRedis.getUserIdsByCabinetIdInRedis( - cabinetId.toString()); - for (String user : users) { - String userName = userOptionalFetcher.findUser(Long.valueOf(user)).getName(); - lentDtos.add(new LentDto(Long.valueOf(user), userName, null, null, null)); - } - } - for (LentHistory lentHistory : lentHistories) { - User findUser = lentHistory.getUser(); - lentDtos.add(lentMapper.toLentDto(findUser, lentHistory)); - } - return cabinetMapper.toCabinetInfoResponseDto(cabinetOptionalFetcher.findCabinet(cabinetId), - lentDtos, lentRedis.getSessionExpiredAtInRedis(cabinetId)); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public CabinetSimplePaginationDto getCabinetsSimpleInfoByVisibleNum(Integer visibleNum) { - log.debug("getCabinetsSimpleInfoByVisibleNum"); - PageRequest page = PageRequest.of(0, Integer.MAX_VALUE); - Page allCabinetsByVisibleNum = cabinetOptionalFetcher.findPaginationByVisibleNum( - visibleNum, page); - List cabinetsSimple = allCabinetsByVisibleNum.stream().map( - cabinetMapper::toCabinetSimpleDto).collect(Collectors.toList()); - return new CabinetSimplePaginationDto(cabinetsSimple, - allCabinetsByVisibleNum.getTotalElements()); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public List getCabinetsPerSection(String building, - Integer floor) { - log.debug("getCabinetsPerSection"); - List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor( - building, floor); - List allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor( - building, floor); - - Map> cabinetLentHistories = currentLentCabinets.stream(). - collect(groupingBy(ActiveCabinetInfoEntities::getCabinet, - mapping(ActiveCabinetInfoEntities::getLentHistory, - Collectors.toList()))); - - Map> cabinetPreviewsBySection = new HashMap<>(); - cabinetLentHistories.forEach((cabinet, lentHistories) -> { - String section = cabinet.getCabinetPlace().getLocation().getSection(); - CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories); - if (cabinetPreviewsBySection.containsKey(section)) { - cabinetPreviewsBySection.get(section).add(preview); - } else { - List previews = new ArrayList<>(); - previews.add(preview); - cabinetPreviewsBySection.put(section, previews); - } - }); - allCabinetsByBuildingAndFloor.forEach(cabinet -> { - if (!cabinetLentHistories.containsKey(cabinet)) { - String section = cabinet.getCabinetPlace().getLocation().getSection(); - CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, - Collections.emptyList()); - if (cabinetPreviewsBySection.containsKey(section)) { - cabinetPreviewsBySection.get(section).add(preview); - } else { - List previews = new ArrayList<>(); - previews.add(preview); - cabinetPreviewsBySection.put(section, previews); - } - } - }); - cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort( - Comparator.comparing(CabinetPreviewDto::getVisibleNum))); - return cabinetPreviewsBySection.entrySet().stream() - .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) - .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), - entry.getValue())) - .collect(Collectors.toList()); - } - - @Override - public List getCabinetsPerSectionRefactor(String building, - Integer floor) { - List cabinets = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(building, - floor); - Map> map = new HashMap<>(); - cabinets.forEach(cabinet -> { - List lentHistories = lentOptionalFetcher - .findActiveLentByCabinetIdWithUser(cabinet.getCabinetId()); - String section = cabinet.getCabinetPlace().getLocation().getSection(); - if (map.containsKey(section)) { - map.get(section) - .add(cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(), - lentHistories.isEmpty() ? null - : lentHistories.get(0).getUser().getName())); - } else { - List cabinetPreviewDtoList = new ArrayList<>(); - cabinetPreviewDtoList.add( - cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(), - lentHistories.isEmpty() ? null - : lentHistories.get(0).getUser().getName())); - map.put(section, cabinetPreviewDtoList); - } - }); - map.forEach( - (key, value) -> value.sort(Comparator.comparing(CabinetPreviewDto::getVisibleNum))); - return map.entrySet().stream() - .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) - .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), - entry.getValue())) - .collect(Collectors.toList()); - } - - private CabinetPreviewDto createCabinetPreviewDto(Cabinet cabinet, - List lentHistories) { - String lentUserName = null; - if (!lentHistories.isEmpty() && lentHistories.get(0).getUser() != null) { - lentUserName = lentHistories.get(0).getUser().getName(); - } - return cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(), lentUserName); - } - - @Override - public List getCabinetsPerSectionDSL(String building, - Integer floor) { - log.debug("getCabinetsPerSection"); - List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2( - building, floor); - List allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor( - building, floor); - - Map> cabinetLentHistories = currentLentCabinets.stream(). - collect(groupingBy(ActiveCabinetInfoEntities::getCabinet, - mapping(ActiveCabinetInfoEntities::getLentHistory, - Collectors.toList()))); - - Map> cabinetPreviewsBySection = new HashMap<>(); - cabinetLentHistories.forEach((cabinet, lentHistories) -> { - String section = cabinet.getCabinetPlace().getLocation().getSection(); - CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories); - if (cabinetPreviewsBySection.containsKey(section)) { - cabinetPreviewsBySection.get(section).add(preview); - } else { - List previews = new ArrayList<>(); - previews.add(preview); - cabinetPreviewsBySection.put(section, previews); - } - }); - allCabinetsByBuildingAndFloor.forEach(cabinet -> { - if (!cabinetLentHistories.containsKey(cabinet)) { - String section = cabinet.getCabinetPlace().getLocation().getSection(); - CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, - Collections.emptyList()); - if (cabinetPreviewsBySection.containsKey(section)) { - cabinetPreviewsBySection.get(section).add(preview); - } else { - List previews = new ArrayList<>(); - previews.add(preview); - cabinetPreviewsBySection.put(section, previews); - } - } - }); - cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort( - Comparator.comparing(CabinetPreviewDto::getVisibleNum))); - return cabinetPreviewsBySection.entrySet().stream() - .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) - .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), - entry.getValue())) - .collect(Collectors.toList()); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public CabinetPaginationDto getCabinetPaginationByLentType(LentType lentType, Integer page, - Integer size) { - log.debug("getCabinetPaginationByLentType"); - if (size <= 0) { - size = Integer.MAX_VALUE; - } - PageRequest pageable = PageRequest.of(page, size); - Page cabinets = cabinetOptionalFetcher.findPaginationByLentType(lentType, - pageable); - List cabinetDtos = cabinets.toList().stream() - .map((cabinet) -> cabinetMapper.toCabinetDto(cabinet)) - .collect(Collectors.toList()); - return cabinetMapper.toCabinetPaginationDtoList(cabinetDtos, - cabinets.getTotalElements()); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public CabinetPaginationDto getCabinetPaginationByStatus(CabinetStatus status, Integer page, - Integer size) { - log.debug("getCabinetPaginationByStatus"); - if (size <= 0) { - size = Integer.MAX_VALUE; - } - PageRequest pageable = PageRequest.of(page, size); - Page cabinets = cabinetOptionalFetcher.findPaginationByStatus(status, pageable); - List cabinetDtos = cabinets.toList().stream() - .map((cabinet) -> cabinetMapper.toCabinetDto(cabinet)) - .collect(Collectors.toList()); - return cabinetMapper.toCabinetPaginationDtoList(cabinetDtos, - cabinets.getTotalElements()); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public CabinetPaginationDto getCabinetPaginationByVisibleNum(Integer visibleNum, Integer page, - Integer size) { - log.debug("getCabinetPaginationByVisibleNum"); - if (size <= 0) { - size = Integer.MAX_VALUE; - } - PageRequest pageable = PageRequest.of(page, size); - Page cabinets = cabinetOptionalFetcher.findPaginationByVisibleNum(visibleNum, - pageable); - List cabinetDtos = cabinets.toList().stream() - .map((cabinet) -> cabinetMapper.toCabinetDto(cabinet)) - .collect(Collectors.toList()); - return cabinetMapper.toCabinetPaginationDtoList(cabinetDtos, - cabinets.getTotalElements()); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional(readOnly = true) - public LentHistoryPaginationDto getCabinetLentHistoriesPagination(Long cabinetId, Integer page, - Integer size) { - log.debug("getCabinetLentHistoriesPagination"); - if (size <= 0) { - size = Integer.MAX_VALUE; - } - PageRequest pageable = PageRequest.of(page, size, - Sort.by(Sort.Direction.DESC, "startedAt")); - Page lentHistories = lentOptionalFetcher.findPaginationByCabinetId(cabinetId, - pageable); - return lentMapper.toLentHistoryPaginationDto( - generateLentHistoryDtoList(lentHistories.toList()), - lentHistories.getTotalElements()); - } - - /** - * 사물함들의 정보와 각각의 대여 정보들을 가져옵니다. - * - * @param cabinetIds 사물함 id 리스트 - * @return 사물함 정보 리스트 - */ - @Transactional(readOnly = true) - public List getCabinetInfoBundle(List cabinetIds) { - log.debug("getCabinetInfoBundle"); - List result = new ArrayList<>(); - for (Long cabinetId : cabinetIds) { - CabinetInfoResponseDto cabinetInfo = getCabinetInfo(cabinetId); - if (CabinetInfoResponseDto.isValid(cabinetInfo)) { - result.add(cabinetInfo); - } - } - // Sorting ASC by Cabinet Floor - Comparator floorComparator = Comparator.comparing( - dto -> dto.getLocation().getFloor()); - - Collections.sort(result, floorComparator); - return result; - } - - @Override - @Transactional(readOnly = true) - public CabinetInfoPaginationDto getCabinetsInfo(Integer visibleNum) { - log.debug("getCabinetsInfo"); - PageRequest page = PageRequest.of(0, Integer.MAX_VALUE); - Page allCabinetsByVisibleNum = cabinetOptionalFetcher.findPaginationByVisibleNum( - visibleNum, page); - List collect = allCabinetsByVisibleNum.map(cabinet -> cabinet.getCabinetId()) - .stream().collect(Collectors.toList()); - return new CabinetInfoPaginationDto(getCabinetInfoBundle(collect), - allCabinetsByVisibleNum.getTotalElements()); - } - - /** - * LentHistory를 이용해 LentHistoryDto로 매핑하여 반환합니다. - * ToDo : new -> mapper 쓰기 + query service 분리 - * - * @param lentHistories 대여 기록 리스트 - * @return LentHistoryDto 리스트 - */ - private List generateLentHistoryDtoList( - List lentHistories) { - log.debug("generateLentHistoryDtoList"); - return lentHistories.stream() - .map(e -> lentMapper.toLentHistoryDto(e, - e.getUser(), - e.getCabinet())) - .collect(Collectors.toList()); - } + ArrayList users = lentRedis.getUserIdsByCabinetIdInRedis( + cabinetId.toString()); + for (String user : users) { + String userName = userOptionalFetcher.findUser(Long.valueOf(user)).getName(); + lentDtos.add(new LentDto(Long.valueOf(user), userName, null, null, null)); + } + } + for (LentHistory lentHistory : lentHistories) { + User findUser = lentHistory.getUser(); + lentDtos.add(lentMapper.toLentDto(findUser, lentHistory)); + } + return cabinetMapper.toCabinetInfoResponseDto(cabinetOptionalFetcher.findCabinet(cabinetId), + lentDtos, lentRedis.getSessionExpiredAtInRedis(cabinetId)); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public CabinetSimplePaginationDto getCabinetsSimpleInfoByVisibleNum(Integer visibleNum) { + log.debug("getCabinetsSimpleInfoByVisibleNum"); + PageRequest page = PageRequest.of(0, Integer.MAX_VALUE); + Page allCabinetsByVisibleNum = cabinetOptionalFetcher.findPaginationByVisibleNum( + visibleNum, page); + List cabinetsSimple = allCabinetsByVisibleNum.stream().map( + cabinetMapper::toCabinetSimpleDto).collect(Collectors.toList()); + return new CabinetSimplePaginationDto(cabinetsSimple, + allCabinetsByVisibleNum.getTotalElements()); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public List getCabinetsPerSection(String building, + Integer floor) { + log.debug("getCabinetsPerSection"); + List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor( + building, floor); + List allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor( + building, floor); + + Map> cabinetLentHistories = currentLentCabinets.stream(). + collect(groupingBy(ActiveCabinetInfoEntities::getCabinet, + mapping(ActiveCabinetInfoEntities::getLentHistory, + Collectors.toList()))); + + Map> cabinetPreviewsBySection = new HashMap<>(); + cabinetLentHistories.forEach((cabinet, lentHistories) -> { + String section = cabinet.getCabinetPlace().getLocation().getSection(); + CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories); + if (cabinetPreviewsBySection.containsKey(section)) { + cabinetPreviewsBySection.get(section).add(preview); + } else { + List previews = new ArrayList<>(); + previews.add(preview); + cabinetPreviewsBySection.put(section, previews); + } + }); + allCabinetsByBuildingAndFloor.forEach(cabinet -> { + if (!cabinetLentHistories.containsKey(cabinet)) { + String section = cabinet.getCabinetPlace().getLocation().getSection(); + CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, + Collections.emptyList()); + if (cabinetPreviewsBySection.containsKey(section)) { + cabinetPreviewsBySection.get(section).add(preview); + } else { + List previews = new ArrayList<>(); + previews.add(preview); + cabinetPreviewsBySection.put(section, previews); + } + } + }); + cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort( + Comparator.comparing(CabinetPreviewDto::getVisibleNum))); + return cabinetPreviewsBySection.entrySet().stream() + .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) + .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), + entry.getValue())) + .collect(Collectors.toList()); + } + + @Override + public List getCabinetsPerSectionRefactor(String building, + Integer floor) { + List cabinets = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor(building, + floor); + Map> map = new HashMap<>(); + cabinets.forEach(cabinet -> { + List lentHistories = lentOptionalFetcher + .findActiveLentByCabinetIdWithUser(cabinet.getCabinetId()); + String section = cabinet.getCabinetPlace().getLocation().getSection(); + if (map.containsKey(section)) { + map.get(section) + .add(cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(), + lentHistories.isEmpty() ? null + : lentHistories.get(0).getUser().getName())); + } else { + List cabinetPreviewDtoList = new ArrayList<>(); + cabinetPreviewDtoList.add( + cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(), + lentHistories.isEmpty() ? null + : lentHistories.get(0).getUser().getName())); + map.put(section, cabinetPreviewDtoList); + } + }); + map.forEach( + (key, value) -> value.sort(Comparator.comparing(CabinetPreviewDto::getVisibleNum))); + return map.entrySet().stream() + .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) + .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), + entry.getValue())) + .collect(Collectors.toList()); + } + + private CabinetPreviewDto createCabinetPreviewDto(Cabinet cabinet, + List lentHistories) { + String lentUserName = null; + if (!lentHistories.isEmpty() && lentHistories.get(0).getUser() != null) { + lentUserName = lentHistories.get(0).getUser().getName(); + } + return cabinetMapper.toCabinetPreviewDto(cabinet, lentHistories.size(), lentUserName); + } + + @Override + public List getCabinetsPerSectionDSL(String building, + Integer floor) { + log.debug("getCabinetsPerSection"); + List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2( + building, floor); + List allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor( + building, floor); + + Map> cabinetLentHistories = currentLentCabinets.stream(). + collect(groupingBy(ActiveCabinetInfoEntities::getCabinet, + mapping(ActiveCabinetInfoEntities::getLentHistory, + Collectors.toList()))); + + Map> cabinetPreviewsBySection = new HashMap<>(); + cabinetLentHistories.forEach((cabinet, lentHistories) -> { + String section = cabinet.getCabinetPlace().getLocation().getSection(); + CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories); + if (cabinetPreviewsBySection.containsKey(section)) { + cabinetPreviewsBySection.get(section).add(preview); + } else { + List previews = new ArrayList<>(); + previews.add(preview); + cabinetPreviewsBySection.put(section, previews); + } + }); + allCabinetsByBuildingAndFloor.forEach(cabinet -> { + if (!cabinetLentHistories.containsKey(cabinet)) { + String section = cabinet.getCabinetPlace().getLocation().getSection(); + CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, + Collections.emptyList()); + if (cabinetPreviewsBySection.containsKey(section)) { + cabinetPreviewsBySection.get(section).add(preview); + } else { + List previews = new ArrayList<>(); + previews.add(preview); + cabinetPreviewsBySection.put(section, previews); + } + } + }); + cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort( + Comparator.comparing(CabinetPreviewDto::getVisibleNum))); + return cabinetPreviewsBySection.entrySet().stream() + .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) + .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), + entry.getValue())) + .collect(Collectors.toList()); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public CabinetPaginationDto getCabinetPaginationByLentType(LentType lentType, Integer page, + Integer size) { + log.debug("getCabinetPaginationByLentType"); + if (size <= 0) { + size = Integer.MAX_VALUE; + } + PageRequest pageable = PageRequest.of(page, size); + Page cabinets = cabinetOptionalFetcher.findPaginationByLentType(lentType, + pageable); + List cabinetDtos = cabinets.toList().stream() + .map((cabinet) -> cabinetMapper.toCabinetDto(cabinet)) + .collect(Collectors.toList()); + return cabinetMapper.toCabinetPaginationDtoList(cabinetDtos, + cabinets.getTotalElements()); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public CabinetPaginationDto getCabinetPaginationByStatus(CabinetStatus status, Integer page, + Integer size) { + log.debug("getCabinetPaginationByStatus"); + if (size <= 0) { + size = Integer.MAX_VALUE; + } + PageRequest pageable = PageRequest.of(page, size); + Page cabinets = cabinetOptionalFetcher.findPaginationByStatus(status, pageable); + List cabinetDtos = cabinets.toList().stream() + .map((cabinet) -> cabinetMapper.toCabinetDto(cabinet)) + .collect(Collectors.toList()); + return cabinetMapper.toCabinetPaginationDtoList(cabinetDtos, + cabinets.getTotalElements()); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public CabinetPaginationDto getCabinetPaginationByVisibleNum(Integer visibleNum, Integer page, + Integer size) { + log.debug("getCabinetPaginationByVisibleNum"); + if (size <= 0) { + size = Integer.MAX_VALUE; + } + PageRequest pageable = PageRequest.of(page, size); + Page cabinets = cabinetOptionalFetcher.findPaginationByVisibleNum(visibleNum, + pageable); + List cabinetDtos = cabinets.toList().stream() + .map((cabinet) -> cabinetMapper.toCabinetDto(cabinet)) + .collect(Collectors.toList()); + return cabinetMapper.toCabinetPaginationDtoList(cabinetDtos, + cabinets.getTotalElements()); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional(readOnly = true) + public LentHistoryPaginationDto getCabinetLentHistoriesPagination(Long cabinetId, Integer page, + Integer size) { + log.debug("getCabinetLentHistoriesPagination"); + if (size <= 0) { + size = Integer.MAX_VALUE; + } + PageRequest pageable = PageRequest.of(page, size, + Sort.by(Sort.Direction.DESC, "startedAt")); + Page lentHistories = lentOptionalFetcher.findPaginationByCabinetId(cabinetId, + pageable); + return lentMapper.toLentHistoryPaginationDto( + generateLentHistoryDtoList(lentHistories.toList()), + lentHistories.getTotalElements()); + } + + /** + * 사물함들의 정보와 각각의 대여 정보들을 가져옵니다. + * + * @param cabinetIds 사물함 id 리스트 + * @return 사물함 정보 리스트 + */ + @Transactional(readOnly = true) + public List getCabinetInfoBundle(List cabinetIds) { + log.debug("getCabinetInfoBundle"); + List result = new ArrayList<>(); + for (Long cabinetId : cabinetIds) { + CabinetInfoResponseDto cabinetInfo = getCabinetInfo(cabinetId); + if (CabinetInfoResponseDto.isValid(cabinetInfo)) { + result.add(cabinetInfo); + } + } + // Sorting ASC by Cabinet Floor + Comparator floorComparator = Comparator.comparing( + dto -> dto.getLocation().getFloor()); + + Collections.sort(result, floorComparator); + return result; + } + + @Override + @Transactional(readOnly = true) + public CabinetInfoPaginationDto getCabinetsInfo(Integer visibleNum) { + log.debug("getCabinetsInfo"); + PageRequest page = PageRequest.of(0, Integer.MAX_VALUE); + Page allCabinetsByVisibleNum = cabinetOptionalFetcher.findPaginationByVisibleNum( + visibleNum, page); + List collect = allCabinetsByVisibleNum.map(cabinet -> cabinet.getCabinetId()) + .stream().collect(Collectors.toList()); + return new CabinetInfoPaginationDto(getCabinetInfoBundle(collect), + allCabinetsByVisibleNum.getTotalElements()); + } + + /** + * LentHistory를 이용해 LentHistoryDto로 매핑하여 반환합니다. + * ToDo : new -> mapper 쓰기 + query service 분리 + * + * @param lentHistories 대여 기록 리스트 + * @return LentHistoryDto 리스트 + */ + private List generateLentHistoryDtoList( + List lentHistories) { + log.debug("generateLentHistoryDtoList"); + return lentHistories.stream() + .map(e -> lentMapper.toLentHistoryDto(e, + e.getUser(), + e.getCabinet())) + .collect(Collectors.toList()); + } /** * {@inheritDoc} @@ -416,7 +396,7 @@ public CabinetPendingResponseDto getPendingCabinets() { List allCabinets = cabinetOptionalFetcher.findAllCabinetsByBuilding(BUILDING_SAEROM); Map> cabinetFloorMap = allCabinets.parallelStream() .filter(cabinet -> !cabinet.isLentType(LentType.CLUB) - && (cabinet.isStatus(PENDING) || cabinet.isStatus(AVAILABLE))) + && (cabinet.isStatus(PENDING))) .collect(groupingBy(cabinet -> cabinet.getCabinetPlace().getLocation().getFloor(), mapping(cabinet -> cabinetMapper.toCabinetPreviewDto(cabinet, 0, null), Collectors.toList()))); @@ -428,73 +408,73 @@ public CabinetPendingResponseDto getPendingCabinets() { /*--------------------------------------------CUD--------------------------------------------*/ - /** - * {@inheritDoc} - */ - @Override - @Transactional - public void updateCabinetStatusNote(Long cabinetId, String statusNote) { - cabinetService.updateStatusNote(cabinetId, statusNote); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional - public void updateCabinetTitle(Long cabinetId, String title) { - cabinetService.updateTitle(cabinetId, title); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional - public void updateCabinetGrid(Long cabinetId, Integer row, Integer col) { - cabinetService.updateGrid(cabinetId, Grid.of(row, col)); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional - public void updateCabinetVisibleNum(Long cabinetId, Integer visibleNum) { - cabinetService.updateVisibleNum(cabinetId, visibleNum); - } - - /** - * {@inheritDoc} - */ - @Override - @Transactional - public void updateCabinetBundleStatus(CabinetStatusRequestDto cabinetStatusRequestDto) { - CabinetStatus status = cabinetStatusRequestDto.getStatus(); - LentType lentType = cabinetStatusRequestDto.getLentType(); - for (Long cabinetId : cabinetStatusRequestDto.getCabinetIds()) { - if (status != null) { - cabinetService.updateStatus(cabinetId, cabinetStatusRequestDto.getStatus()); - } - if (lentType != null) { - cabinetService.updateLentType(cabinetId, cabinetStatusRequestDto.getLentType()); - } - } - } - - - /** - * 사물함에 동아리 유저를 대여 시킵니다. - * - * @param cabinetClubStatusRequestDto - */ - @Override - @Transactional - public void updateCabinetClubStatus(CabinetClubStatusRequestDto cabinetClubStatusRequestDto) { - cabinetService.updateClub(cabinetClubStatusRequestDto.getCabinetId(), - cabinetClubStatusRequestDto.getUserId(), - cabinetClubStatusRequestDto.getStatusNote()); - } + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void updateCabinetStatusNote(Long cabinetId, String statusNote) { + cabinetService.updateStatusNote(cabinetId, statusNote); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void updateCabinetTitle(Long cabinetId, String title) { + cabinetService.updateTitle(cabinetId, title); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void updateCabinetGrid(Long cabinetId, Integer row, Integer col) { + cabinetService.updateGrid(cabinetId, Grid.of(row, col)); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void updateCabinetVisibleNum(Long cabinetId, Integer visibleNum) { + cabinetService.updateVisibleNum(cabinetId, visibleNum); + } + + /** + * {@inheritDoc} + */ + @Override + @Transactional + public void updateCabinetBundleStatus(CabinetStatusRequestDto cabinetStatusRequestDto) { + CabinetStatus status = cabinetStatusRequestDto.getStatus(); + LentType lentType = cabinetStatusRequestDto.getLentType(); + for (Long cabinetId : cabinetStatusRequestDto.getCabinetIds()) { + if (status != null) { + cabinetService.updateStatus(cabinetId, cabinetStatusRequestDto.getStatus()); + } + if (lentType != null) { + cabinetService.updateLentType(cabinetId, cabinetStatusRequestDto.getLentType()); + } + } + } + + + /** + * 사물함에 동아리 유저를 대여 시킵니다. + * + * @param cabinetClubStatusRequestDto + */ + @Override + @Transactional + public void updateCabinetClubStatus(CabinetClubStatusRequestDto cabinetClubStatusRequestDto) { + cabinetService.updateClub(cabinetClubStatusRequestDto.getCabinetId(), + cabinetClubStatusRequestDto.getUserId(), + cabinetClubStatusRequestDto.getStatusNote()); + } // /** // * {@inheritDoc} @@ -506,62 +486,62 @@ public void updateCabinetClubStatus(CabinetClubStatusRequestDto cabinetClubStatu // } // } - /** - * CACHE 적용되어 사용될 코드 - @Transactional(readOnly = true) - public List getCabinetsPerSection2(String building, - Integer floor) { - log.debug("getCabinetsPerSection2"); - List cabinetsByBuildingAndFloor2 = cabinetOptionalFetcher.findCabinetsByBuildingAndFloor2( - building, floor); - List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2( - building, floor); - // List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor(building, floor); - List allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor( - building, floor); - - // 층별 / 건물로 가져온 Cabinet 은 cache - // Cabinet 기준으로 lentHistory 를 조회 - // LentHistory와 연결된 User 조회 - - Map> cabinetLentHistories = currentLentCabinets.stream(). - collect(Collectors.groupingBy(ActiveCabinetInfoEntities::getCabinet, - Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory, - Collectors.toList()))); - - Map> cabinetPreviewsBySection = new HashMap<>(); - cabinetLentHistories.forEach((cabinet, lentHistories) -> { - String section = cabinet.getCabinetPlace().getLocation().getSection(); - CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories); - if (cabinetPreviewsBySection.containsKey(section)) { - cabinetPreviewsBySection.get(section).add(preview); - } else { - List previews = new ArrayList<>(); - previews.add(preview); - cabinetPreviewsBySection.put(section, previews); - } - }); - allCabinetsByBuildingAndFloor.forEach(cabinet -> { - if (!cabinetLentHistories.containsKey(cabinet)) { - String section = cabinet.getCabinetPlace().getLocation().getSection(); - CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, - Collections.emptyList()); - if (cabinetPreviewsBySection.containsKey(section)) { - cabinetPreviewsBySection.get(section).add(preview); - } else { - List previews = new ArrayList<>(); - previews.add(preview); - cabinetPreviewsBySection.put(section, previews); - } - } - }); - cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort( - Comparator.comparing(CabinetPreviewDto::getVisibleNum))); - return cabinetPreviewsBySection.entrySet().stream() - .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) - .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), - entry.getValue())) - .collect(Collectors.toList()); - } - **/ + /** + * CACHE 적용되어 사용될 코드 + @Transactional(readOnly = true) + public List getCabinetsPerSection2(String building, + Integer floor) { + log.debug("getCabinetsPerSection2"); + List cabinetsByBuildingAndFloor2 = cabinetOptionalFetcher.findCabinetsByBuildingAndFloor2( + building, floor); + List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor2( + building, floor); + // List currentLentCabinets = cabinetOptionalFetcher.findCabinetsActiveLentHistoriesByBuildingAndFloor(building, floor); + List allCabinetsByBuildingAndFloor = cabinetOptionalFetcher.findAllCabinetsByBuildingAndFloor( + building, floor); + + // 층별 / 건물로 가져온 Cabinet 은 cache + // Cabinet 기준으로 lentHistory 를 조회 + // LentHistory와 연결된 User 조회 + + Map> cabinetLentHistories = currentLentCabinets.stream(). + collect(Collectors.groupingBy(ActiveCabinetInfoEntities::getCabinet, + Collectors.mapping(ActiveCabinetInfoEntities::getLentHistory, + Collectors.toList()))); + + Map> cabinetPreviewsBySection = new HashMap<>(); + cabinetLentHistories.forEach((cabinet, lentHistories) -> { + String section = cabinet.getCabinetPlace().getLocation().getSection(); + CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, lentHistories); + if (cabinetPreviewsBySection.containsKey(section)) { + cabinetPreviewsBySection.get(section).add(preview); + } else { + List previews = new ArrayList<>(); + previews.add(preview); + cabinetPreviewsBySection.put(section, previews); + } + }); + allCabinetsByBuildingAndFloor.forEach(cabinet -> { + if (!cabinetLentHistories.containsKey(cabinet)) { + String section = cabinet.getCabinetPlace().getLocation().getSection(); + CabinetPreviewDto preview = createCabinetPreviewDto(cabinet, + Collections.emptyList()); + if (cabinetPreviewsBySection.containsKey(section)) { + cabinetPreviewsBySection.get(section).add(preview); + } else { + List previews = new ArrayList<>(); + previews.add(preview); + cabinetPreviewsBySection.put(section, previews); + } + } + }); + cabinetPreviewsBySection.values().forEach(cabinetList -> cabinetList.sort( + Comparator.comparing(CabinetPreviewDto::getVisibleNum))); + return cabinetPreviewsBySection.entrySet().stream() + .sorted(Comparator.comparing(entry -> entry.getValue().get(0).getVisibleNum())) + .map(entry -> cabinetMapper.toCabinetsPerSectionResponseDto(entry.getKey(), + entry.getValue())) + .collect(Collectors.toList()); + } + **/ } From 5ddae1b39ef70a3fff1651b15b386af821968eb1 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Mon, 27 Nov 2023 15:33:47 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[BE]=20HOTFIX=20:=20AVAILABLE=EC=9D=B8=20?= =?UTF-8?q?=EC=82=AC=EB=AC=BC=ED=95=A8=EC=9D=84=20=EC=9A=B0=EC=84=A0?= =?UTF-8?q?=EC=A0=81=EC=9C=BC=EB=A1=9C=20=EB=B3=B4=EC=97=AC=EC=A3=BC?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cabinet/cabinet/service/CabinetFacadeServiceImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java index 9a6ec8387..fc10a2fb7 100644 --- a/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java +++ b/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java @@ -27,7 +27,7 @@ import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.mapping; -import static org.ftclub.cabinet.cabinet.domain.CabinetStatus.PENDING; +import static org.ftclub.cabinet.cabinet.domain.CabinetStatus.AVAILABLE; @Service @RequiredArgsConstructor @@ -396,7 +396,7 @@ public CabinetPendingResponseDto getPendingCabinets() { List allCabinets = cabinetOptionalFetcher.findAllCabinetsByBuilding(BUILDING_SAEROM); Map> cabinetFloorMap = allCabinets.parallelStream() .filter(cabinet -> !cabinet.isLentType(LentType.CLUB) - && (cabinet.isStatus(PENDING))) + && (cabinet.isStatus(AVAILABLE))) .collect(groupingBy(cabinet -> cabinet.getCabinetPlace().getLocation().getFloor(), mapping(cabinet -> cabinetMapper.toCabinetPreviewDto(cabinet, 0, null), Collectors.toList()))); From 11ece426d808548df64f3c1ec973e00cf7abf646 Mon Sep 17 00:00:00 2001 From: Ssuamje Date: Mon, 27 Nov 2023 15:33:59 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[BE]=20HOTFIX=20:=20AVAILABLE=EC=9D=B8=20?= =?UTF-8?q?=EC=82=AC=EB=AC=BC=ED=95=A8=EC=9D=84=20=EC=9A=B0=EC=84=A0?= =?UTF-8?q?=EC=A0=81=EC=9C=BC=EB=A1=9C=20=EB=B3=B4=EC=97=AC=EC=A3=BC?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cabinet/cabinet/service/CabinetFacadeServiceImpl.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java b/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java index fc10a2fb7..3c432cf41 100644 --- a/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java +++ b/backend/src/main/java/org/ftclub/cabinet/cabinet/service/CabinetFacadeServiceImpl.java @@ -395,8 +395,7 @@ public CabinetPendingResponseDto getPendingCabinets() { log.debug("getPendingCabinets"); List allCabinets = cabinetOptionalFetcher.findAllCabinetsByBuilding(BUILDING_SAEROM); Map> cabinetFloorMap = allCabinets.parallelStream() - .filter(cabinet -> !cabinet.isLentType(LentType.CLUB) - && (cabinet.isStatus(AVAILABLE))) + .filter(cabinet -> !cabinet.isLentType(LentType.CLUB) && (cabinet.isStatus(AVAILABLE))) .collect(groupingBy(cabinet -> cabinet.getCabinetPlace().getLocation().getFloor(), mapping(cabinet -> cabinetMapper.toCabinetPreviewDto(cabinet, 0, null), Collectors.toList())));