Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.irum.come2us.domain.order.application.service;

import com.irum.come2us.domain.coupon.domain.repository.AppliedCouponRepository;
import com.irum.come2us.domain.order.application.mapper.OrderMapper;
import com.irum.come2us.domain.order.domain.entity.Order;
import com.irum.come2us.domain.order.domain.entity.OrderDetail;
Expand All @@ -10,11 +11,13 @@
import com.irum.come2us.domain.order.infrastructure.repository.dto.OrderDetailRow;
import com.irum.come2us.domain.order.infrastructure.repository.dto.OrderSummaryRow;
import com.irum.come2us.domain.order.presentation.dto.request.OwnerOrderShippedRequest;
import com.irum.come2us.domain.order.presentation.dto.response.OrderDetailResponse;
import com.irum.come2us.domain.order.presentation.dto.response.OwnerOrderListResponse;
import com.irum.come2us.global.presentation.advice.exception.CommonException;
import com.irum.come2us.global.presentation.advice.exception.errorcode.OrderErrorCode;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand All @@ -31,6 +34,7 @@ public class OrderService {
private final OrderRepository orderRepository;
private final OrderRepositoryCustom orderRepositoryCustom;
private final OrderMapper orderMapper;
private final AppliedCouponRepository appliedCouponRepository;

@Transactional(readOnly = true)
public OwnerOrderListResponse getPreparingOrderList(UUID storeId, UUID cursor, Integer size) {
Expand Down Expand Up @@ -224,4 +228,75 @@ else if (preparingCount == totalCount) {
// 모든 정책에 해당하지 않는 경우
return OrderStatus.PREPARING;
}

@Transactional(readOnly = true)
public OrderDetailResponse detailResponse(UUID orderId) {
Order order =
orderRepository
.findByOrderId(orderId)
.orElseThrow(() -> new CommonException(OrderErrorCode.ORDER_NOT_FOUND));

List<OrderDetailResponse.ProductSummary> productList =
order.getOrderDetails().stream()
.map(
od ->
new OrderDetailResponse.ProductSummary(
od.getOrderDetailId(),
od.getProductName(),
od.getPrice(),
od.getQuantity(),
od.getOptionName()))
.toList();
String couponName = getCouponName(order.getPaymentId());
int discountAmount = getDiscountAmount(order.getPaymentId());
// 아직 결제 상태 Field 없음
String trackingNumber =
order.getOrderDetails().stream()
.map(od -> od.getTrackingNumber())
.filter(Objects::nonNull)
.findFirst()
.map(dt -> dt.toString())
.orElse(null);

String arrivedDate =
order.getOrderDetails().stream()
.map(od -> od.getArrivedDate())
.filter(Objects::nonNull)
.findFirst()
.map(dt -> dt.toString())
.orElse(null);

Integer deliveryFee = order.getDeliveryFee() != null ? order.getDeliveryFee() : 0;
Integer totalProductPrice = order.getTotalPrice() != null ? order.getTotalPrice() : 0;
Integer payingAmount = totalProductPrice + deliveryFee - discountAmount;

return new OrderDetailResponse(
productList,
order.getDeliveryAddress().getRecipientName(),
order.getDeliveryAddress().getRecipientContact(),
order.getDeliveryAddress().getAddress().toString(),
order.getOrderStatusAll().name(),
order.getOrderStatusAll().name(),
order.getDeliveryRequest(),
trackingNumber,
arrivedDate,
couponName,
deliveryFee,
discountAmount,
payingAmount,
order.getCreatedAt().toString(),
totalProductPrice);
}

private String getCouponName(UUID paymentId) {
return appliedCouponRepository.findByPaymentIdWithCoupon(paymentId).stream()
.findFirst()
.map(ac -> ac.getCoupon().getName())
.orElse(null);
}

private int getDiscountAmount(UUID paymentId) {
Integer sum = appliedCouponRepository.getTotalDiscountByPaymentId(paymentId);
return sum != null ? sum : 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.irum.come2us.domain.order.application.service;

import com.irum.come2us.domain.member.domain.entity.Member;
import com.irum.come2us.domain.order.domain.entity.Order;
import com.irum.come2us.domain.order.domain.entity.enums.OrderStatus;
import com.irum.come2us.domain.order.domain.repository.OrderRepository;
import com.irum.come2us.domain.order.presentation.dto.response.SalesResponse;
import com.irum.come2us.domain.refund.domain.entity.Refund;
import com.irum.come2us.domain.refund.domain.entity.enums.RefundStatus;
import com.irum.come2us.domain.refund.domain.repository.RefundRepository;
import com.irum.come2us.domain.store.domain.entity.Store;
import com.irum.come2us.domain.store.domain.repository.StoreRepository;
import com.irum.come2us.global.presentation.advice.exception.CommonException;
import com.irum.come2us.global.presentation.advice.exception.errorcode.StoreErrorCode;
import com.irum.come2us.global.util.MemberUtil;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SalesService {
private final StoreRepository storeRepository;
private final OrderRepository orderRepository;
private final RefundRepository refundRepository;
private final MemberUtil memberUtil;

public SalesResponse getSalesList(UUID storeId) {
Member member = memberUtil.getCurrentMember();
Store store =
storeRepository
.findById(storeId)
.orElseThrow(() -> new CommonException(StoreErrorCode.STORE_NOT_FOUND));
Comment thread
Bal1oon marked this conversation as resolved.
List<OrderStatus> orderStatuses =
List.of(
OrderStatus.DELIVERED,
OrderStatus.PARTIALLY_DELIVERED,
OrderStatus.DELIVERED,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1 : OrderStatus.DELIVERED가 중복 되어 있습니다!

OrderStatus.SHIPPED,
OrderStatus.PREPARING);
List<RefundStatus> refundStatuses =
List.of(
RefundStatus.APPROVED,
RefundStatus.COMPLETED,
RefundStatus.PENDING,
RefundStatus.REJECTED,
RefundStatus.REQUESTED);
Comment thread
Bal1oon marked this conversation as resolved.
Outdated
List<Order> orders = orderRepository.findSalesAll(storeId);
List<SalesResponse.OrderSummary> orderList =
orders.stream().map(this::toOrderSummary).toList();
return new SalesResponse(orderList, null, false);
}

private SalesResponse.OrderSummary toOrderSummary(Order order) {
List<SalesResponse.ProductSummary> productList =
order.getOrderDetails().stream()
.map(
detail ->
new SalesResponse.ProductSummary(
detail.getOrderDetailId(),
detail.getProduct().getName(),
detail.getQuantity(),
detail.getPrice(),
detail.getOptionName()))
.toList();
Optional<Refund> latestRefund = refundRepository.findLatestByOrder(order);

String displayStatus = latestRefund
.map(r -> r.getRefundStatus().name())
.orElse(order.getOrderStatusAll().name());


return new SalesResponse.OrderSummary(
order.getOrderId(),
order.getDeliveryAddress().getRecipientName(),
order.getDeliveryAddress().getRecipientContact(),
order.getDeliveryAddress().getAddress().toString(),
order.getCreatedAt(),
order.getTotalPrice(),
0,
order.getTotalPrice() + order.getDeliveryFee(),
order.getDeliveryFee(),
productList,
displayStatus);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.irum.come2us.domain.store.domain.entity.Store;
import com.irum.come2us.global.domain.BaseEntity;
import jakarta.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.*;
import org.hibernate.annotations.SQLDelete;
Expand Down Expand Up @@ -56,6 +58,11 @@ public class Order extends BaseEntity {
@JoinColumn(name = "delivery_address_id")
private DeliveryAddress deliveryAddress;

@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
private List<OrderDetail> orderDetails = new ArrayList<>();

// 한 주문당 여러 상품이 담겨있기 때문에 List 추가

public void updateOrderStatus(OrderStatus os) {
this.orderStatusAll = os;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.irum.come2us.domain.order.domain.repository;

import com.irum.come2us.domain.order.domain.entity.Order;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -9,4 +10,6 @@
@Repository
public interface OrderRepository extends JpaRepository<Order, UUID> {
Optional<Order> findByOrderId(UUID orderId);

List<Order> findSalesAll(UUID storeId);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
package com.irum.come2us.domain.order.presentation.controller;

import com.irum.come2us.domain.order.application.service.OrderService;
import com.irum.come2us.domain.order.application.service.SalesService;
import com.irum.come2us.domain.order.presentation.dto.request.OwnerOrderShippedRequest;
import com.irum.come2us.domain.order.presentation.dto.response.OrderDetailResponse;
import com.irum.come2us.domain.order.presentation.dto.response.OwnerOrderListResponse;
import com.irum.come2us.domain.order.presentation.dto.response.SalesResponse;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("orders/owner")
@RequestMapping("/orders/owner")
@RequiredArgsConstructor
@Slf4j
public class OwnerOrderController {
private final OrderService orderService;
private final SalesService salesService;

/** [상점] 배송 준비 중 목록 조회 * */
@GetMapping("/{storeId}/preparing")
Expand Down Expand Up @@ -73,4 +77,18 @@ public ResponseEntity<String> orderStatusToDeliveredUpdate(@PathVariable UUID or
orderService.updateOrderStatusToDelivered(orderDetailId);
return ResponseEntity.noContent().build();
}

/* [상점] 주문 상세 */
@GetMapping("/{order-id}")
public ResponseEntity<OrderDetailResponse> ownerOrderDetail(@PathVariable UUID orderId) {
Comment thread
Bal1oon marked this conversation as resolved.
Outdated
OrderDetailResponse response = orderService.detailResponse(orderId);
return ResponseEntity.ok(response);
}

// 정산 내역
@GetMapping("/sales")
Comment thread
Bal1oon marked this conversation as resolved.
Outdated
public ResponseEntity<SalesResponse> salesResponse(@PathVariable UUID storeId) {
SalesResponse response = salesService.getSalesList(storeId);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.irum.come2us.domain.order.presentation.dto.response;

import java.util.List;
import java.util.UUID;

public record OrderDetailResponse(
List<ProductSummary> productList,
String recipientName,
String recipientContact,
String recipientAddress,
String paymentStatus,
String deliveryStatus,
String deliveryRequest,
String trackingNumber,
String arrivedDate,
String couponName,
Integer deliveryFee,
Integer discountAmount,
int payingAmount,
String orderDate,
int totalProductPrice) {
public record ProductSummary(
UUID orderDetailId,
String productName,
int productPrice,
int productCounts,
String productOption) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.irum.come2us.domain.order.presentation.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

public record OwnerOrderDetailResponse(
List<OrderSummary> orderList, UUID nextCursor, boolean hasNext) {
public record OrderSummary(
UUID orderId,
String recipientName,
String recipientContact,
String recipientAddress,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime orderDate,
int totalProductPrice,
int discountAmount,
int payingAmount,
int deliveryFee,
List<ProductSummary> productList) {}

public record ProductSummary(
UUID orderDetailId,
String productName,
int productCounts,
int productPrice,
String optionTitle) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.irum.come2us.domain.order.presentation.dto.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

public record SalesResponse(List<OrderSummary> orderList, UUID nextCursor, boolean hasNext) {
public record OrderSummary(
UUID orderId,
String recipientName,
String recipientContact,
String recipientAddress,
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime orderDate,
int totalProductPrice,
int discountAmount,
int payingAmount,
int deliveryFee,
List<ProductSummary> productList,
String orderStatus) {}

public record ProductSummary(
UUID orderDetailId,
String productName,
int productCounts,
int productPrice,
String optionTitle) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@ public interface RefundRepository extends JpaRepository<Refund, UUID> {
// RefundRepository 추후 orderRepository에서 가져오는 것으로 변경 예정
// public interface OrderRepository extends JpaRepository<Order, UUID> {..}
Optional<Order> findByOrder_OrderId(@Param("orderId") UUID orderId);

Optional<Refund> findLatestByOrder(Order order);
Comment thread
Bal1oon marked this conversation as resolved.
Outdated

}
Loading