Compare commits

..

2 Commits

Author SHA1 Message Date
huk
887ab084e9 feat(order): 添加订单支付未核销自动退款功能
- 新增延迟队列常量 DELAY_ORDER_TO_BE_USED 用于处理已支付未核销订单
- 调整订单取消时间单位从秒改为分钟,值设为15分钟
- 新增订单支付未核销自动退款时间常量,设置为14天
- 实现订单支付后加入延迟队列逻辑,超时未核销自动退款
- 订阅延迟队列处理已支付未核销订单状态更新为已退款
- 异步处理订单退款逻辑,避免阻塞主流程
2025-09-29 11:20:48 +08:00
huk
ee76a4cfda feat(goods): 添加商品分类校验与层级管理功能- 在 ProductCategory 实体类中增加名称和图标的非空校验注解
-为控制器启用参数校验支持,确保请求数据合法性
- 实现分类保存和更新方法,自动设置分类层级并检查重复性
- 引入断言工具防止重复分类插入或更新
- 增加对父级分类不存在情况的默认值处理逻辑
2025-09-29 11:20:40 +08:00
8 changed files with 80 additions and 4 deletions

View File

@ -85,4 +85,10 @@ public interface CacheNames {
*/ */
String DELAY_ORDER_UNPAY = GlobalConstants.GLOBAL_REDIS_KEY + "order_unpay"; String DELAY_ORDER_UNPAY = GlobalConstants.GLOBAL_REDIS_KEY + "order_unpay";
/**
* 延时队列订单支付未核销
*/
String DELAY_ORDER_TO_BE_USED = GlobalConstants.GLOBAL_REDIS_KEY + "order_tobe_used";
} }

View File

@ -73,9 +73,14 @@ public interface Constants {
Long TOP_PARENT_ID = 0L; Long TOP_PARENT_ID = 0L;
/** /**
* 订单未支付自动取消时间 * 订单未支付自动取消时间分钟
*/ */
Long ORDER_CANCEL_TIME = 900L; Long ORDER_CANCEL_TIME = 15L;
/**
* 订单支付未核销自动退款时间
*/
Long ORDER_REFUND_TIME = 14L;
} }

View File

