diff --git a/src/main/java/com/moing/backend/domain/mission/domain/repository/MissionCustomRepositoryImpl.java b/src/main/java/com/moing/backend/domain/mission/domain/repository/MissionCustomRepositoryImpl.java index 0e5b28c9..1afc2552 100644 --- a/src/main/java/com/moing/backend/domain/mission/domain/repository/MissionCustomRepositoryImpl.java +++ b/src/main/java/com/moing/backend/domain/mission/domain/repository/MissionCustomRepositoryImpl.java @@ -16,7 +16,10 @@ import javax.persistence.EntityManager; +import java.time.DayOfWeek; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.temporal.TemporalAdjusters; import java.util.List; import java.util.Optional; @@ -46,6 +49,9 @@ public Long findMissionsCountByTeam(Long teamId) { @Override public Optional> findRepeatMissionByMemberId(Long memberId,Listteams) { + + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); + return Optional.ofNullable(queryFactory .select(Projections.constructor(GatherRepeatMissionRes.class, mission.id, @@ -59,7 +65,8 @@ public Optional> findRepeatMissionByMemberId(Long m .from(mission) .leftJoin(missionState) .on(missionState.mission.eq(mission), - missionState.member.memberId.eq(memberId) + missionState.member.memberId.eq(memberId), + dateInRange ) .where( mission.team.teamId.in(teams), @@ -68,7 +75,8 @@ public Optional> findRepeatMissionByMemberId(Long m ) .groupBy(mission.id,mission.number) - .having(missionState.count().lt(mission.number)) // HAVING 절을 사용하여 조건 적용 + .having(missionState.count().lt(mission.number) + ) // HAVING 절을 사용하여 조건 적용 .orderBy(missionState.count().desc()) .fetch()); } @@ -165,4 +173,36 @@ public Optional findByIds(Long memberId, Long missionId) { } + private BooleanExpression createRepeatTypeConditionByArchive() { + LocalDate now = LocalDate.now(); + DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY; + LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); + LocalDate endOfWeek = startOfWeek.plusDays(6); + + // MissionType.REPEAT 인 경우의 추가적인 날짜 범위 조건 +// BooleanExpression isRepeatType = missionArchive.mission.type.eq(MissionType.REPEAT); + BooleanExpression dateInRange = missionArchive.createdDate.goe(startOfWeek.atStartOfDay()) + .and(missionArchive.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1))); + + // 조건이 MissionType.REPEAT 인 경우에만 날짜 범위 조건 적용 + return dateInRange.and(dateInRange); + } + + private BooleanExpression createRepeatTypeConditionByState() { + LocalDate now = LocalDate.now(); + DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY; + LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); + LocalDate endOfWeek = startOfWeek.plusDays(6); + + // MissionType.REPEAT 인 경우의 추가적인 날짜 범위 조건 +// BooleanExpression isRepeatType = missionArchive.mission.type.eq(MissionType.REPEAT); + BooleanExpression dateInRange = missionState.createdDate.goe(startOfWeek.atStartOfDay()) + .and(missionState.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1))); + + // 조건이 MissionType.REPEAT 인 경우에만 날짜 범위 조건 적용 + return dateInRange.and(dateInRange); + } + + + } diff --git a/src/main/java/com/moing/backend/domain/missionArchive/domain/repository/MissionArchiveCustomRepositoryImpl.java b/src/main/java/com/moing/backend/domain/missionArchive/domain/repository/MissionArchiveCustomRepositoryImpl.java index 1edae340..bd232cf8 100644 --- a/src/main/java/com/moing/backend/domain/missionArchive/domain/repository/MissionArchiveCustomRepositoryImpl.java +++ b/src/main/java/com/moing/backend/domain/missionArchive/domain/repository/MissionArchiveCustomRepositoryImpl.java @@ -52,7 +52,7 @@ public MissionArchiveCustomRepositoryImpl(EntityManager em) { @Override public Optional> findSingleMissionInComplete(Long memberId, Long teamId, MissionStatus status, - OrderCondition orderCondition) { + OrderCondition orderCondition) { OrderSpecifier[] orderSpecifiers = createOrderSpecifier(orderCondition); return Optional.ofNullable(queryFactory .select(Projections.constructor(SingleMissionBoardRes.class, @@ -83,7 +83,7 @@ public Optional> findSingleMissionInComplete(Long me } @Override public Optional> findSingleMissionComplete(Long memberId, Long teamId, MissionStatus status, - OrderCondition orderCondition) { + OrderCondition orderCondition) { OrderSpecifier[] orderSpecifiers = createOrderSpecifier(orderCondition); return Optional.ofNullable(queryFactory .select(Projections.constructor(SingleMissionBoardRes.class, @@ -118,23 +118,21 @@ private OrderSpecifier[] createOrderSpecifier(OrderCondition orderCondition) { return orderSpecifiers.toArray(new OrderSpecifier[orderSpecifiers.size()]); } - @Override + @Override public Optional> findOneMyArchives(Long memberId,Long missionId,Long count) { - - BooleanExpression repeatTypeCondition = createRepeatTypeConditionByArchive(); - BooleanExpression baseCondition = missionArchive.mission.id.eq(missionId); - BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition); + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); return Optional.ofNullable(queryFactory .select(missionArchive) - .from(missionArchive) - .where( - finalCondition, + .from(missionArchive) + .where( + missionArchive.mission.id.eq(missionId), missionArchive.member.memberId.eq(memberId), - missionArchive.count.eq(count) - + missionArchive.count.eq(count), + missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange) + .or(missionArchive.mission.type.eq(MissionType.ONCE)) ) .orderBy(missionArchive.createdDate.desc()) .fetch() @@ -145,17 +143,17 @@ public Optional> findOneMyArchives(Long memberId,Long missi @Override public Optional> findMyArchives(Long memberId,Long missionId) { - - BooleanExpression repeatTypeCondition = createRepeatTypeConditionByArchive(); - BooleanExpression baseCondition = missionArchive.mission.id.eq(missionId); - BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition); + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); return Optional.ofNullable(queryFactory .select(missionArchive) - .from(missionArchive) - .where( - finalCondition, - missionArchive.member.memberId.eq(memberId) + .from(missionArchive) + .where( + missionArchive.mission.id.eq(missionId), + missionArchive.member.memberId.eq(memberId), + missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange) + .or(missionArchive.mission.type.eq(MissionType.ONCE)) + ) .orderBy(missionArchive.createdDate.desc()) .fetch() @@ -167,18 +165,18 @@ public Optional> findMyArchives(Long memberId,Long missionI @Override public Optional> findOthersArchives(Long memberId, Long missionId) { - - BooleanExpression repeatTypeCondition = createRepeatTypeConditionByArchive(); - BooleanExpression baseCondition = missionArchive.mission.id.eq(missionId); - BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition); + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); return Optional.ofNullable(queryFactory .select(missionArchive) .from(missionArchive) .where( - finalCondition, + missionArchive.mission.id.eq(missionId), missionArchive.member.memberId.ne(memberId), - missionArchive.status.eq(MissionArchiveStatus.COMPLETE).or(missionArchive.status.eq(MissionArchiveStatus.SKIP)) + missionArchive.status.eq(MissionArchiveStatus.COMPLETE).or(missionArchive.status.eq(MissionArchiveStatus.SKIP)), + + missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange) + .or(missionArchive.mission.type.eq(MissionType.ONCE)) ) .orderBy(missionArchive.createdDate.desc()) .fetch() @@ -189,18 +187,18 @@ public Optional> findOthersArchives(Long memberId, Long mis @Override public Optional findDonePeopleByMissionId(Long missionId) { - BooleanExpression repeatTypeCondition = createRepeatTypeCondition(); - BooleanExpression baseCondition = missionState.mission.id.eq(missionId); - BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition); + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); return Optional.of(queryFactory - .select(missionState) - .from(missionState) + .select(missionArchive) + .from(missionArchive) .where( - finalCondition + missionArchive.mission.id.eq(missionId), + + missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange) + .or(missionArchive.mission.type.eq(MissionType.ONCE)) ) - .groupBy(missionState.member,missionState.mission.number) - .having(missionState.count().goe(missionState.mission.number)) + .groupBy(missionArchive.member) .fetchCount() ); @@ -209,17 +207,15 @@ public Optional findDonePeopleByMissionId(Long missionId) { @Override public Optional findMyDoneCountByMissionId(Long missionId, Long memberId) { - - BooleanExpression repeatTypeCondition = createRepeatTypeCondition(); - BooleanExpression baseCondition = missionState.mission.id.eq(missionId); - BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition); - + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); return Optional.ofNullable(queryFactory - .select(missionState.count()) - .from(missionState) + .select(missionArchive.count()) + .from(missionArchive) .where( - finalCondition, - missionState.member.memberId.eq(memberId) + missionArchive.mission.id.eq(missionId), + missionArchive.member.memberId.eq(memberId), + missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange) + .or(missionArchive.mission.type.eq(MissionType.ONCE)) ) .fetchFirst() ); @@ -229,34 +225,30 @@ public Optional findMyDoneCountByMissionId(Long missionId, Long memberId) @Override public Optional> findRepeatMissionArchivesByMemberId(Long memberId, Long teamId, MissionStatus status) { - - BooleanExpression repeatTypeCondition = createRepeatTypeCondition(); - - + BooleanExpression dateInRange = createRepeatTypeConditionByState(); return Optional.ofNullable(queryFactory .select(Projections.constructor(RepeatMissionBoardRes.class, - mission.id, - mission.title, + mission.id, + mission.title, // missionState.count().coalesce(0L).as("done"), - missionState.count(), - mission.number, - mission.way.stringValue(), - mission.status.stringValue() - )) + missionState.count(), + mission.number, + mission.way.stringValue(), + mission.status.stringValue() + )) .from(mission) .leftJoin(missionState) .on(missionState.mission.eq(mission), - missionState.member.memberId.eq(memberId) + missionState.member.memberId.eq(memberId), + dateInRange ) .where( mission.team.teamId.eq(teamId), mission.type.eq(MissionType.REPEAT), - mission.status.eq(MissionStatus.ONGOING).or(mission.status.eq(MissionStatus.WAIT)), - repeatTypeCondition - + mission.status.eq(MissionStatus.ONGOING).or(mission.status.eq(MissionStatus.WAIT)) ) .groupBy(mission.id,mission.number) - .having(missionState.count().lt(mission.number)) // HAVING 절을 사용하여 조건 적용 +// .having(missionState.count().lt(mission.number)) // HAVING 절을 사용하여 조건 적용 .orderBy(missionState.count().desc()) .fetch()); @@ -270,22 +262,19 @@ public Optional> findFinishMissionsByStatus(Long mem Expression type = Expressions.stringTemplate(String.valueOf(mission.type)); - BooleanExpression repeatTypeCondition = createRepeatTypeCondition(); - return Optional.ofNullable(queryFactory .selectDistinct(Projections.constructor(FinishMissionBoardRes.class, - mission.id, - mission.dueTo.stringValue(), - mission.title, - missionState.status.stringValue().coalesce("FAIL").as("status"), - mission.type.stringValue(), + mission.id, + mission.dueTo.stringValue(), + mission.title, + missionArchive.status.stringValue().coalesce("FAIL").as("status"), + mission.type.stringValue(), mission.way.stringValue() )) .from(mission) .leftJoin(mission.missionArchiveList,missionArchive) - .on(missionState.member.memberId.eq(memberId)) + .on(missionArchive.member.memberId.eq(memberId)) .where( - repeatTypeCondition, mission.team.teamId.eq(teamId), mission.status.eq(MissionStatus.SUCCESS).or(mission.status.eq(MissionStatus.END)) ) @@ -335,12 +324,17 @@ public Boolean findMyArchivesToday(Long memberId, Long missionId) { LocalDateTime startOfToday = today.withHour(0).withMinute(0).withSecond(0).withNano(0); LocalDateTime endOfToday = today.withHour(23).withMinute(59).withSecond(59).withNano(999999999); + BooleanExpression dateInRange = createRepeatTypeConditionByArchive(); + long count = queryFactory - .selectFrom(missionState) + .selectFrom(missionArchive) .where( - missionState.member.memberId.eq(memberId), - missionState.mission.id.eq(missionId), - missionState.lastModifiedDate.between(startOfToday, endOfToday) // createdDate와 오늘의 시작과 끝을 비교 + missionArchive.member.memberId.eq(memberId), + missionArchive.mission.id.eq(missionId), + missionArchive.lastModifiedDate.between(startOfToday, endOfToday), // createdDate와 오늘의 시작과 끝을 비교, + + missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange) + .or(missionArchive.mission.type.eq(MissionType.ONCE)) ) .fetchCount(); @@ -355,8 +349,8 @@ public Optional> findPeopleRemainMission() { .join(teamMember.team, team) .join(team.missions, mission) .join(mission.missionStateList,missionState) - .where(missionState.isNull()) - .fetch()); + .where(missionState.isNull()) + .fetch()); // .where( // // active 한 미션 필터링 // (((mission.status.eq(MissionStatus.ONGOING).or(mission.status.eq(MissionStatus.WAIT))).and(mission.type.eq(MissionType.ONCE))) @@ -368,24 +362,22 @@ public Optional> findPeopleRemainMission() { // ).fetch()); } - // 날짜 조건 생성 메서드 - private BooleanExpression createRepeatTypeCondition() { + private BooleanExpression createRepeatTypeConditionByArchive() { LocalDate now = LocalDate.now(); DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY; LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); LocalDate endOfWeek = startOfWeek.plusDays(6); // MissionType.REPEAT 인 경우의 추가적인 날짜 범위 조건 -// BooleanExpression isRepeatType = missionState.mission.type.eq(MissionType.REPEAT); - BooleanExpression dateInRange = missionState.createdDate.goe(startOfWeek.atStartOfDay()) - .and(missionState.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1))); +// BooleanExpression isRepeatType = missionArchive.mission.type.eq(MissionType.REPEAT); + BooleanExpression dateInRange = missionArchive.createdDate.goe(startOfWeek.atStartOfDay()) + .and(missionArchive.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1))); // 조건이 MissionType.REPEAT 인 경우에만 날짜 범위 조건 적용 return dateInRange.and(dateInRange); } - // 날짜 조건 생성 메서드 - private BooleanExpression createRepeatTypeConditionByArchive() { + private BooleanExpression createRepeatTypeConditionByState() { LocalDate now = LocalDate.now(); DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY; LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek)); @@ -393,6 +385,9 @@ private BooleanExpression createRepeatTypeConditionByArchive() { // MissionType.REPEAT 인 경우의 추가적인 날짜 범위 조건 // BooleanExpression isRepeatType = missionArchive.mission.type.eq(MissionType.REPEAT); +// BooleanExpression dateInRange = missionState.createdDate.goe(startOfWeek.atStartOfDay()) +// .and(missionState.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1))); + BooleanExpression dateInRange = missionArchive.createdDate.goe(startOfWeek.atStartOfDay()) .and(missionArchive.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1))); @@ -402,4 +397,5 @@ private BooleanExpression createRepeatTypeConditionByArchive() { -} + +} \ No newline at end of file