Skip to content

Commit

Permalink
Merge pull request #128 from Modagbul/feat/mission-alarm
Browse files Browse the repository at this point in the history
Feat/mission alarm
  • Loading branch information
seungueonn authored Dec 10, 2023
2 parents aa1606d + 3c58614 commit 7d03eed
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public Optional<List<FireReceiveRes>> getFireReceivers(Long teamId, Long mission
.where(
teamMember.team.teamId.eq(teamId),
teamMember.member.memberId.ne(memberId),
teamMember.member.memberId.notIn(missionDonePeople)
teamMember.member.memberId.notIn(missionDonePeople),
teamMember.member.isDeleted.ne(Boolean.TRUE)
)
.fetch());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -24,7 +25,10 @@

import javax.persistence.EntityManager;
import javax.swing.text.html.Option;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -117,13 +121,20 @@ private OrderSpecifier[] createOrderSpecifier(OrderCondition orderCondition) {
@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);

return Optional.ofNullable(queryFactory
.select(missionArchive)
.from(missionArchive)
.where(
missionArchive.mission.id.eq(missionId),
finalCondition,
missionArchive.member.memberId.eq(memberId),
missionArchive.count.eq(count)


)
.orderBy(missionArchive.createdDate.desc())
.fetch()
Expand All @@ -134,11 +145,16 @@ 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);

return Optional.ofNullable(queryFactory
.select(missionArchive)
.from(missionArchive)
.where(
missionArchive.mission.id.eq(missionId),
finalCondition,
missionArchive.member.memberId.eq(memberId)
)
.orderBy(missionArchive.createdDate.desc())
Expand All @@ -151,11 +167,16 @@ 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);

return Optional.ofNullable(queryFactory
.select(missionArchive)
.from(missionArchive)
.where(
missionArchive.mission.id.eq(missionId),
finalCondition,
missionArchive.member.memberId.ne(memberId),
missionArchive.status.eq(MissionArchiveStatus.COMPLETE).or(missionArchive.status.eq(MissionArchiveStatus.SKIP))
)
Expand All @@ -167,26 +188,38 @@ 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);

return Optional.of(queryFactory
.select(missionArchive)
.from(missionArchive)
.select(missionState)
.from(missionState)
.where(
missionArchive.mission.id.eq(missionId)
finalCondition
)
.groupBy(missionArchive.member)
.groupBy(missionState.member,missionState.mission.number)
.having(missionState.count().goe(missionState.mission.number))
.fetchCount()

);
}

@Override
public Optional<Long> findMyDoneCountByMissionId(Long missionId, Long memberId) {


BooleanExpression repeatTypeCondition = createRepeatTypeCondition();
BooleanExpression baseCondition = missionState.mission.id.eq(missionId);
BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition);

return Optional.ofNullable(queryFactory
.select(missionArchive.count())
.from(missionArchive)
.select(missionState.count())
.from(missionState)
.where(
missionArchive.mission.id.eq(missionId),
missionArchive.member.memberId.eq(memberId)
finalCondition,
missionState.member.memberId.eq(memberId)
)
.fetchFirst()
);
Expand All @@ -195,6 +228,11 @@ public Optional<Long> findMyDoneCountByMissionId(Long missionId, Long memberId)

