From 5d55aee586c6f5096fd012f7cf98463c34aa1178 Mon Sep 17 00:00:00 2001 From: Timi Date: Wed, 28 Oct 2026 00:35:12 +0800 Subject: [PATCH] cancel sale order with fulfillment and fund record --- .../modules/gao/bean/GaoPermissionCode.java | 2 + .../GaoOrderFulfillmentController.java | 11 ++ .../api/modules/gao/entity/GaoFundRecord.java | 2 +- .../gao/entity/GaoOrderFulfillment.java | 12 +- .../gao/entity/GaoProductStockRecord.java | 3 + .../service/GaoOrderFulfillmentService.java | 10 ++ .../gao/service/GaoOrderPaymentService.java | 6 + .../gao/service/GaoProductStockService.java | 3 + .../GaoOrderFulfillmentServiceImplement.java | 91 +++++++++++++ .../GaoOrderPaymentServiceImplement.java | 28 +++- .../GaoProductStockServiceImplement.java | 8 +- .../GaoSalesOrderServiceImplement.java | 128 ++++++++++++++++-- .../vo/GaoOrderFulfillmentReturnRequest.java | 35 +++++ .../gao/vo/GaoSalesOrderCancelRequest.java | 10 ++ .../modules/system/audit/AuditOperation.java | 1 + ...__support_gao_order_fulfillment_return.sql | 3 + 16 files changed, 336 insertions(+), 17 deletions(-) create mode 100644 src/main/java/com/imyeyu/api/modules/gao/vo/GaoOrderFulfillmentReturnRequest.java create mode 100644 src/main/resources/db/migration/timiserver/V84__support_gao_order_fulfillment_return.sql diff --git a/src/main/java/com/imyeyu/api/modules/gao/bean/GaoPermissionCode.java b/src/main/java/com/imyeyu/api/modules/gao/bean/GaoPermissionCode.java index 4a8bb47..179198c 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/bean/GaoPermissionCode.java +++ b/src/main/java/com/imyeyu/api/modules/gao/bean/GaoPermissionCode.java @@ -99,6 +99,8 @@ public enum GaoPermissionCode implements BuiltinPermissionCode { ORDER_FULFILLMENT_REVOKE("ORDER_FULFILLMENT:REVOKE", "统一履约冲正", true, true), + ORDER_FULFILLMENT_RETURN("ORDER_FULFILLMENT:RETURN", "统一履约退货", true, true), + PACKAGE_CONFIG_CREATE("PACKAGE_CONFIG:CREATE", "套餐配置创建", true, true), PACKAGE_CONFIG_READ("PACKAGE_CONFIG:READ", "套餐配置读取", true, true), diff --git a/src/main/java/com/imyeyu/api/modules/gao/controller/GaoOrderFulfillmentController.java b/src/main/java/com/imyeyu/api/modules/gao/controller/GaoOrderFulfillmentController.java index 5d5227e..1f21e3c 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/controller/GaoOrderFulfillmentController.java +++ b/src/main/java/com/imyeyu/api/modules/gao/controller/GaoOrderFulfillmentController.java @@ -15,6 +15,7 @@ import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentCreateRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentPage; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentPrepareStockRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentRevokeRequest; +import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentReturnRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentCreateRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentCustomerRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentSkuView; @@ -182,6 +183,16 @@ public class GaoOrderFulfillmentController { return result; } + @AOPLog + @RequestRateLimit + @RequireGaoPermission(GaoPermissionCode.ORDER_FULFILLMENT_RETURN) + @PostMapping("/return") + public GaoOrderFulfillment returnGoods(@RequestBody GaoOrderFulfillmentReturnRequest request) { + GaoOrderFulfillment result = service.returnGoods(request); + fillTransient(List.of(result)); + return result; + } + private void fillTransient(List fulfillmentList) { if (fulfillmentList == null || fulfillmentList.isEmpty()) { return; diff --git a/src/main/java/com/imyeyu/api/modules/gao/entity/GaoFundRecord.java b/src/main/java/com/imyeyu/api/modules/gao/entity/GaoFundRecord.java index f4eb139..3ec153f 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/entity/GaoFundRecord.java +++ b/src/main/java/com/imyeyu/api/modules/gao/entity/GaoFundRecord.java @@ -52,7 +52,7 @@ public class GaoFundRecord extends UUIDEntity { /// 店长已拒绝 REJECTED, - /// 提交人已撤销 + /// 核销前已撤销 CANCELLED } diff --git a/src/main/java/com/imyeyu/api/modules/gao/entity/GaoOrderFulfillment.java b/src/main/java/com/imyeyu/api/modules/gao/entity/GaoOrderFulfillment.java index 48f260d..a67c0a5 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/entity/GaoOrderFulfillment.java +++ b/src/main/java/com/imyeyu/api/modules/gao/entity/GaoOrderFulfillment.java @@ -34,11 +34,14 @@ public class GaoOrderFulfillment implements IDEntity, Creatable, Updatab /// 店长已拒绝 REJECTED, - /// 提交人已撤销 + /// 核销前已撤销 CANCELLED, /// 已核销后冲正 - REVOKED + REVOKED, + + /// 已退货 + RETURNED } @Id @@ -66,13 +69,16 @@ public class GaoOrderFulfillment implements IDEntity, Creatable, Updatab /// 实际履约时间 private Long deliveredAt; + /// 退货时间 + private Long returnedAt; + /// 核销或拒绝操作人 private String verifierUserId; /// 核销或拒绝时间 private Long verifiedAt; - /// 冲正单指向原履约单 + /// 退货或冲正单指向原履约单 private String sourceFulfillmentId; /// 拒绝、撤销或冲正原因 diff --git a/src/main/java/com/imyeyu/api/modules/gao/entity/GaoProductStockRecord.java b/src/main/java/com/imyeyu/api/modules/gao/entity/GaoProductStockRecord.java index fa696d9..48894eb 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/entity/GaoProductStockRecord.java +++ b/src/main/java/com/imyeyu/api/modules/gao/entity/GaoProductStockRecord.java @@ -60,6 +60,9 @@ public class GaoProductStockRecord implements IDEntity, Creatable { /// 履约冲正入库 ORDER_REVOKE, + /// 履约退货入库 + ORDER_RETURN_IN, + /// 积分兑换实物出库 POINT_REDEMPTION_OUT, diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/GaoOrderFulfillmentService.java b/src/main/java/com/imyeyu/api/modules/gao/service/GaoOrderFulfillmentService.java index b923030..23f338e 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/GaoOrderFulfillmentService.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/GaoOrderFulfillmentService.java @@ -6,6 +6,7 @@ import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentCreateRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentPage; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentPrepareStockRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentRevokeRequest; +import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentReturnRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentCreateRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentCustomerRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentSkuView; @@ -34,6 +35,12 @@ public interface GaoOrderFulfillmentService extends BaseService revokeForFulfillment(String storeId, String bizId, List items, String requestNo, String operatorUserId); + /// 履约退货入库 + List returnForFulfillment(String storeId, String bizId, List items, String requestNo, String operatorUserId); + /// 为积分兑换实物出库 GaoProductStockRecord outForRedemption(String storeId, String redemptionId, GaoStockChangeItem item, String requestNo, String operatorUserId); diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderFulfillmentServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderFulfillmentServiceImplement.java index eb5dd6b..b8c78dd 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderFulfillmentServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderFulfillmentServiceImplement.java @@ -33,6 +33,7 @@ import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentItemRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentPage; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentPrepareStockRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentRevokeRequest; +import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentReturnRequest; import com.imyeyu.api.modules.gao.vo.GaoProductSkuUnitSelection; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentCreateItemRequest; import com.imyeyu.api.modules.gao.vo.GaoQuickFulfillmentCreateRequest; @@ -168,6 +169,23 @@ public class GaoOrderFulfillmentServiceImplement extends AbstractEntityService listQuickFulfillmentSku(GaoQuickFulfillmentCustomerRequest request) { TimiException.required(request, "快速履约客户查询请求不能为空"); @@ -652,6 +670,79 @@ public class GaoOrderFulfillmentServiceImplement extends AbstractEntityService sourceItemList = itemMapper.selectByFulfillmentId(sourceSnapshot.getId()); + List sourceList = loadPersistedSources(order, sourceItemList); + GaoOrderFulfillment source = lockFulfillment(sourceSnapshot.getId(), order); + storeScopeService.checkStoreId(source.getStoreId()); + TimiException.requiredTrue(source.getStatus() == GaoOrderFulfillment.Status.VERIFIED && source.getSourceFulfillmentId() == null, + "只有原始已核销履约可以退货"); + TimiException.requiredNull(mapper.selectBySourceFulfillmentId(source.getId()), "该履约单已经退货或冲正"); + sourceItemList = itemMapper.selectByFulfillmentIdForUpdate(source.getId()); + TimiException.requiredTrue(!sourceItemList.isEmpty(), "履约单没有明细"); + TimiException.requiredTrue(hasAttachment(request.getAttachmentList()), "退货必须上传照片"); + List stockItems = buildStockItems(order.getStoreId(), sourceList); + if (!stockItems.isEmpty()) { + TimiException.requiredTrue(Boolean.TRUE.equals(request.getReturnedToStore()), "实物履约退货必须确认已退回门店"); + } + String operatorUserId = userLoginService.getRequireLoginUserId(); + Long returnedAt = TimiJava.defaultIfNull(request.getReturnedAt(), Time.now()); + GaoOrderFulfillment returned = newFulfillment(order, GaoOrderFulfillment.Status.RETURNED, + requestNo, operatorUserId, returnedAt, request.getRemark()); + returned.setReturnedAt(returnedAt); + returned.setVerifierUserId(operatorUserId); + returned.setVerifiedAt(Time.now()); + returned.setSourceFulfillmentId(source.getId()); + returned.setReason(reason); + super.create(returned); + Map sourceItemMap = new HashMap<>(); + for (GaoOrderFulfillmentItem sourceItem : sourceItemList) { + sourceItemMap.put(sourceItem.getSourceOrderItemId(), sourceItem); + } + for (SourceEntry sourceEntry : sourceList) { + decreaseFulfilled(sourceEntry); + GaoOrderFulfillmentItem sourceItem = sourceItemMap.get(sourceEntry.sourceId()); + TimiException.required(sourceItem, "履约明细来源订单项不存在"); + GaoOrderFulfillmentItem returnItem = copyFulfillmentItem(sourceItem); + returnItem.setFulfillmentId(returned.getId()); + returnItem.setReservedBaseBefore(sourceEntry.reservedBaseBefore()); + returnItem.setReservedBaseAfter(sourceEntry.reservedBaseBefore()); + returnItem.setFulfilledBaseBefore(sourceEntry.fulfilledBaseBefore()); + returnItem.setFulfilledBaseAfter(sourceEntry.fulfilledBaseBefore().subtract(sourceEntry.baseQuantity())); + itemMapper.insert(returnItem); + } + if (!stockItems.isEmpty()) { + for (GaoStockChangeItem item : stockItems) { + GaoProductStockRecord stockRecord = stockRecordService.getByBizSku(GaoProductStockRecord.BizType.ORDER_FULFILLMENT, + source.getId(), item.getSkuId(), GaoProductStockRecord.OperationType.ORDER_OUT); + TimiException.required(stockRecord, "履约出库流水不存在"); + item.setSourceRecordId(stockRecord.getId()); + item.setSkuSpecSnapshot(stockRecord.getSkuSpecSnapshot()); + } + productStockService.returnForFulfillment(order.getStoreId(), returned.getId(), stockItems, + stockRequestNo(returned, "RETURN"), operatorUserId); + } + attachmentService.updateByBizId(Attachment.BizType.GAO_ORDER_FULFILLMENT, returned.getId(), request.getAttachmentList()); + salesOrderService.refreshFulfillmentStatus(order); + return detail(returned.getId()); + } + private GaoOrderFulfillment newFulfillment(GaoSalesOrder order, GaoOrderFulfillment.Status status, String requestNo, String operatorUserId, Long deliveredAt, String remark) { GaoOrderFulfillment result = new GaoOrderFulfillment(); diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderPaymentServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderPaymentServiceImplement.java index db2e564..2707e2a 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderPaymentServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoOrderPaymentServiceImplement.java @@ -109,9 +109,12 @@ public class GaoOrderPaymentServiceImplement implements GaoOrderPaymentService { @Transactional(TimiServerDBConfig.ROLLBACKER) @Override public GaoFundRecord verify(String fundRecordId) { + GaoFundRecord snapshot = fundRecordService.detail(fundRecordId); + TimiException.requiredTrue(snapshot.getBizType() == GaoFundRecord.BizType.SALES_ORDER, "资金记录不是销售订单资金"); + GaoSalesOrder order = orderService.getForUpdate(snapshot.getBizId()); GaoFundRecord record = lockOrderRecord(fundRecordId); + TimiException.requiredTrue(order.getId().equals(record.getBizId()), "资金流水所属订单已变化"); TimiException.requiredTrue(record.getOperationType() == GaoFundRecord.OperationType.RECEIVE && record.getStatus() == GaoFundRecord.Status.PENDING, "收款记录不是待核销状态"); - GaoSalesOrder order = orderService.getForUpdate(record.getBizId()); Long paidAtBefore = order.getPaidAt(); order.setPendingPaidAmount(safe(order.getPendingPaidAmount()) - record.getAmount()); order.setPaidAmount(safe(order.getPaidAmount()) + record.getAmount()); @@ -144,16 +147,37 @@ public class GaoOrderPaymentServiceImplement implements GaoOrderPaymentService { return releasePending(request, GaoFundRecord.Status.CANCELLED, onlyOperator); } + @Transactional(TimiServerDBConfig.ROLLBACKER) + @Override + public void cancelPendingBySalesOrderId(String salesOrderId, String reason) { + TimiException.required(salesOrderId, "not found salesOrderId"); + String cancelReason = requiredText(reason, "收款撤销原因不能为空"); + for (GaoFundRecord record : fundRecordService.listByBizId(salesOrderId)) { + if (record.getBizType() != GaoFundRecord.BizType.SALES_ORDER + || record.getOperationType() != GaoFundRecord.OperationType.RECEIVE + || record.getStatus() != GaoFundRecord.Status.PENDING) { + continue; + } + GaoOrderPaymentActionRequest request = new GaoOrderPaymentActionRequest(); + request.setFundRecordId(record.getId()); + request.setReason(cancelReason); + releasePending(request, GaoFundRecord.Status.CANCELLED, false); + } + } + private GaoFundRecord releasePending(GaoOrderPaymentActionRequest request, GaoFundRecord.Status targetStatus, boolean onlyOperator) { TimiException.required(request, "not found order payment action request"); TimiException.required(request.getFundRecordId(), "not found fundRecordId"); String reason = requiredText(request.getReason(), "资金操作原因不能为空"); + GaoFundRecord snapshot = fundRecordService.detail(request.getFundRecordId()); + TimiException.requiredTrue(snapshot.getBizType() == GaoFundRecord.BizType.SALES_ORDER, "资金记录不是销售订单资金"); + GaoSalesOrder order = orderService.getForUpdate(snapshot.getBizId()); GaoFundRecord record = lockOrderRecord(request.getFundRecordId()); + TimiException.requiredTrue(order.getId().equals(record.getBizId()), "资金流水所属订单已变化"); TimiException.requiredTrue(record.getOperationType() == GaoFundRecord.OperationType.RECEIVE && record.getStatus() == GaoFundRecord.Status.PENDING, "收款记录不是待核销状态"); if (onlyOperator) { TimiException.requiredTrue(userLoginService.getRequireLoginUserId().equals(record.getOperatorUserId()), "只能撤销自己提交的收款"); } - GaoSalesOrder order = orderService.getForUpdate(record.getBizId()); order.setPendingPaidAmount(safe(order.getPendingPaidAmount()) - record.getAmount()); orderService.recomputePaymentStatus(order); String userId = userLoginService.getRequireLoginUserId(); diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoProductStockServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoProductStockServiceImplement.java index fca9c75..cb02926 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoProductStockServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoProductStockServiceImplement.java @@ -302,6 +302,12 @@ public class GaoProductStockServiceImplement implements GaoProductStockService { return executeInternal(storeId, bizId, items, requestNo, operatorUserId, GaoProductStockRecord.OperationType.ORDER_REVOKE, null); } + @Transactional(TimiServerDBConfig.ROLLBACKER) + @Override + public List returnForFulfillment(String storeId, String bizId, List items, String requestNo, String operatorUserId) { + return executeInternal(storeId, bizId, items, requestNo, operatorUserId, GaoProductStockRecord.OperationType.ORDER_RETURN_IN, null); + } + @Transactional(TimiServerDBConfig.ROLLBACKER) @Override public GaoProductStockRecord outForRedemption(String storeId, String redemptionId, GaoStockChangeItem item, String requestNo, String operatorUserId) { @@ -574,7 +580,7 @@ public class GaoProductStockServiceImplement implements GaoProductStockService { private BigDecimal onHandDelta(GaoProductStockRecord.OperationType operationType, BigDecimal quantity) { return switch (operationType) { - case INITIAL_IN, MANUAL_IN, STOCKTAKE_GAIN, ORDER_REVOKE, POINT_REDEMPTION_RETURN_IN -> quantity; + case INITIAL_IN, MANUAL_IN, STOCKTAKE_GAIN, ORDER_REVOKE, ORDER_RETURN_IN, POINT_REDEMPTION_RETURN_IN -> quantity; case MANUAL_OUT, STOCKTAKE_LOSS, ORDER_OUT, POINT_REDEMPTION_OUT -> quantity.negate(); case ORDER_RESERVE, ORDER_RELEASE -> ZERO; case MANUAL_REVOKE -> throw new TimiException(TimiCode.ERROR, "手工库存冲正必须基于原库存流水"); diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoSalesOrderServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoSalesOrderServiceImplement.java index 566cc88..2ae034d 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoSalesOrderServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoSalesOrderServiceImplement.java @@ -38,6 +38,8 @@ import com.imyeyu.api.modules.gao.vo.GaoCustomerProductPage; import com.imyeyu.api.modules.gao.vo.GaoCustomerProductSummaryView; import com.imyeyu.api.modules.gao.vo.GaoCustomerProductView; import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentRevokeRequest; +import com.imyeyu.api.modules.gao.vo.GaoOrderFulfillmentReturnRequest; +import com.imyeyu.api.modules.gao.vo.GaoOrderPaymentRefundRequest; import com.imyeyu.api.modules.gao.vo.GaoOrderPaymentRevokeRequest; import com.imyeyu.api.modules.gao.vo.GaoProductSkuUnitSelection; import com.imyeyu.api.modules.gao.vo.GaoSalesOrderCancelRequest; @@ -329,24 +331,36 @@ public class GaoSalesOrderServiceImplement extends AbstractEntityService fulfillmentRevokeList = request.getFulfillmentRevokeList(); + List fulfillmentReturnList = request.getFulfillmentReturnList(); Set fulfillmentIdSet = new HashSet<>(); Set selectedFulfillmentIdSet = new HashSet<>(); - if (fulfillmentRevokeList != null && !fulfillmentRevokeList.isEmpty()) { - TimiException.requiredTrue(permissionChecker.hasAny(ModuleCode.GAO, GaoPermissionCode.ORDER_FULFILLMENT_REVOKE.getValue()), "缺少履约冲正权限"); + if ((fulfillmentRevokeList != null && !fulfillmentRevokeList.isEmpty()) + || (fulfillmentReturnList != null && !fulfillmentReturnList.isEmpty())) { for (GaoOrderFulfillment fulfillment : fulfillmentService.listBySalesOrderId(order.getId())) { fulfillmentIdSet.add(fulfillment.getId()); } + } + if (fulfillmentRevokeList != null && !fulfillmentRevokeList.isEmpty()) { + TimiException.requiredTrue(permissionChecker.hasAny(ModuleCode.GAO, GaoPermissionCode.ORDER_FULFILLMENT_REVOKE.getValue()), "缺少履约冲正权限"); for (GaoOrderFulfillmentRevokeRequest revokeRequest : fulfillmentRevokeList) { TimiException.required(revokeRequest, "履约冲正请求不能为空"); TimiException.required(revokeRequest.getFulfillmentId(), "not found fulfillmentId"); TimiException.requiredTrue(fulfillmentIdSet.contains(revokeRequest.getFulfillmentId()), "履约单不属于当前订单"); - TimiException.requiredTrue(selectedFulfillmentIdSet.add(revokeRequest.getFulfillmentId()), "履约单不能重复冲正"); + TimiException.requiredTrue(selectedFulfillmentIdSet.add(revokeRequest.getFulfillmentId()), "同一履约单不能同时退货或冲正"); + } + } + if (fulfillmentReturnList != null && !fulfillmentReturnList.isEmpty()) { + TimiException.requiredTrue(permissionChecker.hasAny(ModuleCode.GAO, GaoPermissionCode.ORDER_FULFILLMENT_RETURN.getValue()), "缺少履约退货权限"); + for (GaoOrderFulfillmentReturnRequest returnRequest : fulfillmentReturnList) { + TimiException.required(returnRequest, "履约退货请求不能为空"); + TimiException.required(returnRequest.getFulfillmentId(), "not found fulfillmentId"); + TimiException.requiredTrue(fulfillmentIdSet.contains(returnRequest.getFulfillmentId()), "履约单不属于当前订单"); + TimiException.requiredTrue(selectedFulfillmentIdSet.add(returnRequest.getFulfillmentId()), "同一履约单不能同时退货或冲正"); } } @@ -365,35 +379,129 @@ public class GaoSalesOrderServiceImplement extends AbstractEntityService fundRefundList = request.getFundRefundList(); + if (fundRefundList != null && !fundRefundList.isEmpty()) { + TimiException.requiredTrue(permissionChecker.hasAny(ModuleCode.GAO, GaoPermissionCode.FUND_REFUND.getValue()), "缺少资金退款权限"); + for (GaoOrderPaymentRefundRequest refundRequest : fundRefundList) { + TimiException.required(refundRequest, "资金退款请求不能为空"); + if (refundRequest.getSalesOrderId() == null || refundRequest.getSalesOrderId().isBlank()) { + refundRequest.setSalesOrderId(order.getId()); + } else { + TimiException.requiredTrue(order.getId().equals(refundRequest.getSalesOrderId()), "退款订单不属于当前订单"); + } + } + } + Set usedTempFileIdSet = new HashSet<>(); + if (fulfillmentReturnList != null) { + for (GaoOrderFulfillmentReturnRequest returnRequest : fulfillmentReturnList) { + if (returnRequest.getReason() == null || returnRequest.getReason().isBlank()) { + returnRequest.setReason(reason); + } + returnRequest.setAttachmentList(prepareCancelActionAttachments(returnRequest.getAttachmentList(), request.getAttachmentList(), usedTempFileIdSet)); + } + } + if (fulfillmentRevokeList != null) { + for (GaoOrderFulfillmentRevokeRequest revokeRequest : fulfillmentRevokeList) { + if (revokeRequest.getReason() == null || revokeRequest.getReason().isBlank()) { + revokeRequest.setReason(reason); + } + revokeRequest.setAttachmentList(prepareCancelActionAttachments(revokeRequest.getAttachmentList(), request.getAttachmentList(), usedTempFileIdSet)); + } + } + if (fundRevokeList != null) { + for (GaoOrderPaymentRevokeRequest revokeRequest : fundRevokeList) { + if (revokeRequest.getReason() == null || revokeRequest.getReason().isBlank()) { + revokeRequest.setReason(reason); + } + revokeRequest.setAttachmentList(prepareCancelActionAttachments(revokeRequest.getAttachmentList(), request.getAttachmentList(), usedTempFileIdSet)); + } + } + if (fundRefundList != null) { + for (GaoOrderPaymentRefundRequest refundRequest : fundRefundList) { + if (refundRequest.getReason() == null || refundRequest.getReason().isBlank()) { + refundRequest.setReason(reason); + } + refundRequest.setAttachmentList(prepareCancelActionAttachments(refundRequest.getAttachmentList(), request.getAttachmentList(), usedTempFileIdSet)); + } + } + + GaoOrderPaymentService paymentService = paymentServiceProvider.getObject(); + if (fulfillmentReturnList != null) { + for (GaoOrderFulfillmentReturnRequest returnRequest : fulfillmentReturnList) { + fulfillmentService.returnGoods(returnRequest); + } + } if (fulfillmentRevokeList != null) { for (GaoOrderFulfillmentRevokeRequest revokeRequest : fulfillmentRevokeList) { fulfillmentService.revoke(revokeRequest); } } if (fundRevokeList != null && !fundRevokeList.isEmpty()) { - GaoOrderPaymentService paymentService = paymentServiceProvider.getObject(); for (GaoOrderPaymentRevokeRequest revokeRequest : fundRevokeList) { paymentService.revoke(revokeRequest); } } + String childCancelReason = "随订单取消:%s".formatted(reason); + fulfillmentService.cancelPendingBySalesOrderId(order.getId(), childCancelReason); + paymentService.cancelPendingBySalesOrderId(order.getId(), childCancelReason); order = getForUpdate(order.getId()); String userId = userLoginService.getRequireLoginUserId(); GaoSalesOrder.RefundStatus refundStatus = safe(order.getPaidAmount()) == 0 ? GaoSalesOrder.RefundStatus.REFUNDED : GaoSalesOrder.RefundStatus.PENDING; Long cancelledAt = Time.now(); - TimiException.requiredTrue(1 == mapper.cancelIf(order.getId(), GaoSalesOrder.Status.ACTIVE, GaoSalesOrder.Status.CANCELLED, + TimiException.requiredTrue(1 == mapper.cancelIf(order.getId(), order.getStatus(), GaoSalesOrder.Status.CANCELLED, cancelledAt, userId, reason, refundStatus), "订单状态已变化"); - // 订单取消立即失去有效消费资格,不等待退款完成 - pointTriggerService.revokeBySalesOrder(order, reason); order.setStatus(GaoSalesOrder.Status.CANCELLED); order.setCancelledAt(cancelledAt); order.setCancelOperatorUserId(userId); order.setCancelReason(reason); order.setRefundStatus(refundStatus); + if (fundRefundList != null) { + for (GaoOrderPaymentRefundRequest refundRequest : fundRefundList) { + paymentService.refund(refundRequest); + } + } + order = getForUpdate(order.getId()); + // 订单取消立即失去有效消费资格,不等待退款完成 + pointTriggerService.revokeBySalesOrder(order, reason); return order; } + /// 准备取消处理附件,在多个处理记录复用同一临时文件时提前复制 + /// + /// @param itemAttachmentList 处理明细附件 + /// @param sharedAttachmentList 共用附件 + /// @param usedTempFileIdSet 已使用的临时文件 ID + /// @return 当前处理记录的附件 + private List prepareCancelActionAttachments(List itemAttachmentList, List sharedAttachmentList, + Set usedTempFileIdSet) { + List sourceList = itemAttachmentList == null || itemAttachmentList.isEmpty() ? sharedAttachmentList : itemAttachmentList; + TimiException.requiredTrue(sourceList != null && !sourceList.isEmpty(), "每条处理记录必须上传专有或共用凭证"); + List result = new ArrayList<>(); + for (Attachment source : sourceList) { + TimiException.required(source, "处理附件不能为空"); + String tempFileId = requiredText(source.getTempFileId(), "处理附件 tempFileId 不能为空"); + Attachment tempFile = attachmentService.get(tempFileId); + TimiException.required(tempFile, "not found temp file"); + TimiException.requiredTrue(tempFile.getBizType() == Attachment.BizType.TEMP_FILE, "处理附件必须是临时文件"); + if (!usedTempFileIdSet.add(tempFileId)) { + Attachment clone = attachmentService.clone(tempFileId); + TimiException.required(clone, "not found cloned temp file"); + TimiException.requiredTrue(clone.getBizType() == Attachment.BizType.TEMP_FILE, "克隆附件必须是临时文件"); + tempFileId = requiredText(clone.getId(), "克隆附件 ID 不能为空"); + TimiException.requiredTrue(usedTempFileIdSet.add(tempFileId), "克隆附件 ID 已被使用"); + } + Attachment attachment = new Attachment(); + attachment.setTempFileId(tempFileId); + attachment.setAttachType(source.getAttachType()); + attachment.setName(source.getName()); + attachment.setMetadata(source.getMetadata()); + result.add(attachment); + } + return result; + } + @Override public void updatePayment(GaoSalesOrder order) { TimiException.required(order, "not found sales order"); diff --git a/src/main/java/com/imyeyu/api/modules/gao/vo/GaoOrderFulfillmentReturnRequest.java b/src/main/java/com/imyeyu/api/modules/gao/vo/GaoOrderFulfillmentReturnRequest.java new file mode 100644 index 0000000..9759e8a --- /dev/null +++ b/src/main/java/com/imyeyu/api/modules/gao/vo/GaoOrderFulfillmentReturnRequest.java @@ -0,0 +1,35 @@ +package com.imyeyu.api.modules.gao.vo; + +import com.imyeyu.api.modules.common.entity.Attachment; +import lombok.Data; + +import java.util.List; + +/// GAO 已核销履约整单退货请求 +/// +/// @author Codex +/// @since 2026-10-27 +@Data +public class GaoOrderFulfillmentReturnRequest { + + /// 原履约单 ID + private String fulfillmentId; + + /// 客户端幂等请求号 + private String requestNo; + + /// 退货时间 + private Long returnedAt; + + /// 退货原因 + private String reason; + + /// true 为实物已退回门店 + private Boolean returnedToStore; + + /// 退货照片 + private List attachmentList; + + /// 备注 + private String remark; +} diff --git a/src/main/java/com/imyeyu/api/modules/gao/vo/GaoSalesOrderCancelRequest.java b/src/main/java/com/imyeyu/api/modules/gao/vo/GaoSalesOrderCancelRequest.java index e1f02f3..223326c 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/vo/GaoSalesOrderCancelRequest.java +++ b/src/main/java/com/imyeyu/api/modules/gao/vo/GaoSalesOrderCancelRequest.java @@ -1,5 +1,6 @@ package com.imyeyu.api.modules.gao.vo; +import com.imyeyu.api.modules.common.entity.Attachment; import lombok.Data; import java.util.List; @@ -20,9 +21,18 @@ public class GaoSalesOrderCancelRequest { /// 幂等请求号 private String requestNo; + /// 冲正明细没有专有附件时使用的共用凭证 + private List attachmentList; + /// 取消时冲正的已核销履约单,按整张履约单冲正 private List fulfillmentRevokeList; + /// 取消时退回的已核销履约单,按整张履约单退货 + private List fulfillmentReturnList; + /// 取消时冲正的资金流水,按整条流水冲正 private List fundRevokeList; + + /// 取消时登记的退款 + private List fundRefundList; } diff --git a/src/main/java/com/imyeyu/api/modules/system/audit/AuditOperation.java b/src/main/java/com/imyeyu/api/modules/system/audit/AuditOperation.java index c81878a..0e0d701 100644 --- a/src/main/java/com/imyeyu/api/modules/system/audit/AuditOperation.java +++ b/src/main/java/com/imyeyu/api/modules/system/audit/AuditOperation.java @@ -30,6 +30,7 @@ public enum AuditOperation { GAO_ORDER_FULFILLMENT_REJECT(Logger.Module.GAO), GAO_ORDER_FULFILLMENT_CANCEL(Logger.Module.GAO), GAO_ORDER_FULFILLMENT_REVOKE(Logger.Module.GAO), + GAO_ORDER_FULFILLMENT_RETURN(Logger.Module.GAO), GAO_PACKAGE_CONFIG_CREATE(Logger.Module.GAO), GAO_PACKAGE_CONFIG_UPDATE(Logger.Module.GAO), GAO_PACKAGE_CONFIG_DELETE(Logger.Module.GAO), diff --git a/src/main/resources/db/migration/timiserver/V84__support_gao_order_fulfillment_return.sql b/src/main/resources/db/migration/timiserver/V84__support_gao_order_fulfillment_return.sql new file mode 100644 index 0000000..e0323f8 --- /dev/null +++ b/src/main/resources/db/migration/timiserver/V84__support_gao_order_fulfillment_return.sql @@ -0,0 +1,3 @@ +ALTER TABLE `gao_order_fulfillment` + ADD COLUMN `returned_at` BIGINT NULL COMMENT '退货时间' AFTER `delivered_at`, + MODIFY COLUMN `source_fulfillment_id` VARCHAR(36) NULL COMMENT '退货或冲正来源履约单 ID';