From a32788855f29c6ec02d6adc01557230e4c00cc4c Mon Sep 17 00:00:00 2001 From: Soyeon-Cha <7103sy@naver.com> Date: Sat, 6 Apr 2024 08:25:13 +0900 Subject: [PATCH 1/3] =?UTF-8?q?fix:=20History=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=97=90=20image=20=EC=BB=AC=EB=9F=BC=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/CookSave/CookSaveback/History/domain/History.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/CookSave/CookSaveback/History/domain/History.java b/src/main/java/CookSave/CookSaveback/History/domain/History.java index 3f9c8c2..04ef3cc 100644 --- a/src/main/java/CookSave/CookSaveback/History/domain/History.java +++ b/src/main/java/CookSave/CookSaveback/History/domain/History.java @@ -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; } } From b2b80d319913760a5c7c759ababbf97ea57c1ca0 Mon Sep 17 00:00:00 2001 From: Soyeon-Cha <7103sy@naver.com> Date: Sat, 6 Apr 2024 08:27:54 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EC=B6=94=EA=B0=80=EB=90=9C=20image?= =?UTF-8?q?=20=EC=BB=AC=EB=9F=BC=EC=9D=84=20History=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=20=EA=B3=BC=EC=A0=95=EC=97=90=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CookSave/CookSaveback/History/service/HistoryService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java b/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java index 4ce433f..0270e18 100644 --- a/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java +++ b/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java @@ -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들 저장 @@ -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들 저장 From fbc85ae0fff373a2aefc55daf16a9c907024ce55 Mon Sep 17 00:00:00 2001 From: Soyeon-Cha <7103sy@naver.com> Date: Sat, 6 Apr 2024 10:46:30 +0900 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=ED=86=B5=EA=B3=84=20=EC=9A=94?= =?UTF-8?q?=EC=95=BD=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../History/controller/HistoryController.java | 8 +++ .../History/dto/HighlightHistoryDto.java | 23 +++++++ .../History/dto/HistoryHighlightResDto.java | 19 ++++++ .../History/service/HistoryService.java | 64 ++++++++++++++++++- 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 src/main/java/CookSave/CookSaveback/History/dto/HighlightHistoryDto.java create mode 100644 src/main/java/CookSave/CookSaveback/History/dto/HistoryHighlightResDto.java diff --git a/src/main/java/CookSave/CookSaveback/History/controller/HistoryController.java b/src/main/java/CookSave/CookSaveback/History/controller/HistoryController.java index 1eb8da0..ab80690 100644 --- a/src/main/java/CookSave/CookSaveback/History/controller/HistoryController.java +++ b/src/main/java/CookSave/CookSaveback/History/controller/HistoryController.java @@ -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); + } } diff --git a/src/main/java/CookSave/CookSaveback/History/dto/HighlightHistoryDto.java b/src/main/java/CookSave/CookSaveback/History/dto/HighlightHistoryDto.java new file mode 100644 index 0000000..e83570e --- /dev/null +++ b/src/main/java/CookSave/CookSaveback/History/dto/HighlightHistoryDto.java @@ -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(); + } +} diff --git a/src/main/java/CookSave/CookSaveback/History/dto/HistoryHighlightResDto.java b/src/main/java/CookSave/CookSaveback/History/dto/HistoryHighlightResDto.java new file mode 100644 index 0000000..62aa6cf --- /dev/null +++ b/src/main/java/CookSave/CookSaveback/History/dto/HistoryHighlightResDto.java @@ -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 highlightHistoryDtoList; +} diff --git a/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java b/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java index 0270e18..449de65 100644 --- a/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java +++ b/src/main/java/CookSave/CookSaveback/History/service/HistoryService.java @@ -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); @@ -146,4 +146,66 @@ public AnnualHistoryResDto getAnnualHistory(Member member, LocalDate date) { } return new AnnualHistoryResDto(expenseDtoList, countDtoList); } + + // 통계 요약 조회 + public HistoryHighlightResDto getHistoryHighlight(Member member, LocalDate date){ + List histories = historyRepository.findAllByMember(member); + List monthHistories = new ArrayList<>(); + List 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); + } }