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
| // 庫存聚合根
public class InventoryAggregate extends AggregateRoot {
private String productId;
private int quantity;
private int reservedQuantity;
private LocalDateTime lastUpdated;
public InventoryAggregate() {
super();
}
// 初始化庫存
public static InventoryAggregate initialize(String productId, int initialQuantity, String userId) {
InventoryAggregate inventory = new InventoryAggregate();
inventory.addEvent(new InventoryInitializedDomainEvent(inventory.getId(), productId, initialQuantity, userId));
return inventory;
}
// 增加庫存
public void addStock(int quantity, String reason, String userId) {
if (quantity <= 0) {
throw new IllegalArgumentException("增加的庫存數量必須大於0");
}
this.addEvent(new StockAddedDomainEvent(this.getId(), this.productId, quantity, reason, userId));
}
// 預留庫存
public void reserveStock(int quantity, String orderId, String userId) {
if (quantity <= 0) {
throw new IllegalArgumentException("預留的庫存數量必須大於0");
}
if (this.quantity - this.reservedQuantity < quantity) {
throw new IllegalStateException("可用庫存不足");
}
this.addEvent(new StockReservedDomainEvent(this.getId(), this.productId, quantity, orderId, userId));
}
// 確認庫存消耗
public void confirmStock(int quantity, String orderId, String userId) {
if (quantity <= 0) {
throw new IllegalArgumentException("確認的庫存數量必須大於0");
}
if (this.reservedQuantity < quantity) {
throw new IllegalStateException("預留庫存不足");
}
this.addEvent(new StockConfirmedDomainEvent(this.getId(), this.productId, quantity, orderId, userId));
}
// 釋放預留庫存
public void releaseReservedStock(int quantity, String orderId, String userId) {
if (quantity <= 0) {
throw new IllegalArgumentException("釋放的庫存數量必須大於0");
}
if (this.reservedQuantity < quantity) {
throw new IllegalStateException("預留庫存不足");
}
this.addEvent(new StockReleasedDomainEvent(this.getId(), this.productId, quantity, orderId, userId));
}
@Override
protected void apply(DomainEvent event) {
if (event instanceof InventoryInitializedDomainEvent) {
apply((InventoryInitializedDomainEvent) event);
} else if (event instanceof StockAddedDomainEvent) {
apply((StockAddedDomainEvent) event);
} else if (event instanceof StockReservedDomainEvent) {
apply((StockReservedDomainEvent) event);
} else if (event instanceof StockConfirmedDomainEvent) {
apply((StockConfirmedDomainEvent) event);
} else if (event instanceof StockReleasedDomainEvent) {
apply((StockReleasedDomainEvent) event);
}
}
private void apply(InventoryInitializedDomainEvent event) {
this.productId = event.getProductId();
this.quantity = event.getInitialQuantity();
this.reservedQuantity = 0;
this.lastUpdated = event.getOccurredAt();
}
private void apply(StockAddedDomainEvent event) {
this.quantity += event.getQuantity();
this.lastUpdated = event.getOccurredAt();
}
private void apply(StockReservedDomainEvent event) {
this.reservedQuantity += event.getQuantity();
this.lastUpdated = event.getOccurredAt();
}
private void apply(StockConfirmedDomainEvent event) {
this.quantity -= event.getQuantity();
this.reservedQuantity -= event.getQuantity();
this.lastUpdated = event.getOccurredAt();
}
private void apply(StockReleasedDomainEvent event) {
this.reservedQuantity -= event.getQuantity();
this.lastUpdated = event.getOccurredAt();
}
// 計算可用庫存
public int getAvailableQuantity() {
return quantity - reservedQuantity;
}
// Getter 方法
public String getProductId() { return productId; }
public int getQuantity() { return quantity; }
public int getReservedQuantity() { return reservedQuantity; }
public LocalDateTime getLastUpdated() { return lastUpdated; }
}
// 庫存相關領域事件
public class InventoryInitializedDomainEvent extends DomainEvent {
private String productId;
private int initialQuantity;
public InventoryInitializedDomainEvent() {
super();
}
public InventoryInitializedDomainEvent(String aggregateId, String productId, int initialQuantity, String userId) {
super(aggregateId, "InventoryInitializedDomainEvent", userId, 1);
this.productId = productId;
this.initialQuantity = initialQuantity;
}
// Getter 和 Setter 方法
public String getProductId() { return productId; }
public void setProductId(String productId) { this.productId = productId; }
public int getInitialQuantity() { return initialQuantity; }
public void setInitialQuantity(int initialQuantity) { this.initialQuantity = initialQuantity; }
}
public class StockAddedDomainEvent extends DomainEvent {
private String productId;
private int quantity;
private String reason;
public StockAddedDomainEvent() {
super();
}
public StockAddedDomainEvent(String aggregateId, String productId, int quantity, String reason, String userId) {
super(aggregateId, "StockAddedDomainEvent", userId, 1);
this.productId = productId;
this.quantity = quantity;
this.reason = reason;
}
// Getter 和 Setter 方法
public String getProductId() { return productId; }
public void setProductId(String productId) { this.productId = productId; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public String getReason() { return reason; }
public void setReason(String reason) { this.reason = reason; }
}
public class StockReservedDomainEvent extends DomainEvent {
private String productId;
private int quantity;
private String orderId;
public StockReservedDomainEvent() {
super();
}
public StockReservedDomainEvent(String aggregateId, String productId, int quantity, String orderId, String userId) {
super(aggregateId, "StockReservedDomainEvent", userId, 1);
this.productId = productId;
this.quantity = quantity;
this.orderId = orderId;
}
// Getter 和 Setter 方法
public String getProductId() { return productId; }
public void setProductId(String productId) { this.productId = productId; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
public String getOrderId() { return orderId; }
public void setOrderId(String orderId) { this.orderId = orderId; }
}
|
留言討論