优化
This commit is contained in:
+9
-5
@@ -12,6 +12,7 @@ import com.baiyue.lastleg.delivery.entity.DeliveryOperation;
|
||||
import com.baiyue.lastleg.delivery.entity.DeliveryOrder;
|
||||
import com.baiyue.lastleg.delivery.mapper.DeliveryOperationMapper;
|
||||
import com.baiyue.lastleg.delivery.mapper.DeliveryOrderMapper;
|
||||
import com.baiyue.lastleg.delivery.model.DeliveryStatusPolicy;
|
||||
import com.baiyue.lastleg.entity.LastlegInboundOrder;
|
||||
import com.baiyue.lastleg.entity.LastlegOrder;
|
||||
import com.baiyue.lastleg.entity.LastlegOrderStatusHistory;
|
||||
@@ -82,10 +83,8 @@ public class CustomerVisitService {
|
||||
public CustomerVisitResponses.Completed completeDelivery(String customerId, String operatorId, String deliveryNo) {
|
||||
DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo);
|
||||
if (delivery == null) throw notFound("派送单不存在");
|
||||
if (Set.of("RETURN_PENDING", "REJECTED", "CANCELLED").contains(delivery.getStatus())) {
|
||||
throw conflict("当前派送状态不能标记为派送完成");
|
||||
}
|
||||
boolean replayed = "DELIVERED".equals(delivery.getStatus());
|
||||
if (!replayed) requireDeliveryTransition(delivery.getStatus(), "DELIVERED");
|
||||
LocalDateTime now = replayed && delivery.getSignedAt() != null ? delivery.getSignedAt() : LocalDateTime.now();
|
||||
if (!replayed) {
|
||||
delivery.setStatus("DELIVERED");
|
||||
@@ -116,18 +115,18 @@ public class CustomerVisitService {
|
||||
String deliveryNo,
|
||||
CustomerVisitRequests.DeliveryStatus request) {
|
||||
String target = trim(request.status()).toUpperCase(Locale.ROOT);
|
||||
if (!Set.of("WAIT_DELIVERY", "DELIVERING", "DELIVERED", "REJECTED", "CANCELLED").contains(target)) {
|
||||
if (!DeliveryStatusPolicy.isEditableTarget(target)) {
|
||||
throw badRequest("不支持的派送状态");
|
||||
}
|
||||
if ("DELIVERED".equals(target)) return completeDelivery(customerId, operatorId, deliveryNo);
|
||||
DeliveryOrder delivery = deliveryOrders.findByNo(customerId, deliveryNo);
|
||||
if (delivery == null) throw notFound("派送单不存在");
|
||||
if ("RETURN_PENDING".equals(delivery.getStatus())) throw conflict("退货处理中的派送单不能直接修改状态");
|
||||
if (target.equals(delivery.getStatus())) {
|
||||
CustomerVisit existing = visits.findByDelivery(customerId, delivery.getId());
|
||||
return new CustomerVisitResponses.Completed(existing == null ? null : existing.getId(), deliveryNo,
|
||||
delivery.getStatus(), existing == null ? null : existing.getStatus(), true);
|
||||
}
|
||||
requireDeliveryTransition(delivery.getStatus(), target);
|
||||
String from = delivery.getStatus();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
delivery.setStatus(target);
|
||||
@@ -309,6 +308,11 @@ public class CustomerVisitService {
|
||||
}
|
||||
|
||||
private static String trim(String value) { return StringUtils.hasText(value) ? value.trim() : null; }
|
||||
private static void requireDeliveryTransition(String from, String target) {
|
||||
if (!DeliveryStatusPolicy.canTransition(from, target)) {
|
||||
throw conflict("派送状态不允许由 " + from + " 变更为 " + target);
|
||||
}
|
||||
}
|
||||
private static ResponseStatusException badRequest(String message) { return new ResponseStatusException(HttpStatus.BAD_REQUEST, message); }
|
||||
private static ResponseStatusException notFound(String message) { return new ResponseStatusException(HttpStatus.NOT_FOUND, message); }
|
||||
private static ResponseStatusException conflict(String message) { return new ResponseStatusException(HttpStatus.CONFLICT, message); }
|
||||
|
||||
Reference in New Issue
Block a user