feat: enrich group buy delivery task info

This commit is contained in:
王鹏
2026-07-07 17:34:15 +08:00
parent d9e8234d27
commit 8a293d8323
3 changed files with 82 additions and 9 deletions

View File

@@ -4,9 +4,12 @@ import com.linhelp.common.enums.DeliveryMethod;
import com.linhelp.common.enums.DeliveryTaskStatus; import com.linhelp.common.enums.DeliveryTaskStatus;
import com.linhelp.common.enums.ExpressOrderStatus; import com.linhelp.common.enums.ExpressOrderStatus;
import com.linhelp.common.enums.GoodsOrderStatus; import com.linhelp.common.enums.GoodsOrderStatus;
import com.linhelp.common.enums.GroupBuyOrderStatus;
import com.linhelp.common.exception.BizException; import com.linhelp.common.exception.BizException;
import com.linhelp.express.ExpressOrderResponse; import com.linhelp.express.ExpressOrderResponse;
import com.linhelp.express.ExpressOrderService; import com.linhelp.express.ExpressOrderService;
import com.linhelp.groupbuy.GroupBuyOrderResponse;
import com.linhelp.groupbuy.GroupBuyOrderService;
import com.linhelp.order.GoodsOrderResponse; import com.linhelp.order.GoodsOrderResponse;
import com.linhelp.order.GoodsOrderService; import com.linhelp.order.GoodsOrderService;
import com.linhelp.user.AddressResponse; import com.linhelp.user.AddressResponse;
@@ -29,6 +32,7 @@ public class DeliveryAssignmentService {
private final DeliveryUserService deliveryUserService; private final DeliveryUserService deliveryUserService;
private final GoodsOrderService goodsOrderService; private final GoodsOrderService goodsOrderService;
private final ExpressOrderService expressOrderService; private final ExpressOrderService expressOrderService;
private final GroupBuyOrderService groupBuyOrderService;
private final UserAddressService addressService; private final UserAddressService addressService;
private final AtomicLong ids = new AtomicLong(1L); private final AtomicLong ids = new AtomicLong(1L);
private final Map<Long, DeliveryOrderResponse> tasks = new LinkedHashMap<Long, DeliveryOrderResponse>(); private final Map<Long, DeliveryOrderResponse> tasks = new LinkedHashMap<Long, DeliveryOrderResponse>();
@@ -36,10 +40,12 @@ public class DeliveryAssignmentService {
public DeliveryAssignmentService(DeliveryUserService deliveryUserService, public DeliveryAssignmentService(DeliveryUserService deliveryUserService,
GoodsOrderService goodsOrderService, GoodsOrderService goodsOrderService,
ExpressOrderService expressOrderService, ExpressOrderService expressOrderService,
GroupBuyOrderService groupBuyOrderService,
UserAddressService addressService) { UserAddressService addressService) {
this.deliveryUserService = deliveryUserService; this.deliveryUserService = deliveryUserService;
this.goodsOrderService = goodsOrderService; this.goodsOrderService = goodsOrderService;
this.expressOrderService = expressOrderService; this.expressOrderService = expressOrderService;
this.groupBuyOrderService = groupBuyOrderService;
this.addressService = addressService; this.addressService = addressService;
} }
@@ -56,7 +62,7 @@ public class DeliveryAssignmentService {
task.setBizType(BIZ_TYPE_GOODS_ORDER); task.setBizType(BIZ_TYPE_GOODS_ORDER);
task.setBizOrderId(order.getId()); task.setBizOrderId(order.getId());
task.setBizOrderNo(order.getOrderNo()); task.setBizOrderNo(order.getOrderNo());
enrichGoodsContact(task, order); enrichAddressContact(task, order.getUserId(), order.getAddressId());
task.setAmountCent(order.getPayableAmountCent()); task.setAmountCent(order.getPayableAmountCent());
task.setRemark(order.getRemark()); task.setRemark(order.getRemark());
tasks.put(task.getId(), task); tasks.put(task.getId(), task);
@@ -87,10 +93,23 @@ public class DeliveryAssignmentService {
public synchronized DeliveryOrderResponse assignGroupBuyOrder(Long communityId, Long groupBuyOrderId, Long riderId) { public synchronized DeliveryOrderResponse assignGroupBuyOrder(Long communityId, Long groupBuyOrderId, Long riderId) {
DeliveryUserResponse rider = deliveryUserService.requireEnabled(riderId); DeliveryUserResponse rider = deliveryUserService.requireEnabled(riderId);
DeliveryOrderResponse task = createBaseTask(rider.getTenantId(), communityId, rider); GroupBuyOrderResponse order = groupBuyOrderService.detail(groupBuyOrderId);
if (!communityId.equals(order.getCommunityId())) {
throw new BizException(404, "团购订单不存在");
}
if (DeliveryMethod.SELF_PICKUP.equals(order.getDeliveryMethod())) {
throw new BizException("自提订单无需派单");
}
if (GroupBuyOrderStatus.PENDING_DELIVERY != order.getStatus()) {
throw new BizException("订单未待配送");
}
DeliveryOrderResponse task = createBaseTask(order.getTenantId(), order.getCommunityId(), rider);
task.setBizType(BIZ_TYPE_GROUP_BUY_ORDER); task.setBizType(BIZ_TYPE_GROUP_BUY_ORDER);
task.setBizOrderId(groupBuyOrderId); task.setBizOrderId(order.getId());
task.setBizOrderNo("GB" + String.format("%08d", groupBuyOrderId)); task.setBizOrderNo(order.getOrderNo());
enrichAddressContact(task, order.getUserId(), order.getAddressId());
task.setAmountCent(order.getAmountCent());
task.setRemark(order.getRemark());
tasks.put(task.getId(), task); tasks.put(task.getId(), task);
return copy(task); return copy(task);
} }
@@ -152,11 +171,11 @@ public class DeliveryAssignmentService {
return task; return task;
} }
private void enrichGoodsContact(DeliveryOrderResponse task, GoodsOrderResponse order) { private void enrichAddressContact(DeliveryOrderResponse task, Long userId, Long addressId) {
if (order.getAddressId() == null) { if (addressId == null) {
return; return;
} }
AddressResponse address = addressService.detailMine(order.getUserId(), order.getAddressId()); AddressResponse address = addressService.detailMine(userId, addressId);
task.setContactName(address.getContactName()); task.setContactName(address.getContactName());
task.setContactPhone(address.getPhone()); task.setContactPhone(address.getPhone());
task.setAddress(buildDeliveryAddress(address)); task.setAddress(buildDeliveryAddress(address));

View File

@@ -2,6 +2,12 @@ package com.linhelp.delivery;
import com.linhelp.common.enums.DeliveryMethod; import com.linhelp.common.enums.DeliveryMethod;
import com.linhelp.common.enums.DeliveryTaskStatus; import com.linhelp.common.enums.DeliveryTaskStatus;
import com.linhelp.groupbuy.GroupBuyOrderRequest;
import com.linhelp.groupbuy.GroupBuyOrderResponse;
import com.linhelp.groupbuy.GroupBuyOrderService;
import com.linhelp.groupbuy.GroupBuyRequest;
import com.linhelp.groupbuy.GroupBuyResponse;
import com.linhelp.groupbuy.GroupBuyService;
import com.linhelp.order.CreateGoodsOrderItemRequest; import com.linhelp.order.CreateGoodsOrderItemRequest;
import com.linhelp.order.CreateGoodsOrderRequest; import com.linhelp.order.CreateGoodsOrderRequest;
import com.linhelp.order.GoodsOrderResponse; import com.linhelp.order.GoodsOrderResponse;
@@ -13,6 +19,7 @@ import com.linhelp.user.AddressResponse;
import com.linhelp.user.UserAddressService; import com.linhelp.user.UserAddressService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.util.Collections; import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@@ -35,6 +42,23 @@ class DeliveryAssignmentServiceTests {
assertThat(task.getAddress()).isEqualTo("1号楼 1201 东门旁"); assertThat(task.getAddress()).isEqualTo("1号楼 1201 东门旁");
} }
@Test
void adminCanAssignReadyGroupBuyOrderWithDeliveryInfoToRider() {
TestFixture fixture = new TestFixture();
GroupBuyOrderResponse order = fixture.preparedGroupBuyDeliveryOrder();
DeliveryUserResponse rider = fixture.rider();
DeliveryOrderResponse task = fixture.assignmentService.assignGroupBuyOrder(order.getCommunityId(), order.getId(), rider.getId());
assertThat(task.getBizType()).isEqualTo(DeliveryAssignmentService.BIZ_TYPE_GROUP_BUY_ORDER);
assertThat(task.getBizOrderNo()).isEqualTo(order.getOrderNo());
assertThat(task.getAmountCent()).isEqualTo(order.getAmountCent());
assertThat(task.getRemark()).isEqualTo("请下午送达");
assertThat(task.getContactName()).isEqualTo("李女士");
assertThat(task.getContactPhone()).isEqualTo("13800001000");
assertThat(task.getAddress()).isEqualTo("1号楼 1201 东门旁");
}
@Test @Test
void cannotAssignSelfPickupGoodsOrder() { void cannotAssignSelfPickupGoodsOrder() {
TestFixture fixture = new TestFixture(); TestFixture fixture = new TestFixture();
@@ -48,10 +72,12 @@ class DeliveryAssignmentServiceTests {
private static class TestFixture { private static class TestFixture {
private final ProductService productService = new ProductService(); private final ProductService productService = new ProductService();
private final GoodsOrderService goodsOrderService = new GoodsOrderService(productService); private final GoodsOrderService goodsOrderService = new GoodsOrderService(productService);
private final GroupBuyService groupBuyService = new GroupBuyService();
private final GroupBuyOrderService groupBuyOrderService = new GroupBuyOrderService(groupBuyService);
private final DeliveryUserService deliveryUserService = new DeliveryUserService(); private final DeliveryUserService deliveryUserService = new DeliveryUserService();
private final UserAddressService addressService = new UserAddressService(); private final UserAddressService addressService = new UserAddressService();
private final DeliveryAssignmentService assignmentService = private final DeliveryAssignmentService assignmentService =
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, addressService); new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, groupBuyOrderService, addressService);
private GoodsOrderResponse preparedDeliveryOrder() { private GoodsOrderResponse preparedDeliveryOrder() {
return preparedOrder(DeliveryMethod.IMMEDIATE, 1L); return preparedOrder(DeliveryMethod.IMMEDIATE, 1L);
@@ -76,6 +102,34 @@ class DeliveryAssignmentServiceTests {
return goodsOrderService.markPrepared(order.getId()); return goodsOrderService.markPrepared(order.getId());
} }
private GroupBuyOrderResponse preparedGroupBuyDeliveryOrder() {
GroupBuyRequest groupBuyRequest = new GroupBuyRequest();
groupBuyRequest.setTenantId(1L);
groupBuyRequest.setCommunityId(1L);
groupBuyRequest.setTitle("今日鸡蛋团购");
groupBuyRequest.setCoverUrl("https://img.test/egg.jpg");
groupBuyRequest.setPriceCent(2990);
groupBuyRequest.setStock(100);
groupBuyRequest.setStartTime(LocalDateTime.now().minusHours(1));
groupBuyRequest.setEndTime(LocalDateTime.now().plusDays(1));
groupBuyRequest.setPickupAddress("小区北门");
GroupBuyResponse groupBuy = groupBuyService.create(groupBuyRequest);
groupBuyService.start(groupBuy.getId());
GroupBuyOrderRequest orderRequest = new GroupBuyOrderRequest();
orderRequest.setTenantId(1L);
orderRequest.setCommunityId(1L);
orderRequest.setGroupBuyId(groupBuy.getId());
orderRequest.setAddressId(address().getId());
orderRequest.setQuantity(2);
orderRequest.setDeliveryMethod(DeliveryMethod.IMMEDIATE);
orderRequest.setRemark("请下午送达");
GroupBuyOrderResponse order = groupBuyOrderService.create(1L, orderRequest);
groupBuyOrderService.confirm(order.getId());
return groupBuyOrderService.markReady(order.getId());
}
private AddressResponse address() { private AddressResponse address() {
AddressRequest request = new AddressRequest(); AddressRequest request = new AddressRequest();
request.setTenantId(1L); request.setTenantId(1L);

View File

@@ -65,7 +65,7 @@ class RiderTaskServiceTests {
private final DeliveryUserService deliveryUserService = new DeliveryUserService(); private final DeliveryUserService deliveryUserService = new DeliveryUserService();
private final UserAddressService addressService = new UserAddressService(); private final UserAddressService addressService = new UserAddressService();
private final DeliveryAssignmentService assignmentService = private final DeliveryAssignmentService assignmentService =
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, addressService); new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, null, addressService);
private final RiderTaskService riderTaskService = private final RiderTaskService riderTaskService =
new RiderTaskService(assignmentService, goodsOrderService, null); new RiderTaskService(assignmentService, goodsOrderService, null);