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

View File

@@ -483,6 +483,7 @@ public class PrinterServiceImpl implements PrinterService {
@Override @Override
public Map<String, Object> createOrder(Long memberId, Long scenicId, Integer printerId, Long faceId) { public Map<String, Object> createOrder(Long memberId, Long scenicId, Integer printerId, Long faceId) {
PrinterEntity printer = null;
if (printerId == null) { if (printerId == null) {
List<PrinterResp> printerList = printerMapper.listByScenicId(scenicId); List<PrinterResp> printerList = printerMapper.listByScenicId(scenicId);
if (printerList.size() != 1) { if (printerList.size() != 1) {
@@ -491,7 +492,8 @@ public class PrinterServiceImpl implements PrinterService {
printerId = printerList.getFirst().getId(); printerId = printerList.getFirst().getId();
} }
} else { } else {
PrinterEntity printer = printerMapper.getById(printerId); printer = printerMapper.getById(printerId);
}
if (printer == null) { if (printer == null) {
throw new BaseException("打印机不存在"); throw new BaseException("打印机不存在");
} }
@@ -501,7 +503,6 @@ public class PrinterServiceImpl implements PrinterService {
if (!printer.getScenicId().equals(scenicId)) { if (!printer.getScenicId().equals(scenicId)) {
throw new BaseException("打印机不属于该景区"); throw new BaseException("打印机不属于该景区");
} }
}
// 验证照片数量 // 验证照片数量
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId, faceId); List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId, faceId);
long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum(); long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum();
@@ -511,6 +512,7 @@ public class PrinterServiceImpl implements PrinterService {
OrderEntity order = new OrderEntity(); OrderEntity order = new OrderEntity();
Long orderId = SnowFlakeUtil.getLongId(); Long orderId = SnowFlakeUtil.getLongId();
redisTemplate.opsForValue().set("printer_size:"+orderId, printer.getPreferPaper(), 60, TimeUnit.SECONDS);
order.setId(orderId); order.setId(orderId);
order.setMemberId(memberId); order.setMemberId(memberId);
order.setFaceId(faceId); order.setFaceId(faceId);