Test #1
@@ -0,0 +1,56 @@
|
||||
package com.baiyue.order.service;
|
||||
|
||||
import com.baiyue.common.core.api.ApiResponse;
|
||||
import com.baiyue.common.core.api.ResultCode;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.order.client.FirstlegUnifiedOrderClient;
|
||||
import com.baiyue.order.entity.OrderEntity;
|
||||
import com.baiyue.order.entity.OrderStatus;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** 客户申报资料只允许在待审核、待测量和待分柜阶段修改。 */
|
||||
@Service
|
||||
public class OrderDeclarationEditGuard {
|
||||
private static final Set<String> EDITABLE_MAIN_STATUSES = Set.of("WAIT_MEASURE", "WAIT_SPLIT");
|
||||
|
||||
@Resource private FirstlegUnifiedOrderClient firstleg;
|
||||
@Value("${baiyue.security.internal-service-token}") private String internalToken;
|
||||
|
||||
public void requireEditable(OrderEntity order) {
|
||||
if (order.getStatus() != OrderStatus.APPROVED) return;
|
||||
try {
|
||||
ApiResponse<List<FirstlegUnifiedOrderClient.Summary>> response = firstleg.summaries(internalToken,
|
||||
new FirstlegUnifiedOrderClient.BatchRequest(order.getTenantId(), List.of(order.getId())));
|
||||
if (response == null) throw unavailable();
|
||||
if (!response.successful()) {
|
||||
ResultCode code = ResultCode.of(response.code());
|
||||
String message = response.message() == null || response.message().isBlank()
|
||||
? code.message() : response.message();
|
||||
throw new BizException(code, message);
|
||||
}
|
||||
List<FirstlegUnifiedOrderClient.Summary> rows = response.data();
|
||||
if (rows == null || rows.size() != 1 || rows.get(0) == null || rows.get(0).inbound() == null
|
||||
|| !Objects.equals(rows.get(0).inbound().orderId(), order.getId())) {
|
||||
throw unavailable();
|
||||
}
|
||||
String mainStatus = rows.get(0).mainStatus();
|
||||
if (!EDITABLE_MAIN_STATUSES.contains(mainStatus)) {
|
||||
throw new BizException(ResultCode.CONFLICT, "订单已进入装柜或后续阶段,不能修改客户申报资料");
|
||||
}
|
||||
} catch (BizException exception) {
|
||||
throw exception;
|
||||
} catch (RuntimeException exception) {
|
||||
throw unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
private BizException unavailable() {
|
||||
return new BizException(ResultCode.SERVICE_UNAVAILABLE, "头程履约状态校验服务暂时不可用");
|
||||
}
|
||||
}
|
||||
@@ -37,6 +37,7 @@ public class OrderDeclarationUpdateService {
|
||||
@Resource private OrderRepository repository;
|
||||
@Resource private OrderPricingChannelGuard pricingChannelGuard;
|
||||
@Resource private OrderFulfillmentChannelChangeGuard fulfillmentChannelGuard;
|
||||
@Resource private OrderDeclarationEditGuard declarationEditGuard;
|
||||
@Resource private ObjectMapper json;
|
||||
|
||||
@Transactional
|
||||
@@ -90,6 +91,7 @@ public class OrderDeclarationUpdateService {
|
||||
if (!Objects.equals(order.getVersion(), request.expectedVersion())) {
|
||||
throw new BizException(ResultCode.CONFLICT, "订单版本已变化,请刷新详情后重试");
|
||||
}
|
||||
declarationEditGuard.requireEditable(order);
|
||||
|
||||
List<OrderCargoEntity> current = repository.findCargo(tenantId, orderId);
|
||||
Map<Integer, OrderCargoEntity> byItemNo = new HashMap<>();
|
||||
|
||||
Reference in New Issue
Block a user