Skip to content

Commit

Permalink
Merge pull request #276 from Modagbul/MNG-22
Browse files Browse the repository at this point in the history
MNG-22 : 진행 중 미션에서 인증여부까지 확인
  • Loading branch information
minsu20 authored May 6, 2024
2 parents 3dc8a2c + 208f739 commit 407791d
Show file tree
Hide file tree
Showing 31 changed files with 231 additions and 589 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import lombok.Builder;
import lombok.Getter;

import java.util.List;

@Getter
@Builder
@AllArgsConstructor
Expand All @@ -16,6 +18,8 @@ public class GatherRepeatMissionRes {
private String totalNum;
private String doneNum;
private String status;
private String donePeople;
private String totalPeople;


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

@Getter
@Builder
@ToString
@AllArgsConstructor
public class GatherSingleMissionRes {
private Long missionId;
Expand All @@ -14,6 +16,16 @@ public class GatherSingleMissionRes {
private String missionTitle;
private String dueTo;
private String status;
private Long done;
private Long total;


public GatherSingleMissionRes(Long missionId, Long teamId, String teamName, String missionTitle, String dueTo, String status, Long total) {
this.missionId = missionId;
this.teamId = teamId;
this.teamName = teamName;
this.missionTitle = missionTitle;
this.dueTo = dueTo;
this.status = status;
this.total = total;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public MissionCreateRes createMission(String socialId, Long teamId, MissionReq m
throw new NoAccessCreateMission();
// 반복미션은 최대 2개 생성 가능
} else if (missionQueryService.isAbleCreateRepeatMission(team.getTeamId())) {
throw new NoAccessCreateMission();
throw new NoMoreCreateMission();
}
// 반복미션 유예 해제
mission.updateStatus(MissionStatus.ONGOING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.moing.backend.domain.mission.domain.entity.constant.MissionWay;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchive;
import com.moing.backend.domain.missionRead.domain.entity.MissionRead;
import com.moing.backend.domain.missionState.domain.entity.MissionState;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.global.entity.BaseTimeEntity;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -56,9 +55,6 @@ public class Mission extends BaseTimeEntity {

@OneToMany(mappedBy = "mission")
List<MissionArchive> missionArchiveList = new ArrayList<>();

@OneToMany(mappedBy = "mission")
List<MissionState> missionStateList = new ArrayList<>();

@OneToMany(mappedBy = "mission")
List<MissionRead> missionReads=new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@
import com.moing.backend.domain.mission.domain.entity.constant.MissionType;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.JPQLQuery;
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.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.List;
import java.util.Optional;
import java.util.*;

import static com.moing.backend.domain.mission.domain.entity.QMission.mission;
import static com.moing.backend.domain.missionArchive.domain.entity.QMissionArchive.missionArchive;
import static com.moing.backend.domain.missionState.domain.entity.QMissionState.missionState;
import static com.moing.backend.domain.teamMember.domain.entity.QTeamMember.teamMember;
import static com.querydsl.jpa.JPAExpressions.select;

public class MissionCustomRepositoryImpl implements MissionCustomRepository{

Expand All @@ -48,7 +51,7 @@ public Long findMissionsCountByTeam(Long teamId) {
@Override
public Optional<List<GatherRepeatMissionRes>> findRepeatMissionByMemberId(Long memberId,List<Long>teams) {

BooleanExpression dateInRange = createRepeatTypeConditionByState();
BooleanExpression dateInRange = createRepeatTypeConditionByArchive();

return Optional.ofNullable(queryFactory
.select(Projections.constructor(GatherRepeatMissionRes.class,
Expand All @@ -57,13 +60,26 @@ public Optional<List<GatherRepeatMissionRes>> findRepeatMissionByMemberId(Long m
mission.team.name,
mission.title,
mission.number.stringValue(),
missionState.count().stringValue(),
mission.status.stringValue()
missionArchive.count.max().coalesce(0L).stringValue(),
missionArchive.status.coalesce(mission.status).stringValue(),

JPAExpressions
.select(teamMember.member.countDistinct().stringValue())
.from(teamMember)
.where(
teamMember.team.eq(mission.team),
teamMember.member.memberId.in(RepeatMissionDonePeopleByWeek(mission.id))
.or(teamMember.member.memberId.in(RepeatMissionDonePeopleByDay(mission.id))),
teamMember.isDeleted.ne(Boolean.TRUE)
),

mission.team.numOfMember.stringValue()

))
.from(mission)
.leftJoin(missionState)
.on(missionState.mission.eq(mission),
missionState.member.memberId.eq(memberId),
.leftJoin(missionArchive)
.on(missionArchive.mission.eq(mission),
missionArchive.member.memberId.eq(memberId),
dateInRange
)
.where(
Expand All @@ -72,13 +88,42 @@ public Optional<List<GatherRepeatMissionRes>> findRepeatMissionByMemberId(Long m
mission.type.eq(MissionType.REPEAT)

)
.groupBy(mission.id,mission.number)
.having(missionState.count().lt(mission.number)
) // HAVING 절을 사용하여 조건 적용
.orderBy(missionState.count().desc())
.groupBy(mission)
.orderBy(mission.createdDate.desc())
.fetch());
}

private JPQLQuery<Long> RepeatMissionDonePeopleByDay(NumberPath<Long> missionId) {

BooleanExpression dateInRange = createRepeatTypeConditionByArchive();
BooleanExpression hasAlreadyVerifiedToday = hasAlreadyVerifiedToday();

return
select(missionArchive.member.memberId)
.from(missionArchive)
.where(
missionArchive.mission.id.eq(missionId),
(missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange).and(hasAlreadyVerifiedToday))
).distinct();
}

private JPQLQuery<Long> RepeatMissionDonePeopleByWeek(NumberPath<Long> missionId) {

BooleanExpression dateInRange = createRepeatTypeConditionByArchive();

return select(missionArchive.member.memberId)
.from(missionArchive)
.where(
missionArchive.mission.id.eq(missionId),
(missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange))
)
.groupBy(missionArchive.mission.number, missionArchive.count)
.having(
missionArchive.count.max().goe(missionArchive.mission.number))
.distinct();

}


@Override
public Optional<List<Mission>> findMissionByDueTo() {
Expand Down Expand Up @@ -135,6 +180,7 @@ public Optional<List<Mission>> findRepeatMissionByStatus(MissionStatus missionSt
@Override
public Optional<List<GatherSingleMissionRes>> findSingleMissionByMemberId(Long memberId, List<Long> teams) {

BooleanExpression dateInRange = createRepeatTypeConditionByArchive();

return Optional.ofNullable(queryFactory
.select(Projections.constructor(GatherSingleMissionRes.class,
Expand All @@ -143,18 +189,29 @@ public Optional<List<GatherSingleMissionRes>> findSingleMissionByMemberId(Long m
mission.team.name,
mission.title,
mission.dueTo.stringValue(),
mission.status.stringValue()
missionArchive.status.coalesce(mission.status).stringValue(),

JPAExpressions
.select(missionArchive.member.count().longValue())
.from(missionArchive)
.where(
missionArchive.mission.id.eq(mission.id),
missionArchive.mission.type.eq(MissionType.REPEAT).and(dateInRange)
.or(missionArchive.mission.type.eq(MissionType.ONCE))
),

mission.team.numOfMember.longValue()
))
.from(mission)
.leftJoin(missionState).on(
mission.eq(missionState.mission),
missionState.member.memberId.eq(memberId)
)
.leftJoin(missionArchive)
.on(
mission.eq(missionArchive.mission),
missionArchive.member.memberId.eq(memberId)
)
.where(
mission.team.teamId.in(teams),
mission.status.eq(MissionStatus.ONGOING).or(mission.status.eq(MissionStatus.WAIT)),
mission.type.eq(MissionType.ONCE),
missionState.id.isNull()
mission.type.eq(MissionType.ONCE)
)
.orderBy(mission.dueTo.asc())
.fetch());
Expand All @@ -171,6 +228,8 @@ public boolean findRepeatMissionsByTeamId(Long teamId) {
).fetchCount() > 2;
}



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

Expand Down Expand Up @@ -270,34 +329,25 @@ public Long getYesterdayRepeatMissions() {
}




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())
return 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)));
private BooleanExpression hasAlreadyVerifiedToday() {
LocalDateTime today = LocalDateTime.now();
LocalDateTime startOfToday = today.withHour(0).withMinute(0).withSecond(0).withNano(0);
LocalDateTime endOfToday = today.withHour(23).withMinute(59).withSecond(59).withNano(999999999);

// 조건이 MissionType.REPEAT 인 경우에만 날짜 범위 조건 적용
return dateInRange.and(dateInRange);
return missionArchive.createdDate.between(startOfToday, endOfToday);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
import com.moing.backend.domain.missionArchive.application.dto.res.MissionArchiveRes;
import com.moing.backend.domain.missionArchive.application.mapper.MissionArchiveMapper;
import com.moing.backend.domain.missionArchive.domain.entity.MissionArchive;
import com.moing.backend.domain.missionArchive.domain.service.MissionArchiveDeleteService;
import com.moing.backend.domain.missionArchive.domain.service.MissionArchiveQueryService;
import com.moing.backend.domain.missionArchive.domain.service.MissionArchiveSaveService;
import com.moing.backend.domain.missionArchive.exception.NoMoreMissionArchiveException;
import com.moing.backend.domain.missionState.application.service.MissionStateUseCase;
import com.moing.backend.domain.missionState.domain.service.MissionStateSaveService;
import com.moing.backend.domain.missionHeart.domain.service.MissionHeartQueryService;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.teamScore.application.service.TeamScoreUpdateUseCase;
import com.moing.backend.domain.teamScore.domain.entity.ScoreStatus;
Expand All @@ -36,8 +32,6 @@ public class MissionArchiveCreateUseCase {
private final MissionQueryService missionQueryService;
private final MemberGetService memberGetService;

private final MissionStateUseCase missionStateUseCase;

private final TeamScoreUpdateUseCase teamScoreUpdateUseCase;

public MissionArchiveRes createArchive(String userSocialId, Long missionId, MissionArchiveReq missionReq) {
Expand All @@ -64,8 +58,6 @@ public MissionArchiveRes createArchive(String userSocialId, Long missionId, Miss
}

newArchive.updateCount(missionArchiveQueryService.findMyDoneArchives(memberId, missionId) + 1);
missionStateUseCase.updateMissionState(member, mission, newArchive);

missionArchiveRes = MissionArchiveMapper.mapToMissionArchiveRes(missionArchiveSaveService.save(newArchive), memberId);

}
Expand All @@ -79,8 +71,6 @@ public MissionArchiveRes createArchive(String userSocialId, Long missionId, Miss
}

newArchive.updateCount(missionArchiveQueryService.findMyDoneArchives(memberId, missionId)+1);
missionStateUseCase.updateMissionState(member, mission, newArchive);

missionArchiveRes = MissionArchiveMapper.mapToMissionArchiveRes(missionArchiveSaveService.save(newArchive), memberId);

// 인증 후 n/n명 인증 성공 리턴값 업데이트
Expand Down Expand Up @@ -137,4 +127,4 @@ private boolean isAbleToFinishOnceMission(Mission mission) {
}


}
}
Loading

0 comments on commit 407791d

Please sign in to comment.