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

@@ -37,6 +37,7 @@ public class TbOrderDetail implements Serializable {
private BigDecimal priceAmount;
private BigDecimal packAmount;
private String remark;
private static final long serialVersionUID = 1L;
}
}

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

View File

@@ -96,4 +96,11 @@ public class RedisConfig {
template.setValueSerializer(new StringRedisSerializer(Charset.forName("UTF-8")));
return template;
}
@Bean
public RedisTemplate<Object, Object> redisTemplate2(RedisConnectionFactory factory) {
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
return template;
}
}

View File

@@ -26,8 +26,10 @@ public class RedisCst {
public static final String CREATE_ORDER_LOCK = "CREATE_ORDER_LOCK:";
public static final String SEND_STOCK_WARN_MSG = "SEND_STOCK_WARN_MSG:";
public static final String SONG_PAY_LOCK = "song_pay_lock:";
public static final String ORDER_PRINT_PRO = "ORDER_PRINT_PRODUCT:";
public static final String ORDER_PRINT = "ORDER_PRINT:";
static String CURRENT_TABLE_ORDER = "CURRENT_TABLE_ORDER:";
static String CURRENT_TABLE_ORDER = "CURRENT_TABLE_ORDER:";
public static String getCurrentOrderKey(String tableId, String shopId) {

View File

@@ -228,7 +228,8 @@ public class CartService {
if (cashierCart.getSkuId().equals(skuId) && cashierCart.getIsVip().intValue() == isVip) {
cashierCart.setTotalNumber(buyNum);
cashierCart.setNumber(buyNum);
if (type == 0 && isVip == 0 && tbProductSkuWithBLOBs.getSuit() != null && tbProductSkuWithBLOBs.getSuit() > 1 && cashierCart.getNumber() < tbProductSkuWithBLOBs.getSuit()) {
if (type == 0 && isVip == 0 && tbProductSkuWithBLOBs.getSuit() != null
&& tbProductSkuWithBLOBs.getSuit() > 1 && cashierCart.getNumber() < tbProductSkuWithBLOBs.getSuit()) {
cashierCartMapper.deleteByPrimaryKey(cashierCart.getId());
continue;
}

View File

@@ -2,6 +2,7 @@ package com.chaozhanggui.system.cashierservice.util;
import cn.hutool.core.util.ObjectUtil;
import com.chaozhanggui.system.cashierservice.model.OrderDetailPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -14,6 +15,7 @@ import java.util.*;
/**
* 打印机
*/
@Slf4j
public class PrinterUtils {
//请求地址
private static final String URL_STR = "https://ioe.car900.com/v1/openApi/dev/customPrint.json";
@@ -83,6 +85,41 @@ public class PrinterUtils {
return builder.toString();
}
/**
* 厨房打印机
*
* @param pickupNumber
* @param date
* @param productName
* @param number
* @param remark
* @return
*/
public static String getPrintData(String type, String pickupNumber, String date, String productName, Integer number, String remark) {
StringBuilder builder = new StringBuilder();
if ("return".equals(type)) {
builder.append("<C><B>" + pickupNumber + "【退】</B></C><BR><BR>");
} else {
builder.append("<C><B>" + pickupNumber + "</B></C><BR><BR>");
}
builder.append("<S><L>时间: " + date + " </L></S><BR><BR><BR>");
if (productName.length() > 4 || remark.length() > 4) {
builder.append("<CS:32>" + productName + " " + number + "</CS><BR>");
builder.append("<CS:32>" + remark + " </CS><BR>");
} else {
builder.append("<B>" + productName + " " + number + "</B><BR>");
builder.append("<B>" + remark + " </B><BR>");
}
builder.append("<OUT:150>");
builder.append("<PCUT>");
return builder.toString();
}
public static String getCashPrintData(OrderDetailPO detailPO,String type){
StringBuilder sb = new StringBuilder();
@@ -162,7 +199,46 @@ public class PrinterUtils {
/**
* 打印票据
*
* @throws Exception
*/
public static void printTickets(String voiceJson, Integer actWay, Integer cn, String devName, String data) {
log.info("开始请求云享印,请求数据:{}, {}", voiceJson, data);
//设备名称
//行为方式 1:只打印数据 2:只播放信息 3:打印数据并播放信息
// actWay = 3;
// //打印联数
// int cn = 1;
//打印内容
//播报语音数据体,字段参考文档IOT_XY_API11001
String time = String.valueOf(System.currentTimeMillis());
String uuid = UUID.randomUUID().toString();
Map<String, String> param = getToken(time, uuid);
//参数
MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("token", param.get("TOKEN"));
multiValueMap.add("devName", devName);
multiValueMap.add("actWay", actWay);
multiValueMap.add("cn", cn);
multiValueMap.add("data", data);
multiValueMap.add("voiceJson", voiceJson);
multiValueMap.add("appId", APP_ID);
multiValueMap.add("timestamp", time);
multiValueMap.add("requestId", uuid);
multiValueMap.add("userCode", USER_CODE);
RestTemplate restTemplate = new RestTemplate();
HttpHeaders header = new HttpHeaders();
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, header);
String httpResponse = restTemplate.postForObject(URL_STR,
httpEntity, String.class);
System.out.println("map" + httpResponse);
}