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());
}
}
}
});

View File

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