diff --git a/src/main/java/me/barion/capstoneprojectbarion/dto/SalesDto.java b/src/main/java/me/barion/capstoneprojectbarion/dto/SalesDto.java index aeba1c0..454ed82 100644 --- a/src/main/java/me/barion/capstoneprojectbarion/dto/SalesDto.java +++ b/src/main/java/me/barion/capstoneprojectbarion/dto/SalesDto.java @@ -1,21 +1,18 @@ package me.barion.capstoneprojectbarion.dto; -import lombok.Getter; -import lombok.Setter; - +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; import java.time.LocalDateTime; @Getter @Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +@JsonInclude(JsonInclude.Include.NON_NULL) public class SalesDto { - private LocalDateTime salesDate; // 매출 날짜/시간 - private Integer totalSales; // 총 매출 금액 - - public SalesDto() { - } - - public SalesDto(LocalDateTime salesDate, Integer totalSales) { - this.salesDate = salesDate; - this.totalSales = totalSales; - } + private LocalDateTime salesDate; // 또는 date + private Integer totalSales; + private Integer totalCost; + private Integer profit; } diff --git a/src/main/java/me/barion/capstoneprojectbarion/service/SalesService.java b/src/main/java/me/barion/capstoneprojectbarion/service/SalesService.java index 7c04880..da45e30 100644 --- a/src/main/java/me/barion/capstoneprojectbarion/service/SalesService.java +++ b/src/main/java/me/barion/capstoneprojectbarion/service/SalesService.java @@ -1,12 +1,14 @@ package me.barion.capstoneprojectbarion.service; import me.barion.capstoneprojectbarion.dto.SalesDto; -import me.barion.capstoneprojectbarion.Entity.Order; // Order 엔티티는 이 메소드에서 직접 사용하지 않음 +import me.barion.capstoneprojectbarion.Entity.Order; import me.barion.capstoneprojectbarion.Entity.Sales; import me.barion.capstoneprojectbarion.repository.OrderRepository; import me.barion.capstoneprojectbarion.repository.SalesRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +// import org.slf4j.Logger; // 필요시 로깅 추가 +// import org.slf4j.LoggerFactory; // 필요시 로깅 추가 import java.time.LocalDateTime; import java.util.ArrayList; @@ -26,7 +28,10 @@ public class SalesService { public SalesDto calculateTotalSales() { Integer totalAmount = orderRepository.sumTotalAmount(); - return new SalesDto(LocalDateTime.now(), totalAmount != null ? totalAmount : 0); + return SalesDto.builder() + .salesDate(LocalDateTime.now()) + .totalSales(totalAmount != null ? totalAmount : 0) + .build(); } public List calculateYearlySales() { @@ -40,7 +45,12 @@ public List calculateYearlySales() { )); salesByYear.forEach((year, total) -> { - yearlySalesDtos.add(new SalesDto(LocalDateTime.of(year, 1, 1, 0, 0), total)); + yearlySalesDtos.add( + SalesDto.builder() + .salesDate(LocalDateTime.of(year, 1, 1, 0, 0)) + .totalSales(total) + .build() + ); }); yearlySalesDtos.sort((s1, s2) -> s1.getSalesDate().compareTo(s2.getSalesDate())); return yearlySalesDtos; @@ -57,7 +67,12 @@ public List calculateMonthlySales() { )); salesByMonth.forEach((yearMonth, total) -> { - monthlySalesDtos.add(new SalesDto(yearMonth.atDay(1).atStartOfDay(), total)); + monthlySalesDtos.add( + SalesDto.builder() + .salesDate(yearMonth.atDay(1).atStartOfDay()) + .totalSales(total) + .build() + ); }); monthlySalesDtos.sort((s1, s2) -> s1.getSalesDate().compareTo(s2.getSalesDate())); return monthlySalesDtos; @@ -73,7 +88,12 @@ public List calculateHourlySales() { )); salesByHour.forEach((hour, total) -> { - hourlySalesDtos.add(new SalesDto(hour, total)); + hourlySalesDtos.add( + SalesDto.builder() + .salesDate(hour) + .totalSales(total) + .build() + ); }); hourlySalesDtos.sort((s1, s2) -> s1.getSalesDate().compareTo(s2.getSalesDate())); return hourlySalesDtos; @@ -90,16 +110,24 @@ public List calculateDailySales() { )); salesByDay.forEach((day, total) -> { - dailySalesDtos.add(new SalesDto(day, total)); + dailySalesDtos.add( + SalesDto.builder() + .salesDate(day) + .totalSales(total) + .build() + ); }); dailySalesDtos.sort((s1, s2) -> s1.getSalesDate().compareTo(s2.getSalesDate())); return dailySalesDtos; } - public SalesDto calculateTotalProfit() { - Integer totalProfit = salesRepository.sumTotalProfit(); - return new SalesDto(LocalDateTime.now(), totalProfit != null ? totalProfit : 0); + + public SalesDto calculateTotalProfit() { + Integer totalProfitValue = salesRepository.sumTotalProfit();return SalesDto.builder() + .salesDate(LocalDateTime.now()) + .profit(totalProfitValue != null ? totalProfitValue : 0) + .build(); } public List calculateMonthlyProfit() { @@ -108,16 +136,25 @@ public List calculateMonthlyProfit() { Map profitByMonth = salesList.stream() .collect(Collectors.groupingBy( - sales -> YearMonth.from(sales.getSalesDate()), // salesDate에서 YearMonth 추출 + sales -> YearMonth.from(sales.getSalesDate()), Collectors.summingInt(sales -> sales.getTotalSales() - sales.getTotalCost()) // 순이익 계산 )); - profitByMonth.forEach((yearMonth, profit) -> { - - monthlyProfitDtos.add(new SalesDto(yearMonth.atDay(1).atStartOfDay(), profit)); + profitByMonth.forEach((yearMonth, profitValue) -> { + monthlyProfitDtos.add( + SalesDto.builder() + .salesDate(yearMonth.atDay(1).atStartOfDay()) + .profit(profitValue) // profit 필드에 할당 + .build() + ); }); - monthlyProfitDtos.sort((dto1, dto2) -> dto1.getSalesDate().compareTo(dto2.getSalesDate())); + monthlyProfitDtos.sort((dto1, dto2) -> { + if (dto1.getSalesDate() == null && dto2.getSalesDate() == null) return 0; + if (dto1.getSalesDate() == null) return -1; + if (dto2.getSalesDate() == null) return 1; + return dto1.getSalesDate().compareTo(dto2.getSalesDate()); + }); return monthlyProfitDtos; }