Skip to content
Merged

fix #44

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/main/java/me/barion/capstoneprojectbarion/dto/SalesDto.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SalesDto> calculateYearlySales() {
Expand All @@ -40,7 +45,12 @@ public List<SalesDto> 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;
Expand All @@ -57,7 +67,12 @@ public List<SalesDto> 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;
Expand All @@ -73,7 +88,12 @@ public List<SalesDto> 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;
Expand All @@ -90,16 +110,24 @@ public List<SalesDto> 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<SalesDto> calculateMonthlyProfit() {
Expand All @@ -108,16 +136,25 @@ public List<SalesDto> calculateMonthlyProfit() {

Map<YearMonth, Integer> 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;
}
Expand Down
Loading