Skip to content

Commit

Permalink
✨ Feature: reportWeek 계산 로직 보완 (#101)
Browse files Browse the repository at this point in the history
* ✨ Feature: reportWeek 계산 로직 보완

* ✨ Feature: 긍정적인 감정 description 추가
  • Loading branch information
ahnsugyeong authored Jun 3, 2024
1 parent 32c3547 commit 2f99a8f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package zzangdol.report.business;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.WeekFields;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import zzangdol.report.domain.Asset;
Expand Down Expand Up @@ -48,14 +48,21 @@ private static List<ReportEmotionResponse> toReportEmotionResponseList(List<Repo
}

public static ReportResponse toReportResponse(Report report, Long prevReportId, Long nextReportId) {
LocalDate previousSunday = report.getCreatedAt().toLocalDate().minusDays(1);
WeekFields weekFields = WeekFields.of(Locale.getDefault());
int weekOfMonth = previousSunday.get(weekFields.weekOfMonth());
int month = previousSunday.getMonthValue();
LocalDate endDate = report.getEndDate();
WeekFields weekFields = WeekFields.ISO;
int weekOfMonth = endDate.get(weekFields.weekOfMonth());
int month = endDate.getMonthValue();

// 목요일 기준으로 주의 월을 결정하는 로직
LocalDate thursdayOfCurrentWeek = endDate.with(DayOfWeek.THURSDAY);
if (thursdayOfCurrentWeek.getMonthValue() != endDate.getMonthValue()) {
month = thursdayOfCurrentWeek.getMonthValue();
weekOfMonth = thursdayOfCurrentWeek.get(weekFields.weekOfMonth());
}

String weekKorean = weekNumberToKorean.getOrDefault(weekOfMonth, weekOfMonth + "째 주");
String reportWeek = String.format("%d월 %s", month, weekKorean);


return ReportResponse.builder()
.id(report.getId())
.reportWeek(reportWeek)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import zzangdol.constant.Constants;
import zzangdol.diary.dao.DiaryRepository;
import zzangdol.diary.domain.Diary;
import zzangdol.emotion.domain.Emotion;
import zzangdol.emotion.domain.EmotionPolarity;
import zzangdol.exception.custom.DiaryNotFoundException;
import zzangdol.exception.custom.ReportEmotionDataMissingException;
import zzangdol.exception.custom.ReportNotFoundException;
import zzangdol.report.dao.AssetRepository;
import zzangdol.report.dao.ReportRepository;
import zzangdol.report.dao.querydsl.AssetQueryRepository;
import zzangdol.report.domain.Asset;
import zzangdol.report.domain.Report;
import zzangdol.report.domain.ReportEmotion;
Expand All @@ -32,7 +32,7 @@ public class ReportCommandServiceImpl implements ReportCommandService {

private final ReportRepository reportRepository;
private final DiaryRepository diaryRepository;
private final AssetQueryRepository assetQueryRepository;
private final AssetRepository assetRepository;
private final UserRepository userRepository;

@Scheduled(cron = "0 0 0 * * MON")
Expand Down Expand Up @@ -76,9 +76,16 @@ public Report createReport(User user) {
double positivePercentage = (double) positiveCount.get() / totalEmotions * 100;
double negativePercentage = (double) negativeCount.get() / totalEmotions * 100;

Optional<Asset> optionalAsset =
(negativePercentage > positivePercentage) ? assetQueryRepository.findRandomAsset() : Optional.empty();
Report report = buildReport(user, positivePercentage, negativePercentage, optionalAsset.orElse(null));
Asset asset;
if (positivePercentage > negativePercentage) {
asset = assetRepository.findById(Constants.POSITIVE_ASSET_ID)
.orElse(null);
} else {
asset = assetRepository.findRandomAssetExcludingId(Constants.POSITIVE_ASSET_ID)
.orElse(null);
}

Report report = buildReport(user, asset, positivePercentage, negativePercentage, lastMonday, thisSunday);
emotionCounts.forEach(
(emotion, count) -> report.addReportEmotion(buildReportEmotion(report, totalEmotions, emotion)));

Expand Down Expand Up @@ -118,9 +125,16 @@ public Report createReportByDate(User user, LocalDate startDate, LocalDate endDa
int positivePercentage = (int) Math.round((double) positiveCount.get() / totalEmotions * 100);
int negativePercentage = (int) Math.round((double) negativeCount.get() / totalEmotions * 100);

Optional<Asset> optionalAsset =
(negativePercentage > positivePercentage) ? assetQueryRepository.findRandomAsset() : Optional.empty();
Report report = buildReport(user, positivePercentage, negativePercentage, optionalAsset.orElse(null));
Asset asset;
if (positivePercentage > negativePercentage) {
asset = assetRepository.findById(Constants.POSITIVE_ASSET_ID)
.orElse(null);
} else {
asset = assetRepository.findRandomAssetExcludingId(Constants.POSITIVE_ASSET_ID)
.orElse(null);
}

Report report = buildReport(user, asset, positivePercentage, negativePercentage, startDate, endDate);

emotionCounts.entrySet().stream()
.sorted(Map.Entry.<Emotion, Integer>comparingByValue().reversed())
Expand Down Expand Up @@ -154,12 +168,15 @@ private ReportEmotion buildReportEmotion(Report report, int percentage, Emotion
.build();
}

private Report buildReport(User user, double positivePercentage, double negativePercentage, Asset asset) {
private Report buildReport(User user, Asset asset, double positivePercentage, double negativePercentage,
LocalDate startDate, LocalDate endDate) {
return Report.builder()
.user(user)
.asset(asset)
.positivePercentage(positivePercentage)
.negativePercentage(negativePercentage)
.asset(asset)
.startDate(startDate)
.endDate(endDate)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class Constants {

public static final String DEFAULT_IMAGE_URL = "https://moodoodle-dev.s3.ap-northeast-2.amazonaws.com/default_category_image.png";
public static final String DEFAULT_CATEGORY_NAME = "모든 스크랩";
public static final Long POSITIVE_ASSET_ID = 6L;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package zzangdol.report.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import zzangdol.report.dao.querydsl.AssetQueryRepository;
import zzangdol.report.domain.Asset;

public interface AssetRepository extends JpaRepository<Asset, Long> {
public interface AssetRepository extends JpaRepository<Asset, Long>, AssetQueryRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

public interface AssetQueryRepository {

Optional<Asset> findRandomAsset();
Optional<Asset> findRandomAssetExcludingId(Long excludedId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static zzangdol.report.domain.QAsset.asset;

import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
Expand All @@ -16,16 +17,14 @@ public class AssetQueryRepositoryImpl implements AssetQueryRepository {
private final JPAQueryFactory queryFactory;

@Override
public Optional<Asset> findRandomAsset() {
int count = queryFactory.selectFrom(asset).fetch().size();

if (count > 0) {
int index = (int) (Math.random() * count);
Asset selectedAsset = queryFactory.selectFrom(asset)
.offset(index)
.limit(1)
.fetchOne();
return Optional.ofNullable(selectedAsset);
public Optional<Asset> findRandomAssetExcludingId(Long excludedId) {
List<Asset> assets = queryFactory.selectFrom(asset)
.where(asset.id.ne(excludedId))
.fetch();

if (assets.size() > 0) {
int index = (int) (Math.random() * assets.size());
return Optional.of(assets.get(index));
}

return Optional.empty();
Expand Down
12 changes: 10 additions & 2 deletions moodoodle-domain/src/main/java/zzangdol/report/domain/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import lombok.AccessLevel;
Expand Down Expand Up @@ -40,12 +41,19 @@ public class Report extends BaseTimeEntity {
private double positivePercentage;
private double negativePercentage;

private LocalDate startDate;
private LocalDate endDate;

@Builder
public Report(User user, double positivePercentage, double negativePercentage, Asset asset) {
public Report(User user, Asset asset,
double positivePercentage, double negativePercentage,
LocalDate startDate, LocalDate endDate) {
this.user = user;
this.asset = asset;
this.positivePercentage = positivePercentage;
this.negativePercentage = negativePercentage;
this.asset = asset;
this.startDate = startDate;
this.endDate = endDate;
}

public void addReportEmotion(ReportEmotion reportEmotion) {
Expand Down

0 comments on commit 2f99a8f

Please sign in to comment.