打印整合修改

This commit is contained in:
2024-09-14 11:49:56 +08:00
parent 5c0adcbe37
commit b74441a62e
6 changed files with 480 additions and 35 deletions

View File

@@ -12,6 +12,7 @@ import com.chaozhanggui.system.cashierservice.model.CategoryInfo;
import com.chaozhanggui.system.cashierservice.model.OrderDetailPO;
import com.chaozhanggui.system.cashierservice.mybatis.MPOrderDetailMapper;
import com.chaozhanggui.system.cashierservice.mybatis.MpPrintMachineMapper;
import com.chaozhanggui.system.cashierservice.rabbit.print.PrinterHandler;
import com.chaozhanggui.system.cashierservice.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
@@ -21,10 +22,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
@@ -63,8 +61,11 @@ public class PrintConsumer {
@Autowired
private MPOrderDetailMapper mPOrderDetailMapper;
public PrintConsumer(RedisTemplate<String, Object> redisTemplate) {
private final PrinterHandler printerHandler;
public PrintConsumer(RedisTemplate<String, Object> redisTemplate, PrinterHandler printerHandler) {
this.redisTemplate = redisTemplate;
this.printerHandler = printerHandler;
}
@RabbitListener(queues = {RabbitConstants.QUEUE_PRINT_DISHES})
@@ -76,24 +77,19 @@ public class PrintConsumer {
Boolean isReturn = jsonObject.getBoolean("isReturn");
TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(orderId);
if (ObjectUtil.isEmpty(orderInfo)) {
log.error("没有对应的订单信息");
Utils.checkValueUnReturn(orderInfo, "订单信息不存在");
List<TbOrderDetail> orderDetails = new ArrayList<>();
for (Object orderDetail : orderDetailIds) {
orderDetails.add(JSONObject.parseObject(orderDetail.toString(), TbOrderDetail.class));
}
if (orderDetails.isEmpty()) {
return;
}
List<TbOrderDetail> orderDetails = mPOrderDetailMapper.selectList(new LambdaQueryWrapper<TbOrderDetail>()
.in(TbOrderDetail::getId, orderDetailIds));
getPrintMachine(Integer.valueOf(orderInfo.getShopId()), "cash").forEach(machine -> {
log.info("打印机信息: {}", machine);
JSONObject config = JSONObject.parseObject(machine.getConfig());
String model = config.getString("model");
if (!"one".equals(model)) {
return;
}
List<CategoryInfo> categoryInfos = JSONUtil.parseJSONStr2TList(config.getJSONArray("categoryList").toString(), CategoryInfo.class);
orderDetails.forEach(item -> {
printDishesTicket(isReturn, machine, item, orderInfo, categoryInfos);
});
printerHandler.handleRequest(machine, isReturn, orderInfo, orderDetails);
});
} catch (Exception e) {
@@ -110,25 +106,15 @@ public class PrintConsumer {
Boolean isReturn = jsonObject.getBoolean("isReturn");
TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(orderId);
if (ObjectUtil.isEmpty(orderInfo)) {
log.error("没有对应的订单信息");
return;
}
Utils.checkValueUnReturn(orderInfo, "订单信息不存在");
TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getShopId()));
if (shopInfo == null) {
log.error("店铺信息不存在");
return;
}
Utils.checkValueUnReturn(shopInfo, "店铺信息不存在");
getPrintMachine(shopInfo.getId(), "cash").forEach(machine -> {
log.info("打印机信息: {}", machine);
JSONObject config = JSONObject.parseObject(machine.getConfig());
String model = config.getString("model");
if (!"normal".equals(model)) {
return;
}
printPlaceTicket(isReturn, machine, orderInfo, shopInfo);
List<TbOrderDetail> tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(orderInfo.getId());
printerHandler.handleRequest(machine, isReturn, orderInfo, tbOrderDetails);
// printPlaceTicket(isReturn, machine, orderInfo, shopInfo);
});
} catch (Exception e) {
@@ -136,6 +122,13 @@ public class PrintConsumer {
}
}
private boolean checkMachineModel(String type, TbPrintMachine machine) {
Utils.checkValueUnReturn(machine, "打印机为null");
JSONObject config = JSONObject.parseObject(machine.getConfig());
String model = config.getString("model");
return type.equals(model);
}
private List<TbPrintMachine> getPrintMachine(Integer shopId, String subType) {
TbShopInfo shopInfo = tbShopInfoMapper.selectByPrimaryKey(shopId);
@@ -211,6 +204,13 @@ public class PrintConsumer {
* 打印结算单
*/
private void printPlaceTicket(boolean isReturn, TbPrintMachine printMachine, TbOrderInfo orderInfo, TbShopInfo shopInfo) {
log.info("打印机信息: {}", printMachine);
JSONObject config = JSONObject.parseObject(printMachine.getConfig());
String model = config.getString("model");
if (!"normal".equals(model)) {
log.info("当前打印机非小票打印机, {}", printMachine);
return;
}
List<TbOrderDetail> tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(orderInfo.getId());
if (!tbOrderDetails.isEmpty()) {

View File

@@ -0,0 +1,40 @@
package com.chaozhanggui.system.cashierservice.rabbit.print;
import com.chaozhanggui.system.cashierservice.dao.TbProductMapper;
import com.chaozhanggui.system.cashierservice.dao.TbProductSkuMapper;
import com.chaozhanggui.system.cashierservice.dao.TbShopUserMapper;
import com.chaozhanggui.system.cashierservice.entity.TbOrderDetail;
import com.chaozhanggui.system.cashierservice.entity.TbOrderInfo;
import com.chaozhanggui.system.cashierservice.entity.TbPrintMachine;
import com.chaozhanggui.system.cashierservice.model.OrderDetailPO;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class FeiPrinter extends PrinterHandler{
public FeiPrinter(TbProductMapper productMapper, TbProductSkuMapper tbProductSkuMapper, TbShopUserMapper tbShopUserMapper) {
super("Feie", productMapper, tbProductSkuMapper, tbShopUserMapper);
}
@Override
protected void returnDishesPrint(TbOrderInfo orderInfo, TbOrderDetail orderDetail, TbPrintMachine machine) {
}
@Override
protected void normalDishesPrint(TbOrderInfo orderInfo, TbOrderDetail orderDetail, TbPrintMachine machine) {
}
@Override
protected void returnOrderPrint(TbOrderInfo orderInfo, TbPrintMachine machine, String balance, List<OrderDetailPO.Detail> detailList) {
}
@Override
protected void normalOrderPrint(TbOrderInfo orderInfo, TbPrintMachine machine, String balance, List<OrderDetailPO.Detail> detailList) {
}
}

View File

@@ -0,0 +1,35 @@
package com.chaozhanggui.system.cashierservice.rabbit.print;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import javax.annotation.PostConstruct;
import java.util.List;
@Configuration
public class PrintConfig {
private final YxyPrinter yxyPrinter;
private final FeiPrinter feiPrinter;
public PrintConfig(YxyPrinter yxyPrinter, FeiPrinter feiPrinter) {
this.yxyPrinter = yxyPrinter;
this.feiPrinter = feiPrinter;
}
// 初始化责任链
@PostConstruct
public void initChain() {
yxyPrinter.setNextPrinter(feiPrinter);
feiPrinter.setNextPrinter(null);
}
@Bean
@Primary
public PrinterHandler printerHandler() {
return yxyPrinter;
}
}

View File

@@ -0,0 +1,153 @@
package com.chaozhanggui.system.cashierservice.rabbit.print;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.chaozhanggui.system.cashierservice.dao.TbProductMapper;
import com.chaozhanggui.system.cashierservice.dao.TbProductSkuMapper;
import com.chaozhanggui.system.cashierservice.dao.TbShopUserMapper;
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.util.DateUtils;
import com.chaozhanggui.system.cashierservice.util.JSONUtil;
import com.chaozhanggui.system.cashierservice.util.PrinterUtils;
import com.chaozhanggui.system.cashierservice.util.Utils;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Slf4j
@ToString
public abstract class PrinterHandler {
@Setter
protected PrinterHandler nextPrinter;
protected String printerBrand;
private final TbProductMapper tbProductMapper;
private final TbProductSkuMapper tbProductSkuMapper;
private final TbShopUserMapper tbShopUserMapper;
public PrinterHandler(String printerBrand, TbProductMapper productMapper, TbProductSkuMapper tbProductSkuMapper, TbShopUserMapper tbShopUserMapper) {
this.printerBrand = printerBrand;
this.tbProductMapper = productMapper;
this.tbProductSkuMapper = tbProductSkuMapper;
this.tbShopUserMapper = tbShopUserMapper;
}
protected void print(TbPrintMachine machine, boolean isReturn, TbOrderInfo orderInfo, List<TbOrderDetail> tbOrderDetailList) {
String configStr = machine.getConfig();
Utils.checkValueUnReturn(configStr, "打印机配置为空");
JSONObject config = JSONObject.parseObject(configStr);
String model = JSONObject.parseObject(configStr).getString("model");
List<CategoryInfo> categoryInfos = JSONUtil.parseJSONStr2TList(config.getJSONArray("categoryList").toString(), CategoryInfo.class);
switch (model) {
case "one":
tbOrderDetailList.forEach(item -> {
log.info("开始打印退单菜品,商品名:{}", item.getProductName());
// 台位费不打印
if (item.getProductId().equals(-999)) {
log.info("台位费商品,不打印");
return;
}
String categoryId = tbProductMapper.selectByPrimaryKey(item.getProductId()).getCategoryId();
TbProductSkuWithBLOBs sku = tbProductSkuMapper.selectByPrimaryKey(item.getProductSkuId());
if (sku == null) {
log.error("商品不存在, id: {}", item.getProductSkuId());
return;
}
long count = categoryInfos.stream().filter(c ->
c.getId().toString().equals(categoryId)
).count();
if (count == 0) {
log.warn("分类未添加菜品: {} : {}", item.getProductName(), sku.getSpecSnap());
return;
}
String remark = StrUtil.isNotBlank(sku.getSpecSnap()) ? sku.getSpecSnap() : "";
item.setRemark(remark);
String data;
String voiceJson;
if (isReturn) {
returnDishesPrint(orderInfo, item, machine);
} else {
normalDishesPrint(orderInfo, item, machine);
}
});
break;
case "normal":
if (tbOrderDetailList.isEmpty()) {
log.info("待打印列表为空");
return;
}
List<OrderDetailPO.Detail> detailList = new ArrayList<>();
tbOrderDetailList.forEach(it -> {
TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(it.getProductSkuId());
String remark = "";
if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) {
remark = tbProductSkuWithBLOBs.getSpecSnap();
}
OrderDetailPO.Detail detail = new OrderDetailPO.Detail(it.getProductName(), it.getNum().toString(), it.getPriceAmount().toPlainString(), remark);
detailList.add(detail);
});
String balance = "0";
if ("deposit".equals(orderInfo.getPayType())) {
TbShopUser user = tbShopUserMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getMemberId()));
if (ObjectUtil.isNotEmpty(user) && ObjectUtil.isNotEmpty(user.getAmount())) {
balance = user.getAmount().toPlainString();
}
}
if (!detailList.isEmpty()) {
if (isReturn) {
returnOrderPrint(orderInfo, machine, balance, detailList);
} else {
normalOrderPrint(orderInfo, machine, balance, detailList);
}
}
default:
log.warn("未知打印类型: {}", model);
}
}
public void handleRequest(TbPrintMachine machine, boolean isReturn, TbOrderInfo orderInfo, List<TbOrderDetail> tbOrderDetailList) {
if (canHandleRequest(machine.getContentType(), machine.getConnectionType())) {
log.info("打印机: {}, 是否退款单: {}, 订单信息: {}, 订单详情信息: {}", machine, isReturn, orderInfo, tbOrderDetailList);
print(machine, isReturn, orderInfo, tbOrderDetailList);
} else if (nextPrinter != null) {
log.info("当前打印机无法处理: {},将请求传递给下一个打印机:{}...", this, nextPrinter);
nextPrinter.handleRequest(machine, isReturn, orderInfo, tbOrderDetailList);
} else {
log.warn("未找到匹配打印机");
}
}
protected abstract void returnDishesPrint(TbOrderInfo orderInfo, TbOrderDetail orderDetail, TbPrintMachine machine);
protected abstract void normalDishesPrint(TbOrderInfo orderInfo, TbOrderDetail orderDetail, TbPrintMachine machine);
protected abstract void returnOrderPrint(TbOrderInfo orderInfo, TbPrintMachine machine, String balance, List<OrderDetailPO.Detail> detailList);
protected abstract void normalOrderPrint(TbOrderInfo orderInfo, TbPrintMachine machine, String balance, List<OrderDetailPO.Detail> detailList);
// 抽象方法,子类实现判断能否处理请求
boolean canHandleRequest(String currentBrand, String connectType) {
log.info("handle判断是否可处理: {}, 连接类型: {}, handler类型: {}", currentBrand, connectType, printerBrand);
Utils.checkValueUnReturn(printerBrand, "打印机品牌未赋值");
return printerBrand.equals(currentBrand) && "network".equals(connectType);
}
}

View File

@@ -0,0 +1,86 @@
package com.chaozhanggui.system.cashierservice.rabbit.print;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.chaozhanggui.system.cashierservice.dao.TbProductMapper;
import com.chaozhanggui.system.cashierservice.dao.TbProductSkuMapper;
import com.chaozhanggui.system.cashierservice.dao.TbShopInfoMapper;
import com.chaozhanggui.system.cashierservice.dao.TbShopUserMapper;
import com.chaozhanggui.system.cashierservice.entity.TbOrderDetail;
import com.chaozhanggui.system.cashierservice.entity.TbOrderInfo;
import com.chaozhanggui.system.cashierservice.entity.TbPrintMachine;
import com.chaozhanggui.system.cashierservice.entity.TbShopInfo;
import com.chaozhanggui.system.cashierservice.model.OrderDetailPO;
import com.chaozhanggui.system.cashierservice.util.DateUtils;
import com.chaozhanggui.system.cashierservice.util.PrinterUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
@Slf4j
@Component
public class YxyPrinter extends PrinterHandler{
private final TbShopInfoMapper shopInfoMapper;
public YxyPrinter(TbShopInfoMapper shopInfoMapper, TbProductMapper productMapper, TbProductSkuMapper productSkuMapper, TbShopUserMapper shopUserMapper) {
super("yxyPrinter", productMapper, productSkuMapper, shopUserMapper);
this.shopInfoMapper = shopInfoMapper;
}
@Override
protected void returnDishesPrint(TbOrderInfo orderInfo, TbOrderDetail orderDetail, TbPrintMachine machine) {
String data = PrinterUtils.getPrintData("return",
StrUtil.isBlank(orderInfo.getTableName()) ? orderInfo.getMasterId() : orderInfo.getTableName(),
DateUtils.getTime(new Date(orderInfo.getCreatedAt())), orderDetail.getProductName(), Math.abs(orderDetail.getNum()), orderDetail.getRemark());
String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔退款订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 3, 1, machine.getAddress(), data);
}
@Override
protected void normalDishesPrint(TbOrderInfo orderInfo, TbOrderDetail orderDetail, TbPrintMachine machine) {
String data = PrinterUtils.getPrintData("", orderInfo.getMasterId(),
DateUtils.getTime(new Date(orderInfo.getCreatedAt())), orderDetail.getProductName(),
orderDetail.getNum(), orderDetail.getRemark());
String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 3, 1, machine.getAddress(), data);
}
@Override
protected void returnOrderPrint(TbOrderInfo orderInfo, TbPrintMachine machine, String balance, List<OrderDetailPO.Detail> detailList) {
TbShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getShopId()));
OrderDetailPO detailPO = new OrderDetailPO(shopInfo.getShopName(), "普通打印",
ObjectUtil.isEmpty(orderInfo.getMasterId()) ||
ObjectUtil.isNull(orderInfo.getMasterId()) ? orderInfo.getTableName() : orderInfo.getMasterId(),
orderInfo.getOrderNo(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())),
"【POS-1】001", orderInfo.getPayAmount().toPlainString(), balance, orderInfo.getPayType(),
"0", detailList, orderInfo.getRemark(), null, null);
String printType = "退款单";
String data = PrinterUtils.getCashPrintData(detailPO, printType, "return");
String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 1, 1, machine.getAddress(), data);
}
@Override
protected void normalOrderPrint(TbOrderInfo orderInfo, TbPrintMachine machine, String balance, List<OrderDetailPO.Detail> detailList) {
TbShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(Integer.valueOf(orderInfo.getShopId()));
OrderDetailPO detailPO = new OrderDetailPO(shopInfo.getShopName(), "普通打印",
orderInfo.getOrderType().equals("miniapp") ? orderInfo.getTableName() : orderInfo.getMasterId(),
orderInfo.getOrderNo(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())),
"【POS-1】001", orderInfo.getOrderAmount().toPlainString(), balance,
(ObjectUtil.isEmpty(orderInfo.getPayType()) || ObjectUtil.isNull(orderInfo.getPayType()) ? "" : orderInfo.getPayType()),
"0", detailList, orderInfo.getRemark(), orderInfo.getDiscountAmount() != null ? orderInfo.getDiscountAmount().toPlainString() : null,
orderInfo.getDiscountRatio() != null ? orderInfo.getDiscountRatio().toPlainString() : null);
detailPO.setOutNumber(orderInfo.getOutNumber());
String printType = "结算单";
String data = PrinterUtils.getCashPrintData(detailPO, printType, orderInfo.getOrderType());
String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}";
PrinterUtils.printTickets(voiceJson, 3, 1, machine.getAddress(), data);
}
}

