feat(订单): 添加库存扣减状态字段以优化库存管理

在订单表中添加`is_deduct_stock`字段,用于标记库存是否已扣减,避免重复扣减库存。同时,在退款逻辑中增加对余额退款的支持,确保退款流程的完整性。
This commit is contained in:
dzq 2025-04-23 16:39:40 +08:00
parent 911a52f554
commit 3803ae0446
6 changed files with 35 additions and 6 deletions

View File

@ -65,6 +65,7 @@ public class ReturnApprovalApplicationService {
private final AccessTokenApplicationService accessTokenApplicationService;
private final QyUserService qyUserService;
private final CabinetCellModelFactory cabinetCellModelFactory;
private final QyUserService userService;
/**
* 获取退货审批列表
@ -141,7 +142,8 @@ public class ReturnApprovalApplicationService {
throw new IllegalArgumentException("退款金额不能超过订单总额");
}
if (!Objects.equals(orderModel.getPaymentMethod(), "balance")) {
if (Objects.equals(orderModel.getPaymentMethod(), "wechat")) {
// 微信退款
RefundVO refundVO = null;
try {
refundVO = paymentApplicationService.refund(
@ -155,6 +157,19 @@ public class ReturnApprovalApplicationService {
if (null == refundVO || !refundVO.getSuccess()) {
throw new RuntimeException("退款失败");
}
} else if (Objects.equals(orderModel.getPaymentMethod(), "balance")) {
// 余额退款
try {
QyAccessTokenEntity accessToken = accessTokenApplicationService.getByAppid("QWTONG_YS_WXSHOP", WeixinConstants.corpid);
String userid = QywxApiUtil.convertToUserid(accessToken.getAccessToken(), orderModel.getOpenid()).getUserid();
QyUserEntity qyUser = userService.getUserByUserId(userid, WeixinConstants.corpid);
if (null != qyUser) {
qyUser.setBalance(qyUser.getBalance().add(command.getReturnAmount()));
}
userService.updateById(qyUser);
} catch (Exception e) {
log.error("余额退款失败", e);
}
}
// 审批人信息

View File

@ -151,6 +151,7 @@ public class OrderApplicationService {
orderModel.setIsInternal(command.getIsInternal());
orderModel.setUserid(command.getQyUserid());
orderModel.setName(command.getName());
orderModel.setIsDeductStock(0);
orderModel.insert();
processOrderGoods(orderModel, goodsList);

View File

@ -77,6 +77,10 @@ public class ShopOrderEntity extends BaseEntity<ShopOrderEntity> {
@TableField("pay_status")
private Integer payStatus;
@ApiModelProperty("已扣减库存0否 1是")
@TableField("is_deduct_stock")
private Integer isDeductStock;
@ApiModelProperty("支付方式")
@TableField("payment_method")
private String paymentMethod;

View File

@ -41,6 +41,8 @@ public class ShopOrderDTO {
private Integer status;
@ApiModelProperty("支付状态1未支付 2已支付 3退款中 4已退款")
private Integer payStatus;
@ApiModelProperty("已扣减库存0否 1是")
private Integer isDeductStock;
@ApiModelProperty("支付方式")
private String paymentMethod;
@ApiModelProperty("支付时间")

View File

@ -93,12 +93,16 @@ public class OrderModel extends ShopOrderEntity {
} catch (Exception e) {
log.error("支付时间转换失败", e);
}
boolean isDeductStock = this.getIsDeductStock().equals(1);
this.setIsDeductStock(1);
this.updateById();
orderGoodsService.selectByOrderId(this.getOrderId()).forEach(orderGoods -> {
// 扣减库存
deductGoodsStock(orderGoods.getGoodsId(), orderGoods.getQuantity(), orderGoods.getCellId());
});
if (!isDeductStock) {
orderGoodsService.selectByOrderId(this.getOrderId()).forEach(orderGoods -> {
// 扣减库存
deductGoodsStock(orderGoods.getGoodsId(), orderGoods.getQuantity(), orderGoods.getCellId());
});
}
}
public void handleRefundSuccess() {

View File

@ -24,4 +24,7 @@ CREATE TABLE `cabinet_cell_operation` (
ALTER TABLE `shop_goods`
ADD COLUMN `auto_approval` TINYINT NOT NULL DEFAULT 0 COMMENT '免审批0否 1是' AFTER `status`;
ADD COLUMN `auto_approval` TINYINT NOT NULL DEFAULT 0 COMMENT '免审批0否 1是' AFTER `status`;
ALTER TABLE `shop_order`
ADD COLUMN `is_deduct_stock` TINYINT NOT NULL DEFAULT 0 COMMENT '已扣减库存0否 1是' AFTER `pay_status`;