feat: enrich group buy delivery task info
This commit is contained in:
@@ -4,9 +4,12 @@ import com.linhelp.common.enums.DeliveryMethod;
|
||||
import com.linhelp.common.enums.DeliveryTaskStatus;
|
||||
import com.linhelp.common.enums.ExpressOrderStatus;
|
||||
import com.linhelp.common.enums.GoodsOrderStatus;
|
||||
import com.linhelp.common.enums.GroupBuyOrderStatus;
|
||||
import com.linhelp.common.exception.BizException;
|
||||
import com.linhelp.express.ExpressOrderResponse;
|
||||
import com.linhelp.express.ExpressOrderService;
|
||||
import com.linhelp.groupbuy.GroupBuyOrderResponse;
|
||||
import com.linhelp.groupbuy.GroupBuyOrderService;
|
||||
import com.linhelp.order.GoodsOrderResponse;
|
||||
import com.linhelp.order.GoodsOrderService;
|
||||
import com.linhelp.user.AddressResponse;
|
||||
@@ -29,6 +32,7 @@ public class DeliveryAssignmentService {
|
||||
private final DeliveryUserService deliveryUserService;
|
||||
private final GoodsOrderService goodsOrderService;
|
||||
private final ExpressOrderService expressOrderService;
|
||||
private final GroupBuyOrderService groupBuyOrderService;
|
||||
private final UserAddressService addressService;
|
||||
private final AtomicLong ids = new AtomicLong(1L);
|
||||
private final Map<Long, DeliveryOrderResponse> tasks = new LinkedHashMap<Long, DeliveryOrderResponse>();
|
||||
@@ -36,10 +40,12 @@ public class DeliveryAssignmentService {
|
||||
public DeliveryAssignmentService(DeliveryUserService deliveryUserService,
|
||||
GoodsOrderService goodsOrderService,
|
||||
ExpressOrderService expressOrderService,
|
||||
GroupBuyOrderService groupBuyOrderService,
|
||||
UserAddressService addressService) {
|
||||
this.deliveryUserService = deliveryUserService;
|
||||
this.goodsOrderService = goodsOrderService;
|
||||
this.expressOrderService = expressOrderService;
|
||||
this.groupBuyOrderService = groupBuyOrderService;
|
||||
this.addressService = addressService;
|
||||
}
|
||||
|
||||
@@ -56,7 +62,7 @@ public class DeliveryAssignmentService {
|
||||
task.setBizType(BIZ_TYPE_GOODS_ORDER);
|
||||
task.setBizOrderId(order.getId());
|
||||
task.setBizOrderNo(order.getOrderNo());
|
||||
enrichGoodsContact(task, order);
|
||||
enrichAddressContact(task, order.getUserId(), order.getAddressId());
|
||||
task.setAmountCent(order.getPayableAmountCent());
|
||||
task.setRemark(order.getRemark());
|
||||
tasks.put(task.getId(), task);
|
||||
@@ -87,10 +93,23 @@ public class DeliveryAssignmentService {
|
||||
|
||||
public synchronized DeliveryOrderResponse assignGroupBuyOrder(Long communityId, Long groupBuyOrderId, Long 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.setBizOrderId(groupBuyOrderId);
|
||||
task.setBizOrderNo("GB" + String.format("%08d", groupBuyOrderId));
|
||||
task.setBizOrderId(order.getId());
|
||||
task.setBizOrderNo(order.getOrderNo());
|
||||
enrichAddressContact(task, order.getUserId(), order.getAddressId());
|
||||
task.setAmountCent(order.getAmountCent());
|
||||
task.setRemark(order.getRemark());
|
||||
tasks.put(task.getId(), task);
|
||||
return copy(task);
|
||||
}
|
||||
@@ -152,11 +171,11 @@ public class DeliveryAssignmentService {
|
||||
return task;
|
||||
}
|
||||
|
||||
private void enrichGoodsContact(DeliveryOrderResponse task, GoodsOrderResponse order) {
|
||||
if (order.getAddressId() == null) {
|
||||
private void enrichAddressContact(DeliveryOrderResponse task, Long userId, Long addressId) {
|
||||
if (addressId == null) {
|
||||
return;
|
||||
}
|
||||
AddressResponse address = addressService.detailMine(order.getUserId(), order.getAddressId());
|
||||
AddressResponse address = addressService.detailMine(userId, addressId);
|
||||
task.setContactName(address.getContactName());
|
||||
task.setContactPhone(address.getPhone());
|
||||
task.setAddress(buildDeliveryAddress(address));
|
||||
|
||||
@@ -2,6 +2,12 @@ package com.linhelp.delivery;
|
||||
|
||||
import com.linhelp.common.enums.DeliveryMethod;
|
||||
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.CreateGoodsOrderRequest;
|
||||
import com.linhelp.order.GoodsOrderResponse;
|
||||
@@ -13,6 +19,7 @@ import com.linhelp.user.AddressResponse;
|
||||
import com.linhelp.user.UserAddressService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -35,6 +42,23 @@ class DeliveryAssignmentServiceTests {
|
||||
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
|
||||
void cannotAssignSelfPickupGoodsOrder() {
|
||||
TestFixture fixture = new TestFixture();
|
||||
@@ -48,10 +72,12 @@ class DeliveryAssignmentServiceTests {
|
||||
private static class TestFixture {
|
||||
private final ProductService productService = new 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 UserAddressService addressService = new UserAddressService();
|
||||
private final DeliveryAssignmentService assignmentService =
|
||||
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, addressService);
|
||||
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, groupBuyOrderService, addressService);
|
||||
|
||||
private GoodsOrderResponse preparedDeliveryOrder() {
|
||||
return preparedOrder(DeliveryMethod.IMMEDIATE, 1L);
|
||||
@@ -76,6 +102,34 @@ class DeliveryAssignmentServiceTests {
|
||||
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() {
|
||||
AddressRequest request = new AddressRequest();
|
||||
request.setTenantId(1L);
|
||||
|
||||
@@ -65,7 +65,7 @@ class RiderTaskServiceTests {
|
||||
private final DeliveryUserService deliveryUserService = new DeliveryUserService();
|
||||
private final UserAddressService addressService = new UserAddressService();
|
||||
private final DeliveryAssignmentService assignmentService =
|
||||
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, addressService);
|
||||
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, null, addressService);
|
||||
private final RiderTaskService riderTaskService =
|
||||
new RiderTaskService(assignmentService, goodsOrderService, null);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user