feat: enrich goods delivery task contact info

This commit is contained in:
王鹏
2026-07-07 17:29:06 +08:00
parent d2886fbd8f
commit d9e8234d27
3 changed files with 76 additions and 6 deletions

View File

@@ -9,6 +9,8 @@ import com.linhelp.express.ExpressOrderResponse;
import com.linhelp.express.ExpressOrderService; import com.linhelp.express.ExpressOrderService;
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.UserAddressService;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDateTime; import java.time.LocalDateTime;
@@ -27,15 +29,18 @@ 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 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>();
public DeliveryAssignmentService(DeliveryUserService deliveryUserService, public DeliveryAssignmentService(DeliveryUserService deliveryUserService,
GoodsOrderService goodsOrderService, GoodsOrderService goodsOrderService,
ExpressOrderService expressOrderService) { ExpressOrderService expressOrderService,
UserAddressService addressService) {
this.deliveryUserService = deliveryUserService; this.deliveryUserService = deliveryUserService;
this.goodsOrderService = goodsOrderService; this.goodsOrderService = goodsOrderService;
this.expressOrderService = expressOrderService; this.expressOrderService = expressOrderService;
this.addressService = addressService;
} }
public synchronized DeliveryOrderResponse assignGoodsOrder(Long goodsOrderId, Long riderId) { public synchronized DeliveryOrderResponse assignGoodsOrder(Long goodsOrderId, Long riderId) {
@@ -51,7 +56,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());
task.setAddress(order.getAddressId() == null ? null : "地址ID " + order.getAddressId()); enrichGoodsContact(task, order);
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);
@@ -147,6 +152,34 @@ public class DeliveryAssignmentService {
return task; return task;
} }
private void enrichGoodsContact(DeliveryOrderResponse task, GoodsOrderResponse order) {
if (order.getAddressId() == null) {
return;
}
AddressResponse address = addressService.detailMine(order.getUserId(), order.getAddressId());
task.setContactName(address.getContactName());
task.setContactPhone(address.getPhone());
task.setAddress(buildDeliveryAddress(address));
}
private String buildDeliveryAddress(AddressResponse address) {
StringBuilder builder = new StringBuilder();
append(builder, address.getBuilding());
append(builder, address.getRoom());
append(builder, address.getDetail());
return builder.toString();
}
private void append(StringBuilder builder, String value) {
if (value == null || value.trim().isEmpty()) {
return;
}
if (builder.length() > 0) {
builder.append(' ');
}
builder.append(value);
}
private DeliveryOrderResponse require(Long id) { private DeliveryOrderResponse require(Long id) {
DeliveryOrderResponse task = tasks.get(id); DeliveryOrderResponse task = tasks.get(id);
if (task == null) { if (task == null) {

View File

@@ -8,6 +8,9 @@ import com.linhelp.order.GoodsOrderResponse;
import com.linhelp.order.GoodsOrderService; import com.linhelp.order.GoodsOrderService;
import com.linhelp.product.ProductService; import com.linhelp.product.ProductService;
import com.linhelp.product.ProductSkuResponse; import com.linhelp.product.ProductSkuResponse;
import com.linhelp.user.AddressRequest;
import com.linhelp.user.AddressResponse;
import com.linhelp.user.UserAddressService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Collections; import java.util.Collections;
@@ -27,6 +30,9 @@ class DeliveryAssignmentServiceTests {
assertThat(task.getBizType()).isEqualTo(DeliveryAssignmentService.BIZ_TYPE_GOODS_ORDER); assertThat(task.getBizType()).isEqualTo(DeliveryAssignmentService.BIZ_TYPE_GOODS_ORDER);
assertThat(task.getStatus()).isEqualTo(DeliveryTaskStatus.ASSIGNED); assertThat(task.getStatus()).isEqualTo(DeliveryTaskStatus.ASSIGNED);
assertThat(task.getRiderId()).isEqualTo(rider.getId()); assertThat(task.getRiderId()).isEqualTo(rider.getId());
assertThat(task.getContactName()).isEqualTo("李女士");
assertThat(task.getContactPhone()).isEqualTo("13800001000");
assertThat(task.getAddress()).isEqualTo("1号楼 1201 东门旁");
} }
@Test @Test
@@ -43,8 +49,9 @@ class DeliveryAssignmentServiceTests {
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 DeliveryUserService deliveryUserService = new DeliveryUserService(); private final DeliveryUserService deliveryUserService = new DeliveryUserService();
private final UserAddressService addressService = new UserAddressService();
private final DeliveryAssignmentService assignmentService = private final DeliveryAssignmentService assignmentService =
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null); new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, addressService);
private GoodsOrderResponse preparedDeliveryOrder() { private GoodsOrderResponse preparedDeliveryOrder() {
return preparedOrder(DeliveryMethod.IMMEDIATE, 1L); return preparedOrder(DeliveryMethod.IMMEDIATE, 1L);
@@ -55,12 +62,13 @@ class DeliveryAssignmentServiceTests {
} }
private GoodsOrderResponse preparedOrder(DeliveryMethod deliveryMethod, Long addressId) { private GoodsOrderResponse preparedOrder(DeliveryMethod deliveryMethod, Long addressId) {
Long resolvedAddressId = addressId == null ? null : address().getId();
ProductSkuResponse sku = productService.createDemoSku(1L, 1L, 1L, "500g", 990, 5); ProductSkuResponse sku = productService.createDemoSku(1L, 1L, 1L, "500g", 990, 5);
CreateGoodsOrderRequest request = new CreateGoodsOrderRequest(); CreateGoodsOrderRequest request = new CreateGoodsOrderRequest();
request.setTenantId(1L); request.setTenantId(1L);
request.setCommunityId(1L); request.setCommunityId(1L);
request.setMerchantId(1L); request.setMerchantId(1L);
request.setAddressId(addressId); request.setAddressId(resolvedAddressId);
request.setDeliveryMethod(deliveryMethod); request.setDeliveryMethod(deliveryMethod);
request.setItems(Collections.singletonList(new CreateGoodsOrderItemRequest(1L, sku.getId(), 1))); request.setItems(Collections.singletonList(new CreateGoodsOrderItemRequest(1L, sku.getId(), 1)));
GoodsOrderResponse order = goodsOrderService.create(1L, request); GoodsOrderResponse order = goodsOrderService.create(1L, request);
@@ -68,6 +76,18 @@ class DeliveryAssignmentServiceTests {
return goodsOrderService.markPrepared(order.getId()); return goodsOrderService.markPrepared(order.getId());
} }
private AddressResponse address() {
AddressRequest request = new AddressRequest();
request.setTenantId(1L);
request.setCommunityId(1L);
request.setContactName("李女士");
request.setPhone("13800001000");
request.setBuilding("1号楼");
request.setRoom("1201");
request.setDetail("东门旁");
return addressService.create(1L, request);
}
private DeliveryUserResponse rider() { private DeliveryUserResponse rider() {
DeliveryUserRequest request = new DeliveryUserRequest(); DeliveryUserRequest request = new DeliveryUserRequest();
request.setTenantId(1L); request.setTenantId(1L);

View File

@@ -9,6 +9,9 @@ import com.linhelp.order.GoodsOrderResponse;
import com.linhelp.order.GoodsOrderService; import com.linhelp.order.GoodsOrderService;
import com.linhelp.product.ProductService; import com.linhelp.product.ProductService;
import com.linhelp.product.ProductSkuResponse; import com.linhelp.product.ProductSkuResponse;
import com.linhelp.user.AddressRequest;
import com.linhelp.user.AddressResponse;
import com.linhelp.user.UserAddressService;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Collections; import java.util.Collections;
@@ -60,18 +63,20 @@ class RiderTaskServiceTests {
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 DeliveryUserService deliveryUserService = new DeliveryUserService(); private final DeliveryUserService deliveryUserService = new DeliveryUserService();
private final UserAddressService addressService = new UserAddressService();
private final DeliveryAssignmentService assignmentService = private final DeliveryAssignmentService assignmentService =
new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null); new DeliveryAssignmentService(deliveryUserService, goodsOrderService, null, addressService);
private final RiderTaskService riderTaskService = private final RiderTaskService riderTaskService =
new RiderTaskService(assignmentService, goodsOrderService, null); new RiderTaskService(assignmentService, goodsOrderService, null);
private GoodsOrderResponse preparedDeliveryOrder() { private GoodsOrderResponse preparedDeliveryOrder() {
ProductSkuResponse sku = productService.createDemoSku(1L, 1L, 1L, "500g", 990, 5); ProductSkuResponse sku = productService.createDemoSku(1L, 1L, 1L, "500g", 990, 5);
CreateGoodsOrderRequest request = new CreateGoodsOrderRequest(); CreateGoodsOrderRequest request = new CreateGoodsOrderRequest();
AddressResponse address = address();
request.setTenantId(1L); request.setTenantId(1L);
request.setCommunityId(1L); request.setCommunityId(1L);
request.setMerchantId(1L); request.setMerchantId(1L);
request.setAddressId(1L); request.setAddressId(address.getId());
request.setDeliveryMethod(DeliveryMethod.IMMEDIATE); request.setDeliveryMethod(DeliveryMethod.IMMEDIATE);
request.setItems(Collections.singletonList(new CreateGoodsOrderItemRequest(1L, sku.getId(), 1))); request.setItems(Collections.singletonList(new CreateGoodsOrderItemRequest(1L, sku.getId(), 1)));
GoodsOrderResponse order = goodsOrderService.create(1L, request); GoodsOrderResponse order = goodsOrderService.create(1L, request);
@@ -79,6 +84,18 @@ class RiderTaskServiceTests {
return goodsOrderService.markPrepared(order.getId()); return goodsOrderService.markPrepared(order.getId());
} }
private AddressResponse address() {
AddressRequest request = new AddressRequest();
request.setTenantId(1L);
request.setCommunityId(1L);
request.setContactName("李女士");
request.setPhone("13800001000");
request.setBuilding("1号楼");
request.setRoom("1201");
request.setDetail("东门旁");
return addressService.create(1L, request);
}
private DeliveryUserResponse rider(Long userId, String name) { private DeliveryUserResponse rider(Long userId, String name) {
DeliveryUserRequest request = new DeliveryUserRequest(); DeliveryUserRequest request = new DeliveryUserRequest();
request.setTenantId(1L); request.setTenantId(1L);