-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] 판매 금액 정산 - 모든 점주 및 OrderPayment 목록 조회, OrderPayment 상태 일괄 갱신 (#93
) * [refactor] 메뉴 가격 데이터 타입을 Long 으로 통일 * [refactor] 메뉴 가격 데이터 타입을 Long 으로 통일 * [feat] 모든 점주를 조회하는 기능 * [feat] 각 점주의 정산해야할 OrderPayment 목록을 조회하는 기능 * [feat] PointPayment -> OrderPayment 이름 변경 및 생성자 추가 * [feat] 정산해야할 OrderPayment 가 올바른지 검증하는 기능 - Vendor가 동일한지 검증 - OrderPaymentStatus 상태 검증 * [feat] OrderPayment 목록의 OrderPaymentStatus 상태를 ADJUSTMENT_SUCCESS 로 갱신하는 기능 * [style] 주문 금액 정산 기능 뼈대 코드 TODO 작성 * [feat] VendorId와 OrderPaymentStatus 기준으로 OrderPayment 목록을 조회하는 기능 * [feat] OrderPaymentStatus 를 일괄 업데이트하는 기능 - clearAutomatically 옵션 true 설정 영속성 컨텍스트에서 관리하는 엔티티와 DB 데이터의 동기화 이슈를 예방하기 위함 * [feat] OrderPaymentStatus 상태 Enum * [feat] @Getter Lombok 추가 * [style] 에러 코드 수정 * [fix] 주문 가격 타입을 long 으로 변경하여 컴파일 오류 수정 * [fix] OrderItem 패키지 이동 변경사항 반영하여 컴파일 오류 수정
- Loading branch information
Showing
18 changed files
with
253 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/camp/woowak/lab/payment/domain/OrderPayment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package camp.woowak.lab.payment.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
import camp.woowak.lab.customer.domain.Customer; | ||
import camp.woowak.lab.order.domain.Order; | ||
import camp.woowak.lab.vendor.domain.Vendor; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.EnumType; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import lombok.AccessLevel; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@Getter | ||
public class OrderPayment { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private Order order; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "sendor_id", nullable = false) | ||
private Customer sender; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "recipient_id", nullable = false) | ||
private Vendor recipient; | ||
|
||
@Enumerated(value = EnumType.STRING) | ||
private OrderPaymentStatus orderPaymentStatus; | ||
|
||
private LocalDateTime createdAt; | ||
|
||
public OrderPayment(Order order, Customer sender, Vendor recipient, | ||
OrderPaymentStatus orderPaymentStatus, LocalDateTime createdAt | ||
) { | ||
this.order = order; | ||
this.sender = sender; | ||
this.recipient = recipient; | ||
this.orderPaymentStatus = orderPaymentStatus; | ||
this.createdAt = createdAt; | ||
} | ||
|
||
public void validateReadyToAdjustment(final Vendor adjustmentTarget) { | ||
if (isEqualsRecipient(adjustmentTarget) && orderPaymentStatusIsSuccess()) { | ||
return; | ||
} | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
private boolean isEqualsRecipient(Vendor recipient) { | ||
return this.recipient.equals(recipient); | ||
} | ||
|
||
private boolean orderPaymentStatusIsSuccess() { | ||
return this.orderPaymentStatus.equals(OrderPaymentStatus.ORDER_SUCCESS); | ||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/camp/woowak/lab/payment/domain/OrderPaymentStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package camp.woowak.lab.payment.domain; | ||
|
||
public enum OrderPaymentStatus { | ||
ORDER_SUCCESS, | ||
ADJUSTMENT_SUCCESS, | ||
} |
24 changes: 0 additions & 24 deletions
24
src/main/java/camp/woowak/lab/payment/domain/PointPayment.java
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/main/java/camp/woowak/lab/payment/repository/OrderPaymentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package camp.woowak.lab.payment.repository; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
import camp.woowak.lab.payment.domain.OrderPayment; | ||
import camp.woowak.lab.payment.domain.OrderPaymentStatus; | ||
|
||
public interface OrderPaymentRepository extends JpaRepository<OrderPayment, Long> { | ||
|
||
@Query("SELECT op FROM OrderPayment op " | ||
+ "WHERE op.recipient.id = :recipientId " | ||
+ "AND op.orderPaymentStatus = :orderPaymentStatus") | ||
List<OrderPayment> findByRecipientIdAndOrderPaymentStatus(@Param("recipientId") UUID recipientId, | ||
@Param("orderPaymentStatus") OrderPaymentStatus orderPaymentStatus); | ||
|
||
@Modifying(clearAutomatically = true) | ||
@Query("UPDATE OrderPayment op " | ||
+ "SET op.orderPaymentStatus = :newStatus " | ||
+ "WHERE op.id IN :ids") | ||
int updateOrderPaymentStatus(@Param("ids") List<Long> ids, @Param("newStatus") OrderPaymentStatus newStatus); | ||
|
||
} |
99 changes: 99 additions & 0 deletions
99
src/main/java/camp/woowak/lab/payment/service/OrderPaymentAdjustmentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package camp.woowak.lab.payment.service; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import camp.woowak.lab.menu.repository.MenuRepository; | ||
import camp.woowak.lab.order.domain.vo.OrderItem; | ||
import camp.woowak.lab.payment.domain.OrderPayment; | ||
import camp.woowak.lab.payment.domain.OrderPaymentStatus; | ||
import camp.woowak.lab.payment.repository.OrderPaymentRepository; | ||
import camp.woowak.lab.vendor.domain.Vendor; | ||
import camp.woowak.lab.vendor.exception.NotFoundVendorException; | ||
import camp.woowak.lab.vendor.repository.VendorRepository; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
/** | ||
* 정산을 담당하는 서비스 | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class OrderPaymentAdjustmentService { | ||
|
||
private final VendorRepository vendorRepository; | ||
private final MenuRepository menuRepository; | ||
private final OrderPaymentRepository orderPaymentRepository; | ||
|
||
/** | ||
* @throws NotFoundVendorException vendorId에 해당하는 점주를 찾을 수 없을 떄 | ||
*/ | ||
@Transactional | ||
public void adjustment() { | ||
// 1. 모든 점주 조회 | ||
List<Vendor> vendors = findAllVendors(); | ||
for (Vendor vendor : vendors) { | ||
// 2. 각 점주의 정산해야할 OrderPayment 목록을 조회 | ||
List<OrderPayment> orderPayments = findOrderPaymentsToAdjustment(vendor); | ||
|
||
// 3. 각 OrderPayment 의 Order 주문 금액을 계산 | ||
Long totalOrderPrice = calculateTotalOrderPrice(orderPayments); | ||
|
||
// 4. 총 주문 금액에서 수수료 5%를 계산 | ||
Double commission = calculateCommission(totalOrderPrice); | ||
|
||
// 5. 수수료를 제외한 금액을 점주에게 송금 | ||
|
||
// 6. 송금을 성공하면, OrderPayment 목록의 OrderPaymentStatus 상태를 ADJUSTMENT_SUCCESS 로 갱신 | ||
updateOrderPaymentStatus(orderPayments); | ||
} | ||
} | ||
|
||
// 모든 점주 조회 | ||
private List<Vendor> findAllVendors() { | ||
return vendorRepository.findAll(); | ||
} | ||
|
||
// 각 점주의 정산해야할 OrderPayment 목록을 조회 | ||
private List<OrderPayment> findOrderPaymentsToAdjustment(final Vendor vendor) { | ||
List<OrderPayment> orderPayments = orderPaymentRepository.findByRecipientIdAndOrderPaymentStatus( | ||
vendor.getId(), OrderPaymentStatus.ORDER_SUCCESS); | ||
|
||
for (OrderPayment orderPayment : orderPayments) { | ||
// 정산해야할 OrderPayment 가 맞는지 검증 | ||
orderPayment.validateReadyToAdjustment(vendor); | ||
} | ||
|
||
return orderPayments; | ||
} | ||
|
||
// TODO: OrderPayment 목록의 Order 총 주문 금액을 계산한다. | ||
// 메뉴의 가격은 계속 바뀔 수 있어서, 정산 시점에 주문 - 메뉴 정보로 금액을 다시 계산하는 부분이 제약이 있음 | ||
private long calculateTotalOrderPrice(final List<OrderPayment> orderPayments) { | ||
Long totalOrderPrice = 0L; | ||
|
||
return totalOrderPrice; | ||
} | ||
|
||
// TODO: 각 OrderPayment 의 Order 주문 금액을 계산한다. | ||
private Long calculateOrderPrice(List<OrderItem> orderItems) { | ||
long totalOrderPrice = 0L; | ||
|
||
return totalOrderPrice; | ||
} | ||
|
||
// 총 주문 금액에서 수수료 5%를 계산한다. | ||
private Double calculateCommission(Long totalOrderPrice) { | ||
|
||
return 0.0; | ||
} | ||
|
||
// OrderPayment 목록의 OrderPaymentStatus 상태를 ADJUSTMENT_SUCCESS 로 갱신 | ||
private void updateOrderPaymentStatus(List<OrderPayment> orderPayments) { | ||
orderPaymentRepository.updateOrderPaymentStatus( | ||
orderPayments.stream().map(OrderPayment::getId).toList(), | ||
OrderPaymentStatus.ADJUSTMENT_SUCCESS); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.