diff --git a/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java b/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java index 42b5f04..31902bc 100644 --- a/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java +++ b/src/main/java/com/hanaro/hanafun/revenue/domain/RevenueRepository.java @@ -11,10 +11,8 @@ public interface RevenueRepository extends JpaRepository { Optional findByCreatedDateBetween(LocalDateTime startDateTime, LocalDateTime endDateTime); - @Query(value = "SELECT SUM(R.revenue) FROM RevenueEntity R WHERE R.lessonEntity = :lessonEntity") Long totalRevenueByLessonId(@Param("lessonEntity") LessonEntity lessonEntity); - Optional findByLessonEntityAndCreatedDateBetween(LessonEntity lessonEntity, LocalDateTime startDateTime, LocalDateTime endDateTime); Optional> findByLessonEntityLessonIdAndCreatedDateBetween(Long lessonId, LocalDateTime startDateTime, LocalDateTime endDateTime); } diff --git a/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java b/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java index d55be5e..1a6a9c4 100644 --- a/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java +++ b/src/main/java/com/hanaro/hanafun/revenue/service/impl/RevenueServiceImpl.java @@ -5,9 +5,11 @@ import com.hanaro.hanafun.host.exception.HostNotFoundException; import com.hanaro.hanafun.lesson.domain.LessonEntity; import com.hanaro.hanafun.lesson.domain.LessonRepository; +import com.hanaro.hanafun.lesson.exception.LessonNotFoundException; import com.hanaro.hanafun.revenue.domain.RevenueEntity; import com.hanaro.hanafun.revenue.domain.RevenueRepository; import com.hanaro.hanafun.revenue.dto.*; +import com.hanaro.hanafun.revenue.exception.RevenueNotFoundException; import com.hanaro.hanafun.revenue.mapper.RevenueMapper; import com.hanaro.hanafun.revenue.service.RevenueService; import lombok.RequiredArgsConstructor; @@ -45,13 +47,19 @@ public TotalRevenueResDto totalRevenue(Long userId) { @Override @Transactional public List monthRevenue(Long userId, Integer year, Integer month) { - HostEntity hostEntity = hostRepository.findByUserEntityUserId(userId).orElseThrow(() -> new HostNotFoundException()); - List lessonEntityList = lessonRepository.findByHostEntityHostId(hostEntity.getHostId()).orElseThrow(); + //호스트 가져오기 + HostEntity hostEntity = hostRepository.findByUserEntityUserId(userId) + .orElseThrow(() -> new HostNotFoundException()); + + //호스트의 전체 강좌 목록 가져오기 + List lessonEntityList = lessonRepository.findByHostEntityHostId(hostEntity.getHostId()) + .orElseThrow(() -> new LessonNotFoundException()); YearMonth yearMonth = YearMonth.of(year, month); LocalDateTime startOfMonth = yearMonth.atDay(1).atStartOfDay(); LocalDateTime endOfMonth = yearMonth.atEndOfMonth().atTime(23, 59, 59); + //강좌마다 해당년월의 매출 하나씩 건지고, collect 합쳐서 해당년월의 전체 매출 리스트 구하기 List revenueEntityList = lessonEntityList.stream() .flatMap(lessonEntity -> revenueRepository.findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth).stream()) .collect(Collectors.toList()); @@ -68,7 +76,10 @@ public List lessonRevenue(Integer year, Long lessonId) { LocalDateTime startOfYear = searchYear.atMonth(1).atDay(1).atStartOfDay(); LocalDateTime endOfYear = searchYear.atMonth(12).atEndOfMonth().atTime(23,59,59); - List revenueEntityList = revenueRepository.findByLessonEntityLessonIdAndCreatedDateBetween(lessonId, startOfYear, endOfYear).orElseThrow(); + //강좌의 해당 년 매출 리스트 구하기 + List revenueEntityList = revenueRepository + .findByLessonEntityLessonIdAndCreatedDateBetween(lessonId, startOfYear, endOfYear) + .orElseThrow(() -> new RevenueNotFoundException()); return revenueEntityList.stream() .map(RevenueMapper::revenueEntityToLessonRevenueResDto) @@ -78,13 +89,16 @@ public List lessonRevenue(Integer year, Long lessonId) { @Override @Transactional public UpdatePriceResDto updatePrice(UpdatePriceReqDto updatePriceReqDto) { - LessonEntity lessonEntity = lessonRepository.findById(updatePriceReqDto.getLessonId()).orElseThrow(); + LessonEntity lessonEntity = lessonRepository.findById(updatePriceReqDto.getLessonId()) + .orElseThrow(() -> new LessonNotFoundException()); YearMonth yearMonth = YearMonth.of(updatePriceReqDto.getYear(), updatePriceReqDto.getMonth()); LocalDateTime startOfMonth = yearMonth.atDay(1).atStartOfDay(); LocalDateTime endOfMonth = yearMonth.atEndOfMonth().atTime(23, 59, 59); - RevenueEntity revenueEntity = revenueRepository.findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth).orElseThrow(); + RevenueEntity revenueEntity = revenueRepository + .findByLessonEntityAndCreatedDateBetween(lessonEntity, startOfMonth, endOfMonth) + .orElseThrow(() -> new RevenueNotFoundException()); revenueEntity.setMaterialPrice(updatePriceReqDto.getMaterialPrice()); revenueEntity.setRentalPrice(updatePriceReqDto.getRentalPrice()); revenueEntity.setEtcPrice(updatePriceReqDto.getEtcPrice());