优化
This commit is contained in:
@@ -70,7 +70,7 @@ public class OrderApplicationService {
|
||||
long orderId = IdWorker.getId();
|
||||
order.setId(orderId); order.setTenantId(tenantId); order.setOrderNo(nextNo(orderId));
|
||||
order.setStatus(OrderStatus.DRAFT);
|
||||
apply(order, request, snapshots);
|
||||
apply(order, tenantId, request, snapshots);
|
||||
order.setInboundCode(nextInboundCode(inboundCodePrefix));
|
||||
order.setCreatedBy(userId); order.setUpdatedBy(userId);
|
||||
repository.insertOrder(order);
|
||||
@@ -108,7 +108,7 @@ public class OrderApplicationService {
|
||||
}
|
||||
pricingChannelGuard.requireAvailable(tenantId, request.receivingWarehouseId(), request.channelCode(),
|
||||
request.destinationType());
|
||||
apply(order, request, snapshots); order.setStatus(OrderStatus.DRAFT); order.setUpdatedBy(userId);
|
||||
apply(order, tenantId, request, snapshots); order.setStatus(OrderStatus.DRAFT); order.setUpdatedBy(userId);
|
||||
if (before == OrderStatus.SUBMITTED) {
|
||||
order.setStatus(OrderStatus.SUBMITTED);
|
||||
}
|
||||
@@ -411,9 +411,9 @@ public class OrderApplicationService {
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
private void apply(OrderEntity order, OrderRequests.SaveRequest request, OrderMasterDataGuard.Snapshots snapshots) {
|
||||
order.setCustomerId(snapshots.partner() == null ? null : snapshots.partner().id());
|
||||
order.setCustomerName(snapshots.partner() == null ? request.customerName() : snapshots.partner().partnerName());
|
||||
private void apply(OrderEntity order, long tenantId, OrderRequests.SaveRequest request, OrderMasterDataGuard.Snapshots snapshots) {
|
||||
order.setCustomerId(tenantId);
|
||||
order.setCustomerName(request.customerName().trim());
|
||||
order.setTransportMode(request.transportMode()); order.setTradeTerm(request.tradeTerm());
|
||||
order.setDestinationCountryCode(snapshots.destination() == null ? request.destinationCountryCode()
|
||||
: snapshots.destination().warehouse().countryCode());
|
||||
|
||||
@@ -5,7 +5,6 @@ import jakarta.annotation.Resource;
|
||||
import com.baiyue.common.core.api.ResultCode;
|
||||
import com.baiyue.common.core.api.ApiResponse;
|
||||
import com.baiyue.common.core.exception.BizException;
|
||||
import com.baiyue.order.client.PartnerClient;
|
||||
import com.baiyue.order.client.WarehouseClient;
|
||||
import com.baiyue.order.dto.OrderRequests;
|
||||
import feign.FeignException;
|
||||
@@ -21,13 +20,11 @@ import org.slf4j.LoggerFactory;
|
||||
public class OrderMasterDataGuard {
|
||||
private static final Logger log = LoggerFactory.getLogger(OrderMasterDataGuard.class);
|
||||
@Resource private WarehouseClient warehouses;
|
||||
@Resource private PartnerClient partners;
|
||||
@Value("${baiyue.security.internal-service-token}") private String token;
|
||||
@Value("${baiyue.warehouse.public-tenant-id:0}")
|
||||
private long publicTenantId = 0L;
|
||||
public Snapshots validate(long tenantId, OrderRequests.SaveRequest request) {
|
||||
try {
|
||||
PartnerClient.Partner partner=request.customerId()==null ? null : loadPartner(request.customerId());
|
||||
var receiving=loadWarehouse(request.receivingWarehouseId(),"收货仓库");
|
||||
requireWarehouseType(receiving, "DOMESTIC", "收货仓库");
|
||||
if (receiving.addresses()==null || receiving.addresses().stream().noneMatch(Objects::nonNull))
|
||||
@@ -47,7 +44,7 @@ public class OrderMasterDataGuard {
|
||||
destination=loadWarehouse(request.destinationWarehouseId(),"目的仓库");
|
||||
requireWarehouseType(destination, request.destinationType(), "目的仓库");
|
||||
}
|
||||
return new Snapshots(partner,receiving,destination);
|
||||
return new Snapshots(receiving,destination);
|
||||
} catch (FeignException exception) {
|
||||
throw downstreamFailure(exception);
|
||||
} catch (BizException exception) {
|
||||
@@ -56,10 +53,12 @@ public class OrderMasterDataGuard {
|
||||
throw downstreamFailure(exception);
|
||||
}
|
||||
}
|
||||
public void validatePersisted(long tenantId, Long partnerId, Long receivingWarehouseId,
|
||||
public void validatePersisted(long tenantId, Long customerId, Long receivingWarehouseId,
|
||||
Long destinationWarehouseId, String destinationType) {
|
||||
try {
|
||||
if (partnerId!=null) loadPartner(partnerId);
|
||||
if (customerId != null && customerId != tenantId) {
|
||||
throw new BizException(ResultCode.FORBIDDEN,"订单客户必须是当前登录企业");
|
||||
}
|
||||
if (receivingWarehouseId==null) throw new BizException(ResultCode.NOT_FOUND,"收货仓库已停用或不可用");
|
||||
WarehouseClient.Detail receiving=loadWarehouse(receivingWarehouseId,"收货仓库");
|
||||
requireWarehouseType(receiving,"DOMESTIC","收货仓库");
|
||||
@@ -78,25 +77,6 @@ public class OrderMasterDataGuard {
|
||||
throw downstreamFailure(exception);
|
||||
}
|
||||
}
|
||||
private PartnerClient.Partner loadPartner(long id) {
|
||||
ApiResponse<PartnerClient.Partner> response;
|
||||
try {
|
||||
response=partners.active(token,publicTenantId,id);
|
||||
} catch (FeignException.NotFound exception) {
|
||||
throw new BizException(ResultCode.NOT_FOUND,"平台往来单位不存在或不可用");
|
||||
}
|
||||
requireSuccessful(response,"往来单位");
|
||||
if (response.data()==null) throw unavailable("往来单位");
|
||||
var value=response.data();
|
||||
if (!Long.valueOf(publicTenantId).equals(value.tenantId()))
|
||||
throw new BizException(ResultCode.FORBIDDEN,"往来单位不属于平台公共主数据");
|
||||
if (!"ACTIVE".equals(value.status()))
|
||||
throw new BizException(ResultCode.CONFLICT,"往来单位已停用");
|
||||
if (value.types() == null || !value.types().contains("SHIPPER"))
|
||||
throw new BizException(ResultCode.VALIDATION_FAILED,"所选往来单位不具备发货单位类型");
|
||||
if (!StringUtils.hasText(value.partnerName())) throw unavailable("往来单位");
|
||||
return value;
|
||||
}
|
||||
private WarehouseClient.Detail loadWarehouse(long id,String name) {
|
||||
ApiResponse<WarehouseClient.Detail> response;
|
||||
try {
|
||||
@@ -139,6 +119,5 @@ public class OrderMasterDataGuard {
|
||||
throw new BizException(code, message);
|
||||
}
|
||||
}
|
||||
public record Snapshots(PartnerClient.Partner partner, WarehouseClient.Detail receiving,
|
||||
WarehouseClient.Detail destination) { }
|
||||
public record Snapshots(WarehouseClient.Detail receiving, WarehouseClient.Detail destination) { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user