提交
This commit is contained in:
+8
@@ -0,0 +1,8 @@
|
||||
package com.baiyue.lastleg.dropship.model;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record DropshipPalletSnapshot(String sourcePalletNo, long packageCount,
|
||||
List<Sku> skus) {
|
||||
public record Sku(String skuCode, String productName, long packageCount, long productQuantity) {}
|
||||
}
|
||||
+24
-10
@@ -7,6 +7,7 @@ import com.baiyue.lastleg.dropship.mapper.*;
|
||||
import com.baiyue.lastleg.entity.LastlegOrder;
|
||||
import com.baiyue.lastleg.inboundplan.pda.entity.InboundStockReceipt;
|
||||
import com.baiyue.lastleg.mapper.LastlegOrderMapper;
|
||||
import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -109,6 +110,21 @@ public class DropshipOrderService {
|
||||
return profile;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<DropshipPalletSnapshot> palletSnapshots(String customerId, Long orderId) {
|
||||
List<DropshipOrderProfile> orderProfiles = profiles.listOrder(customerId, orderId);
|
||||
Map<Long, List<DropshipSkuInventory>> skusByProfile = skuInventory.listOrder(customerId, orderId).stream()
|
||||
.collect(Collectors.groupingBy(DropshipSkuInventory::getProfileId));
|
||||
return orderProfiles.stream().map(profile -> {
|
||||
List<DropshipPalletSnapshot.Sku> skus = skusByProfile.getOrDefault(profile.getId(), List.of()).stream()
|
||||
.map(sku -> new DropshipPalletSnapshot.Sku(sku.getSkuCode(), sku.getProductName(),
|
||||
value(sku.getPackageCount()), value(sku.getProductQuantity())))
|
||||
.toList();
|
||||
return new DropshipPalletSnapshot(profile.getSourcePalletNo(),
|
||||
skus.stream().mapToLong(DropshipPalletSnapshot.Sku::packageCount).sum(), skus);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void activateAfterStockIn(String customerId, Long orderId, Long taskId,
|
||||
List<InboundStockReceipt> receipts) {
|
||||
@@ -118,20 +134,18 @@ public class DropshipOrderService {
|
||||
if (profilesForOrder.stream().anyMatch(profile -> "ACTIVE".equals(profile.getStatus()))) {
|
||||
throw conflict("Dropship pallet activation is incomplete; repair profiles before retrying");
|
||||
}
|
||||
Map<Long, List<InboundStockReceipt>> receiptsByPallet = receipts.stream()
|
||||
.filter(receipt -> receipt.getWarehousePalletId() != null)
|
||||
.collect(Collectors.groupingBy(InboundStockReceipt::getWarehousePalletId));
|
||||
Map<String, List<InboundStockReceipt>> receiptsByPallet = receipts.stream()
|
||||
.filter(receipt -> receipt.getPalletNo() != null)
|
||||
.collect(Collectors.groupingBy(InboundStockReceipt::getPalletNo));
|
||||
if (receiptsByPallet.size() != profilesForOrder.size()) {
|
||||
throw conflict("PDA pallet count does not match dropship source pallet count");
|
||||
}
|
||||
List<DropshipOrderProfile> orderedProfiles = profilesForOrder.stream()
|
||||
.sorted(java.util.Comparator.comparing(DropshipOrderProfile::getSourcePalletNo)).toList();
|
||||
List<List<InboundStockReceipt>> orderedReceipts = receiptsByPallet.values().stream()
|
||||
.sorted(java.util.Comparator.comparing(value -> value.get(0).getWarehousePalletId())).toList();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
for (int index = 0; index < orderedProfiles.size(); index++) {
|
||||
DropshipOrderProfile profile = orderedProfiles.get(index);
|
||||
List<InboundStockReceipt> actual = orderedReceipts.get(index);
|
||||
for (DropshipOrderProfile profile : profilesForOrder) {
|
||||
List<InboundStockReceipt> actual = receiptsByPallet.get(profile.getSourcePalletNo());
|
||||
if (actual == null || actual.isEmpty()) {
|
||||
throw conflict("PDA receipt is missing for dropship source pallet " + profile.getSourcePalletNo());
|
||||
}
|
||||
List<DropshipCarton> profileCartons = cartons.listProfile(customerId, profile.getId());
|
||||
long expected = profileCartons.size();
|
||||
long received = actual.stream().mapToLong(value -> value.getReceivedPackages() == null ? 0 : value.getReceivedPackages()).sum();
|
||||
|
||||
+10
@@ -65,6 +65,16 @@ public class InboundPdaController {
|
||||
return ApiResponse.success(service.pick(scope(user, customerId), planNo, allocationId, request));
|
||||
}
|
||||
|
||||
@PostMapping("/allocations/{allocationId}/pick-whole-pallet")
|
||||
public ApiResponse<PdaResponses.Operation> pickWholePallet(
|
||||
@AuthenticationPrincipal AuthenticatedUser user, @PathVariable String planNo,
|
||||
@PathVariable long allocationId, @RequestParam(required = false) String customerId,
|
||||
@Valid @RequestBody PdaRequests.WholePalletOperation request) {
|
||||
requireOperate(user);
|
||||
return ApiResponse.success(service.pickWholePallet(
|
||||
scope(user, customerId), planNo, allocationId, request));
|
||||
}
|
||||
|
||||
@PostMapping("/cartons/scan")
|
||||
public ApiResponse<PdaResponses.Carton> scanCarton(@AuthenticationPrincipal AuthenticatedUser user,
|
||||
@PathVariable String planNo,
|
||||
|
||||
@@ -17,6 +17,9 @@ public final class PdaRequests {
|
||||
public record PackageOperation(@Positive int packageCount, @NotBlank String idempotencyKey,
|
||||
String operatorId, String operatorName) {}
|
||||
|
||||
public record WholePalletOperation(@NotBlank String idempotencyKey,
|
||||
String operatorId, String operatorName) {}
|
||||
|
||||
public record ConfirmMerge(@Positive long sourceAllocationId, @Positive int packageCount,
|
||||
@NotBlank String idempotencyKey, String operatorId, String operatorName) {}
|
||||
|
||||
|
||||
+16
-1
@@ -11,7 +11,18 @@ public final class PdaResponses {
|
||||
LocalDateTime pushedAt, LocalDateTime startedAt, LocalDateTime completedAt,
|
||||
List<Long> unloadPhotoFileIds, LocalDateTime unloadPhotoUploadedAt,
|
||||
LocalDateTime estimatedEmptyReturnAt, int expectedPackageCount,
|
||||
int pickedPackageCount) {
|
||||
int pickedPackageCount, String orderType, boolean wholePalletInbound,
|
||||
List<DropshipPallet> dropshipPallets) {
|
||||
public Task(long taskId, String taskNo, String planNo, String status,
|
||||
LocalDateTime pushedAt, LocalDateTime startedAt, LocalDateTime completedAt,
|
||||
List<Long> unloadPhotoFileIds, LocalDateTime unloadPhotoUploadedAt,
|
||||
LocalDateTime estimatedEmptyReturnAt, int expectedPackageCount,
|
||||
int pickedPackageCount) {
|
||||
this(taskId, taskNo, planNo, status, pushedAt, startedAt, completedAt,
|
||||
unloadPhotoFileIds, unloadPhotoUploadedAt, estimatedEmptyReturnAt,
|
||||
expectedPackageCount, pickedPackageCount, null, false, List.of());
|
||||
}
|
||||
|
||||
public Task(long taskId, String taskNo, String planNo, String status,
|
||||
LocalDateTime pushedAt, LocalDateTime startedAt, LocalDateTime completedAt,
|
||||
List<Long> unloadPhotoFileIds, LocalDateTime unloadPhotoUploadedAt,
|
||||
@@ -21,6 +32,10 @@ public final class PdaResponses {
|
||||
}
|
||||
}
|
||||
|
||||
public record DropshipPallet(String sourcePalletNo, long planPalletId, long allocationId,
|
||||
long packageCount, List<DropshipSku> skus) {}
|
||||
public record DropshipSku(String skuCode, String productName, long packageCount, long productQuantity) {}
|
||||
|
||||
public record UnloadPhotoFile(long fileId, String fileName, String contentType, long sizeBytes) {}
|
||||
|
||||
public record Operation(String result, long allocationId, int requiredPackageCount,
|
||||
|
||||
+5
@@ -17,6 +17,8 @@ import com.baiyue.lastleg.inboundplan.pda.entity.InboundPdaTask;
|
||||
import com.baiyue.lastleg.inboundplan.pda.mapper.InboundPalletMergeMapper;
|
||||
import com.baiyue.lastleg.inboundplan.pda.mapper.InboundPdaOperationMapper;
|
||||
import com.baiyue.lastleg.inboundplan.pda.mapper.InboundPdaTaskMapper;
|
||||
import com.baiyue.lastleg.entity.LastlegOrder;
|
||||
import com.baiyue.lastleg.mapper.LastlegOrderMapper;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
@@ -32,6 +34,7 @@ public class PdaWorkflowRepository {
|
||||
@Resource private InboundPdaOperationMapper operations;
|
||||
@Resource private InboundPalletMergeMapper merges;
|
||||
@Resource private InboundPlanCartonMapper cartons;
|
||||
@Resource private LastlegOrderMapper orders;
|
||||
|
||||
public InboundPlan lockPlan(String customerId, String planNo) {
|
||||
return plans.selectOne(new LambdaQueryWrapper<InboundPlan>()
|
||||
@@ -92,6 +95,8 @@ public class PdaWorkflowRepository {
|
||||
.eq(InboundPlanPalletAllocation::getPlanId, planId));
|
||||
}
|
||||
|
||||
public LastlegOrder order(Long orderId) { return orderId == null ? null : orders.selectById(orderId); }
|
||||
|
||||
public void insert(InboundPdaTask value) { tasks.insert(value); }
|
||||
public void insert(InboundPdaOperation value) { operations.insert(value); }
|
||||
public void insert(InboundPalletMerge value) { merges.insert(value); }
|
||||
|
||||
+88
-11
@@ -14,6 +14,9 @@ import com.baiyue.lastleg.inboundplan.pda.entity.InboundPdaTask;
|
||||
import com.baiyue.lastleg.inboundplan.pda.gateway.WarehouseResourceGateway;
|
||||
import com.baiyue.lastleg.inboundplan.pda.gateway.UnloadPhotoFileGateway;
|
||||
import com.baiyue.lastleg.inboundplan.pda.repository.PdaWorkflowRepository;
|
||||
import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot;
|
||||
import com.baiyue.lastleg.dropship.service.DropshipOrderService;
|
||||
import com.baiyue.lastleg.entity.LastlegOrder;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.MapperFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
@@ -50,6 +53,7 @@ public class InboundPdaWorkflowService {
|
||||
@Resource private InboundStockReceiptService stockReceipts;
|
||||
@Resource private InboundUnloadCompletedOutboxService unloadCompletedOutbox;
|
||||
@Resource private UnloadPhotoFileGateway unloadPhotoFiles;
|
||||
@Resource private DropshipOrderService dropshipOrders;
|
||||
@Resource private ObjectMapper sourceObjectMapper;
|
||||
|
||||
private ObjectMapper objectMapper;
|
||||
@@ -209,6 +213,39 @@ public class InboundPdaWorkflowService {
|
||||
return result;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public PdaResponses.Operation pickWholePallet(String customerId, String planNo, long allocationId,
|
||||
PdaRequests.WholePalletOperation request) {
|
||||
InboundPlan plan = requirePlan(customerId, planNo);
|
||||
requireDropship(plan);
|
||||
InboundPdaOperation replay = replay(customerId, "PICK_WHOLE_PALLET", request.idempotencyKey(), request);
|
||||
if (replay != null) return read(replay, PdaResponses.Operation.class);
|
||||
InboundPdaTask task = requireActiveTask(customerId, plan);
|
||||
InboundPlanPalletAllocation allocation = requireAllocation(plan.getId(), allocationId);
|
||||
InboundPlanLine line = requireLine(plan.getId(), allocation.getLineId());
|
||||
int planned = value(allocation.getPlannedPackageCount());
|
||||
if (planned <= 0) throw conflict("One-click dropship pallet has no planned packages");
|
||||
if (value(allocation.getPickedPackageCount()) != 0) {
|
||||
throw conflict("One-click dropship pallet has already been partially operated");
|
||||
}
|
||||
allocation.setCountedPackageCount(planned);
|
||||
allocation.setPickedPackageCount(planned);
|
||||
allocation.setActualVolumeCbm(volume(allocation.getAllocatedVolumeCbm()));
|
||||
allocation.setStatus("PICKED");
|
||||
bump(allocation);
|
||||
repository.update(allocation);
|
||||
InboundPlanPallet pallet = requirePallet(plan.getId(), allocation.getPalletId());
|
||||
pallet.setActualPackageCount(planned);
|
||||
pallet.setActualVolumeCbm(volume(allocation.getAllocatedVolumeCbm()));
|
||||
repository.update(pallet);
|
||||
startPicking(task, plan, request.operatorId(), request.operatorName());
|
||||
PdaResponses.Operation result = operation("WHOLE_PALLET_PICKED", allocation, line,
|
||||
List.of(), 0, planned, 0);
|
||||
saveOperation(customerId, task.getId(), allocationId, "PICK_WHOLE_PALLET",
|
||||
request.idempotencyKey(), request, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 海外首次扫描箱唛:仅接受本柜国内冻结且已归属本计划的箱。 */
|
||||
@Transactional
|
||||
public PdaResponses.Carton scanCarton(String customerId, String planNo, PdaRequests.ScanCarton request) {
|
||||
@@ -423,7 +460,8 @@ public class InboundPdaWorkflowService {
|
||||
}
|
||||
}
|
||||
List<InboundPlanCarton> cartons = repository.cartons(plan.getId());
|
||||
if (!cartons.isEmpty() && cartons.stream().anyMatch(carton -> !"ON_PALLET".equals(carton.getStatus()))) {
|
||||
if (!isDropship(plan) && !cartons.isEmpty()
|
||||
&& cartons.stream().anyMatch(carton -> !"ON_PALLET".equals(carton.getStatus()))) {
|
||||
throw conflict("All expected carton marks must be scanned onto a pallet before stock-in");
|
||||
}
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
@@ -639,6 +677,9 @@ public class InboundPdaWorkflowService {
|
||||
}
|
||||
|
||||
private void requireLegacyCounting(InboundPlan plan) {
|
||||
if (isDropship(plan)) {
|
||||
throw conflict("One-click dropship inbound must be picked by complete source pallet");
|
||||
}
|
||||
if (repository.hasCartonLabelPool(plan.getId())) {
|
||||
throw conflict("This inbound plan has a carton manifest and must be operated by carton scanning");
|
||||
}
|
||||
@@ -708,20 +749,56 @@ public class InboundPdaWorkflowService {
|
||||
volume(allocation.getActualVolumeCbm()), remaining, requested, merge, candidates);
|
||||
}
|
||||
|
||||
private PdaResponses.Task task(InboundPdaTask task, String planNo) {
|
||||
return task(task, planNo, 0, 0);
|
||||
}
|
||||
|
||||
private PdaResponses.Task taskResponse(InboundPdaTask task, InboundPlan plan, String planNo) {
|
||||
int expected = repository.lines(plan.getId()).stream().mapToInt(line -> value(line.getRequiredPackageCount())).sum();
|
||||
int picked = repository.allocations(plan.getId()).stream().mapToInt(allocation -> value(allocation.getPickedPackageCount())).sum();
|
||||
return task(task, planNo, expected, picked);
|
||||
}
|
||||
|
||||
private PdaResponses.Task task(InboundPdaTask task, String planNo, int expected, int picked) {
|
||||
List<InboundPlanPalletAllocation> allocations = repository.allocations(plan.getId());
|
||||
int picked = allocations.stream().mapToInt(allocation -> value(allocation.getPickedPackageCount())).sum();
|
||||
LastlegOrder order = repository.order(plan.getSourceOrderId());
|
||||
String orderType = order == null ? null : order.getOrderType();
|
||||
boolean wholePalletInbound = "ONE_CLICK_DROPSHIP".equals(orderType);
|
||||
Map<String, InboundPlanPallet> palletsByNo = wholePalletInbound
|
||||
? repository.pallets(plan.getId()).stream().collect(java.util.stream.Collectors.toMap(
|
||||
InboundPlanPallet::getPalletNo, value -> value)) : Map.of();
|
||||
Map<Long, InboundPlanPalletAllocation> allocationsByPallet = wholePalletInbound
|
||||
? allocations.stream().collect(java.util.stream.Collectors.toMap(
|
||||
InboundPlanPalletAllocation::getPalletId, value -> value)) : Map.of();
|
||||
List<PdaResponses.DropshipPallet> dropshipPallets = wholePalletInbound
|
||||
? dropshipOrders.palletSnapshots(task.getCustomerId(), order.getId()).stream()
|
||||
.map(pallet -> dropshipPallet(pallet, palletsByNo, allocationsByPallet)).toList() : List.of();
|
||||
return new PdaResponses.Task(task.getId(), task.getTaskNo(), planNo, task.getStatus(),
|
||||
task.getPushedAt(), task.getStartedAt(), task.getCompletedAt(), unloadPhotoFileIds(task),
|
||||
task.getUnloadPhotoUploadedAt(), task.getEstimatedEmptyReturnAt(), expected, picked);
|
||||
task.getUnloadPhotoUploadedAt(), task.getEstimatedEmptyReturnAt(), expected, picked,
|
||||
orderType, wholePalletInbound, dropshipPallets);
|
||||
}
|
||||
|
||||
private PdaResponses.Task task(InboundPdaTask task, String planNo) {
|
||||
return new PdaResponses.Task(task.getId(), task.getTaskNo(), planNo, task.getStatus(),
|
||||
task.getPushedAt(), task.getStartedAt(), task.getCompletedAt(), unloadPhotoFileIds(task),
|
||||
task.getUnloadPhotoUploadedAt(), task.getEstimatedEmptyReturnAt());
|
||||
}
|
||||
|
||||
private PdaResponses.DropshipPallet dropshipPallet(DropshipPalletSnapshot pallet,
|
||||
Map<String, InboundPlanPallet> palletsByNo,
|
||||
Map<Long, InboundPlanPalletAllocation> allocationsByPallet) {
|
||||
InboundPlanPallet planPallet = palletsByNo.get(pallet.sourcePalletNo());
|
||||
if (planPallet == null) throw conflict("Dropship source pallet is missing from the inbound plan");
|
||||
InboundPlanPalletAllocation allocation = allocationsByPallet.get(planPallet.getId());
|
||||
if (allocation == null) throw conflict("Dropship source pallet has no inbound allocation");
|
||||
return new PdaResponses.DropshipPallet(pallet.sourcePalletNo(), planPallet.getId(), allocation.getId(),
|
||||
pallet.packageCount(), pallet.skus().stream()
|
||||
.map(sku -> new PdaResponses.DropshipSku(sku.skuCode(), sku.productName(),
|
||||
sku.packageCount(), sku.productQuantity())).toList());
|
||||
}
|
||||
|
||||
private void requireDropship(InboundPlan plan) {
|
||||
if (!isDropship(plan)) {
|
||||
throw conflict("Whole-pallet picking is only available for one-click dropship inbound plans");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isDropship(InboundPlan plan) {
|
||||
LastlegOrder order = repository.order(plan.getSourceOrderId());
|
||||
return order != null && "ONE_CLICK_DROPSHIP".equals(order.getOrderType());
|
||||
}
|
||||
|
||||
private List<Long> unloadPhotoFileIds(InboundPdaTask task) {
|
||||
|
||||
+17
-7
@@ -18,6 +18,7 @@ import com.baiyue.lastleg.mapper.LastlegOrderDetailMapper;
|
||||
import com.baiyue.lastleg.mapper.LastlegOrderStatusHistoryMapper;
|
||||
import com.baiyue.lastleg.entity.LastlegOrderStatusHistory;
|
||||
import com.baiyue.lastleg.dropship.service.DropshipOrderService;
|
||||
import com.baiyue.lastleg.dropship.model.DropshipPalletSnapshot;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -93,11 +94,24 @@ public class InboundStockReceiptService {
|
||||
}
|
||||
List<SkuRemainder> skuRemainders = "ONE_CLICK_DROPSHIP".equals(order.getOrderType())
|
||||
? List.of() : skuRemainders(order.getId(), value(line.getRequiredPackageCount()));
|
||||
Map<String, DropshipPalletSnapshot> dropshipPallets = "ONE_CLICK_DROPSHIP".equals(order.getOrderType())
|
||||
? dropshipOrders.palletSnapshots(customerId, order.getId()).stream()
|
||||
.collect(Collectors.toMap(DropshipPalletSnapshot::sourcePalletNo, value -> value))
|
||||
: Map.of();
|
||||
for (InboundPlanPalletAllocation allocation : lineAllocations) {
|
||||
InboundPlanPallet pallet = requirePallet(palletById, allocation);
|
||||
if ("ONE_CLICK_DROPSHIP".equals(order.getOrderType())) {
|
||||
created.add(persist(customerId, plan, task, line, allocation, pallet, order,
|
||||
SkuSnapshot.unspecified(value(allocation.getPickedPackageCount())), receivedAt));
|
||||
if (value(allocation.getPickedPackageCount()) != value(allocation.getPlannedPackageCount())) {
|
||||
throw conflict("One-click dropship inbound must receive a complete source pallet");
|
||||
}
|
||||
DropshipPalletSnapshot sourcePallet = dropshipPallets.get(pallet.getPalletNo());
|
||||
if (sourcePallet == null || sourcePallet.packageCount() != value(allocation.getPickedPackageCount())) {
|
||||
throw conflict("One-click dropship source pallet SKU package total does not match PDA receipt");
|
||||
}
|
||||
for (DropshipPalletSnapshot.Sku sku : sourcePallet.skus()) {
|
||||
created.add(persist(customerId, plan, task, line, allocation, pallet, order,
|
||||
new SkuSnapshot(sku.skuCode(), sku.productName(), null, sku.packageCount()), receivedAt));
|
||||
}
|
||||
} else {
|
||||
long allocationPackages = value(allocation.getPickedPackageCount());
|
||||
for (SkuSnapshot sku : consume(skuRemainders, allocationPackages)) {
|
||||
@@ -279,9 +293,5 @@ public class InboundStockReceiptService {
|
||||
}
|
||||
}
|
||||
|
||||
private record SkuSnapshot(String skuCode, String productNameZh, String productNameEn, long packages) {
|
||||
private static SkuSnapshot unspecified(long packages) {
|
||||
return new SkuSnapshot("UNSPECIFIED", null, null, packages);
|
||||
}
|
||||
}
|
||||
private record SkuSnapshot(String skuCode, String productNameZh, String productNameEn, long packages) {}
|
||||
}
|
||||
|
||||
+92
-11
@@ -30,6 +30,7 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.HashSet;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -62,6 +63,32 @@ public class InboundPlanService {
|
||||
|
||||
@Transactional
|
||||
public InboundPlanResponse convert(String customerId, Long selectedOrderId, List<String> sourcePalletNos) {
|
||||
return convert(customerId, selectedOrderId, sourcePalletNos, null);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public InboundPlanResponse convertDropship(String customerId, Long selectedOrderId,
|
||||
List<WholePalletInput> sourcePallets) {
|
||||
if (sourcePallets == null || sourcePallets.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
|
||||
"One-click dropship inbound requires source pallets");
|
||||
}
|
||||
HashSet<String> palletNos = new HashSet<>();
|
||||
for (WholePalletInput pallet : sourcePallets) {
|
||||
if (pallet == null || trimToNull(pallet.palletNo()) == null || pallet.packageCount() <= 0) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
|
||||
"One-click dropship source pallet number and package count are required");
|
||||
}
|
||||
if (!palletNos.add(pallet.palletNo().trim())) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
|
||||
"Duplicate one-click dropship source pallet number: " + pallet.palletNo());
|
||||
}
|
||||
}
|
||||
return convert(customerId, selectedOrderId, List.of(), List.copyOf(sourcePallets));
|
||||
}
|
||||
|
||||
private InboundPlanResponse convert(String customerId, Long selectedOrderId, List<String> sourcePalletNos,
|
||||
List<WholePalletInput> wholePallets) {
|
||||
List<SourceOrderSnapshot> orders = orderMapper.lockOrder(customerId, selectedOrderId);
|
||||
if (orders == null || orders.isEmpty()) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Inbound order not found");
|
||||
@@ -91,21 +118,27 @@ public class InboundPlanService {
|
||||
Map<Long, SourceOrderSnapshot> orderById = orders.stream()
|
||||
.collect(Collectors.toMap(SourceOrderSnapshot::orderId, value -> value));
|
||||
Map<Long, InboundPlanLine> lineByOrder = createLines(plan, orders);
|
||||
PalletAllocator.Result allocation = PalletAllocator.allocate(orders.stream()
|
||||
PalletAllocator.Result allocation = wholePallets == null ? PalletAllocator.allocate(orders.stream()
|
||||
.map(order -> new PalletAllocator.LineInput(order.orderId(), warehouseKey(order), volume(order),
|
||||
order.packageCount(), order.recommendedPalletCount()))
|
||||
.toList());
|
||||
plan.setPalletCount(allocation.pallets().size());
|
||||
order.packageCount(), order.recommendedPalletCount())).toList()) : null;
|
||||
if (wholePallets != null) validateWholePallets(orders, wholePallets);
|
||||
plan.setPalletCount(wholePallets == null ? allocation.pallets().size() : wholePallets.size());
|
||||
planMapper.insert(plan);
|
||||
|
||||
lineByOrder.values().forEach(lineMapper::insert);
|
||||
Map<PalletAllocator.PalletDraft, InboundPlanPallet> persistedPallets = createPallets(plan, allocation, orderById,
|
||||
sourcePalletNos == null ? List.of() : sourcePalletNos);
|
||||
createAllocations(plan, allocation, lineByOrder, persistedPallets);
|
||||
|
||||
Map<Long, Integer> palletCounts = allocation.allocations().stream().collect(Collectors.groupingBy(
|
||||
PalletAllocator.AllocationDraft::orderId,
|
||||
Collectors.collectingAndThen(Collectors.mapping(item -> item.pallet().sequenceNo(), Collectors.toSet()), java.util.Set::size)));
|
||||
Map<Long, Integer> palletCounts;
|
||||
if (wholePallets == null) {
|
||||
Map<PalletAllocator.PalletDraft, InboundPlanPallet> persistedPallets = createPallets(plan, allocation,
|
||||
orderById, sourcePalletNos == null ? List.of() : sourcePalletNos);
|
||||
createAllocations(plan, allocation, lineByOrder, persistedPallets);
|
||||
palletCounts = allocation.allocations().stream().collect(Collectors.groupingBy(
|
||||
PalletAllocator.AllocationDraft::orderId, Collectors.collectingAndThen(Collectors.mapping(
|
||||
item -> item.pallet().sequenceNo(), Collectors.toSet()), java.util.Set::size)));
|
||||
} else {
|
||||
SourceOrderSnapshot order = orders.get(0);
|
||||
createWholePallets(plan, order, lineByOrder.get(order.orderId()), wholePallets);
|
||||
palletCounts = Map.of(order.orderId(), wholePallets.size());
|
||||
}
|
||||
for (SourceOrderSnapshot order : orders) {
|
||||
int count = palletCounts.getOrDefault(order.orderId(), 0);
|
||||
InboundPlanLine line = lineByOrder.get(order.orderId());
|
||||
@@ -120,6 +153,54 @@ public class InboundPlanService {
|
||||
return detail(customerId, plan);
|
||||
}
|
||||
|
||||
private void validateWholePallets(List<SourceOrderSnapshot> orders, List<WholePalletInput> pallets) {
|
||||
if (orders.size() != 1) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
|
||||
"One-click dropship inbound plan must belong to one inbound order");
|
||||
}
|
||||
int packages = pallets.stream().mapToInt(WholePalletInput::packageCount).sum();
|
||||
if (packages != orders.get(0).packageCount()) {
|
||||
throw new ResponseStatusException(HttpStatus.BAD_REQUEST,
|
||||
"One-click dropship pallet package total must equal inbound order package count");
|
||||
}
|
||||
}
|
||||
|
||||
private void createWholePallets(InboundPlan plan, SourceOrderSnapshot order, InboundPlanLine line,
|
||||
List<WholePalletInput> sourcePallets) {
|
||||
BigDecimal remainingVolume = volume(order);
|
||||
int remainingPackages = order.packageCount();
|
||||
for (int index = 0; index < sourcePallets.size(); index++) {
|
||||
WholePalletInput source = sourcePallets.get(index);
|
||||
BigDecimal palletVolume = index == sourcePallets.size() - 1 ? remainingVolume
|
||||
: volume(order).multiply(BigDecimal.valueOf(source.packageCount()))
|
||||
.divide(BigDecimal.valueOf(order.packageCount()), 4, java.math.RoundingMode.HALF_UP);
|
||||
if (palletVolume.compareTo(remainingVolume) > 0) palletVolume = remainingVolume;
|
||||
InboundPlanPallet pallet = new InboundPlanPallet();
|
||||
pallet.setId(IdWorker.getId()); pallet.setCustomerId(plan.getCustomerId()); pallet.setPlanId(plan.getId());
|
||||
pallet.setPalletNo(source.palletNo().trim()); pallet.setDestinationWarehouseId(order.destinationWarehouseId());
|
||||
pallet.setDestinationWarehouseCode(order.destinationWarehouseCode());
|
||||
pallet.setDestinationWarehouseName(order.destinationWarehouseName());
|
||||
pallet.setCapacityCbm(PalletAllocator.PALLET_CAPACITY_CBM); pallet.setAllocatedVolumeCbm(palletVolume);
|
||||
pallet.setSequenceNo(index + 1); pallet.setAssignmentStatus("UNASSIGNED");
|
||||
pallet.setActualVolumeCbm(BigDecimal.ZERO); pallet.setActualPackageCount(0); palletMapper.insert(pallet);
|
||||
|
||||
InboundPlanPalletAllocation allocation = new InboundPlanPalletAllocation();
|
||||
allocation.setId(IdWorker.getId()); allocation.setCustomerId(plan.getCustomerId()); allocation.setPlanId(plan.getId());
|
||||
allocation.setLineId(line.getId()); allocation.setPalletId(pallet.getId());
|
||||
allocation.setAllocatedVolumeCbm(palletVolume); allocation.setPlannedPackageCount(source.packageCount());
|
||||
allocation.setCountedPackageCount(0); allocation.setPickedPackageCount(0);
|
||||
allocation.setActualVolumeCbm(BigDecimal.ZERO); allocation.setStatus("PENDING_COUNT"); allocation.setVersion(0);
|
||||
allocationMapper.insert(allocation);
|
||||
remainingVolume = remainingVolume.subtract(palletVolume);
|
||||
remainingPackages -= source.packageCount();
|
||||
}
|
||||
if (remainingPackages != 0 || remainingVolume.signum() != 0) {
|
||||
throw new IllegalStateException("Failed to conserve one-click dropship pallet totals");
|
||||
}
|
||||
}
|
||||
|
||||
public record WholePalletInput(String palletNo, int packageCount) {}
|
||||
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public InboundPlanResponse detailByPlanNo(String customerId, String planNo) {
|
||||
|
||||
@@ -207,6 +207,11 @@ public class LoadingCompletedConsumer {
|
||||
catch (Exception exception) { throw new IllegalArgumentException("One-click dropship warehouseId is invalid", exception); }
|
||||
dropshipOrders.receive(new DropshipRequests.Receive(customerId, order.getId(), warehouseId,
|
||||
event.eventId() + ":DROPSHIP", dropshipPallets));
|
||||
InboundPlanResponse plan = inboundPlans.convertDropship(customerId, order.getId(), dropshipPallets.stream()
|
||||
.map(pallet -> new InboundPlanService.WholePalletInput(pallet.palletNo(), pallet.skuPackages().stream()
|
||||
.mapToInt(sku -> Math.toIntExact(sku.packageCount())).sum()))
|
||||
.toList());
|
||||
registerInTransit(event, payload, source, order, plan.planNo());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user