diff --git a/cash-service/order-service/src/main/java/com/czg/service/order/print/FeiPrinter.java b/cash-service/order-service/src/main/java/com/czg/service/order/print/FeiPrinter.java index 55a1245fc..e6090ac86 100644 --- a/cash-service/order-service/src/main/java/com/czg/service/order/print/FeiPrinter.java +++ b/cash-service/order-service/src/main/java/com/czg/service/order/print/FeiPrinter.java @@ -100,10 +100,8 @@ public class FeiPrinter extends PrinterHandler implements PrinterImpl { .setBalance(balance).setPayType((ObjectUtil.isEmpty(orderInfo.getPayType()) || ObjectUtil.isNull(orderInfo.getPayType()) ? "" : orderInfo.getPayType())).setIntegral("0") .setOutNumber(orderInfo.getTakeCode()).setPrintTitle("结算单") .setRemark(orderInfo.getRemark()) - // 使用累计的总优惠金额,而不是倒推计算 - .setDiscountAmount(orderInfo.getDiscountAllAmount() != null - ? orderInfo.getDiscountAllAmount().toPlainString() - : "0.00"); + // 使用累计的总优惠金额,如果为 null 则用原价减去实付计算 + .setDiscountAmount(calculateDiscountAmount(orderInfo)); String string = buildOrderPrintData(printInfoDTO, detailList); String o = sendPrintRequest(machine.getAddress(), string, null, printerNum); printMachineLogService.save(orderInfo.getId(), machine, "结算单", string, o); @@ -131,10 +129,8 @@ public class FeiPrinter extends PrinterHandler implements PrinterImpl { .setBalance(balance).setPayType((ObjectUtil.isEmpty(orderInfo.getPayType()) || ObjectUtil.isNull(orderInfo.getPayType()) ? "" : orderInfo.getPayType())).setIntegral("0") .setOutNumber(orderInfo.getTakeCode()).setPrintTitle("结算单") .setRemark(orderInfo.getRemark()) - // 使用累计的总优惠金额,而不是倒推计算 - .setDiscountAmount(orderInfo.getDiscountAllAmount() != null - ? orderInfo.getDiscountAllAmount().toPlainString() - : "0.00"); + // 使用累计的总优惠金额,如果为 null 则用原价减去实付计算 + .setDiscountAmount(calculateDiscountAmount(orderInfo)); printInfoDTO.setPrintTitle(printInfoDTO.getPrintTitle()); if (orderInfo.getSeatNum() != null && orderInfo.getSeatAmount().compareTo(BigDecimal.ZERO) > 0) { printInfoDTO.setSeatNum(orderInfo.getSeatNum().toString()); @@ -247,7 +243,7 @@ public class FeiPrinter extends PrinterHandler implements PrinterImpl { //成功 开机 {"msg":"ok","ret":0,"data":"在线,工作状态正常。","serverExecutedTime":4} //成功 离线 {"msg":"ok","ret":0,"data":"离线。","serverExecutedTime":7} JSONObject json = JSONUtil.parseObj(UnicodeUtil.toString(resp)); - log.info("飞鹅打印机状态响应: {}", json); + log.info("飞鹅打印机状态响应:{}", json); msg = json.getStr("data"); } catch (Exception e) { msg = "未知错误"; @@ -255,4 +251,19 @@ public class FeiPrinter extends PrinterHandler implements PrinterImpl { return msg; } + /** + * 计算优惠金额:优先使用 discountAllAmount,如果为 null 则用原价 - 实付计算 + */ + private String calculateDiscountAmount(OrderInfo orderInfo) { + if (orderInfo.getDiscountAllAmount() != null) { + return orderInfo.getDiscountAllAmount().toPlainString(); + } + // 兜底计算:原价 + 餐位费 + 打包费 - 实付 + BigDecimal originalTotal = orderInfo.getOriginAmount() + .add(orderInfo.getSeatAmount() != null ? orderInfo.getSeatAmount() : BigDecimal.ZERO) + .add(orderInfo.getPackFee() != null ? orderInfo.getPackFee() : BigDecimal.ZERO); + BigDecimal discount = originalTotal.subtract(orderInfo.getPayAmount()); + return discount.compareTo(BigDecimal.ZERO) >= 0 ? discount.toPlainString() : "0.00"; + } + } 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 f6c0d070d..18c80de43 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 @@ -133,10 +133,8 @@ public class YxyPrinter extends PrinterHandler implements PrinterImpl { .setBalance(balance).setPayType((ObjectUtil.isEmpty(orderInfo.getPayType()) || ObjectUtil.isNull(orderInfo.getPayType()) ? "" : orderInfo.getPayType())).setIntegral("0") .setOutNumber(orderInfo.getTakeCode()).setPrintTitle("退款单") .setRemark(orderInfo.getRemark()) - // 使用累计的总优惠金额,而不是倒推计算 - .setDiscountAmount(orderInfo.getDiscountAllAmount() != null - ? orderInfo.getDiscountAllAmount().toPlainString() - : "0.00"); + // 使用累计的总优惠金额,如果为 null 则用原价减去实付计算 + .setDiscountAmount(calculateDiscountAmount(orderInfo)); String data = buildOrderPrintData(printInfoDTO, detailList); String voiceJson = "{\"PbizType\":\"2\",\"content\":\"您有一笔新的订单,请及时处理\"}"; @@ -166,10 +164,8 @@ public class YxyPrinter extends PrinterHandler implements PrinterImpl { .setPrintTitle(isPre ? "预结算单" : "结算单") .setCount(count) .setRemark(orderInfo.getRemark()) - // 使用累计的总优惠金额,而不是倒推计算 - .setDiscountAmount(orderInfo.getDiscountAllAmount() != null - ? orderInfo.getDiscountAllAmount().toPlainString() - : "0.00"); + // 使用累计的总优惠金额,如果为 null 则用原价减去实付计算 + .setDiscountAmount(calculateDiscountAmount(orderInfo)); printInfoDTO.setPrintTitle(printInfoDTO.getPrintTitle()); if (orderInfo.getSeatNum() != null && orderInfo.getSeatAmount().compareTo(BigDecimal.ZERO) > 0) { printInfoDTO.setSeatNum(orderInfo.getSeatNum().toString()); @@ -264,4 +260,19 @@ public class YxyPrinter extends PrinterHandler implements PrinterImpl { return s; } + /** + * 计算优惠金额:优先使用 discountAllAmount,如果为 null 则用原价 - 实付计算 + */ + private String calculateDiscountAmount(OrderInfo orderInfo) { + if (orderInfo.getDiscountAllAmount() != null) { + return orderInfo.getDiscountAllAmount().toPlainString(); + } + // 兜底计算:原价 + 餐位费 + 打包费 - 实付 + BigDecimal originalTotal = orderInfo.getOriginAmount() + .add(orderInfo.getSeatAmount() != null ? orderInfo.getSeatAmount() : BigDecimal.ZERO) + .add(orderInfo.getPackFee() != null ? orderInfo.getPackFee() : BigDecimal.ZERO); + BigDecimal discount = originalTotal.subtract(orderInfo.getPayAmount()); + return discount.compareTo(BigDecimal.ZERO) >= 0 ? discount.toPlainString() : "0.00"; + } + }