fix: prevent duplicate delivery assignments

This commit is contained in:
王鹏
2026-07-07 17:52:54 +08:00
parent c4b32da54d
commit 060051cb44
2 changed files with 23 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ public class DeliveryAssignmentService {
task.setBizType(BIZ_TYPE_GOODS_ORDER);
task.setBizOrderId(order.getId());
task.setBizOrderNo(order.getOrderNo());
ensureNotAssigned(task.getBizType(), task.getBizOrderId());
enrichAddressContact(task, order.getUserId(), order.getAddressId());
task.setAmountCent(order.getPayableAmountCent());
task.setRemark(order.getRemark());
@@ -82,6 +83,7 @@ public class DeliveryAssignmentService {
task.setBizType(BIZ_TYPE_EXPRESS_ORDER);
task.setBizOrderId(order.getId());
task.setBizOrderNo(order.getOrderNo());
ensureNotAssigned(task.getBizType(), task.getBizOrderId());
task.setContactName(order.getContactName());
task.setContactPhone(order.getContactPhone());
task.setAddress(order.getDeliveryAddress());
@@ -107,6 +109,7 @@ public class DeliveryAssignmentService {
task.setBizType(BIZ_TYPE_GROUP_BUY_ORDER);
task.setBizOrderId(order.getId());
task.setBizOrderNo(order.getOrderNo());
ensureNotAssigned(task.getBizType(), task.getBizOrderId());
enrichAddressContact(task, order.getUserId(), order.getAddressId());
task.setAmountCent(order.getAmountCent());
task.setRemark(order.getRemark());
@@ -171,6 +174,14 @@ public class DeliveryAssignmentService {
return task;
}
private void ensureNotAssigned(String bizType, Long bizOrderId) {
for (DeliveryOrderResponse task : tasks.values()) {
if (bizType.equals(task.getBizType()) && bizOrderId.equals(task.getBizOrderId())) {
throw new BizException("订单已派单");
}
}
}
private void enrichAddressContact(DeliveryOrderResponse task, Long userId, Long addressId) {
if (addressId == null) {
return;

View File

@@ -69,6 +69,18 @@ class DeliveryAssignmentServiceTests {
.hasMessageContaining("自提订单无需派单");
}
@Test
void cannotAssignSameGoodsOrderTwice() {
TestFixture fixture = new TestFixture();
GoodsOrderResponse order = fixture.preparedDeliveryOrder();
DeliveryUserResponse rider = fixture.rider();
fixture.assignmentService.assignGoodsOrder(order.getId(), rider.getId());
assertThatThrownBy(() -> fixture.assignmentService.assignGoodsOrder(order.getId(), rider.getId()))
.hasMessageContaining("订单已派单");
}
private static class TestFixture {
private final ProductService productService = new ProductService();
private final GoodsOrderService goodsOrderService = new GoodsOrderService(productService);