-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFoodOrder.java
More file actions
193 lines (160 loc) · 7.02 KB
/
Copy pathFoodOrder.java
File metadata and controls
193 lines (160 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cgv_23rd.ceos.entity.food;
import cgv_23rd.ceos.entity.BaseEntity;
import cgv_23rd.ceos.entity.enums.FoodOrderStatus;
import cgv_23rd.ceos.entity.enums.PaymentStatus;
import cgv_23rd.ceos.entity.theater.Theater;
import cgv_23rd.ceos.entity.user.User;
import cgv_23rd.ceos.global.apiPayload.code.GeneralErrorCode;
import cgv_23rd.ceos.global.apiPayload.exception.GeneralException;
import jakarta.persistence.*;
import lombok.*;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Builder(access = AccessLevel.PRIVATE)
@Table(
indexes = {
@Index(name = "idx_food_order_status_created_at", columnList = "status, createdAt")
}
)
public class FoodOrder extends BaseEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Integer totalPrice;
@Enumerated(EnumType.STRING)
private FoodOrderStatus status;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private PaymentStatus paymentStatus;
@Column(length = 100)
private String paymentId;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "theater_id", nullable = false)
private Theater theater;
@OneToMany(mappedBy = "foodOrder", cascade = CascadeType.ALL, orphanRemoval = true)
private List<FoodOrderItem> foodOrderItems = new ArrayList<>();
public static FoodOrder create(User user, Theater theater) {
return FoodOrder.builder()
.user(user)
.theater(theater)
.status(FoodOrderStatus.대기)
.paymentStatus(PaymentStatus.READY)
.paymentId(null)
.totalPrice(0)
.foodOrderItems(new ArrayList<>())
.build();
}
public void assignPaymentId(String paymentId) {
if (this.status != FoodOrderStatus.대기) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY);
}
if (this.paymentStatus == PaymentStatus.PROCESSING || this.paymentStatus == PaymentStatus.PAID) {
throw new GeneralException(GeneralErrorCode.PAYMENT_ALREADY_PROCESSED, "이미 결제가 진행 중이거나 완료된 주문입니다.");
}
if (this.paymentId != null && !this.paymentId.isBlank()) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "이미 결제 식별자가 할당된 주문입니다.");
}
this.paymentId = paymentId;
this.paymentStatus = PaymentStatus.PROCESSING;
}
public void confirm() {
if (this.status != FoodOrderStatus.대기) {
throw new GeneralException(
GeneralErrorCode.PAYMENT_ALREADY_PROCESSED,
"대기 상태의 주문만 완료할 수 있습니다."
);
}
if (this.paymentStatus != PaymentStatus.PAID) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "결제 완료 상태의 주문만 확정할 수 있습니다.");
}
this.status = FoodOrderStatus.완료;
}
public void cancel() {
if (this.status == FoodOrderStatus.취소) {
throw new GeneralException(GeneralErrorCode.FOOD_ORDER_ALREADY_CANCELED);
}
if (this.status == FoodOrderStatus.완료) {
throw new GeneralException(
GeneralErrorCode.FOOD_ORDER_CANNOT_CANCEL);
}
this.status = FoodOrderStatus.취소;
}
public void cancelAfterPaymentCancellation() {
if (this.status == FoodOrderStatus.취소) {
throw new GeneralException(GeneralErrorCode.FOOD_ORDER_ALREADY_CANCELED);
}
if (this.paymentStatus != PaymentStatus.CANCELLED) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_CANCELLABLE, "결제 취소가 완료된 주문만 취소할 수 있습니다.");
}
this.status = FoodOrderStatus.취소;
}
public void markPaymentPaid() {
validateTransitionTo(PaymentStatus.PAID, EnumSet.of(PaymentStatus.PROCESSING), EnumSet.of(FoodOrderStatus.대기));
this.paymentStatus = PaymentStatus.PAID;
}
public void markPaymentFailed() {
if (this.paymentStatus == PaymentStatus.FAILED) {
return;
}
validateTransitionTo(PaymentStatus.FAILED, EnumSet.of(PaymentStatus.PROCESSING), EnumSet.of(FoodOrderStatus.대기));
this.paymentStatus = PaymentStatus.FAILED;
}
public void markPaymentUnknown() {
if (this.paymentStatus == PaymentStatus.UNKNOWN) {
return;
}
validateTransitionTo(PaymentStatus.UNKNOWN, EnumSet.of(PaymentStatus.PROCESSING), EnumSet.of(FoodOrderStatus.대기));
this.paymentStatus = PaymentStatus.UNKNOWN;
}
public void markPaymentCancelled() {
if (this.paymentStatus == PaymentStatus.CANCELLED) {
return;
}
validateTransitionTo(PaymentStatus.CANCELLED,
EnumSet.of(PaymentStatus.PAID, PaymentStatus.UNKNOWN),
EnumSet.of(FoodOrderStatus.대기, FoodOrderStatus.완료));
this.paymentStatus = PaymentStatus.CANCELLED;
}
public boolean isOwnedBy(Long userId) {
return this.user.getId().equals(userId);
}
public void addItem(Food food, int quantity) {
if (this.status != FoodOrderStatus.대기) {
throw new GeneralException(GeneralErrorCode.FOOD_ORDER_INVALID_STATE);
}
int itemTotalPrice = food.getPrice() * quantity;
FoodOrderItem orderItem = FoodOrderItem.builder()
.foodOrder(this)
.food(food)
.quantity(quantity)
.price(itemTotalPrice)
.build();
this.foodOrderItems.add(orderItem);
this.totalPrice += itemTotalPrice;
}
private void validatePaymentIdExists() {
if (this.paymentId == null || this.paymentId.isBlank()) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY, "결제 식별자가 없는 주문입니다.");
}
}
private void validateTransitionTo(PaymentStatus targetStatus,
EnumSet<PaymentStatus> allowedPaymentStatuses,
EnumSet<FoodOrderStatus> allowedOrderStatuses) {
validatePaymentIdExists();
if (!allowedOrderStatuses.contains(this.status)) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY,
"현재 주문 상태에서는 결제 상태를 " + targetStatus + "로 변경할 수 없습니다.");
}
if (!allowedPaymentStatuses.contains(this.paymentStatus)) {
throw new GeneralException(GeneralErrorCode.PAYMENT_NOT_READY,
"현재 결제 상태에서는 " + targetStatus + "로 변경할 수 없습니다. current=" + this.paymentStatus);
}
}
}