Skip to content

Commit

Permalink
Merge pull request #14 from EWHA-CAPSTONE-COOKSAVE/feature/history
Browse files Browse the repository at this point in the history
[차소연] 통계 요약 조회 기능 구현
  • Loading branch information
Soyeon-Cha authored Apr 6, 2024
2 parents 44557df + fbc85ae commit 6f5adb6
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ public AnnualHistoryResDto getAnnualHistory(@RequestParam(required = false) Loca
Member member = memberService.getLoginMember();
return historyService.getAnnualHistory(member, date);
}

// 통계 요약 조회
@GetMapping("/highlight")
@ResponseStatus(HttpStatus.OK)
public HistoryHighlightResDto getHistoryHighlight(@RequestParam(required = false) LocalDate date){
Member member = memberService.getLoginMember();
return historyService.getHistoryHighlight(member, date);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public class History extends BaseTimeEntity {
@Column(nullable = false)
private String name; // 요리명

@Column
private String image; // 요리 사진 URL

@Column(nullable = false)
private Integer total; // 사용한 재료값 총합

@Builder
public History(Member member, String name, Integer total){
public History(Member member, String name, String image, Integer total){
this.member = member;
this.name = name;
this.image = image;
this.total = total;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package CookSave.CookSaveback.History.dto;

import CookSave.CookSaveback.History.domain.History;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;

@Getter
public class HighlightHistoryDto {
private String image;
private String name;
private Integer total;
private LocalDate createdAt;

@Builder
public HighlightHistoryDto(History history){
this.image = history.getImage();
this.name = history.getName();
this.total = history.getTotal();
this.createdAt = history.getCreatedAt();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package CookSave.CookSaveback.History.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

@Getter
@AllArgsConstructor
public class HistoryHighlightResDto {
private Integer percentage;
private Integer count;
private Integer budget;
private Integer monthExpense;
private Integer balance;
private Integer average;
private Integer previousAverage;
private List<HighlightHistoryDto> highlightHistoryDtoList;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void createRecipeHistory(Member member, Long recipeId, RecipeHistoryReqDt
// History 저장
Recipe recipe = recipeRepository.findById(recipeId)
.orElseThrow(() -> new EntityNotFoundException("recipeId " + recipeId + "인 레시피가 존재하지 않습니다."));
History history = new History(member, recipe.getName(), recipeHistoryReqDto.getTotal());
History history = new History(member, recipe.getName(), recipe.getImage(), recipeHistoryReqDto.getTotal());
historyRepository.save(history);

// HistoryIngredient들 저장
Expand All @@ -48,7 +48,7 @@ public void createRecipeHistory(Member member, Long recipeId, RecipeHistoryReqDt

public void createInputHistory(Member member, InputHistoryReqDto inputHistoryReqDto) {
// History 저장
History history = new History(member, inputHistoryReqDto.getName(), inputHistoryReqDto.getTotal());
History history = new History(member, inputHistoryReqDto.getName(), null, inputHistoryReqDto.getTotal());
historyRepository.save(history);

// HistoryIngredient들 저장
Expand Down Expand Up @@ -95,7 +95,7 @@ public AnnualHistoryResDto getAnnualHistory(Member member, LocalDate date) {
Integer expense = 0;
Integer count = 0;

// date가 null일 경우 현재 날짜를 기준으로 통계 조회
// date가 null인 경우 현재 날짜를 기준으로 통계 조회
if (date == null){
searchingDate = LocalDate.now();
String cutDate = searchingDate.toString().substring(0, 7);
Expand Down Expand Up @@ -146,4 +146,66 @@ public AnnualHistoryResDto getAnnualHistory(Member member, LocalDate date) {
}
return new AnnualHistoryResDto(expenseDtoList, countDtoList);
}

// 통계 요약 조회
public HistoryHighlightResDto getHistoryHighlight(Member member, LocalDate date){
List<History> histories = historyRepository.findAllByMember(member);
List<History> monthHistories = new ArrayList<>();
List<HighlightHistoryDto> highlightHistoryDtoList = new ArrayList<>();
Integer count = 0;
Integer expense = 0;
Integer previousCount = 0;
Integer previousExpense = 0;

// date가 null인 경우 현재 달의 1일로 시작일 설정
if (date == null){
date = LocalDate.now();
String cutDate = date.toString().substring(0, 7);
date = LocalDate.parse(cutDate+"-01", DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}

LocalDate endDate = date.plusMonths(1);

for (History history : histories){
LocalDate createdAt = history.getCreatedAt();
if ((createdAt.isEqual(date)|createdAt.isAfter(date))&&(createdAt.isBefore(endDate))){
count++;
expense += history.getTotal();
monthHistories.add(history);
}

// 지난 달 사용 금액 구하기
else if ((createdAt.isEqual(date.minusMonths(1))|createdAt.isAfter(date.minusMonths(1)))&&(createdAt.isBefore(date))){
previousCount++;
previousExpense += history.getTotal();
}
}

Integer budget = member.getBudget();
double percentage = 0;
if (budget!=0){
percentage = (expense.doubleValue())/(budget.doubleValue())*100.0;
}

Integer balance = budget-expense;
if (balance<=0){
balance = 0;
}

Integer average = 0;
if (count!=0){
average = expense/count;
}

Integer previousAverage = 0;
if (previousCount!=0){
previousAverage = previousExpense/previousCount;
}

for (History history : monthHistories){
highlightHistoryDtoList.add(new HighlightHistoryDto(history));
}

return new HistoryHighlightResDto((int) percentage, count, budget, expense, balance, average, previousAverage, highlightHistoryDtoList);
}
}

0 comments on commit 6f5adb6

Please sign in to comment.