From 83d3aa444f8a96ff8ae5e2f32b07c73f9638636e Mon Sep 17 00:00:00 2001 From: SongZhang <2064194730@qq.com> Date: Fri, 30 Aug 2024 15:06:56 +0800 Subject: [PATCH] =?UTF-8?q?1.=E4=BB=A3=E5=AE=A2=E4=B8=8B=E5=8D=95=20?= =?UTF-8?q?=E5=95=86=E5=93=81=E6=95=B0=E9=87=8F=E5=87=8F=E5=B0=91=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=89=93=E5=8D=B0=E9=80=80=E5=8D=95=E7=A5=A8=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cashierservice/bean/PrintProductInfo.java | 6 + .../cashierservice/entity/TbPrintMachine.java | 11 +- .../mybatis/MpPrintMachineMapper.java | 8 + .../rabbit/PrintMechineConsumer.java | 330 ++++++++++++------ .../cashierservice/service/OrderService.java | 33 +- .../system/cashierservice/util/RedisCst.java | 1 + 6 files changed, 280 insertions(+), 109 deletions(-) create mode 100644 src/main/java/com/chaozhanggui/system/cashierservice/bean/PrintProductInfo.java create mode 100644 src/main/java/com/chaozhanggui/system/cashierservice/mybatis/MpPrintMachineMapper.java diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/bean/PrintProductInfo.java b/src/main/java/com/chaozhanggui/system/cashierservice/bean/PrintProductInfo.java new file mode 100644 index 0000000..bae8c62 --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/bean/PrintProductInfo.java @@ -0,0 +1,6 @@ +package com.chaozhanggui.system.cashierservice.bean; + +public class PrintProductInfo { + private Integer orderDetailId; + private Integer num; +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPrintMachine.java b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPrintMachine.java index 3bdee44..e07c140 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPrintMachine.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/entity/TbPrintMachine.java @@ -34,6 +34,15 @@ public class TbPrintMachine implements Serializable { private String vendorId; private String productId; + private String config; + + public String getConfig() { + return config; + } + + public void setConfig(String config) { + this.config = config; + } private static final long serialVersionUID = 1L; @@ -164,4 +173,4 @@ public class TbPrintMachine implements Serializable { public void setProductId(String productId) { this.productId = productId == null ? null : productId.trim(); } -} \ No newline at end of file +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/mybatis/MpPrintMachineMapper.java b/src/main/java/com/chaozhanggui/system/cashierservice/mybatis/MpPrintMachineMapper.java new file mode 100644 index 0000000..c5fff9f --- /dev/null +++ b/src/main/java/com/chaozhanggui/system/cashierservice/mybatis/MpPrintMachineMapper.java @@ -0,0 +1,8 @@ +package com.chaozhanggui.system.cashierservice.mybatis; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.chaozhanggui.system.cashierservice.entity.TbCashierCart; +import com.chaozhanggui.system.cashierservice.entity.TbPrintMachine; + +public interface MpPrintMachineMapper extends BaseMapper { +} diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/PrintMechineConsumer.java b/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/PrintMechineConsumer.java index 3264992..8f13ba9 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/PrintMechineConsumer.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/rabbit/PrintMechineConsumer.java @@ -3,24 +3,25 @@ package com.chaozhanggui.system.cashierservice.rabbit; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson.JSONObject; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; 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.sign.CodeEnum; -import com.chaozhanggui.system.cashierservice.sign.Result; +import com.chaozhanggui.system.cashierservice.mybatis.MpPrintMachineMapper; import com.chaozhanggui.system.cashierservice.util.*; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.annotation.RabbitHandler; import org.springframework.amqp.rabbit.annotation.RabbitListener; -import org.springframework.amqp.rabbit.annotation.RabbitListeners; import org.springframework.beans.factory.annotation.Autowired; +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.*; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; @Slf4j @Component @@ -51,6 +52,14 @@ public class PrintMechineConsumer { private RedisUtil redisUtils; + private final RedisTemplate redisTemplate; + @Autowired + private MpPrintMachineMapper mpPrintMachineMapper; + + public PrintMechineConsumer(RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } + @RabbitHandler public void listener(String message) { @@ -81,15 +90,15 @@ public class PrintMechineConsumer { } - list.parallelStream().forEach(it->{ + list.parallelStream().forEach(it -> { if (!"network".equals(it.getConnectionType())) { - log.error("非网络打印机:{},{}",it.getAddress(),it.getConnectionType()); + log.error("非网络打印机:{},{}", it.getAddress(), it.getConnectionType()); return; } if (!"1".equals(it.getStatus().toString())) { - log.error("打印机状态异常:{},{}",it.getAddress(),it.getStatus()); + log.error("打印机状态异常:{},{}", it.getAddress(), it.getStatus()); return; } @@ -101,37 +110,37 @@ public class PrintMechineConsumer { String feet = config.getString("feet"); String autoCut = config.getString("autoCut"); - List categoryInfos=JSONUtil.parseJSONStr2TList(config.getJSONArray("categoryList").toString(),CategoryInfo.class); + List categoryInfos = JSONUtil.parseJSONStr2TList(config.getJSONArray("categoryList").toString(), CategoryInfo.class); - switch (it.getContentType()){ + switch (it.getContentType()) { case "yxyPrinter": - yxyPrinter(it,model,orderInfo,shopInfo,printerNum,categoryInfos); + yxyPrinter(it, model, orderInfo, shopInfo, printerNum, categoryInfos); break; case "fePrinter": - fePrinter(it,model,orderInfo,shopInfo,printerNum,categoryInfos); + fePrinter(it, model, orderInfo, shopInfo, printerNum, categoryInfos); break; } }); - - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } /** - * 博时结云打印机 + * 博时结云打印机 + * * @param tbPrintMachineWithBLOBs * @param model * @param orderInfo * @param shopInfo * @param printerNum */ - private void yxyPrinter(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs,String model, - TbOrderInfo orderInfo,TbShopInfo shopInfo,String printerNum, List categoryInfos){ - String orderId=orderInfo.getId().toString(); + private void yxyPrinter(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs, String model, + TbOrderInfo orderInfo, TbShopInfo shopInfo, String printerNum, List categoryInfos) { + String orderId = orderInfo.getId().toString(); switch (tbPrintMachineWithBLOBs.getSubType()) { @@ -142,17 +151,17 @@ public class PrintMechineConsumer { case "normal": //普通出单 - if("return".equals(orderInfo.getOrderType())){ - List tbOrderDetails=tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); - if(ObjectUtil.isNotEmpty(tbOrderDetails)&&tbOrderDetails.size()>0){ + if ("return".equals(orderInfo.getOrderType())) { + List tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); + if (ObjectUtil.isNotEmpty(tbOrderDetails) && tbOrderDetails.size() > 0) { List detailList = new ArrayList<>(); - tbOrderDetails.parallelStream().forEach(it->{ - String categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); + tbOrderDetails.parallelStream().forEach(it -> { + String categoryId = tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); - Long count= categoryInfos.stream().filter(c-> + Long count = categoryInfos.stream().filter(c -> c.getId().toString().equals(categoryId) ).count(); - log.info("获取当前类别是否未打印类别:{}",count); + log.info("获取当前类别是否未打印类别:{}", count); TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getProductSkuId())); @@ -174,14 +183,14 @@ public class PrintMechineConsumer { } - if(ObjectUtil.isNotEmpty(detailList)&&detailList.size()>0){ - 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.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList,orderInfo.getRemark(),null,null); + if (ObjectUtil.isNotEmpty(detailList) && detailList.size() > 0) { + 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.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList, orderInfo.getRemark(), null, null); - String printType="退款单"; + String printType = "退款单"; - String data= PrinterUtils.getCashPrintData(detailPO,printType,"return"); + String data = PrinterUtils.getCashPrintData(detailPO, printType, "return"); String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; - PrinterUtils.printTickets(voiceJson,1, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data); + PrinterUtils.printTickets(voiceJson, 1, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data); } } @@ -192,8 +201,8 @@ public class PrintMechineConsumer { && (!orderInfo.getStatus().equals("closed"))) { return; } - List tbOrderDetails=tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); - if(ObjectUtil.isNotEmpty(tbOrderDetails)&&tbOrderDetails.size()>0) { + List tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); + if (ObjectUtil.isNotEmpty(tbOrderDetails) && tbOrderDetails.size() > 0) { List detailList = new ArrayList<>(); tbOrderDetails.stream().forEach(it -> { String categoryId = tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); @@ -224,21 +233,21 @@ public class PrintMechineConsumer { if (ObjectUtil.isNotEmpty(detailList) && detailList.size() > 0) { - 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); + 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); // OrderDetailPO detailPO = new OrderDetailPO(shopInfo.getShopName(), "普通打印", orderInfo.getMasterId(), orderInfo.getOrderNo(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), "【POS-1】001", orderInfo.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList); - detailPO.setOutNumber(orderInfo.getOutNumber()); - String printType = "结算单"; + detailPO.setOutNumber(orderInfo.getOutNumber()); + String printType = "结算单"; - String data = PrinterUtils.getCashPrintData(detailPO, printType, orderInfo.getOrderType()); - String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; + String data = PrinterUtils.getCashPrintData(detailPO, printType, orderInfo.getOrderType()); + String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; - PrinterUtils.printTickets(voiceJson, 3, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data); + PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data); } } @@ -249,10 +258,10 @@ public class PrintMechineConsumer { && (!orderInfo.getStatus().equals("unpaid"))) { return; } - if("return".equals(orderInfo.getOrderType())){ + if ("return".equals(orderInfo.getOrderType())) { printReturnTicket(tbPrintMachineWithBLOBs, orderInfo, printerNum, categoryInfos, orderId); return; - }else { + } else { printTicket(Integer.valueOf(orderId), categoryInfos, tbPrintMachineWithBLOBs, orderInfo); // printNormalTicket(tbPrintMachineWithBLOBs, orderInfo, printerNum, categoryInfos, orderId); } @@ -270,8 +279,11 @@ public class PrintMechineConsumer { private void printTicket(Integer orderId, List categoryInfos, TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs, TbOrderInfo orderInfo) { List tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(orderId); if (!tbOrderDetails.isEmpty()) { - - tbOrderDetails.parallelStream().forEach(it -> { + String printKey = RedisCst.ORDER_PRINT_PRO + orderId; + AtomicReference> printProductSet = new AtomicReference<>(redisTemplate.opsForSet().members(printKey)); + // 重置打印数据 + redisTemplate.delete(printKey); + tbOrderDetails.forEach(it -> { log.info("开始打印一菜一品票据,:{}", it.getProductName()); String categoryId = tbProductMapper.selectByPrimaryKey(it.getProductId()).getCategoryId(); @@ -280,7 +292,7 @@ public class PrintMechineConsumer { ).count(); log.info("获取当前类别是否未打印类别:{}", count); - if(count>0){ + if (count > 0) { // 统计已打数量 int printerNum = 0; @@ -288,21 +300,40 @@ public class PrintMechineConsumer { String key = RedisCst.ORDER_PRINT + orderId + ":" + it.getProductId() + ":" + it.getProductSkuId(); String info = redisUtils.getMessage(key); redisUtils.saveMessage(key, String.valueOf(it.getNum()), 60 * 60 * 24); + + // 删除已打印数据 + 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(); } + + TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(it.getProductSkuId()); + String remark = ""; + if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) { + remark = tbProductSkuWithBLOBs.getSpecSnap(); + } + + // 将已打印信息加入redis + it.setRemark(remark); + redisTemplate.opsForSet().add(printKey, it); + redisTemplate.expire(printKey, 24, TimeUnit.HOURS); + // 已打印不再打印 if (info != null && it.getNum() - Integer.parseInt(info) == 0) { return; } - TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getProductSkuId())); - String remark = ""; - if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) { - remark = tbProductSkuWithBLOBs.getSpecSnap(); - } - String data; String voiceJson; if (isReturn) { @@ -311,27 +342,114 @@ public class PrintMechineConsumer { StrUtil.isBlank(orderInfo.getTableName()) ? orderInfo.getMasterId() : orderInfo.getTableName(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(), it.getNum(), remark); voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔退款订单,请及时处理\"}"; - PrinterUtils.printTickets(voiceJson,3, 2, tbPrintMachineWithBLOBs.getAddress(), data); + PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data); - }else { + } else { data = PrinterUtils.getPrintData("", orderInfo.getMasterId(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(), it.getNum(), remark); voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; - PrinterUtils.printTickets(voiceJson,3, 1, tbPrintMachineWithBLOBs.getAddress(), data); + PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data); } -// PrinterUtils.printTickets(voiceJson,3, printerNum, 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); + redisUtils.deleteByKey(key); + }); + } + } } + /** + * 打印退款订单 + * @param orderId 订单id + * @param tbOrderDetails 现有订单 + */ + public void printReturnTicket(Integer orderId, List tbOrderDetails) { + TbOrderInfo orderInfo = tbOrderInfoMapper.selectByPrimaryKey(orderId); + List list = mpPrintMachineMapper.selectList(new LambdaUpdateWrapper() + .eq(TbPrintMachine::getContentType, "yxyPrinter") + .eq(TbPrintMachine::getConnectionType, "network") + .eq(TbPrintMachine::getSubType, "cash") + .eq(TbPrintMachine::getStatus, 1) + .eq(TbPrintMachine::getShopId, orderInfo.getShopId())); + String printKey = RedisCst.ORDER_PRINT_PRO + orderId; + Set printProductSet = redisTemplate.opsForSet().members(printKey); + Set printProductSetCopy = redisTemplate.opsForSet().members(printKey); + + log.info("-------------------开始打印退款订单,打印机列表: {}", list); + log.info("-------------------待打印数据: {}", printProductSet); + if (printProductSet != null && !printProductSet.isEmpty()) { + + // 过滤出已经删除的订单 + if (tbOrderDetails != null && !tbOrderDetails.isEmpty()) { + for (TbOrderDetail it : tbOrderDetails) { + printProductSet = printProductSet.stream().filter(r -> { + TbOrderDetail detail = (TbOrderDetail) r; + return !detail.getProductSkuId().equals(it.getProductSkuId()) || !detail.getProductId().equals(it.getProductId()); + }).collect(Collectors.toSet()); + + printProductSetCopy = printProductSet.stream().filter(r -> { + TbOrderDetail detail = (TbOrderDetail) r; + return detail.getProductSkuId().equals(it.getProductSkuId()) && detail.getProductId().equals(it.getProductId()); + }).collect(Collectors.toSet()); + } + + redisTemplate.delete(printKey); + for (Object o : printProductSetCopy) { + redisTemplate.opsForSet().add(printKey, o); + } + }else if (tbOrderDetails != null) { + redisTemplate.delete(printKey); + } + + log.info("-------------------打印机数量{}", list.size()); + for (TbPrintMachine machine : list) { + + String config = machine.getConfig(); + if (config != null) { + JSONObject jsonObject = JSONObject.parseObject(config); + if (!"one".equals(jsonObject.getString("model"))) { + continue; + } + } + log.info("-------------------商品数量{}", printProductSet.size()); + printProductSet.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, machine.getAddress(), data); + + String key = RedisCst.ORDER_PRINT + orderId + ":" + orderDetail.getProductId() + ":" + orderDetail.getProductSkuId(); + log.info("删除商品数量记录key, {}", key); + redisUtils.deleteByKey(key); + }); + } + + } + + } + /** * 普通出菜单打印 */ @@ -352,7 +470,7 @@ public class PrintMechineConsumer { ).count(); log.info("获取当前类别是否未打印类别:{}", count); - if(count>0){ + if (count > 0) { TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getProductSkuId())); String remark = ""; if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) { @@ -362,7 +480,7 @@ public class PrintMechineConsumer { DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(), it.getNum(), remark); String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; - PrinterUtils.printTickets(voiceJson,3, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data); + PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data); } }); } @@ -372,25 +490,25 @@ public class PrintMechineConsumer { * 退款单打印 */ private void printReturnTicket(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs, TbOrderInfo orderInfo, String printerNum, List categoryInfos, String orderId) { - List details= tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); - if(ObjectUtil.isNotEmpty(details)&&details.size()>0){ - details.parallelStream().forEach(it->{ + List details = tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); + if (ObjectUtil.isNotEmpty(details) && details.size() > 0) { + details.parallelStream().forEach(it -> { - String categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); - Long count= categoryInfos.stream().filter(c-> + String categoryId = tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); + Long count = categoryInfos.stream().filter(c -> c.getId().toString().equals(categoryId) ).count(); - if(count>0){ + if (count > 0) { TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getProductSkuId())); String remark = ""; if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) { remark = tbProductSkuWithBLOBs.getSpecSnap(); } - String data = PrinterUtils.getPrintData("return", orderInfo.getPayType().equals("wx_lite")? orderInfo.getTableName(): orderInfo.getMasterId(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(), it.getNum(), remark); + String data = PrinterUtils.getPrintData("return", orderInfo.getPayType().equals("wx_lite") ? orderInfo.getTableName() : orderInfo.getMasterId(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getProductName(), it.getNum(), remark); String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔退款订单,请及时处理\"}"; - PrinterUtils.printTickets(voiceJson,3, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data); + PrinterUtils.printTickets(voiceJson, 3, 1, tbPrintMachineWithBLOBs.getAddress(), data); } }); } @@ -399,9 +517,10 @@ public class PrintMechineConsumer { /** * 判断商品是否已打票 + * * @return true 已打 false 未打 */ - private boolean isPrint(TbOrderDetail orderDetail, Integer orderId) { + private boolean isPrint(TbOrderDetail orderDetail, Integer orderId) { String key = RedisCst.ORDER_PRINT + orderId + ":" + orderDetail.getProductId() + ":" + orderDetail.getProductSkuId(); String info = redisUtils.getMessage(key); redisUtils.saveMessage(key, String.valueOf(orderDetail.getNum()), 60 * 60 * 24); @@ -413,38 +532,36 @@ public class PrintMechineConsumer { } - - - private void fePrinter(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs,String model,TbOrderInfo orderInfo,TbShopInfo shopInfo,String printerNum, List categoryInfos){ - String orderId=orderInfo.getId().toString(); + private void fePrinter(TbPrintMachineWithBLOBs tbPrintMachineWithBLOBs, String model, TbOrderInfo orderInfo, TbShopInfo shopInfo, String printerNum, List categoryInfos) { + String orderId = orderInfo.getId().toString(); switch (tbPrintMachineWithBLOBs.getSubType()) { case "label": //标签打印机 - List cashierCarts = tbCashierCartMapper.selectByOrderId(orderInfo.getId().toString(),"final"); + List cashierCarts = tbCashierCartMapper.selectByOrderId(orderInfo.getId().toString(), "final"); if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) { - cashierCarts.parallelStream().forEach(it->{ + cashierCarts.parallelStream().forEach(it -> { String categoryId; - if(ObjectUtil.isEmpty(it.getCategoryId())){ - categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); + if (ObjectUtil.isEmpty(it.getCategoryId())) { + categoryId = tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); } else { categoryId = it.getCategoryId(); } - Long count= categoryInfos.stream().filter(c-> + Long count = categoryInfos.stream().filter(c -> c.getId().toString().equals(categoryId) ).count(); - log.info("获取当前类别是否未打印类别:{}",count); + log.info("获取当前类别是否未打印类别:{}", count); - if(count>0) { + if (count > 0) { TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getSkuId())); String remark = ""; if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) { remark = tbProductSkuWithBLOBs.getSpecSnap(); } - for(int i=0;i tbOrderDetails=tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); - if(ObjectUtil.isNotEmpty(tbOrderDetails)&&tbOrderDetails.size()>0){ + if ("return".equals(orderInfo.getOrderType())) { + List tbOrderDetails = tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(orderId)); + if (ObjectUtil.isNotEmpty(tbOrderDetails) && tbOrderDetails.size() > 0) { List detailList = new ArrayList<>(); - tbOrderDetails.parallelStream().forEach(it->{ - String categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); + tbOrderDetails.parallelStream().forEach(it -> { + String categoryId = tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); - Long count= categoryInfos.stream().filter(c-> + Long count = categoryInfos.stream().filter(c -> c.getId().toString().equals(categoryId) ).count(); - log.info("获取当前类别是否未打印类别:{}",count); + log.info("获取当前类别是否未打印类别:{}", count); TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getProductSkuId())); @@ -488,20 +605,20 @@ public class PrintMechineConsumer { } - if(ObjectUtil.isNotEmpty(detailList)&&detailList.size()>0){ + if (ObjectUtil.isNotEmpty(detailList) && detailList.size() > 0) { // OrderDetailPO detailPO = new OrderDetailPO(shopInfo.getShopName(), "普通打印", orderInfo.getMasterId(), orderInfo.getOrderNo(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), "【POS-1】001", orderInfo.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList); - 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.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList,orderInfo.getRemark(),orderInfo.getDiscountAmount().toPlainString(),orderInfo.getDiscountRatio().toPlainString()); + 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.getOrderAmount().toPlainString(), balance, orderInfo.getPayType(), "0", detailList, orderInfo.getRemark(), orderInfo.getDiscountAmount().toPlainString(), orderInfo.getDiscountRatio().toPlainString()); - String printType="退款单"; + String printType = "退款单"; - String data= PrinterUtils.getCashPrintData(detailPO,printType,"return"); + String data = PrinterUtils.getCashPrintData(detailPO, printType, "return"); // String voiceJson = "{\"bizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; // PrinterUtils.printTickets(voiceJson,1, Integer.valueOf(printerNum), tbPrintMachineWithBLOBs.getAddress(), data); } } - }else { + } else { cashierCarts = cashierCarts = tbCashierCartMapper.selectByOrderId(orderInfo.getId().toString(), "final"); if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) { @@ -540,7 +657,7 @@ public class PrintMechineConsumer { } } if (ObjectUtil.isNotEmpty(detailList) && detailList.size() > 0) { - 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(),null,null); + 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(), null, null); String printType = "结算单"; if ("return".equals(orderInfo.getOrderType())) { @@ -561,24 +678,24 @@ public class PrintMechineConsumer { return; } - cashierCarts = tbCashierCartMapper.selectByOrderId(orderId,"final"); + cashierCarts = tbCashierCartMapper.selectByOrderId(orderId, "final"); if (ObjectUtil.isNotEmpty(cashierCarts) && cashierCarts.size() > 0) { cashierCarts.parallelStream().forEach(it -> { String categoryId; - if(ObjectUtil.isEmpty(it.getCategoryId())){ - categoryId= tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); + if (ObjectUtil.isEmpty(it.getCategoryId())) { + categoryId = tbProductMapper.selectByPrimaryKey(Integer.valueOf(it.getProductId())).getCategoryId(); } else { categoryId = it.getCategoryId(); } - Long count= categoryInfos.stream().filter(c-> + Long count = categoryInfos.stream().filter(c -> c.getId().toString().equals(categoryId) ).count(); - if(count>0){ + if (count > 0) { TbProductSkuWithBLOBs tbProductSkuWithBLOBs = tbProductSkuMapper.selectByPrimaryKey(Integer.valueOf(it.getSkuId())); String remark = ""; if (ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs) && ObjectUtil.isNotEmpty(tbProductSkuWithBLOBs.getSpecSnap())) { @@ -586,8 +703,7 @@ public class PrintMechineConsumer { } - - FeieyunPrintUtil.getPrintData(tbPrintMachineWithBLOBs.getAddress(),orderInfo.getMasterId(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getName(), it.getNumber(), remark); + FeieyunPrintUtil.getPrintData(tbPrintMachineWithBLOBs.getAddress(), orderInfo.getMasterId(), DateUtils.getTime(new Date(orderInfo.getCreatedAt())), it.getName(), it.getNumber(), remark); } }); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java b/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java index f980d08..7cb9343 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/service/OrderService.java @@ -19,6 +19,7 @@ import com.chaozhanggui.system.cashierservice.entity.vo.CartVo; import com.chaozhanggui.system.cashierservice.exception.MsgException; import com.chaozhanggui.system.cashierservice.mybatis.MPCashierCartMapper; import com.chaozhanggui.system.cashierservice.mybatis.MpShopTableMapper; +import com.chaozhanggui.system.cashierservice.rabbit.PrintMechineConsumer; import com.chaozhanggui.system.cashierservice.rabbit.RabbitProducer; import com.chaozhanggui.system.cashierservice.sign.CodeEnum; import com.chaozhanggui.system.cashierservice.sign.Result; @@ -28,6 +29,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -91,20 +93,28 @@ public class OrderService { @Autowired TbConsInfoMapper tbConsInfoMapper; + private final TbCashierCartMapper tbCashierCartMapper; private static ConcurrentHashMap> codeMap = new ConcurrentHashMap<>(); private static ConcurrentHashMap> userMap = new ConcurrentHashMap<>(); @Autowired private ProductService productService; + @Qualifier("printMechineConsumer") + @Autowired + private PrintMechineConsumer printMechineConsumer; + @Qualifier("tbOrderDetailMapper") + @Autowired + private TbOrderDetailMapper tbOrderDetailMapper; - public OrderService(WxAccountUtil wxAccountUtil, MPCashierCartMapper mpCashierCartMapper, WechatUtil wechatUtil, TbUserShopMsgMapper tbUserShopMsgMapper, TbShopOpenIdMapper shopOpenIdMapper, MpShopTableMapper mpShopTableMapper) { + public OrderService(WxAccountUtil wxAccountUtil, MPCashierCartMapper mpCashierCartMapper, WechatUtil wechatUtil, TbUserShopMsgMapper tbUserShopMsgMapper, TbShopOpenIdMapper shopOpenIdMapper, MpShopTableMapper mpShopTableMapper, TbCashierCartMapper tbCashierCartMapper) { this.wxAccountUtil = wxAccountUtil; this.mpCashierCartMapper = mpCashierCartMapper; this.wechatUtil = wechatUtil; this.tbUserShopMsgMapper = tbUserShopMsgMapper; this.shopOpenIdMapper = shopOpenIdMapper; this.mpShopTableMapper = mpShopTableMapper; + this.tbCashierCartMapper = tbCashierCartMapper; } @Transactional(rollbackFor = Exception.class) @@ -203,6 +213,7 @@ public class OrderService { tbOrderInfoMapper.deleteByPrimaryKey(Integer.valueOf(finalCart.getOrderId())); } } + return Result.success(CodeEnum.SUCCESS, masterId); } @@ -343,6 +354,14 @@ public class OrderService { redisUtil.saveMessage("SHOP:CODE:SET" + clientType + ":" + shopId + ":" + DateUtils.getDay(),masterId.substring(1,masterId.length())); + TbCashierCart finalCart1 = cart; + ThreadUtil.execute(() -> { + ThreadUtil.sleep(1, TimeUnit.SECONDS); + if (finalCart1.getOrderId() != null && finalCart1.getTableId() != null) { + log.info("购物车数量改变,开始校验订单是否为空"); + printMechineConsumer.printReturnTicket(Integer.valueOf(finalCart1.getOrderId()), tbOrderDetailMapper.selectAllByOrderId(Integer.valueOf(finalCart1.getOrderId()))); + } + }); return Result.success(CodeEnum.SUCCESS, masterId); } @@ -546,7 +565,13 @@ public class OrderService { if (list.isEmpty()) { tbOrderInfoMapper.deleteByPrimaryKey(Integer.valueOf(cashierCart.getOrderId())); + ThreadUtil.execute(() -> { + log.info("购物车数量改变,开始校验订单是否为空"); + printMechineConsumer.printReturnTicket(Integer.valueOf(cashierCart.getOrderId()), null); + }); } + + } return Result.success(CodeEnum.SUCCESS); @@ -1213,6 +1238,12 @@ public class OrderService { .eq(TbShopTable::getQrcode, cartVo.getTableId()) .set(TbShopTable::getStatus, TableStateEnum.IDLE.getState())); + // 打印退款票据 + int finalOrderId = orderId; + ThreadUtil.execute(() -> { + printMechineConsumer.printReturnTicket(finalOrderId, null); + }); + return Result.success(CodeEnum.SUCCESS); } diff --git a/src/main/java/com/chaozhanggui/system/cashierservice/util/RedisCst.java b/src/main/java/com/chaozhanggui/system/cashierservice/util/RedisCst.java index 25f4abf..294173d 100644 --- a/src/main/java/com/chaozhanggui/system/cashierservice/util/RedisCst.java +++ b/src/main/java/com/chaozhanggui/system/cashierservice/util/RedisCst.java @@ -21,4 +21,5 @@ public class RedisCst { public static final String ORDER_PRODUCT_NUM = "ORDER_NUM:"; public static final String ORDER_CART_EXISTS = "ORDER_CART_EXISTS:"; public static final String ORDER_PRINT = "ORDER_PRINT:"; + public static final String ORDER_PRINT_PRO = "ORDER_PRINT_PRODUCT:"; }