@ -14,6 +14,7 @@ import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log; import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType; import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController; import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@ -29,6 +30,7 @@ import java.util.List;
@RestController @RestController
@RequestMapping("/pms/product/category") @RequestMapping("/pms/product/category")
@RequiredArgsConstructor @RequiredArgsConstructor
@Validated
public class ProductCategoryController extends BaseController { public class ProductCategoryController extends BaseController {
private final ProductCategoryService service; private final ProductCategoryService service;

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data; import lombok.Data;
import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.JdbcType;
import org.dromara.common.core.domain.model.BaseAudit; import org.dromara.common.core.domain.model.BaseAudit;
@ -30,6 +31,7 @@ public class ProductCategory extends BaseAudit {
@Schema(description = "NAME") @Schema(description = "NAME")
@Excel(name = "NAME") @Excel(name = "NAME")
@NotBlank(message = "名称不能为空")
private String name; private String name;
@Schema(description = "分类级别0->1级1->2级") @Schema(description = "分类级别0->1级1->2级")
@ -46,6 +48,7 @@ public class ProductCategory extends BaseAudit {
@Schema(description = "图标") @Schema(description = "图标")
@Excel(name = "图标") @Excel(name = "图标")
@NotBlank(message = "图标不能为空")
private String icon; private String icon;
@Schema(description = "删除标志0代表存在1代表删除") @Schema(description = "删除标志0代表存在1代表删除")

View File

@ -8,5 +8,10 @@ import com.wzj.soopin.goods.domain.vo.ProductCategoryVO;
import java.util.List; import java.util.List;
public interface ProductCategoryService extends IService<ProductCategory> { public interface ProductCategoryService extends IService<ProductCategory> {
boolean save(ProductCategory productCategory);
boolean updateById(ProductCategory productCategory);
List<ProductCategoryVO> tree(Wrapper<ProductCategory> wrapper); List<ProductCategoryVO> tree(Wrapper<ProductCategory> wrapper);
} }

View File

@ -1,6 +1,8 @@
package com.wzj.soopin.goods.service.impl; package com.wzj.soopin.goods.service.impl;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzj.soopin.goods.convert.ProductCategoryConvert; import com.wzj.soopin.goods.convert.ProductCategoryConvert;
import com.wzj.soopin.goods.domain.entity.ProductCategory; import com.wzj.soopin.goods.domain.entity.ProductCategory;
@ -8,6 +10,7 @@ import com.wzj.soopin.goods.domain.vo.ProductCategoryVO;
import com.wzj.soopin.goods.mapper.ProductCategoryMapper; import com.wzj.soopin.goods.mapper.ProductCategoryMapper;
import com.wzj.soopin.goods.service.ProductCategoryService; import com.wzj.soopin.goods.service.ProductCategoryService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.dromara.common.core.exception.ServiceException;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -28,6 +31,32 @@ public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMappe
private final ProductCategoryConvert convert; private final ProductCategoryConvert convert;
@Override
public boolean save(ProductCategory productCategory) {
if (productCategory.getParentId() == null || productCategory.getParentId() == 0) {
productCategory.setLevel(1);
productCategory.setParentId(0L);
} else {
ProductCategory parent = this.getById(productCategory.getParentId());
productCategory.setLevel(parent.getLevel() + 1);
}
boolean exists = this.exists(Wrappers.lambdaQuery(ProductCategory.class)
.eq(ProductCategory::getName, productCategory.getName())
.eq(ProductCategory::getParentId, productCategory.getParentId()));
Assert.isFalse(exists, () -> ServiceException.of("分类已存在"));
return super.save(productCategory);
}
@Override
public boolean updateById(ProductCategory productCategory) {
boolean exists = this.exists(Wrappers.lambdaQuery(ProductCategory.class)
.ne(ProductCategory::getId, productCategory.getId())
.eq(ProductCategory::getName, productCategory.getName())
.eq(ProductCategory::getParentId, productCategory.getParentId()));
Assert.isFalse(exists, () -> ServiceException.of("分类已存在"));
return super.updateById(productCategory);
}
@Override @Override
public List<ProductCategoryVO> tree(Wrapper<ProductCategory> wrapper) { public List<ProductCategoryVO> tree(Wrapper<ProductCategory> wrapper) {
List<ProductCategory> list = this.list(wrapper); List<ProductCategory> list = this.list(wrapper);

View File

@ -64,8 +64,10 @@ import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.dromara.common.core.constant.CacheNames.DELAY_ORDER_TO_BE_USED;
import static org.dromara.common.core.constant.CacheNames.DELAY_ORDER_UNPAY; import static org.dromara.common.core.constant.CacheNames.DELAY_ORDER_UNPAY;
import static org.dromara.common.core.constant.Constants.ORDER_CANCEL_TIME; import static org.dromara.common.core.constant.Constants.ORDER_CANCEL_TIME;
import static org.dromara.common.core.constant.Constants.ORDER_REFUND_TIME;
/** /**
@ -281,7 +283,7 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
}).toList(); }).toList();
orderItemMapper.insert(orderItemList); orderItemMapper.insert(orderItemList);
// 添加订单到延迟队列 // 添加订单到延迟队列
QueueUtils.addDelayedQueueObject(DELAY_ORDER_UNPAY, order.getId(), ORDER_CANCEL_TIME, TimeUnit.SECONDS); QueueUtils.addDelayedQueueObject(DELAY_ORDER_UNPAY, order.getId(), ORDER_CANCEL_TIME, TimeUnit.MINUTES);
return BeanUtil.copyProperties(order, OrderVO.class); return BeanUtil.copyProperties(order, OrderVO.class);
} }
@ -565,7 +567,8 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements
.paymentTime(LocalDateTime.now()) .paymentTime(LocalDateTime.now())
.payType(payType) .payType(payType)
.build()); .build());
// 添加已支付订单到延迟队列14天未使用自动退款
QueueUtils.addDelayedQueueObject(DELAY_ORDER_TO_BE_USED, order.getId(), ORDER_REFUND_TIME, TimeUnit.DAYS);
//发出消息 //发出消息
MqUtil.sendIMMessage(buildMQMessage(order)); MqUtil.sendIMMessage(buildMQMessage(order));
} }

View File

@ -14,6 +14,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.annotation.PostConstruct;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import static org.dromara.common.core.constant.CacheNames.DELAY_ORDER_TO_BE_USED;
import static org.dromara.common.core.constant.CacheNames.DELAY_ORDER_UNPAY; import static org.dromara.common.core.constant.CacheNames.DELAY_ORDER_UNPAY;
/** /**
@ -65,4 +66,26 @@ public class OrderScheduledTask {
}); });
}, true); }, true);
} }
/**
* 订阅redis延迟队列退款已支付未核销的订单
*/
@PostConstruct
public void orderRefundByDelayedQueue() {
log.info("订单已支付未核销延时队列: {} 监听中......",DELAY_ORDER_TO_BE_USED);
// 项目初始化设置一次即可
QueueUtils.subscribeBlockingQueue(DELAY_ORDER_TO_BE_USED, (Long orderId) -> {
// 观察接收时间
log.info("订单未支付延时队列: {}, 收到订单id: {}", DELAY_ORDER_TO_BE_USED, orderId);
return CompletableFuture.runAsync(() -> {
// 异步处理数据逻辑 不要在上方处理业务逻辑
orderService.update(Wrappers.lambdaUpdate(Order.class)
.eq(Order::getId, orderId)
.eq(Order::getStatus, OrderStatusEnum.VERIFY.getValue())
.set(Order::getStatus, OrderStatusEnum.REFUNDED.getValue()));
// TODO退款
});
}, true);
}
} }