Skip to content

Commit

Permalink
Merge pull request #133 from Modagbul/feat/mission-alarm
Browse files Browse the repository at this point in the history
fix : missionArchive -> missionState 변경 중에 생긴 오류 해결
  • Loading branch information
seungueonn authored Dec 11, 2023
2 parents ad6347b + 527792e commit fa698db
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -46,6 +49,9 @@ public Long findMissionsCountByTeam(Long teamId) {
@Override
public Optional<List<GatherRepeatMissionRes>> findRepeatMissionByMemberId(Long memberId,List<Long>teams) {


BooleanExpression dateInRange = createRepeatTypeConditionByArchive();

return Optional.ofNullable(queryFactory
.select(Projections.constructor(GatherRepeatMissionRes.class,
mission.id,
Expand All @@ -59,7 +65,8 @@ public Optional<List<GatherRepeatMissionRes>> 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),
Expand All @@ -68,7 +75,8 @@ public Optional<List<GatherRepeatMissionRes>> 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());
}
Expand Down Expand Up @@ -165,4 +173,36 @@ public Optional<MissionReadRes> 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);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MissionArchiveCustomRepositoryImpl(EntityManager em) {

@Override
public Optional<List<SingleMissionBoardRes>> findSingleMissionInComplete(Long memberId, Long teamId, MissionStatus status,
OrderCondition orderCondition) {
OrderCondition orderCondition) {
OrderSpecifier[] orderSpecifiers = createOrderSpecifier(orderCondition);
return Optional.ofNullable(queryFactory
.select(Projections.constructor(SingleMissionBoardRes.class,
Expand Down Expand Up @@ -83,7 +83,7 @@ public Optional<List<SingleMissionBoardRes>> findSingleMissionInComplete(Long me
}
@Override
public Optional<List<SingleMissionBoardRes>> findSingleMissionComplete(Long memberId, Long teamId, MissionStatus status,
OrderCondition orderCondition) {
OrderCondition orderCondition) {
OrderSpecifier[] orderSpecifiers = createOrderSpecifier(orderCondition);
return Optional.ofNullable(queryFactory
.select(Projections.constructor(SingleMissionBoardRes.class,
Expand Down Expand Up @@ -118,23 +118,21 @@ private OrderSpecifier[] createOrderSpecifier(OrderCondition orderCondition) {
return orderSpecifiers.toArray(new OrderSpecifier[orderSpecifiers.size()]);
}

@Override
@Override
public Optional<List<MissionArchive>> 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()
Expand All @@ -145,17 +143,17 @@ public Optional<List<MissionArchive>> findOneMyArchives(Long memberId,Long missi
@Override
public Optional<List<MissionArchive>> 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()
Expand All @@ -167,18 +165,18 @@ public Optional<List<MissionArchive>> findMyArchives(Long memberId,Long missionI
@Override
public Optional<List<MissionArchive>> 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()
Expand All @@ -189,18 +187,18 @@ public Optional<List<MissionArchive>> findOthersArchives(Long memberId, Long mis
@Override
public Optional<Long> 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()

);
Expand All @@ -209,17 +207,15 @@ public Optional<Long> findDonePeopleByMissionId(Long missionId) {
@Override
public Optional<Long> 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()
);
Expand All @@ -229,34 +225,30 @@ public Optional<Long> findMyDoneCountByMissionId(Long missionId, Long memberId)
@Override
public Optional<List<RepeatMissionBoardRes>> 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());

Expand All @@ -270,22 +262,19 @@ public Optional<List<FinishMissionBoardRes>> findFinishMissionsByStatus(Long mem
Expression<String> 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))
)
Expand Down Expand Up @@ -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();

Expand All @@ -355,8 +349,8 @@ public Optional<List<String>> 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)))
Expand All @@ -368,31 +362,32 @@ public Optional<List<String>> 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));
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)));

BooleanExpression dateInRange = missionArchive.createdDate.goe(startOfWeek.atStartOfDay())
.and(missionArchive.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1)));

Expand All @@ -402,4 +397,5 @@ private BooleanExpression createRepeatTypeConditionByArchive() {



}

}

0 comments on commit fa698db

Please sign in to comment.