diff --git a/cash-api/order-server/src/main/java/com/czg/mq/PrintMqListener.java b/cash-api/order-server/src/main/java/com/czg/mq/PrintMqListener.java index 41fa4c1e2..c5c3cc4f1 100644 --- a/cash-api/order-server/src/main/java/com/czg/mq/PrintMqListener.java +++ b/cash-api/order-server/src/main/java/com/czg/mq/PrintMqListener.java @@ -7,11 +7,11 @@ import com.czg.config.RedisCst; import com.czg.order.entity.MqLog; import com.czg.order.service.MqLogService; import com.czg.service.RedisService; +import com.czg.service.order.print.PrintConfig; import com.czg.service.order.print.PrinterHandler; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitListener; -import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import java.util.function.Consumer; @@ -28,9 +28,8 @@ public class PrintMqListener { private MqLogService mqLogService; @Resource private RedisService redisService; - @Lazy @Resource - private PrinterHandler printerHandler; + private PrintConfig printConfig; private void invokeFun(String queue, String type, String plat, T data, Consumer consumer) { long startTime = DateUtil.date().getTime(); @@ -61,11 +60,11 @@ public class PrintMqListener { Boolean printOrder = jsonObject.getBoolean("printOrder"); redisService.runFunAndCheckKey(() -> { if (printOrder) { - printerHandler.orderHandler(orderId, PrinterHandler.PrintTypeEnum.ORDER, null); + printConfig.orderHandler(orderId, PrinterHandler.PrintTypeEnum.ORDER, null); } //菜品打印 全是后端 - printerHandler.orderHandler(orderId, PrinterHandler.PrintTypeEnum.ONLY_KITCHEN, null); - printerHandler.orderHandler(orderId, PrinterHandler.PrintTypeEnum.ALL_KITCHEN, null); + printConfig.orderHandler(orderId, PrinterHandler.PrintTypeEnum.ONLY_KITCHEN, null); + printConfig.orderHandler(orderId, PrinterHandler.PrintTypeEnum.ALL_KITCHEN, null); return null; }, RedisCst.getLockKey("orderPrint", orderId)); }); @@ -75,6 +74,6 @@ public class PrintMqListener { @RabbitListener(queues = {"${spring.profiles.active}-" + RabbitConstants.Queue.OTHER_PRINT_QUEUE}) public void otherPrint(Long shopId, Object o, String printTypeEnum) { PrinterHandler.PrintTypeEnum typeEnum = PrinterHandler.PrintTypeEnum.valueOf(printTypeEnum); - printerHandler.otherHandler(shopId, o, typeEnum); + printConfig.otherHandler(shopId, o, typeEnum); } } diff --git a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/PrintMachineServiceImpl.java b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/PrintMachineServiceImpl.java index 159fdc152..8e1f6f26b 100644 --- a/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/PrintMachineServiceImpl.java +++ b/cash-service/account-service/src/main/java/com/czg/service/account/service/impl/PrintMachineServiceImpl.java @@ -22,6 +22,7 @@ public class PrintMachineServiceImpl extends ServiceImpl paramMap = new HashMap<>(); paramMap.put("user", USER); diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/print/PrintConfig.java b/cash-service/order-service/src/main/java/com/czg/service/order/print/PrintConfig.java index 15bce48e3..a828861b5 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/print/PrintConfig.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/print/PrintConfig.java @@ -1,14 +1,39 @@ package com.czg.service.order.print; +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.fastjson2.JSONObject; +import com.czg.account.dto.HandoverRecordDTO; +import com.czg.account.entity.CallQueue; +import com.czg.account.entity.PrintMachine; +import com.czg.account.entity.ShopInfo; +import com.czg.account.service.PrintMachineService; +import com.czg.account.service.ShopInfoService; +import com.czg.config.RedisCst; import com.czg.exception.CzgException; +import com.czg.market.service.OrderInfoService; +import com.czg.order.entity.OrderDetail; +import com.czg.order.entity.OrderInfo; +import com.czg.order.service.OrderDetailService; +import com.czg.print.*; +import com.czg.product.entity.Product; +import com.czg.product.service.ProductService; +import com.czg.service.RedisService; +import com.czg.service.order.enums.OrderStatusEnums; +import com.czg.utils.FunUtils; +import com.mybatisflex.core.query.QueryWrapper; +import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; +import org.apache.dubbo.config.annotation.DubboReference; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; -import javax.annotation.PostConstruct; -import javax.annotation.Resource; -import java.util.Map; +import java.math.BigDecimal; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; /** @@ -17,28 +42,55 @@ import java.util.concurrent.ConcurrentHashMap; * @author Administrator */ @Slf4j -@Configuration -public class PrintConfig { +@Component +public class PrintConfig implements ApplicationRunner { @Resource private ApplicationContext applicationContext; + @Resource + protected OrderDetailService orderDetailService; + @Resource + protected OrderInfoService orderInfoService; + @DubboReference + private PrintMachineService printMachineService; + @DubboReference + protected ShopInfoService shopInfoService; + @DubboReference + protected ProductService productService; + @Resource + protected RedisService redisService; + public static final Map PRINTER_MAP = new ConcurrentHashMap<>(); + + // @PostConstruct // 直接使用实例变量,不需要静态 - private Map printerMap; + // private Map printerMap; + // public void init() { +// Map printers = applicationContext.getBeansOfType(PrinterHandler.class); +// printerMap = new ConcurrentHashMap<>(); +// +// printers.forEach((beanName, printer) -> { +// String brand = printer.printerBrand; +// if (brand != null && !brand.trim().isEmpty()) { +// printerMap.put(brand, printer); +// } +// }); +// +// log.info("已注册打印机: {}", printerMap.keySet()); +// } + @Override + public void run(ApplicationArguments args) throws Exception { + // 🔥 这里是 Spring 容器完全启动成功后才执行! + // 🔥 所有 feiPrinter、yxyPrinter 都已经加载好了! - @PostConstruct - public void init() { - Map printers = applicationContext.getBeansOfType(PrinterHandler.class); - printerMap = new ConcurrentHashMap<>(); + Map beans = applicationContext.getBeansOfType(PrinterHandler.class); - printers.forEach((beanName, printer) -> { - String brand = printer.printerBrand; - if (brand != null && !brand.trim().isEmpty()) { - printerMap.put(brand, printer); - } - }); - - log.info("已注册打印机: {}", printerMap.keySet()); + for (Map.Entry entry : beans.entrySet()) { + PrinterHandler handler = entry.getValue(); + String brand = handler.printerBrand; + PRINTER_MAP.put(brand, handler); + } + log.info("已注册打印机品牌:{}", PRINTER_MAP.keySet()); } @@ -46,11 +98,265 @@ public class PrintConfig { * 获取打印机 */ public PrinterHandler getPrinter(String key) { - PrinterHandler printer = printerMap.get(key); + PrinterHandler printer = PRINTER_MAP.get(key); if (printer == null) { throw new CzgException("未找到打印机: " + key); } return printer; } + + /** + * 处理订单打印 + * + * @param data 传递的数据 + * @param printTypeEnum GUEST_ORDER ORDER PRE_ORDER ONLY_KITCHEN ALL_KITCHEN + */ + public void orderHandler(String data, PrinterHandler.PrintTypeEnum printTypeEnum, Integer placeNum) { + OrderInfo orderInfo = orderInfoService.getById(data); + if (orderInfo == null) { + throw new RuntimeException("订单打印失败,订单不存在"); + } + List orderDetailList = new ArrayList<>(); + orderDetailList = switch (printTypeEnum) { + case PrinterHandler.PrintTypeEnum.GUEST_ORDER -> { + orderInfo.setPlaceNum(placeNum); + yield orderDetailService.list(new QueryWrapper().eq(OrderDetail::getOrderId, orderInfo.getId()).eq(OrderDetail::getPlaceNum, placeNum)); + } + case PrinterHandler.PrintTypeEnum.ORDER, PrinterHandler.PrintTypeEnum.PRE_ORDER, + PrinterHandler.PrintTypeEnum.ONLY_KITCHEN, PrinterHandler.PrintTypeEnum.ALL_KITCHEN -> + orderDetailService.list(new QueryWrapper().eq(OrderDetail::getOrderId, orderInfo.getId())); + default -> orderDetailList; + }; + for (PrintMachine machine : getPrintMachine(orderInfo.getShopId(), printTypeEnum)) { + PrinterHandler printer = getPrinter(machine.getBrand()); + redisService.set("order:print:" + orderInfo.getId(), "", 180); + switch (printTypeEnum) { + case PrinterHandler.PrintTypeEnum.GUEST_ORDER: + log.info("准备开始打印客看订单"); + printer.guestOrderPrint(orderInfo, machine, orderDetailList); + break; + case PrinterHandler.PrintTypeEnum.ORDER: + log.info("准备开始打印订单"); + printer.orderPrintBefore(machine, orderInfo, orderDetailList); + + break; + case PrinterHandler.PrintTypeEnum.PRE_ORDER: + log.info("准备开始打印预结算订单"); + printer.preOrderPrintBefore(machine, orderInfo, orderDetailList); + break; + case PrinterHandler.PrintTypeEnum.ONLY_KITCHEN: + // 判断订单是否是先付费或者已结算 + if (!"after-pay".equals(orderInfo.getPayMode()) && (OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus()) || OrderStatusEnums.CANCELLED.getCode().equals(orderInfo.getStatus()))) { + log.warn("此订单未支付, 订单信息: {}", orderInfo.getId()); + } + List tbOrderDetailList = getCanPrintOrderDetails(machine, orderInfo.getId(), orderDetailList); + if (CollUtil.isEmpty(tbOrderDetailList)) { + log.info("此订单无打印菜品, 订单信息: {}", orderInfo.getId()); + break; + } + log.info("准备开始打印菜品单"); + //后厨单菜品 + tbOrderDetailList.forEach(item -> { + Boolean b = redisService.hasKey(RedisCst.kitchenNormal(orderInfo.getId(), machine.getId(), item.getId())); + if (!b) { + FunUtils.safeRunVoid(() -> printer.onlyKitchenPrint(orderInfo, item, machine), + "订单id:{} ,后厨分单打印失败", orderInfo.getId()); + // 保存已打印信息 + redisService.set(RedisCst.kitchenNormal(orderInfo.getId(), machine.getId(), item.getId()), "", 180); + } + }); + break; + case PrinterHandler.PrintTypeEnum.ALL_KITCHEN: + Boolean exit = redisService.hasKey(RedisCst.kitchenAll(orderInfo.getId(), machine.getId())); + if (exit) { + break; + } + // 判断订单是否是先付费或者已结算 + if (!"after-pay".equals(orderInfo.getPayMode()) && (OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus()) || OrderStatusEnums.CANCELLED.getCode().equals(orderInfo.getStatus()))) { + log.warn("此订单未支付, 订单信息: {}", orderInfo.getId()); + } + List allOrderDetailList = getCanPrintOrderDetails(machine, orderInfo.getId(), orderDetailList); + if (CollUtil.isEmpty(allOrderDetailList)) { + log.info("此订单无打印菜品, 订单信息: {}", orderInfo.getId()); + break; + } + log.info("准备开始打印后厨整单"); + printer.allKitchenPrint(orderInfo, allOrderDetailList, machine); + redisService.set(RedisCst.kitchenAll(orderInfo.getId(), machine.getId()), "", 180); + break; + } + } + } + + /** + * 处理其它打印 + * + * @param data 传递的数据 + * @param printTypeEnum IN_STOCK DAY_REPORT PRODUCT_REPORT RECHARGE STOCK_CHECK + */ + public void otherHandler(Long shopId, Object data, PrinterHandler.PrintTypeEnum printTypeEnum) { + if (shopId == null || Objects.isNull(data)) { + log.info("otherHandler 打印数据为空, shopId: {}, data: {}", shopId, data); + return; + } + ShopInfo shopInfo = shopInfoService.getById(shopId); + switch (printTypeEnum) { + case PrinterHandler.PrintTypeEnum.STOCK: + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()) + .stockPrint(machine, shopInfo.getShopName(), (StockPrintDTO) data)); + break; + case PrinterHandler.PrintTypeEnum.DAY_REPORT: + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()) + .dayReportPrint(machine, shopInfo.getShopName(), (DayReportPrintDTO) data)); + break; + case PrinterHandler.PrintTypeEnum.PRODUCT_REPORT: + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()) + .productReportPrint(machine, shopInfo.getShopName(), (ProductReportPrintDTO) data)); + break; + case PrinterHandler.PrintTypeEnum.RECHARGE: + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()) + .rechargePrint(machine, shopInfo.getShopName(), (RechargePrintDTO) data)); + break; + case PrinterHandler.PrintTypeEnum.STOCK_CHECK: + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()) + .stockCheckPrint(machine, shopInfo.getShopName(), (StockCheckPrintDTO) data)); + break; + case PrinterHandler.PrintTypeEnum.HANDOVER: + log.info("准备开始打印交班"); + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()).handoverPrint(machine, shopInfo.getShopName(), (HandoverRecordDTO) data)); + break; + case PrinterHandler.PrintTypeEnum.CALL: + log.info("准备开始打印叫号单"); + getPrintMachine(shopId, printTypeEnum) + .forEach(machine -> getPrinter(machine.getBrand()) + .callNumPrintBefore(machine, shopInfo.getShopName(), (CallQueue) data)); + break; + default: + throw new CzgException("otherHandler 未知打印类型"); + } + } + + /** + * 处理退菜退款订单打印 + */ + public void kitchenRefundAllHandler(String operator, OrderInfo orderInfo, List detailList) { + //后厨退菜单 + getPrintMachine(orderInfo.getShopId(), PrinterHandler.PrintTypeEnum.REFUND_KITCHEN).forEach(machine -> { + Boolean isPrint = redisService.hasKey(RedisCst.kitchenRefundAll(orderInfo.getId(), machine.getId())); + if (!isPrint) { + getPrinter(machine.getBrand()).returnKitchenPrint(operator, orderInfo, detailList, machine); + redisService.set(RedisCst.kitchenRefundAll(orderInfo.getId(), machine.getId()), "", 180); + } + }); + } + + /** + * 处理退菜退款订单打印 + * + * @param printTitle 退菜单 / 退款单 + * @param operator 操作人 + * @param refundAmount 退款金额 + * @param refundReason 退款原因 + * @param refundType 退款方式 + * @param orderInfo 订单信息 + * @param detailList 退菜/退款 列表 + */ + public void refundOrderHandler(String printTitle, String operator, String refundAmount, String refundReason, String refundType, + OrderInfo orderInfo, List detailList) { + //前台退菜单 + if ("退菜单".equals(printTitle)) { + getPrintMachine(orderInfo.getShopId(), PrinterHandler.PrintTypeEnum.RETURN_ORDER).forEach(machine -> + getPrinter(machine.getBrand()).returnOrderPrint(printTitle, operator, refundAmount, refundReason, refundType, orderInfo, machine, detailList)); + } else if ("退款单".equals(printTitle)) { + getPrintMachine(orderInfo.getShopId(), PrinterHandler.PrintTypeEnum.REFUND_ORDER).forEach(machine -> + getPrinter(machine.getBrand()).returnOrderPrint(printTitle, operator, refundAmount, refundReason, refundType, orderInfo, machine, detailList)); + } else { + log.error("未知的前台退款打印类型: {}", printTitle); + } + } + + + protected List getCanPrintOrderDetails(PrintMachine machine, Long orderId, List tbOrderDetailList) { + Set canPrintProSet = new HashSet<>(); + boolean partPrint = false; + if ("1".equals(machine.getClassifyPrint())) { + partPrint = true; + List categoryIds = JSONObject.parseObject(StrUtil.emptyToDefault(machine.getCategoryIds(), "[]"), List.class); + if (CollUtil.isEmpty(categoryIds)) { + return Collections.emptyList(); + } + List productIds = tbOrderDetailList.stream().map(OrderDetail::getProductId).collect(Collectors.toList()); + canPrintProSet = productService.list(new QueryWrapper() + .in(Product::getCategoryId, categoryIds) + .in(Product::getId, productIds)) + .stream() + .map(Product::getId) + .collect(Collectors.toSet()); + } + ArrayList orderDetails = new ArrayList<>(); + for (OrderDetail item : tbOrderDetailList) { + //-999 台位费 + if (item.getNum().compareTo(BigDecimal.ZERO) <= 0 || item.getProductId().equals(-999L)) { + continue; + } + Boolean isPrint = redisService.hasKey(RedisCst.kitchenNormal(orderId, machine.getId(), item.getId())); + if (isPrint) { + continue; + } + if (item.getIsPrint() != null && item.getIsPrint() == 1 && (!partPrint || canPrintProSet.contains(item.getProductId()))) { + orderDetails.add(item); + } else { + log.info("此菜品不在打印分类或属于免打, {} {} {}", item.getProductName(), item.getSkuName(), item.getId()); + } + } + return orderDetails; + } + + /** + * 获取可用打印机 + * + * @param shopId 店铺id + * @param contentType GUEST_ORDER("客看单"), + * PRE_ORDER("预结算单"), + * ORDER("订单结算单"), + * RETURN_ORDER("退菜单"), + * REFUND_ORDER("退款单"), + *

+ * ALL_KITCHEN("后厨整单"), + * ONLY_KITCHEN("后厨分单"), + * REFUND_KITCHEN("后厨退菜单"), + *

+ * HANDOVER("交班单"), + * CALL("排队取号"), + * RECHARGE("储值单"), + * STOCK("出入库单"), + * STOCK_CHECK("盘点单"), + * PRODUCT_REPORT("商品报表"), + * DAY_REPORT("经营日报"), + * DAY_ORDER("日结单"); + * @return 打印机列表 + */ + private List getPrintMachine(Long shopId, PrinterHandler.PrintTypeEnum contentType) { + QueryWrapper wrapper = new QueryWrapper() + .eq(PrintMachine::getStatus, 1) + .eq(PrintMachine::getShopId, shopId) + .eq(PrintMachine::getPrintType, "cash") + .like(PrintMachine::getPrintContentType, contentType) + .eq(PrintMachine::getConnectionType, "云打印"); + List list = printMachineService.list(wrapper); + if (list.isEmpty()) { + log.error("店铺未配置打印机,店铺id: {}", shopId); + return list; + } + log.info("打印机列表: {}", list); + return list; + } + } diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/print/PrinterHandler.java b/cash-service/order-service/src/main/java/com/czg/service/order/print/PrinterHandler.java index 63029df74..853357a00 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/print/PrinterHandler.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/print/PrinterHandler.java @@ -1,17 +1,13 @@ package com.czg.service.order.print; -import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; -import com.alibaba.fastjson2.JSONObject; import com.czg.account.dto.HandoverRecordDTO; import com.czg.account.dto.PrintOrderDetailDTO; import com.czg.account.entity.*; import com.czg.account.service.*; -import com.czg.config.RedisCst; import com.czg.constants.ParamCodeCst; -import com.czg.exception.CzgException; import com.czg.market.service.OrderInfoService; import com.czg.order.entity.OrderDetail; import com.czg.order.entity.OrderInfo; @@ -19,13 +15,10 @@ import com.czg.order.service.OrderDetailService; import com.czg.order.service.PrintMachineLogService; import com.czg.print.*; import com.czg.product.entity.ProdSku; -import com.czg.product.entity.Product; import com.czg.product.service.ProdSkuService; -import com.czg.product.service.ProductService; import com.czg.service.RedisService; import com.czg.service.order.enums.OrderStatusEnums; import com.czg.system.service.SysParamsService; -import com.czg.utils.FunUtils; import com.mybatisflex.core.query.QueryWrapper; import jakarta.annotation.Resource; import lombok.Getter; @@ -37,8 +30,8 @@ import org.springframework.web.client.RestTemplate; import java.math.BigDecimal; import java.math.RoundingMode; import java.time.LocalDateTime; -import java.util.*; -import java.util.stream.Collectors; +import java.util.ArrayList; +import java.util.List; /** * @author Administrator @@ -59,7 +52,6 @@ public abstract class PrinterHandler { protected RedisService redisService; @Resource protected PrintMachineLogService printMachineLogService; - @DubboReference protected SysParamsService sysParamsService; @DubboReference @@ -75,12 +67,8 @@ public abstract class PrinterHandler { @DubboReference private ShopStaffService shopStaffService; @DubboReference - protected ProductService productService; - @DubboReference protected ProdSkuService prodSkuService; @DubboReference - private PrintMachineService printMachineService; - @DubboReference private HandoverRecordRpcService handoverRecordService; @Getter @@ -116,257 +104,11 @@ public abstract class PrinterHandler { this.printerBrand = printerBrand; } - /** - * 处理订单打印 - * - * @param data 传递的数据 - * @param printTypeEnum GUEST_ORDER ORDER PRE_ORDER ONLY_KITCHEN ALL_KITCHEN - */ - public void orderHandler(String data, PrintTypeEnum printTypeEnum, Integer placeNum) { - OrderInfo orderInfo = orderInfoService.getById(data); - if (orderInfo == null) { - throw new RuntimeException("订单打印失败,订单不存在"); - } - List orderDetailList = new ArrayList<>(); - orderDetailList = switch (printTypeEnum) { - case PrintTypeEnum.GUEST_ORDER -> { - orderInfo.setPlaceNum(placeNum); - yield orderDetailService.list(new QueryWrapper().eq(OrderDetail::getOrderId, orderInfo.getId()).eq(OrderDetail::getPlaceNum, placeNum)); - } - case PrintTypeEnum.ORDER, PrintTypeEnum.PRE_ORDER, PrintTypeEnum.ONLY_KITCHEN, PrintTypeEnum.ALL_KITCHEN -> - orderDetailService.list(new QueryWrapper().eq(OrderDetail::getOrderId, orderInfo.getId())); - default -> orderDetailList; - }; - for (PrintMachine machine : getPrintMachine(orderInfo.getShopId(), printTypeEnum)) { - PrinterHandler printer = printConfig.getPrinter(machine.getBrand()); - redisService.set("order:print:" + orderInfo.getId(), "", 180); - switch (printTypeEnum) { - case PrintTypeEnum.GUEST_ORDER: - log.info("准备开始打印客看订单"); - printer.guestOrderPrint(orderInfo, machine, orderDetailList); - break; - case PrintTypeEnum.ORDER: - log.info("准备开始打印订单"); - printer.orderPrint(machine, orderInfo, orderDetailList); - - break; - case PrintTypeEnum.PRE_ORDER: - log.info("准备开始打印预结算订单"); - printer.preOrderPrint(machine, orderInfo, orderDetailList); - break; - case PrintTypeEnum.ONLY_KITCHEN: - // 判断订单是否是先付费或者已结算 - if (!"after-pay".equals(orderInfo.getPayMode()) && (OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus()) || OrderStatusEnums.CANCELLED.getCode().equals(orderInfo.getStatus()))) { - log.warn("此订单未支付, 订单信息: {}", orderInfo.getId()); - } - List tbOrderDetailList = getCanPrintOrderDetails(machine, orderInfo.getId(), orderDetailList); - if (CollUtil.isEmpty(tbOrderDetailList)) { - log.info("此订单无打印菜品, 订单信息: {}", orderInfo.getId()); - break; - } - log.info("准备开始打印菜品单"); - //后厨单菜品 - tbOrderDetailList.forEach(item -> { - Boolean b = redisService.hasKey(RedisCst.kitchenNormal(orderInfo.getId(), machine.getId(), item.getId())); - if (!b) { - FunUtils.safeRunVoid(() -> onlyKitchenPrint(orderInfo, item, machine), - "订单id:{} ,后厨分单打印失败", orderInfo.getId()); - // 保存已打印信息 - redisService.set(RedisCst.kitchenNormal(orderInfo.getId(), machine.getId(), item.getId()), "", 180); - } - }); - break; - case PrintTypeEnum.ALL_KITCHEN: - Boolean exit = redisService.hasKey(RedisCst.kitchenAll(orderInfo.getId(), machine.getId())); - if (exit) { - break; - } - // 判断订单是否是先付费或者已结算 - if (!"after-pay".equals(orderInfo.getPayMode()) && (OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus()) || OrderStatusEnums.CANCELLED.getCode().equals(orderInfo.getStatus()))) { - log.warn("此订单未支付, 订单信息: {}", orderInfo.getId()); - } - List allOrderDetailList = getCanPrintOrderDetails(machine, orderInfo.getId(), orderDetailList); - if (CollUtil.isEmpty(allOrderDetailList)) { - log.info("此订单无打印菜品, 订单信息: {}", orderInfo.getId()); - break; - } - log.info("准备开始打印后厨整单"); - printer.allKitchenPrint(orderInfo, allOrderDetailList, machine); - redisService.set(RedisCst.kitchenAll(orderInfo.getId(), machine.getId()), "", 180); - break; - } - } - } - - /** - * 处理其它打印 - * - * @param data 传递的数据 - * @param printTypeEnum IN_STOCK DAY_REPORT PRODUCT_REPORT RECHARGE STOCK_CHECK - */ - public void otherHandler(Long shopId, Object data, PrintTypeEnum printTypeEnum) { - if (shopId == null || Objects.isNull(data)) { - log.info("otherHandler 打印数据为空, shopId: {}, data: {}", shopId, data); - return; - } - ShopInfo shopInfo = shopInfoService.getById(shopId); - switch (printTypeEnum) { - case PrintTypeEnum.STOCK: - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).stockPrint(machine, shopInfo.getShopName(), (StockPrintDTO) data)); - break; - case PrintTypeEnum.DAY_REPORT: - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).dayReportPrint(machine, shopInfo.getShopName(), (DayReportPrintDTO) data)); - break; - case PrintTypeEnum.PRODUCT_REPORT: - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).productReportPrint(machine, shopInfo.getShopName(), (ProductReportPrintDTO) data)); - break; - case PrintTypeEnum.RECHARGE: - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).rechargePrint(machine, shopInfo.getShopName(), (RechargePrintDTO) data)); - break; - case PrintTypeEnum.STOCK_CHECK: - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).stockCheckPrint(machine, shopInfo.getShopName(), (StockCheckPrintDTO) data)); - break; - case PrintTypeEnum.HANDOVER: - log.info("准备开始打印交班"); - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).handoverPrint(machine, shopInfo.getShopName(), (HandoverRecordDTO) data)); - break; - case PrintTypeEnum.CALL: - log.info("准备开始打印叫号单"); - getPrintMachine(shopId, printTypeEnum) - .forEach(machine -> printConfig.getPrinter(machine.getBrand()).callNumPrintBefore(machine, shopInfo.getShopName(), (CallQueue) data)); - break; - default: - throw new CzgException("otherHandler 未知打印类型"); - } - } - - /** - * 获取可用打印机 - * - * @param shopId 店铺id - * @param contentType GUEST_ORDER("客看单"), - * PRE_ORDER("预结算单"), - * ORDER("订单结算单"), - * RETURN_ORDER("退菜单"), - * REFUND_ORDER("退款单"), - *

- * ALL_KITCHEN("后厨整单"), - * ONLY_KITCHEN("后厨分单"), - * REFUND_KITCHEN("后厨退菜单"), - *

- * HANDOVER("交班单"), - * CALL("排队取号"), - * RECHARGE("储值单"), - * STOCK("出入库单"), - * STOCK_CHECK("盘点单"), - * PRODUCT_REPORT("商品报表"), - * DAY_REPORT("经营日报"), - * DAY_ORDER("日结单"); - * @return 打印机列表 - */ - private List getPrintMachine(Long shopId, PrintTypeEnum contentType) { - QueryWrapper wrapper = new QueryWrapper() - .eq(PrintMachine::getStatus, 1) - .eq(PrintMachine::getShopId, shopId) - .eq(PrintMachine::getPrintType, "cash") - .like(PrintMachine::getPrintContentType, contentType) - .eq(PrintMachine::getConnectionType, "云打印"); - List list = printMachineService.list(wrapper); - if (list.isEmpty()) { - log.error("店铺未配置打印机,店铺id: {}", shopId); - return list; - } - log.info("打印机列表: {}", list); - return list; - } - - - /** - * 处理退菜退款订单打印 - * - * @param printTitle 退菜单 / 退款单 - * @param operator 操作人 - * @param refundAmount 退款金额 - * @param refundReason 退款原因 - * @param refundType 退款方式 - * @param orderInfo 订单信息 - * @param detailList 退菜/退款 列表 - */ - public void refundOrderHandler(String printTitle, String operator, String refundAmount, String refundReason, String refundType, - OrderInfo orderInfo, List detailList) { - //前台退菜单 - if ("退菜单".equals(printTitle)) { - getPrintMachine(orderInfo.getShopId(), PrintTypeEnum.RETURN_ORDER).forEach(machine -> - printConfig.getPrinter(machine.getBrand()).returnOrderPrint(printTitle, operator, refundAmount, refundReason, refundType, orderInfo, machine, detailList)); - } else if ("退款单".equals(printTitle)) { - getPrintMachine(orderInfo.getShopId(), PrintTypeEnum.REFUND_ORDER).forEach(machine -> - printConfig.getPrinter(machine.getBrand()).returnOrderPrint(printTitle, operator, refundAmount, refundReason, refundType, orderInfo, machine, detailList)); - } else { - log.error("未知的前台退款打印类型: {}", printTitle); - } - } - - /** - * 处理退菜退款订单打印 - */ - public void kitchenRefundAllHandler(String operator, OrderInfo orderInfo, List detailList) { - //后厨退菜单 - getPrintMachine(orderInfo.getShopId(), PrintTypeEnum.REFUND_KITCHEN).forEach(machine -> { - Boolean isPrint = redisService.hasKey(RedisCst.kitchenRefundAll(orderInfo.getId(), machine.getId())); - if (!isPrint) { - printConfig.getPrinter(machine.getBrand()).returnKitchenPrint(operator, orderInfo, detailList, machine); - redisService.set(RedisCst.kitchenRefundAll(orderInfo.getId(), machine.getId()), "", 180); - } - }); - } - - - protected List getCanPrintOrderDetails(PrintMachine machine, Long orderId, List tbOrderDetailList) { - Set canPrintProSet = new HashSet<>(); - boolean partPrint = false; - if ("1".equals(machine.getClassifyPrint())) { - partPrint = true; - List categoryIds = JSONObject.parseObject(StrUtil.emptyToDefault(machine.getCategoryIds(), "[]"), List.class); - if (CollUtil.isEmpty(categoryIds)) { - return Collections.emptyList(); - } - List productIds = tbOrderDetailList.stream().map(OrderDetail::getProductId).collect(Collectors.toList()); - canPrintProSet = productService.list(new QueryWrapper() - .in(Product::getCategoryId, categoryIds) - .in(Product::getId, productIds)) - .stream() - .map(Product::getId) - .collect(Collectors.toSet()); - } - ArrayList orderDetails = new ArrayList<>(); - for (OrderDetail item : tbOrderDetailList) { - //-999 台位费 - if (item.getNum().compareTo(BigDecimal.ZERO) <= 0 || item.getProductId().equals(-999L)) { - continue; - } - Boolean isPrint = redisService.hasKey(RedisCst.kitchenNormal(orderId, machine.getId(), item.getId())); - if (isPrint) { - continue; - } - if (item.getIsPrint() != null && item.getIsPrint() == 1 && (!partPrint || canPrintProSet.contains(item.getProductId()))) { - orderDetails.add(item); - } else { - log.info("此菜品不在打印分类或属于免打, {} {} {}", item.getProductName(), item.getSkuName(), item.getId()); - } - } - return orderDetails; - } /** * 打印预结算单「前台」 */ - private void preOrderPrint(PrintMachine machine, OrderInfo orderInfo, List orderDetail) { + public void preOrderPrintBefore(PrintMachine machine, OrderInfo orderInfo, List orderDetail) { // 判断订单是否是先付费或者已结算 if (OrderStatusEnums.CANCELLED.getCode().equals(orderInfo.getStatus())) { log.warn("已取消, 订单信息: {}", orderInfo); @@ -416,7 +158,7 @@ public abstract class PrinterHandler { /** * 仅打印结算单「前台」 */ - private void orderPrint(PrintMachine machine, OrderInfo orderInfo, List tbOrderDetailList) { + public void orderPrintBefore(PrintMachine machine, OrderInfo orderInfo, List tbOrderDetailList) { // 判断订单是否是先付费或者已结算 if (OrderStatusEnums.UNPAID.getCode().equals(orderInfo.getStatus()) || OrderStatusEnums.CANCELLED.getCode().equals(orderInfo.getStatus())) { log.warn("此订单未支付或已取消, 订单信息: {}", orderInfo); @@ -464,7 +206,7 @@ public abstract class PrinterHandler { /** * 打印排队小票 */ - private void callNumPrintBefore(PrintMachine machine, String shopName, CallQueue queue) { + public void callNumPrintBefore(PrintMachine machine, String shopName, CallQueue queue) { if (queue == null) { log.warn("叫号记录不存在"); return; @@ -489,8 +231,11 @@ public abstract class PrinterHandler { } - public OrderPrintDTO returnOrderPrint(String printTitle, String operator, String refundAmount, String refundReason, String refundType, - OrderInfo orderInfo, PrintMachine machine, List detailList) { + /** + * 打印退菜/退款单 + */ + protected OrderPrintDTO returnOrderPrint(String printTitle, String operator, String refundAmount, String refundReason, String refundType, + OrderInfo orderInfo, PrintMachine machine, List detailList) { ShopInfo shopInfo = shopInfoService.getById(orderInfo.getShopId()); OrderPrintDTO orderPrintDTO = new OrderPrintDTO().setShopName(shopInfo.getShopName()).setPrintType("收银-堂食") .setOrderNo(orderInfo.getOrderNo()) @@ -506,8 +251,10 @@ public abstract class PrinterHandler { return orderPrintDTO; } - - public OrderPrintDTO guestOrderPrint(OrderInfo orderInfo, PrintMachine machine, List detailList) { + /** + * 打印客看单 + */ + protected OrderPrintDTO guestOrderPrint(OrderInfo orderInfo, PrintMachine machine, List detailList) { OrderPrintDTO orderPrintDTO = getPrintInfoDTO(orderInfo, "客看单"); BigDecimal originalAmount = BigDecimal.ZERO; @@ -549,13 +296,15 @@ public abstract class PrinterHandler { return orderPrintDTO; } - public OrderPrintDTO preOrderPrint(OrderInfo orderInfo, PrintMachine machine, List detailList) { + /** + * 打印预结算单 + */ + protected OrderPrintDTO preOrderPrint(OrderInfo orderInfo, PrintMachine machine, List detailList) { OrderPrintDTO orderPrintDTO = getPrintInfoDTO(orderInfo, "预结算单"); BigDecimal originalAmount = BigDecimal.ZERO; for (OrderDetail orderDetail : detailList) { if (orderDetail.getIsGift() == 1) { - continue; } else if (orderDetail.getDiscountSaleAmount().compareTo(BigDecimal.ZERO) > 0) { originalAmount = originalAmount.add((orderDetail.getNum().subtract(orderDetail.getReturnNum()).subtract(orderDetail.getRefundNum())).multiply(orderDetail.getUnitPrice())); } else { @@ -572,7 +321,10 @@ public abstract class PrinterHandler { return orderPrintDTO; } - public OrderPrintDTO orderPrint(OrderInfo orderInfo, PrintMachine machine, List detailList) { + /** + * 打印结算单 + */ + protected OrderPrintDTO orderPrint(OrderInfo orderInfo, PrintMachine machine, List detailList) { OrderPrintDTO orderPrintDTO = getPrintInfoDTO(orderInfo, "结算单"); BigDecimal originalAmount = orderInfo.getOriginAmount() == null ? BigDecimal.ZERO : orderInfo.getOriginAmount(); @@ -587,7 +339,7 @@ public abstract class PrinterHandler { /** * 后厨 总单打印 */ - public OrderPrintDTO allKitchenPrint(OrderInfo orderInfo, List orderDetails, PrintMachine machine) { + protected OrderPrintDTO allKitchenPrint(OrderInfo orderInfo, List orderDetails, PrintMachine machine) { return getPrintInfoDTO(orderInfo, "后厨总单"); } @@ -599,7 +351,7 @@ public abstract class PrinterHandler { /** * 后厨 退菜整单打印 */ - public OrderPrintDTO returnKitchenPrint(String operator, OrderInfo orderInfo, List orderDetail, PrintMachine machine) { + protected OrderPrintDTO returnKitchenPrint(String operator, OrderInfo orderInfo, List orderDetail, PrintMachine machine) { OrderPrintDTO printInfo = getPrintInfoDTO(orderInfo, "后厨退菜单"); printInfo.setOperator(operator); return printInfo; diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/print/YxyPrinter.java b/cash-service/order-service/src/main/java/com/czg/service/order/print/YxyPrinter.java index e4cf0c878..c8262aa62 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/print/YxyPrinter.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/print/YxyPrinter.java @@ -8,7 +8,6 @@ import com.czg.order.entity.OrderDetail; import com.czg.order.entity.OrderInfo; import com.czg.print.*; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -27,7 +26,6 @@ import java.util.*; */ @Slf4j @Component -@Qualifier("yxyPrinter") public class YxyPrinter extends PrinterHandler implements PrinterImpl { //请求地址 @@ -43,7 +41,7 @@ public class YxyPrinter extends PrinterHandler implements PrinterImpl { super("云想印"); } - private PrintSignLabel printSignLabel = new PrintSignLabel() + private final PrintSignLabel printSignLabel = new PrintSignLabel() .setBr("
") .setCut("") .setOut("OUT") diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoCustomServiceImpl.java b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoCustomServiceImpl.java index a888f5a6f..2426a3ac9 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoCustomServiceImpl.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderInfoCustomServiceImpl.java @@ -55,6 +55,7 @@ import com.czg.sa.StpKit; import com.czg.service.RedisService; import com.czg.service.order.enums.OrderStatusEnums; import com.czg.service.order.mapper.OrderInfoCustomMapper; +import com.czg.service.order.print.PrintConfig; import com.czg.service.order.print.PrinterHandler; import com.czg.utils.*; import com.mybatisflex.core.paginate.Page; @@ -64,7 +65,6 @@ import jakarta.validation.constraints.NotBlank; import lombok.extern.slf4j.Slf4j; import org.apache.dubbo.config.annotation.DubboReference; import org.jetbrains.annotations.NotNull; -import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionSynchronization; @@ -94,9 +94,8 @@ public class OrderInfoCustomServiceImpl implements OrderInfoCustomService { private OrderInfoCustomMapper orderInfoCustomMapper; @Resource private OrderInfoService orderInfoService; - @Lazy @Resource - private PrinterHandler printerHandler; + private PrintConfig printConfig; @Resource private OrderDetailService orderDetailService; @Resource @@ -333,7 +332,7 @@ public class OrderInfoCustomServiceImpl implements OrderInfoCustomService { // log.info("后付费生成订单{},第{}", orderInfo.getId(), orderInfo.getPlaceNum()); //客看单 - printerHandler.orderHandler(orderInfo.getId().toString(), PrinterHandler.PrintTypeEnum.GUEST_ORDER, orderInfo.getPlaceNum()); + printConfig.orderHandler(orderInfo.getId().toString(), PrinterHandler.PrintTypeEnum.GUEST_ORDER, orderInfo.getPlaceNum()); //orderId_0_0 订单ID_先付后付(1先付0后付)_订单状态 0未完成 1完成_第几次下单 //orderInfo.getId() + "_" + (!"after-pay".equals(orderInfo.getPayMode()) ? 1 : 0) + "_0" //后付费 菜品单 @@ -1619,10 +1618,10 @@ public class OrderInfoCustomServiceImpl implements OrderInfoCustomService { switch (orderInfoPrintDTO.getType()) { case 1://预结算单 - printerHandler.orderHandler(orderInfo.getId().toString(), PrinterHandler.PrintTypeEnum.PRE_ORDER, null); + printConfig.orderHandler(orderInfo.getId().toString(), PrinterHandler.PrintTypeEnum.PRE_ORDER, null); break; case 2://结算单 - printerHandler.orderHandler(orderInfo.getId().toString(), PrinterHandler.PrintTypeEnum.ORDER, null); + printConfig.orderHandler(orderInfo.getId().toString(), PrinterHandler.PrintTypeEnum.ORDER, null); break; } return true; diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderPayServiceImpl.java b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderPayServiceImpl.java index fb2b0ae57..143d34258 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderPayServiceImpl.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/OrderPayServiceImpl.java @@ -42,7 +42,7 @@ import com.czg.resp.CzgResult; import com.czg.service.RedisService; import com.czg.service.order.dto.OrderPayParamDTO; import com.czg.service.order.enums.OrderStatusEnums; -import com.czg.service.order.print.PrinterHandler; +import com.czg.service.order.print.PrintConfig; import com.czg.service.order.service.OrderPayService; import com.czg.service.order.service.PayService; import com.czg.utils.AssertUtil; @@ -92,7 +92,7 @@ public class OrderPayServiceImpl implements OrderPayService { @Resource private RabbitPublisher rabbitPublisher; @Resource - private PrinterHandler printerHandler; + private PrintConfig printConfig; @Resource private MkShopRechargeService shopRechargeService; @Resource @@ -526,13 +526,13 @@ public class OrderPayServiceImpl implements OrderPayService { if (param.isPrint()) { String finalPrintTitle = printTitle; boolean finalIsPay = isPay; - FunUtils.safeRunVoid(() -> printerHandler.refundOrderHandler(finalPrintTitle, StrUtil.isNotBlank(param.getOperator()) ? param.getOperator() : "" + FunUtils.safeRunVoid(() -> printConfig.refundOrderHandler(finalPrintTitle, StrUtil.isNotBlank(param.getOperator()) ? param.getOperator() : "" , finalIsPay ? param.getRefundAmount().toPlainString() : "0" , param.getRefundReason(), orderInfo.getRefundType(), orderInfo, param.getRefundDetails()), "订单id:{} 退款,前台打印消息失败", orderInfo.getId()); } //后厨退菜单 - FunUtils.safeRunVoid(() -> printerHandler.kitchenRefundAllHandler(StrUtil.isNotBlank(param.getOperator()) ? param.getOperator() : "", orderInfo, param.getRefundDetails()), + FunUtils.safeRunVoid(() -> printConfig.kitchenRefundAllHandler(StrUtil.isNotBlank(param.getOperator()) ? param.getOperator() : "", orderInfo, param.getRefundDetails()), "订单id:{} 退款,后厨退菜单打印消息失败", orderInfo.getId()); //退款返还库存 if (!productStockMap.isEmpty()) { diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/PrintMachineLogServiceImpl.java b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/PrintMachineLogServiceImpl.java index bf61615f2..977661319 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/PrintMachineLogServiceImpl.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/service/impl/PrintMachineLogServiceImpl.java @@ -186,7 +186,7 @@ public class PrintMachineLogServiceImpl extends ServiceImpl