1.redis序列化导致打票缓存无法正常保存fix

This commit is contained in:
2024-09-03 10:34:19 +08:00
parent 9a9e43ba71
commit c11a3f1df8
6 changed files with 214 additions and 7 deletions

View File

@@ -8,6 +8,7 @@ import com.chaozhanggui.system.cashierservice.dao.*;
import com.chaozhanggui.system.cashierservice.entity.*;
import com.chaozhanggui.system.cashierservice.model.CategoryInfo;
import com.chaozhanggui.system.cashierservice.model.OrderDetailPO;
import com.chaozhanggui.system.cashierservice.redis.RedisCst;
import com.chaozhanggui.system.cashierservice.util.DateUtils;
import com.chaozhanggui.system.cashierservice.util.FeieyunPrintUtil;
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
@@ -16,13 +17,15 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@Slf4j
@Component
@@ -50,6 +53,10 @@ public class PrintMechineConsumer {
@Autowired
private TbOrderDetailMapper tbOrderDetailMapper;
@Autowired
private RedisTemplate<Object, Object> redisTemplate2;
@Autowired
private StringRedisTemplate stringRedisTemplate;
@RabbitHandler
public void listener(String message) {
@@ -188,6 +195,7 @@ public class PrintMechineConsumer {
break;
case "one": //一菜一品
cashierCarts = tbCashierCartMapper.selectByOrderId(orderId,"final");
log.info("一菜一品打印,待打印信息:{}", cashierCarts);
if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) {
@@ -195,8 +203,11 @@ public class PrintMechineConsumer {
// 取餐号不为空为代客下单
if ("postPay".equals(orderInfo.getUseType()) && StrUtil.isNotBlank(it.getMasterId())) {
log.info("--------------------代客下单 打印一菜一品");
printTicket(Integer.valueOf(orderId), categoryInfos, tbPrintMachineWithBLOBs, orderInfo);
return;
}
log.info("--------------------非代客下单 打印一菜一品");
String categoryId;
if(ObjectUtil.isEmpty(it.getCategoryId())){
@@ -232,6 +243,115 @@ public class PrintMechineConsumer {
}
}
private void printTicket(Integer orderId, List<CategoryInfo> categoryInfos, TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs, TbOrderInfo orderInfo) {
String printKey = RedisCst.ORDER_PRINT_PRO + orderId;
AtomicReference<Set<Object>> printProductSet = new AtomicReference<>(redisTemplate2.opsForSet().members(printKey));
List<TbOrderDetail> tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(orderId);
log.info("--------------------订单detail列表: {}", tbOrderDetails);
log.info("--------------------缓存打印printProductSet: {}", printProductSet);
if (!tbOrderDetails.isEmpty()) {
// 重置打印数据
redisTemplate2.delete(printKey);
tbOrderDetails.forEach(it -> {
log.info("开始打印一菜一品票据,:{}", it.getProductName());
String categoryId = tbProductMapper.selectByPrimaryKey(it.getProductId()).getCategoryId();
long count = categoryInfos.stream().filter(c ->
c.getId().toString().equals(categoryId)
).count();
log.info("获取当前类别是否未打印类别:{}", count);
if (count > 0) {
// 统计已打数量
int printerNum = 0;
boolean isReturn = false;
String key = RedisCst.ORDER_PRINT + orderId + ":" + it.getProductId() + ":" + it.getProductSkuId();
String info = stringRedisTemplate.opsForValue().get(key);
stringRedisTemplate.opsForValue().set(key, String.valueOf(it.getNum()), 60 * 60 * 24, TimeUnit.SECONDS);
log.info("--------------------已打印数量: {}", info);
// 删除已打印数据
if (printProductSet.get() != null) {
printProductSet.set(printProductSet.get().stream().filter(r -> {
TbOrderDetail detail = (TbOrderDetail) r;
return !detail.getProductSkuId().equals(it.getProductSkuId()) || !detail.getProductId().equals(it.getProductId());
}).collect(Collectors.toSet()));
}
if (info != null) {
isReturn = it.getNum() - Integer.parseInt(info) < 0;
printerNum = it.getNum() - Integer.parseInt(info);
}else {
printerNum = it.getNum();
}
log.info("--------------------isReturn: {}, 数量: {}", isReturn, printerNum);
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(it.getProductSkuId());
String remark = "";
if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) {
remark = tbProductSkuWithBLOBs.getSpecSnap();
}
// 将已打印信息加入redis
it.setRemark(remark);
redisTemplate2.opsForSet().add(printKey, it);
redisTemplate2.expire(printKey, 24, TimeUnit.HOURS);
// 已打印不再打印
if (info != null && printerNum == 0) {
log.info("--------------------------订单已打印,跳过打印");
return;
}
String data;
String voiceJson;
if (isReturn) {
data = PrinterUtils.getPrintData("return",
StrUtil.isBlank(orderInfo.getTableName()) ? orderInfo.getMasterId() : orderInfo.getTableName(),
DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(), Math.abs(printerNum), remark);
voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔退款订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data);
} else {
data = PrinterUtils.getPrintData("", orderInfo.getMasterId(),
DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(),
printerNum, remark);
voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data);
}
}
});
}
// 已删除的商品打印退款信息
if (printProductSet.get() != null) {
printProductSet.get().forEach(item -> {
log.info("已删除订单,打印退款票据, {}", item);
TbOrderDetail orderDetail = (TbOrderDetail) item;
String data = PrinterUtils.getPrintData("return",
StrUtil.isBlank(orderInfo.getTableName()) ? orderInfo.getMasterId() : orderInfo.getTableName(),
DateUtils.getTime(new Date(orderInfo.getCreatedAt())), orderDetail.getProductName(), orderDetail.getNum(), orderDetail.getRemark());
String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔退款订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data);
String key = RedisCst.ORDER_PRINT + orderId + ":" + orderDetail.getProductId() + ":" + orderDetail.getProductSkuId();
log.info("删除商品数量记录key, {}", key);
stringRedisTemplate.delete(key);
});
}
}