feat(printer):优化订单打印纸张尺寸获取逻辑

- 引入Redis缓存存储打印纸张尺寸信息
- 优先从Redis中获取纸张尺寸,减少数据库查询
- 在创建订单时将打印机偏好纸张存入Redis,有效期60秒
- 修复打印机对象作用域问题,避免空指针异常
- 统一打印机状态校验逻辑,提高代码可读性
This commit is contained in:
2025-11-08 11:04:22 +08:00
parent eda4ed2955
commit fb75cbf230
2 changed files with 23 additions and 13 deletions

View File

@@ -70,6 +70,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -140,6 +141,8 @@ public class OrderServiceImpl implements OrderService {
private ICouponService iCouponService;
@Autowired
private MemberRelationRepository memberRelationRepository;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Override
public ApiResponse<PageInfo<OrderRespVO>> pageQuery(OrderReqQuery query) {
@@ -583,9 +586,14 @@ public class OrderServiceImpl implements OrderService {
if (orderAppRespVO != null && orderAppRespVO.getOrderItemList() != null && !orderAppRespVO.getOrderItemList().isEmpty()) {
orderAppRespVO.getOrderItemList().forEach(orderItem -> {
if (orderItem.getGoodsType() == 3) {
PrintTaskResp printTaskResp = printerMapper.queryTaskByMpId(Math.toIntExact(orderItem.getGoodsId()));
if (printTaskResp != null) {
orderItem.setPrinterPaper(printTaskResp.getPaper());
String size = redisTemplate.opsForValue().get("printer_size:" + id);
if (size != null) {
orderItem.setPrinterPaper(size);
} else {
PrintTaskResp printTaskResp = printerMapper.queryTaskByMpId(Math.toIntExact(orderItem.getGoodsId()));
if (printTaskResp != null) {
orderItem.setPrinterPaper(printTaskResp.getPaper());
}
}
}
});