@Override
public Optional<List<RepeatMissionBoardRes>> findRepeatMissionArchivesByMemberId(Long memberId, Long teamId, MissionStatus status) {


BooleanExpression repeatTypeCondition = createRepeatTypeCondition();


return Optional.ofNullable(queryFactory
.select(Projections.constructor(RepeatMissionBoardRes.class,
mission.id,
Expand All @@ -213,7 +251,9 @@ public Optional<List<RepeatMissionBoardRes>> findRepeatMissionArchivesByMemberId
.where(
mission.team.teamId.eq(teamId),
mission.type.eq(MissionType.REPEAT),
mission.status.eq(MissionStatus.ONGOING).or(mission.status.eq(MissionStatus.WAIT))
mission.status.eq(MissionStatus.ONGOING),
repeatTypeCondition

)
.groupBy(mission.id,mission.number)
.having(missionState.count().lt(mission.number)) // HAVING 절을 사용하여 조건 적용
Expand All @@ -230,19 +270,22 @@ 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,
missionArchive.status.stringValue().coalesce("FAIL").as("status"),
missionState.status.stringValue().coalesce("FAIL").as("status"),
mission.type.stringValue(),
mission.way.stringValue()
))
.from(mission)
.leftJoin(mission.missionArchiveList,missionArchive)
.on(missionArchive.member.memberId.eq(memberId))
.on(missionState.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 @@ -293,11 +336,11 @@ public Boolean findMyArchivesToday(Long memberId, Long missionId) {
LocalDateTime endOfToday = today.withHour(23).withMinute(59).withSecond(59).withNano(999999999);

long count = queryFactory
.selectFrom(missionArchive)
.selectFrom(missionState)
.where(
missionArchive.member.memberId.eq(memberId),
missionArchive.mission.id.eq(missionId),
missionArchive.lastModifiedDate.between(startOfToday, endOfToday) // createdDate와 오늘의 시작과 끝을 비교
missionState.member.memberId.eq(memberId),
missionState.mission.id.eq(missionId),
missionState.lastModifiedDate.between(startOfToday, endOfToday) // createdDate와 오늘의 시작과 끝을 비교
)
.fetchCount();

Expand Down Expand Up @@ -325,6 +368,38 @@ public Optional<List<String>> findPeopleRemainMission() {
// ).fetch());
}

// 날짜 조건 생성 메서드
private BooleanExpression createRepeatTypeCondition() {
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)));

// 조건이 MissionType.REPEAT 인 경우에만 날짜 범위 조건 적용
return isRepeatType.and(dateInRange);
}

// 날짜 조건 생성 메서드
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 isRepeatType.and(dateInRange);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,9 @@ public void sundayRepeatMissionRoutine() {
for (Long id : ongoingRepeatMissions) {
teamScoreLogicUseCase.updateTeamScore(id);
}
// 미션 state 에서 지우기 ( 이것도 대용량 )
missionStateReset(ongoingRepeatMissions);
}

public void missionStateReset(List<Long> missionIds) {
List<MissionState> missionStates = missionStateQueryService.findByMissionId(missionIds);
missionStateDeleteService.deleteMissionState(missionStates);
}


// /**
// * 단일 미션 마감
// * 미션 단위 마감
// * 한시간 마다 실행
// */
// public void singleMissionEndRoutineByMission() {
//
// Mission mission = new Mission();
// LocalDateTime now = LocalDateTime.now();
//
// if (mission.getDueTo().isAfter(now)) {
// mission.updateStatus(MissionStatus.END);
// teamScoreLogicUseCase.updateTeamScore(mission.getId());
// }
// }

/**
* 단일 미션 마감
* 해당 시간 미션 마감
Expand All @@ -94,15 +71,14 @@ public void singleMissionEndRoutine() {

}


/**
* 미션 시작
* 월요일 아침
*/
@Scheduled(cron = "0 0 0 * * MON")
public void RepeatMissionStart() {
List<Mission> startMission = missionQueryService.findMissionByStatus(MissionStatus.WAIT);
startMission.stream().forEach(
startMission.forEach(
mission -> mission.updateStatus(MissionStatus.ONGOING)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,7 @@ public void updateMissionState(Member member, Mission mission, MissionArchive mi
teamScoreLogicUseCase.updateTeamScore(mission.getId());
}


}

public void deleteMissionState(Member member, Mission mission, MissionArchive missionArchive) {

MissionState missionState = missionStateQueryService.findMissionState(member, mission);
missionStateDeleteService.deleteMissionState(missionState);

}

public void missionStateReset(List<Long> missionIds) {
List<MissionState> missionStates = missionStateQueryService.findByMissionId(missionIds);
missionStateDeleteService.deleteMissionState(missionStates);
}

public void deleteAllMissionState(Long missionId) {
missionStateDeleteService.deleteMissionStateByMission(missionId);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.mission.domain.entity.Mission;
import com.moing.backend.domain.mission.domain.entity.constant.MissionStatus;
import com.moing.backend.domain.mission.domain.entity.constant.MissionType;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchiveStatus;
import com.moing.backend.domain.missionState.domain.entity.MissionState;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;

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 All @@ -27,11 +32,26 @@ public MissionStateCustomRepositoryImpl(EntityManager entityManager) {

@Override
public int getCountsByMissionId(Long missionId) {

LocalDate now = LocalDate.now();
DayOfWeek firstDayOfWeek = DayOfWeek.MONDAY; // 한 주의 시작일을 월요일로 설정
LocalDate startOfWeek = now.with(TemporalAdjusters.previousOrSame(firstDayOfWeek));
LocalDate endOfWeek = startOfWeek.plusDays(6); // 한 주의 마지막일을 일요일로 설정

BooleanExpression repeatTypeCondition = missionState.mission.type.eq(MissionType.REPEAT)
.and(missionState.createdDate.goe(startOfWeek.atStartOfDay()))
.and(missionState.createdDate.loe(endOfWeek.atStartOfDay().plusDays(1).minusNanos(1)));

// 기본 조건
BooleanExpression baseCondition = missionState.mission.id.eq(missionId);
// 조건 적용
BooleanExpression finalCondition = baseCondition.and(repeatTypeCondition);


return queryFactory
.select(missionState)
.from(missionState)
.where(
missionState.mission.id.eq(missionId))
.where(finalCondition)
.fetch().size();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public Long updateTeamScore(Long missionId) {

teamScore.updateScore(getScoreByMission(mission));
teamScore.levelUp();
team.updateLevelOfFire();

teamScoreSaveService.save(teamScore);
teamSaveService.saveTeam(team);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public void levelUp() {

for (int i = 5; i > 0; i--) {
if (steps[i-1] <= this.level && this.level <= steps[i]) {
if (20 + ((i-1) * 15) <= score) {
if ((20 + ((i-1) * 15)) <= score) {
this.level+=1;
this.score -= (20 + ((i-1) * 15));
this.team.updateLevelOfFire();
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//import static com.moing.backend.domain.mission.domain.entity.QMission.mission;
//import static com.moing.backend.domain.missionState.domain.entity.QMissionState.missionState;
//@SpringBootTest
//
//class MissionArchiveCustomRepositoryImplTest {
//
// @Autowired
Expand Down

0 comments on commit 7d03eed

Please sign in to comment.