diff --git a/src/main/java/com/jangburich/domain/team/application/TeamService.java b/src/main/java/com/jangburich/domain/team/application/TeamService.java index f34da05..dbb9fd9 100644 --- a/src/main/java/com/jangburich/domain/team/application/TeamService.java +++ b/src/main/java/com/jangburich/domain/team/application/TeamService.java @@ -207,8 +207,10 @@ public IndividualStoreDetailsResponse getIndividualStoreDetails(String userId, L Store store = storeRepository.findById(storeId) .orElseThrow(() -> new IllegalArgumentException("해당하는 가게를 찾을 수 없습니다.")); + boolean isMeLeader = team.getTeamLeader().getUser_id().equals(user.getUserId()); + IndividualStoreDetailsResponse individualStoreDetails = teamRepository.findIndividualStoreDetails( - user.getUserId(), team.getId(), store.getId()); + user.getUserId(), team.getId(), store.getId(), isMeLeader); return individualStoreDetails; } diff --git a/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepository.java b/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepository.java index 76ac9d5..9652866 100644 --- a/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepository.java +++ b/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepository.java @@ -8,5 +8,5 @@ public interface TeamQueryDslRepository { MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId); - IndividualStoreDetailsResponse findIndividualStoreDetails(Long userId, Long teamId, Long storeId); + IndividualStoreDetailsResponse findIndividualStoreDetails(Long userId, Long teamId, Long storeId, boolean isMeLeader); } diff --git a/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java b/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java index 1f820c6..401710c 100644 --- a/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java +++ b/src/main/java/com/jangburich/domain/team/domain/repository/TeamQueryDslRepositoryImpl.java @@ -12,6 +12,7 @@ import com.jangburich.domain.common.Status; import com.jangburich.domain.point.domain.TransactionType; import com.jangburich.domain.store.domain.QStoreTeam; +import com.jangburich.domain.team.domain.Team; import com.jangburich.domain.team.dto.response.IndividualStoreDetailsResponse; import com.jangburich.domain.team.dto.response.MyPaymentHistory; import com.jangburich.domain.team.dto.response.MyTeamDetailsResponse; @@ -26,17 +27,21 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.util.List; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Repository; +@Slf4j @RequiredArgsConstructor @Repository public class TeamQueryDslRepositoryImpl implements TeamQueryDslRepository { private final JPAQueryFactory queryFactory; + LocalDate currentDate = LocalDate.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); String formattedDate = currentDate.format(formatter); @@ -68,7 +73,7 @@ public MyTeamDetailsResponse findMyTeamDetailsAsMember(Long userId, Long teamId) List todayPayments = queryFactory .selectDistinct(new QTodayPayment( Expressions.constant(formattedDate), - pointTransaction.createdAt.stringValue(), + Expressions.stringTemplate("DATE_FORMAT({0}, '%H:%i')", pointTransaction.createdAt), menu.name, cart.user.name, menu.price @@ -104,13 +109,6 @@ public MyTeamDetailsResponse findMyTeamDetailsAsMember(Long userId, Long teamId) .leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), pointTransaction.user.userId.eq(userId)) .where(storeTeam.team.id.eq(teamId)) - .groupBy( - storeTeam.team.id, - storeTeam.team.name, - storeTeam.team.description, - storeTeam.remainPoint, - storeTeam.personalAllocatedPoint - ) .fetchOne(); } @@ -118,6 +116,9 @@ public MyTeamDetailsResponse findMyTeamDetailsAsMember(Long userId, Long teamId) @Override public MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId) { + LocalDateTime startOfToday = LocalDate.now().atStartOfDay(); + LocalDateTime endOfToday = LocalDate.now().atTime(LocalTime.NOON); + List prepayedStores = queryFactory .select(new QPrepayedStore( store.id, @@ -139,7 +140,7 @@ public MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId) List todayPayments = queryFactory .selectDistinct(new QTodayPayment( Expressions.constant(formattedDate), - pointTransaction.createdAt.stringValue(), + Expressions.stringTemplate("DATE_FORMAT({0}, '%H:%i')", pointTransaction.createdAt), menu.name, cart.user.name, menu.price @@ -148,7 +149,7 @@ public MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId) .leftJoin(store).on(store.id.eq(pointTransaction.store.id)) .leftJoin(cart).on(cart.store.id.eq(store.id)) .leftJoin(menu).on(menu.id.eq(cart.menu.id), cart.status.eq(Status.INACTIVE)) - .where(pointTransaction.createdAt.between(startOfDay, endOfDay), pointTransaction.transactionType.eq( + .where(pointTransaction.createdAt.between(startOfToday, endOfToday), pointTransaction.transactionType.eq( TransactionType.FOOD_PURCHASE)) .fetch(); @@ -175,22 +176,82 @@ public MyTeamDetailsResponse findMyTeamDetailsAsLeader(Long userId, Long teamId) .leftJoin(pointTransaction).on(pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), pointTransaction.user.userId.eq(userId)) .where(storeTeam.team.id.eq(teamId)) - .groupBy( - storeTeam.team.id, - storeTeam.team.name, - storeTeam.team.description, - storeTeam.point, - storeTeam.remainPoint - ) .fetchOne(); } @Override - public IndividualStoreDetailsResponse findIndividualStoreDetails(Long userId, Long teamId, Long storeId) { + public IndividualStoreDetailsResponse findIndividualStoreDetails(Long userId, Long teamId, Long storeId, boolean isMeLeader) { LocalDateTime now = LocalDateTime.now(); LocalDateTime oneMonthAgo = now.minusMonths(1); + + if (!isMeLeader) { + + List myPaymentHistories = queryFactory + .select(new QMyPaymentHistory( + Expressions.stringTemplate( + "DATE_FORMAT({0}, '%m.%d')", pointTransaction.createdAt + ), + Expressions.stringTemplate( + "DATE_FORMAT({0}, '%H:%i')", pointTransaction.createdAt + ), + menu.name, + menu.price + )) + .from(pointTransaction) + .leftJoin(menu).on(pointTransaction.menuId.eq(menu.id)) + .where(pointTransaction.store.id.eq(storeId), + pointTransaction.team.id.eq(teamId), + pointTransaction.user.userId.eq(userId), + pointTransaction.createdAt.between(oneMonthAgo, now)) + .fetch(); + + log.info("myPaymentHistories: {}", myPaymentHistories); + + Integer totalPrice = queryFactory + .select(pointTransaction.transactionedPoint.sum()) + .from(pointTransaction) + .where(pointTransaction.store.id.eq(storeId), + pointTransaction.team.id.eq(teamId), + pointTransaction.user.userId.eq(userId), + pointTransaction.createdAt.between(oneMonthAgo, now)) + .fetchOne(); + + System.out.println("totalPrice = " + totalPrice); + + return queryFactory + .selectDistinct(new QIndividualStoreDetailsResponse( + store.id, + Expressions.constant(false), + store.name, + Expressions.constant(false), + storeTeam.remainPoint, + storeTeam.personalAllocatedPoint, + Expressions.constant(totalPrice), + Expressions.nullExpression(), + Expressions.nullExpression(), + Expressions.nullExpression(), + Expressions.stringTemplate( + "DATE_FORMAT({0}, '%y.%m.%d')", oneMonthAgo + ), + Expressions.stringTemplate( + "DATE_FORMAT({0}, '%y.%m.%d')", now + ), + Expressions.constant(myPaymentHistories) + )) + .from(storeTeam) + .leftJoin(store).on(storeTeam.store.id.eq(store.id)) + .leftJoin(team).on(team.id.eq(storeTeam.team.id)) + .leftJoin(pointTransaction).on(pointTransaction.store.id.eq(storeTeam.store.id), + pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), + pointTransaction.user.userId.eq(userId)) + .where(storeTeam.store.id.eq(storeId), + storeTeam.team.id.eq(teamId)) + .fetchOne(); + } + + // 리더일 때 List myPaymentHistories = queryFactory .select(new QMyPaymentHistory( Expressions.stringTemplate( @@ -210,23 +271,20 @@ public IndividualStoreDetailsResponse findIndividualStoreDetails(Long userId, Lo pointTransaction.createdAt.between(oneMonthAgo, now)) .fetch(); - Integer totalPrice = queryFactory - .select(pointTransaction.transactionedPoint.sum()) - .from(pointTransaction) - .where(pointTransaction.store.id.eq(storeId), - pointTransaction.team.id.eq(teamId), - pointTransaction.user.userId.eq(userId), - pointTransaction.createdAt.between(oneMonthAgo, now)) - .fetchOne(); + log.info("myPaymentHistories: {}", myPaymentHistories); return queryFactory .selectDistinct(new QIndividualStoreDetailsResponse( store.id, + Expressions.constant(true), store.name, Expressions.constant(false), - storeTeam.remainPoint, + Expressions.nullExpression(), + Expressions.nullExpression(), + Expressions.nullExpression(), + pointTransaction.transactionedPoint.sum(), + team.point, storeTeam.personalAllocatedPoint, - Expressions.constant(totalPrice), Expressions.stringTemplate( "DATE_FORMAT({0}, '%y.%m.%d')", oneMonthAgo ), @@ -239,7 +297,7 @@ public IndividualStoreDetailsResponse findIndividualStoreDetails(Long userId, Lo .leftJoin(store).on(storeTeam.store.id.eq(store.id)) .leftJoin(team).on(team.id.eq(storeTeam.team.id)) .leftJoin(pointTransaction).on(pointTransaction.store.id.eq(storeTeam.store.id), - pointTransaction.transactionType.eq(TransactionType.FOOD_PURCHASE), + pointTransaction.transactionType.eq(TransactionType.POINT_PURCHASE), pointTransaction.user.userId.eq(userId)) .where(storeTeam.store.id.eq(storeId), storeTeam.team.id.eq(teamId)) diff --git a/src/main/java/com/jangburich/domain/team/dto/response/IndividualStoreDetailsResponse.java b/src/main/java/com/jangburich/domain/team/dto/response/IndividualStoreDetailsResponse.java index 4914a2c..55614db 100644 --- a/src/main/java/com/jangburich/domain/team/dto/response/IndividualStoreDetailsResponse.java +++ b/src/main/java/com/jangburich/domain/team/dto/response/IndividualStoreDetailsResponse.java @@ -5,27 +5,37 @@ public record IndividualStoreDetailsResponse( Long storeId, + Boolean isMeLeader, String storeName, boolean isLiked, int remainingAmount, int availableAmount, int myUsedAmount, + int totalPrepayedAmount, + int remainingPrepayedAmount, + int personalUsableAmount, String usageStartDate, String usageEndDate, List myPaymentHistories ) { @QueryProjection - public IndividualStoreDetailsResponse(Long storeId, String storeName, boolean isLiked, int remainingAmount, - int availableAmount, int myUsedAmount, String usageStartDate, - String usageEndDate, + public IndividualStoreDetailsResponse(Long storeId, Boolean isMeLeader, String storeName, boolean isLiked, + int remainingAmount, int availableAmount, int myUsedAmount, + int totalPrepayedAmount, int remainingPrepayedAmount, + int personalUsableAmount, + String usageStartDate, String usageEndDate, List myPaymentHistories) { this.storeId = storeId; + this.isMeLeader = isMeLeader; this.storeName = storeName; this.isLiked = isLiked; this.remainingAmount = remainingAmount; this.availableAmount = availableAmount; this.myUsedAmount = myUsedAmount; + this.totalPrepayedAmount = totalPrepayedAmount; + this.remainingPrepayedAmount = remainingPrepayedAmount; + this.personalUsableAmount = personalUsableAmount; this.usageStartDate = usageStartDate; this.usageEndDate = usageEndDate; this.myPaymentHistories = myPaymentHistories;