View File

@@ -0,0 +1,131 @@
package com.chaozhanggui.system.cashierservice.util;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.chaozhanggui.system.cashierservice.exception.MsgException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class Utils {
public static int retryCount = 5;
private static final Logger log = LoggerFactory.getLogger(Utils.class);
public static <T> void catchErrNoReturn(Supplier<T> supplier) {
try {
supplier.get();
}catch (Exception e) {
log.error("执行方法出现异常", e);
}
}
public static <T, R> void runFunAndRetryNoReturn(
Supplier<R> function,
Function<R, Boolean> check, Consumer<R> errFun) {
log.info("工具类开始执行函数");
R result = function.get();
boolean flag = check.apply(result);
log.info("执行结果: {}", result);
while (flag && retryCount-- > 0) {
log.info("执行函数失败, 剩余尝试次数{}", retryCount);
result = function.get();
log.info("执行结果: {}", result);
flag = check.apply(result);
}
if (flag) {
errFun.accept(result);
}
}
public static<T> T runFunAndCheckKey(Supplier<T> supplier, StringRedisTemplate redisTemplate, String lockKey) {
try{
// 创建线程id, 用作判断
String clientId = UUID.randomUUID().toString();
// 设置分布式锁
boolean lock = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.MILLISECONDS));
int count = 0;
while (!lock) {
if (count++ > 100) {
throw new MsgException("系统繁忙, 稍后再试");
}
Thread.sleep(20);
lock = Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(lockKey, clientId, 30, TimeUnit.MILLISECONDS));
}
return supplier.get();
} catch (RuntimeException e){
log.info("执行出错:{}", e.getMessage());
throw e;
} catch (InterruptedException e) {
throw new RuntimeException(e);
} finally{
redisTemplate.delete(lockKey);
}
}
public static <T, R> R runFunAndRetry(
Supplier<R> function,
Function<R, Boolean> check, Consumer<R> errFun) {
log.info("工具类开始执行函数");
R result = function.get();
boolean flag = check.apply(result);
log.info("执行结果: {}", result);
while (flag && retryCount-- > 0) {
log.info("执行函数失败, 剩余尝试次数{}", retryCount);
result = function.get();
log.info("执行结果: {}", result);
flag = check.apply(result);
}
if (flag) {
errFun.accept(result);
}
return result;
}
public static <T> T checkValue(T data, String msg, Func0<T>... errFunc) {
if (ObjectUtil.isEmpty(data)) {
for (Func0<T> tFunc0 : errFunc) {
try {
return tFunc0.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
throw new MsgException(msg);
}
return data;
}
public static void checkValueUnReturn(Object data, String msg, Func0<?>... errFunc) {
if(data == null) {
throw new MsgException(msg);
}
if (data instanceof String && StrUtil.isBlank((String)data)) {
throw new MsgException(msg);
}
if (ObjectUtil.isEmpty(data)) {
for (Func0<?> tFunc0 : errFunc) {
try {
tFunc0.call();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
throw new MsgException(msg);
}
}
}