|
@@ -19,8 +19,10 @@ import com.fuint.common.vo.printer.*;
|
|
|
import com.fuint.framework.annoation.OperationServiceLog;
|
|
import com.fuint.framework.annoation.OperationServiceLog;
|
|
|
import com.fuint.framework.exception.BusinessCheckException;
|
|
import com.fuint.framework.exception.BusinessCheckException;
|
|
|
import com.fuint.framework.pagination.PaginationResponse;
|
|
import com.fuint.framework.pagination.PaginationResponse;
|
|
|
|
|
+import com.fuint.repository.mapper.MtPrinterCateMapper;
|
|
|
import com.fuint.repository.mapper.MtPrinterMapper;
|
|
import com.fuint.repository.mapper.MtPrinterMapper;
|
|
|
import com.fuint.repository.model.MtPrinter;
|
|
import com.fuint.repository.model.MtPrinter;
|
|
|
|
|
+import com.fuint.repository.model.MtPrinterCate;
|
|
|
import com.fuint.repository.model.MtSetting;
|
|
import com.fuint.repository.model.MtSetting;
|
|
|
import com.fuint.repository.model.MtStore;
|
|
import com.fuint.repository.model.MtStore;
|
|
|
import com.fuint.utils.StringUtil;
|
|
import com.fuint.utils.StringUtil;
|
|
@@ -38,6 +40,8 @@ import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
import java.util.concurrent.*;
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
@@ -55,6 +59,11 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
|
|
|
|
|
private MtPrinterMapper mtPrinterMapper;
|
|
private MtPrinterMapper mtPrinterMapper;
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 打印机与商品分类映射 Mapper
|
|
|
|
|
+ * */
|
|
|
|
|
+ private MtPrinterCateMapper mtPrinterCateMapper;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 系统配置服务接口
|
|
* 系统配置服务接口
|
|
|
* */
|
|
* */
|
|
@@ -164,7 +173,7 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
* @param autoPrint 自动打印
|
|
* @param autoPrint 自动打印
|
|
|
* @param beforePay 支付前打印
|
|
* @param beforePay 支付前打印
|
|
|
* @param afterPay 支付后打印
|
|
* @param afterPay 支付后打印
|
|
|
- * @param goodsIds 打印的商品Id
|
|
|
|
|
|
|
+ * @param goodsIds 本次新增的订单商品行ID集合,为空时打印订单全部商品
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
* @return
|
|
* @return
|
|
|
* */
|
|
* */
|
|
@@ -204,109 +213,181 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
throw new BusinessCheckException("打印失败:该店铺还没有添加云打印机!");
|
|
throw new BusinessCheckException("打印失败:该店铺还没有添加云打印机!");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- MtStore storeInfo = orderInfo.getStoreInfo();
|
|
|
|
|
|
|
+ // 按打印机与商品分类的映射关系,计算每台打印机需要打印的订单商品行
|
|
|
|
|
+ Map<Integer, List<Integer>> printerGoodsMap = resolvePrintGoodsIds(printers, orderInfo, goodsIds);
|
|
|
|
|
+
|
|
|
for (MtPrinter mtPrinter : printers) {
|
|
for (MtPrinter mtPrinter : printers) {
|
|
|
|
|
+ List<Integer> printGoodsIds = printerGoodsMap.get(mtPrinter.getId());
|
|
|
|
|
+ if (printGoodsIds == null || printGoodsIds.size() < 1) {
|
|
|
|
|
+ logger.info("云打印订单:打印机{}(ID:{})本次没有需要打印的商品,跳过", mtPrinter.getName(), mtPrinter.getId());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
printRequest.setSn(mtPrinter.getSn());
|
|
printRequest.setSn(mtPrinter.getSn());
|
|
|
-
|
|
|
|
|
- StringBuilder printContent = new StringBuilder();
|
|
|
|
|
- printContent.append("<C>").append("<B>" + storeInfo.getName() + "</B>").append("<BR></C>");
|
|
|
|
|
- printContent.append("<BR>");
|
|
|
|
|
-
|
|
|
|
|
- // 分割线
|
|
|
|
|
- printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
-
|
|
|
|
|
- // 订单ID、单号、桌码
|
|
|
|
|
- printContent.append("<L>订单ID:").append(orderInfo.getId()).append("</L>");
|
|
|
|
|
- printContent.append("<BR>");
|
|
|
|
|
- printContent.append("<L>订单号:").append(orderInfo.getOrderSn()).append("</L>");
|
|
|
|
|
- if (orderInfo.getTableInfo() != null && StringUtil.isNotEmpty(orderInfo.getTableInfo().getCode())) {
|
|
|
|
|
- printContent.append("<BR>");
|
|
|
|
|
- printContent.append("<R><BOLD>桌码:").append(orderInfo.getTableInfo().getCode()).append("</BOLD></R>");
|
|
|
|
|
|
|
+ printRequest.setContent(buildOrderPrintContent(orderInfo, printGoodsIds));
|
|
|
|
|
+ printRequest.setCopies(1);
|
|
|
|
|
+ printRequest.setVoice(2);
|
|
|
|
|
+ printRequest.setMode(0);
|
|
|
|
|
+ ObjectRestResponse<String> result = PrinterUtil.print(printRequest);
|
|
|
|
|
+ if (result != null && result.getCode() != 0) {
|
|
|
|
|
+ logger.error("云打印订单失败:" + result.getMsg());
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:" + result.getMsg());
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 订单状态
|
|
|
|
|
- String payStatus = PayStatusEnum.WAIT.getValue();
|
|
|
|
|
- if (orderInfo.getPayStatus().equals(PayStatusEnum.SUCCESS.getKey())) {
|
|
|
|
|
- payStatus = PayStatusEnum.SUCCESS.getValue();
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 构建订单小票的打印内容
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderInfo 订单信息
|
|
|
|
|
+ * @param printGoodsIds 需要打印的订单商品行ID,为空表示打印订单全部商品
|
|
|
|
|
+ * @return 打印内容
|
|
|
|
|
+ */
|
|
|
|
|
+ private String buildOrderPrintContent(UserOrderDto orderInfo, List<Integer> printGoodsIds) {
|
|
|
|
|
+ StringBuilder printContent = new StringBuilder();
|
|
|
|
|
+ printContent.append("<C>").append("<B>" + orderInfo.getStoreInfo().getName() + "</B>").append("<BR></C>");
|
|
|
|
|
+ printContent.append("<BR>");
|
|
|
|
|
+
|
|
|
|
|
+ // 分割线
|
|
|
|
|
+ printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
+
|
|
|
|
|
+ // 订单ID、单号、桌码
|
|
|
|
|
+ printContent.append("<L>订单ID:").append(orderInfo.getId()).append("</L>");
|
|
|
|
|
+ printContent.append("<BR>");
|
|
|
|
|
+ printContent.append("<L>订单号:").append(orderInfo.getOrderSn()).append("</L>");
|
|
|
|
|
+ if (orderInfo.getTableInfo() != null && StringUtil.isNotEmpty(orderInfo.getTableInfo().getCode())) {
|
|
|
printContent.append("<BR>");
|
|
printContent.append("<BR>");
|
|
|
- printContent.append("<L>").append("支付状态:").append(payStatus).append("<BR></L>");
|
|
|
|
|
|
|
+ printContent.append("<R><BOLD>桌码:").append(orderInfo.getTableInfo().getCode()).append("</BOLD></R>");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 分割线
|
|
|
|
|
- printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
|
|
+ // 订单状态
|
|
|
|
|
+ String payStatus = PayStatusEnum.WAIT.getValue();
|
|
|
|
|
+ if (orderInfo.getPayStatus().equals(PayStatusEnum.SUCCESS.getKey())) {
|
|
|
|
|
+ payStatus = PayStatusEnum.SUCCESS.getValue();
|
|
|
|
|
+ }
|
|
|
|
|
+ printContent.append("<BR>");
|
|
|
|
|
+ printContent.append("<L>").append("支付状态:").append(payStatus).append("<BR></L>");
|
|
|
|
|
|
|
|
- printContent.append("品名").append(StringUtils.repeat(" ", 16))
|
|
|
|
|
- .append("数量").append(StringUtils.repeat(" ", 2))
|
|
|
|
|
- .append("单价").append(StringUtils.repeat(" ", 2))
|
|
|
|
|
- .append("<BR>");
|
|
|
|
|
|
|
+ // 分割线
|
|
|
|
|
+ printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
|
|
|
- // 分割线
|
|
|
|
|
- printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
|
|
+ printContent.append("品名").append(StringUtils.repeat(" ", 16))
|
|
|
|
|
+ .append("数量").append(StringUtils.repeat(" ", 2))
|
|
|
|
|
+ .append("单价").append(StringUtils.repeat(" ", 2))
|
|
|
|
|
+ .append("<BR>");
|
|
|
|
|
|
|
|
- // 商品列表
|
|
|
|
|
- if (orderInfo.getGoods() != null && orderInfo.getGoods().size() > 0) {
|
|
|
|
|
- for (OrderGoodsDto goodsDto : orderInfo.getGoods()) {
|
|
|
|
|
- List<GoodsSpecValueDto> specList = goodsDto.getSpecList();
|
|
|
|
|
- String name = goodsDto.getName();
|
|
|
|
|
- List<String> specValue = new ArrayList<>();
|
|
|
|
|
- if (specList != null && specList.size() > 0) {
|
|
|
|
|
- for (GoodsSpecValueDto spec : specList) {
|
|
|
|
|
- if (StringUtil.isNotEmpty(spec.getSpecValue())) {
|
|
|
|
|
- specValue.add(spec.getSpecValue());
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (specValue.size() > 0) {
|
|
|
|
|
- name = name + "(" + String.join(",", specValue) + ")";
|
|
|
|
|
|
|
+ // 分割线
|
|
|
|
|
+ printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
+
|
|
|
|
|
+ // 商品列表(只打印指定的订单商品行)
|
|
|
|
|
+ BigDecimal printGoodsAmount = BigDecimal.ZERO;
|
|
|
|
|
+ int printGoodsCount = 0;
|
|
|
|
|
+ if (orderInfo.getGoods() != null && orderInfo.getGoods().size() > 0) {
|
|
|
|
|
+ for (OrderGoodsDto goodsDto : orderInfo.getGoods()) {
|
|
|
|
|
+ if (printGoodsIds != null && printGoodsIds.size() > 0 && !printGoodsIds.contains(goodsDto.getId())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ List<GoodsSpecValueDto> specList = goodsDto.getSpecList();
|
|
|
|
|
+ String name = goodsDto.getName();
|
|
|
|
|
+ List<String> specValue = new ArrayList<>();
|
|
|
|
|
+ if (specList != null && specList.size() > 0) {
|
|
|
|
|
+ for (GoodsSpecValueDto spec : specList) {
|
|
|
|
|
+ if (StringUtil.isNotEmpty(spec.getSpecValue())) {
|
|
|
|
|
+ specValue.add(spec.getSpecValue());
|
|
|
}
|
|
}
|
|
|
- }
|
|
|
|
|
- if (goodsIds != null && goodsIds.size() > 0) {
|
|
|
|
|
- if (goodsIds.contains(goodsDto.getGoodsId())) {
|
|
|
|
|
- printContent.append(NoteFormatter.formatPrintOrderItemForNewLine80(name, goodsDto.getNum(), Double.parseDouble(goodsDto.getPrice())));
|
|
|
|
|
- }
|
|
|
|
|
- } else {
|
|
|
|
|
- printContent.append(NoteFormatter.formatPrintOrderItemForNewLine80(name, goodsDto.getNum(), Double.parseDouble(goodsDto.getPrice())));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ if (specValue.size() > 0) {
|
|
|
|
|
+ name = name + "(" + String.join(",", specValue) + ")";
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ printContent.append(NoteFormatter.formatPrintOrderItemForNewLine80(name, goodsDto.getNum(), Double.parseDouble(goodsDto.getPrice())));
|
|
|
|
|
+ printGoodsAmount = printGoodsAmount.add(new BigDecimal(goodsDto.getPrice()).multiply(BigDecimal.valueOf(goodsDto.getNum())));
|
|
|
|
|
+ printGoodsCount++;
|
|
|
}
|
|
}
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 分割线
|
|
|
|
|
- printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
|
|
+ // 分割线
|
|
|
|
|
+ printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
|
|
|
|
|
+ // 合计:只打印部分商品时,合计只计算本次打印的商品金额,避免与商品清单对不上
|
|
|
|
|
+ int totalGoodsCount = orderInfo.getGoods() == null ? 0 : orderInfo.getGoods().size();
|
|
|
|
|
+ if (printGoodsCount < totalGoodsCount) {
|
|
|
|
|
+ printContent.append("<R>").append("本单合计:").append(printGoodsAmount.setScale(2, RoundingMode.HALF_UP)).append("元").append("<BR></R>");
|
|
|
|
|
+ } else {
|
|
|
printContent.append("<R>").append("合计:").append(orderInfo.getPayAmount()).append("元").append("<BR></R>");
|
|
printContent.append("<R>").append("合计:").append(orderInfo.getPayAmount()).append("元").append("<BR></R>");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- // 配送订单,打印配送信息
|
|
|
|
|
- if (orderInfo.getOrderMode().equals(OrderModeEnum.EXPRESS.getKey())) {
|
|
|
|
|
- printContent.append("<BR>");
|
|
|
|
|
- printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
|
|
- printContent.append("<L>")
|
|
|
|
|
- .append("配送姓名:").append(orderInfo.getAddress().getName()).append("<BR>")
|
|
|
|
|
- .append("联系电话:").append(orderInfo.getAddress().getMobile()).append("<BR>")
|
|
|
|
|
- .append("详细地址:").append(orderInfo.getAddress().getProvinceName() + orderInfo.getAddress().getCityName() + orderInfo.getAddress().getRegionName() + orderInfo.getAddress().getDetail()).append("<BR>");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ // 配送订单,打印配送信息
|
|
|
|
|
+ if (orderInfo.getOrderMode().equals(OrderModeEnum.EXPRESS.getKey())) {
|
|
|
printContent.append("<BR>");
|
|
printContent.append("<BR>");
|
|
|
|
|
+ printContent.append(StringUtils.repeat("-", 32)).append("<BR>");
|
|
|
printContent.append("<L>")
|
|
printContent.append("<L>")
|
|
|
- .append("店铺地址:").append((orderInfo.getStoreInfo().getAddress() == null) ? "无" : orderInfo.getStoreInfo().getAddress()).append("<BR>")
|
|
|
|
|
- .append("联系电话:").append((orderInfo.getStoreInfo().getPhone() == null) ? "无" : orderInfo.getStoreInfo().getPhone()).append("<BR>")
|
|
|
|
|
- .append("下单时间:").append(orderInfo.getCreateTime()).append("<BR>")
|
|
|
|
|
- .append("订单备注:").append(StringUtil.isEmpty(orderInfo.getRemark()) ? "无" : orderInfo.getRemark()).append("<BR>");
|
|
|
|
|
-
|
|
|
|
|
- // 网站二维码
|
|
|
|
|
- String webSite = env.getProperty("website.url");
|
|
|
|
|
- if (StringUtil.isNotEmpty(webSite)) {
|
|
|
|
|
- printContent.append("<C>").append("<QR>" + webSite + "</QR>").append("</C>");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ .append("配送姓名:").append(orderInfo.getAddress().getName()).append("<BR>")
|
|
|
|
|
+ .append("联系电话:").append(orderInfo.getAddress().getMobile()).append("<BR>")
|
|
|
|
|
+ .append("详细地址:").append(orderInfo.getAddress().getProvinceName() + orderInfo.getAddress().getCityName() + orderInfo.getAddress().getRegionName() + orderInfo.getAddress().getDetail()).append("<BR>");
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- printRequest.setContent(printContent.toString());
|
|
|
|
|
- printRequest.setCopies(1);
|
|
|
|
|
- printRequest.setVoice(2);
|
|
|
|
|
- printRequest.setMode(0);
|
|
|
|
|
- ObjectRestResponse<String> result = PrinterUtil.print(printRequest);
|
|
|
|
|
- if (result != null && result.getCode() != 0) {
|
|
|
|
|
- logger.error("云打印订单失败:" + result.getMsg());
|
|
|
|
|
- throw new BusinessCheckException("打印失败:" + result.getMsg());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ printContent.append("<BR>");
|
|
|
|
|
+ printContent.append("<L>")
|
|
|
|
|
+ .append("店铺地址:").append((orderInfo.getStoreInfo().getAddress() == null) ? "无" : orderInfo.getStoreInfo().getAddress()).append("<BR>")
|
|
|
|
|
+ .append("联系电话:").append((orderInfo.getStoreInfo().getPhone() == null) ? "无" : orderInfo.getStoreInfo().getPhone()).append("<BR>")
|
|
|
|
|
+ .append("下单时间:").append(orderInfo.getCreateTime()).append("<BR>")
|
|
|
|
|
+ .append("订单备注:").append(StringUtil.isEmpty(orderInfo.getRemark()) ? "无" : orderInfo.getRemark()).append("<BR>");
|
|
|
|
|
+
|
|
|
|
|
+ // 网站二维码
|
|
|
|
|
+ String webSite = env.getProperty("website.url");
|
|
|
|
|
+ if (StringUtil.isNotEmpty(webSite)) {
|
|
|
|
|
+ printContent.append("<C>").append("<QR>" + webSite + "</QR>").append("</C>");
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ return printContent.toString();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 向指定打印机打印整个订单的明细(不按商品分类分单,且不播报来单语音)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderInfo 订单信息
|
|
|
|
|
+ * @param mtPrinter 指定的打印机
|
|
|
|
|
+ * @throws Exception
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Boolean printOrderToPrinter(UserOrderDto orderInfo, MtPrinter mtPrinter) throws Exception {
|
|
|
|
|
+ if (orderInfo == null) {
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:订单不存在!");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (mtPrinter == null) {
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:打印机不存在!");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StatusEnum.ENABLED.getKey().equals(mtPrinter.getStatus())) {
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:该打印机已停用!");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (orderInfo.getStoreInfo() == null) {
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:订单所属店铺信息为空!");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (mtPrinter.getMerchantId() != null && mtPrinter.getMerchantId() > 0 && !mtPrinter.getMerchantId().equals(orderInfo.getMerchantId())) {
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:打印机与订单不属于同一商户!");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ logger.info("云打印订单到指定打印机,订单编号:{},打印机:{}(SN:{})", orderInfo.getOrderSn(), mtPrinter.getName(), mtPrinter.getSn());
|
|
|
|
|
+
|
|
|
|
|
+ PrintRequest printRequest = new PrintRequest();
|
|
|
|
|
+ createRequestHeader(orderInfo.getMerchantId(), printRequest);
|
|
|
|
|
+ printRequest.setSn(mtPrinter.getSn());
|
|
|
|
|
+ // 打印整个订单的明细,不按商品分类分单
|
|
|
|
|
+ printRequest.setContent(buildOrderPrintContent(orderInfo, null));
|
|
|
|
|
+ printRequest.setCopies(1);
|
|
|
|
|
+ // 静音模式,不播报“您有新的订单,请及时处理”(0取消订单模式 1静音模式 2来单播放模式)
|
|
|
|
|
+ printRequest.setVoice(1);
|
|
|
|
|
+ printRequest.setMode(0);
|
|
|
|
|
+
|
|
|
|
|
+ ObjectRestResponse<String> result = PrinterUtil.print(printRequest);
|
|
|
|
|
+ if (result == null || result.getCode() != 0) {
|
|
|
|
|
+ String errorMsg = result == null ? "未知错误" : result.getMsg();
|
|
|
|
|
+ logger.error("云打印订单失败:{}", errorMsg);
|
|
|
|
|
+ throw new BusinessCheckException("打印失败:" + errorMsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -317,7 +398,7 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
* @param autoPrint 自动打印
|
|
* @param autoPrint 自动打印
|
|
|
* @param beforePay 支付前打印
|
|
* @param beforePay 支付前打印
|
|
|
* @param afterPay 支付后打印
|
|
* @param afterPay 支付后打印
|
|
|
- * @param goodsIds 打印的商品Id
|
|
|
|
|
|
|
+ * @param goodsIds 本次新增的订单商品行ID集合,为空时打印订单全部商品
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
* @return
|
|
* @return
|
|
|
* */
|
|
* */
|
|
@@ -356,12 +437,23 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
logger.error("云打印标签失败:该店铺还没有添加云打印机!");
|
|
logger.error("云打印标签失败:该店铺还没有添加云打印机!");
|
|
|
throw new BusinessCheckException("打印失败:该店铺还没有添加云打印机!");
|
|
throw new BusinessCheckException("打印失败:该店铺还没有添加云打印机!");
|
|
|
}
|
|
}
|
|
|
|
|
+ // 按打印机与商品分类的映射关系,计算每台打印机需要打印的订单商品行
|
|
|
|
|
+ Map<Integer, List<Integer>> printerGoodsMap = resolvePrintGoodsIds(printers, orderInfo, goodsIds);
|
|
|
|
|
+
|
|
|
for (MtPrinter mtPrinter : printers) {
|
|
for (MtPrinter mtPrinter : printers) {
|
|
|
|
|
+ List<Integer> printGoodsIds = printerGoodsMap.get(mtPrinter.getId());
|
|
|
|
|
+ if (printGoodsIds == null || printGoodsIds.size() < 1) {
|
|
|
|
|
+ logger.info("云打印标签:打印机{}(ID:{})本次没有需要打印的商品,跳过", mtPrinter.getName(), mtPrinter.getId());
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
printRequest.setSn(mtPrinter.getSn());
|
|
printRequest.setSn(mtPrinter.getSn());
|
|
|
StringBuilder printContent = new StringBuilder();
|
|
StringBuilder printContent = new StringBuilder();
|
|
|
- // 商品列表
|
|
|
|
|
|
|
+ // 商品列表(只打印该打印机负责的商品行)
|
|
|
if (orderInfo.getGoods() != null && orderInfo.getGoods().size() > 0) {
|
|
if (orderInfo.getGoods() != null && orderInfo.getGoods().size() > 0) {
|
|
|
for (OrderGoodsDto goodsDto : orderInfo.getGoods()) {
|
|
for (OrderGoodsDto goodsDto : orderInfo.getGoods()) {
|
|
|
|
|
+ if (!printGoodsIds.contains(goodsDto.getId())) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
String goodsName = goodsDto.getName();
|
|
String goodsName = goodsDto.getName();
|
|
|
String tableCode = (orderInfo.getTableInfo() != null ? orderInfo.getTableInfo().getCode() : ""); // 为空时直接设为空字符串
|
|
String tableCode = (orderInfo.getTableInfo() != null ? orderInfo.getTableInfo().getCode() : ""); // 为空时直接设为空字符串
|
|
|
String remarkOrTime = (orderInfo.getRemark() != null ? orderInfo.getRemark() : orderInfo.getPayTime());
|
|
String remarkOrTime = (orderInfo.getRemark() != null ? orderInfo.getRemark() : orderInfo.getPayTime());
|
|
@@ -446,6 +538,11 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
restRequest.setSnlist(snList);
|
|
restRequest.setSnlist(snList);
|
|
|
PrinterUtil.delPrinters(restRequest);
|
|
PrinterUtil.delPrinters(restRequest);
|
|
|
}
|
|
}
|
|
|
|
|
+ // 删除该打印机与商品分类的映射关系
|
|
|
|
|
+ LambdaQueryWrapper<MtPrinterCate> deleteWrapper = Wrappers.lambdaQuery();
|
|
|
|
|
+ deleteWrapper.eq(MtPrinterCate::getPrinterId, mtPrinter.getId());
|
|
|
|
|
+ mtPrinterCateMapper.delete(deleteWrapper);
|
|
|
|
|
+
|
|
|
mtPrinter.setStatus(StatusEnum.DISABLE.getKey());
|
|
mtPrinter.setStatus(StatusEnum.DISABLE.getKey());
|
|
|
mtPrinter.setUpdateTime(new Date());
|
|
mtPrinter.setUpdateTime(new Date());
|
|
|
mtPrinterMapper.updateById(mtPrinter);
|
|
mtPrinterMapper.updateById(mtPrinter);
|
|
@@ -508,6 +605,7 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
String beforePay = params.get("beforePay") == null ? "" : params.get("beforePay").toString();
|
|
String beforePay = params.get("beforePay") == null ? "" : params.get("beforePay").toString();
|
|
|
String afterPay = params.get("afterPay") == null ? "" : params.get("afterPay").toString();
|
|
String afterPay = params.get("afterPay") == null ? "" : params.get("afterPay").toString();
|
|
|
String type = params.get("type") == null ? "" : params.get("type").toString();
|
|
String type = params.get("type") == null ? "" : params.get("type").toString();
|
|
|
|
|
+ String isDefault = params.get("isDefault") == null ? "" : params.get("isDefault").toString();
|
|
|
|
|
|
|
|
LambdaQueryWrapper<MtPrinter> lambdaQueryWrapper = Wrappers.lambdaQuery();
|
|
LambdaQueryWrapper<MtPrinter> lambdaQueryWrapper = Wrappers.lambdaQuery();
|
|
|
if (StringUtils.isNotBlank(status)) {
|
|
if (StringUtils.isNotBlank(status)) {
|
|
@@ -540,11 +638,227 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
if (StringUtils.isNotBlank(type)) {
|
|
if (StringUtils.isNotBlank(type)) {
|
|
|
lambdaQueryWrapper.eq(MtPrinter::getType, type);
|
|
lambdaQueryWrapper.eq(MtPrinter::getType, type);
|
|
|
}
|
|
}
|
|
|
|
|
+ if (StringUtils.isNotBlank(isDefault)) {
|
|
|
|
|
+ lambdaQueryWrapper.eq(MtPrinter::getIsDefault, isDefault);
|
|
|
|
|
+ }
|
|
|
lambdaQueryWrapper.orderByAsc(MtPrinter::getId);
|
|
lambdaQueryWrapper.orderByAsc(MtPrinter::getId);
|
|
|
|
|
|
|
|
return mtPrinterMapper.selectList(lambdaQueryWrapper);
|
|
return mtPrinterMapper.selectList(lambdaQueryWrapper);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 判断该订单是否已配置支付前(下单即打)的订单打印机
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param orderInfo 订单信息
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean hasBeforePayPrinter(UserOrderDto orderInfo) {
|
|
|
|
|
+ if (orderInfo == null || orderInfo.getStoreInfo() == null) {
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("storeId", orderInfo.getStoreInfo().getId());
|
|
|
|
|
+ params.put("status", StatusEnum.ENABLED.getKey());
|
|
|
|
|
+ params.put("type", PrinterTypeEnum.ORDER.getKey());
|
|
|
|
|
+ params.put("autoPrint", YesOrNoEnum.YES.getKey());
|
|
|
|
|
+ params.put("beforePay", YesOrNoEnum.YES.getKey());
|
|
|
|
|
+ List<MtPrinter> printers = queryPrinterListByParams(params);
|
|
|
|
|
+ return printers != null && printers.size() > 0;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询打印机已配置的商品分类ID
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param printerId 打印机ID
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Integer> queryCateIdsByPrinterId(Integer printerId) {
|
|
|
|
|
+ List<Integer> cateIds = new ArrayList<>();
|
|
|
|
|
+ if (printerId == null || printerId < 1) {
|
|
|
|
|
+ return cateIds;
|
|
|
|
|
+ }
|
|
|
|
|
+ LambdaQueryWrapper<MtPrinterCate> lambdaQueryWrapper = Wrappers.lambdaQuery();
|
|
|
|
|
+ lambdaQueryWrapper.eq(MtPrinterCate::getPrinterId, printerId);
|
|
|
|
|
+ lambdaQueryWrapper.eq(MtPrinterCate::getStatus, StatusEnum.ENABLED.getKey());
|
|
|
|
|
+ List<MtPrinterCate> list = mtPrinterCateMapper.selectList(lambdaQueryWrapper);
|
|
|
|
|
+ if (list != null && list.size() > 0) {
|
|
|
|
|
+ for (MtPrinterCate mtPrinterCate : list) {
|
|
|
|
|
+ cateIds.add(mtPrinterCate.getCateId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return cateIds;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 保存打印机与商品分类的映射(先清后插)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param mtPrinter 打印机信息
|
|
|
|
|
+ * @param cateIds 商品分类ID集合
|
|
|
|
|
+ * @param accountInfo 操作人
|
|
|
|
|
+ * @throws BusinessCheckException
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ @OperationServiceLog(description = "保存打印机与商品分类映射")
|
|
|
|
|
+ public void savePrinterCate(MtPrinter mtPrinter, List<Integer> cateIds, AccountInfo accountInfo) throws BusinessCheckException {
|
|
|
|
|
+ if (mtPrinter == null || mtPrinter.getId() == null || mtPrinter.getId() < 1) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 同一个店铺下最多只能有一台默认打印机
|
|
|
|
|
+ if (YesOrNoEnum.YES.getKey().equals(mtPrinter.getIsDefault())) {
|
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
|
+ params.put("status", StatusEnum.ENABLED.getKey());
|
|
|
|
|
+ params.put("storeId", mtPrinter.getStoreId());
|
|
|
|
|
+ params.put("isDefault", YesOrNoEnum.YES.getKey());
|
|
|
|
|
+ List<MtPrinter> printerList = queryPrinterListByParams(params);
|
|
|
|
|
+ if (printerList != null && printerList.size() > 0) {
|
|
|
|
|
+ for (MtPrinter printer : printerList) {
|
|
|
|
|
+ if (!printer.getId().equals(mtPrinter.getId())) {
|
|
|
|
|
+ throw new BusinessCheckException("该店铺已存在默认打印机:" + printer.getName() + ",请先取消其默认设置");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 先删除该打印机原有的映射关系
|
|
|
|
|
+ LambdaQueryWrapper<MtPrinterCate> deleteWrapper = Wrappers.lambdaQuery();
|
|
|
|
|
+ deleteWrapper.eq(MtPrinterCate::getPrinterId, mtPrinter.getId());
|
|
|
|
|
+ mtPrinterCateMapper.delete(deleteWrapper);
|
|
|
|
|
+
|
|
|
|
|
+ if (cateIds == null || cateIds.size() < 1) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+ for (Integer cateId : cateIds) {
|
|
|
|
|
+ if (cateId == null || cateId < 1) {
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+ MtPrinterCate mtPrinterCate = new MtPrinterCate();
|
|
|
|
|
+ mtPrinterCate.setMerchantId(mtPrinter.getMerchantId());
|
|
|
|
|
+ mtPrinterCate.setStoreId(mtPrinter.getStoreId());
|
|
|
|
|
+ mtPrinterCate.setPrinterId(mtPrinter.getId());
|
|
|
|
|
+ mtPrinterCate.setCateId(cateId);
|
|
|
|
|
+ mtPrinterCate.setStatus(StatusEnum.ENABLED.getKey());
|
|
|
|
|
+ mtPrinterCate.setOperator(accountInfo == null ? "" : accountInfo.getAccountName());
|
|
|
|
|
+ mtPrinterCate.setCreateTime(now);
|
|
|
|
|
+ mtPrinterCate.setUpdateTime(now);
|
|
|
|
|
+ mtPrinterCateMapper.insert(mtPrinterCate);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 计算每台打印机需要打印的订单商品行ID
|
|
|
|
|
+ * 规则:
|
|
|
|
|
+ * 1、只有一台打印机时,该打印机打印全部商品,不区分映射
|
|
|
|
|
+ * 2、多台打印机但都没有配置映射时,每台都打印全部商品(兼容未配置映射的场景)
|
|
|
|
|
+ * 3、多台打印机且至少一台配置了映射时,按映射分单:
|
|
|
|
|
+ * 配置了映射的打印机只打印其映射品类内的商品;
|
|
|
|
|
+ * 未配置映射的打印机不打印;
|
|
|
|
|
+ * 默认打印机额外兜底打印未被任何映射认领的商品(无分类或分类未被认领)
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param printers 打印机列表
|
|
|
|
|
+ * @param orderInfo 订单信息
|
|
|
|
|
+ * @param goodsIds 本次新增的订单商品行ID,为空表示订单全部商品
|
|
|
|
|
+ * @return key=打印机ID,value=该打印机需要打印的订单商品行ID集合
|
|
|
|
|
+ */
|
|
|
|
|
+ private Map<Integer, List<Integer>> resolvePrintGoodsIds(List<MtPrinter> printers, UserOrderDto orderInfo, List<Integer> goodsIds) {
|
|
|
|
|
+ Map<Integer, List<Integer>> result = new HashMap<>();
|
|
|
|
|
+ if (printers == null || printers.size() < 1 || orderInfo == null || orderInfo.getGoods() == null || orderInfo.getGoods().size() < 1) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 本次需要打印的商品行:加单时只打印本次新增的商品行
|
|
|
|
|
+ List<OrderGoodsDto> printGoods = new ArrayList<>();
|
|
|
|
|
+ for (OrderGoodsDto goodsDto : orderInfo.getGoods()) {
|
|
|
|
|
+ if (goodsIds == null || goodsIds.size() < 1 || goodsIds.contains(goodsDto.getId())) {
|
|
|
|
|
+ printGoods.add(goodsDto);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (printGoods.size() < 1) {
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 各打印机配置的商品分类
|
|
|
|
|
+ Map<Integer, List<Integer>> printerCateMap = new HashMap<>();
|
|
|
|
|
+ boolean hasMapping = false;
|
|
|
|
|
+ for (MtPrinter printer : printers) {
|
|
|
|
|
+ List<Integer> cateIds = queryCateIdsByPrinterId(printer.getId());
|
|
|
|
|
+ printerCateMap.put(printer.getId(), cateIds);
|
|
|
|
|
+ if (cateIds != null && cateIds.size() > 0) {
|
|
|
|
|
+ hasMapping = true;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 只有一台打印机,或所有打印机都没有配置映射:每台都打印全部商品
|
|
|
|
|
+ if (printers.size() == 1 || !hasMapping) {
|
|
|
|
|
+ List<Integer> allGoodsIds = new ArrayList<>();
|
|
|
|
|
+ for (OrderGoodsDto goodsDto : printGoods) {
|
|
|
|
|
+ allGoodsIds.add(goodsDto.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ for (MtPrinter printer : printers) {
|
|
|
|
|
+ result.put(printer.getId(), allGoodsIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 已被映射认领的商品分类
|
|
|
|
|
+ Set<Integer> mappedCateIds = new HashSet<>();
|
|
|
|
|
+ for (List<Integer> cateIds : printerCateMap.values()) {
|
|
|
|
|
+ if (cateIds != null && cateIds.size() > 0) {
|
|
|
|
|
+ mappedCateIds.addAll(cateIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 默认打印机:未指定时,用ID最小的那台兜底,避免漏打
|
|
|
|
|
+ MtPrinter defaultPrinter = null;
|
|
|
|
|
+ for (MtPrinter printer : printers) {
|
|
|
|
|
+ if (YesOrNoEnum.YES.getKey().equals(printer.getIsDefault())) {
|
|
|
|
|
+ defaultPrinter = printer;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (defaultPrinter == null) {
|
|
|
|
|
+ defaultPrinter = printers.get(0);
|
|
|
|
|
+ logger.warn("打印分单:未指定默认打印机,回退使用打印机{}(ID:{})兜底打印未分配商品", defaultPrinter.getName(), defaultPrinter.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 未被任何映射认领的商品行
|
|
|
|
|
+ List<Integer> unassignedGoodsIds = new ArrayList<>();
|
|
|
|
|
+ for (OrderGoodsDto goodsDto : printGoods) {
|
|
|
|
|
+ if (goodsDto.getCateId() == null || goodsDto.getCateId() < 1 || !mappedCateIds.contains(goodsDto.getCateId())) {
|
|
|
|
|
+ unassignedGoodsIds.add(goodsDto.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (MtPrinter printer : printers) {
|
|
|
|
|
+ List<Integer> cateIds = printerCateMap.get(printer.getId());
|
|
|
|
|
+ List<Integer> myGoodsIds = new ArrayList<>();
|
|
|
|
|
+ if (cateIds != null && cateIds.size() > 0) {
|
|
|
|
|
+ for (OrderGoodsDto goodsDto : printGoods) {
|
|
|
|
|
+ if (goodsDto.getCateId() != null && cateIds.contains(goodsDto.getCateId())) {
|
|
|
|
|
+ myGoodsIds.add(goodsDto.getId());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 默认打印机兜底打印未分配的商品
|
|
|
|
|
+ if (printer.getId().equals(defaultPrinter.getId()) && unassignedGoodsIds.size() > 0) {
|
|
|
|
|
+ for (Integer goodsId : unassignedGoodsIds) {
|
|
|
|
|
+ if (!myGoodsIds.contains(goodsId)) {
|
|
|
|
|
+ myGoodsIds.add(goodsId);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (myGoodsIds.size() > 0) {
|
|
|
|
|
+ result.put(printer.getId(), myGoodsIds);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 同时打印订单和标签(双线程并行执行)
|
|
* 同时打印订单和标签(双线程并行执行)
|
|
|
*
|
|
*
|
|
@@ -552,7 +866,7 @@ public class PrinterServiceImpl extends ServiceImpl<MtPrinterMapper, MtPrinter>
|
|
|
* @param autoPrint 自动打印
|
|
* @param autoPrint 自动打印
|
|
|
* @param beforePay 支付前打印
|
|
* @param beforePay 支付前打印
|
|
|
* @param afterPay 支付后打印
|
|
* @param afterPay 支付后打印
|
|
|
- * @param goodsIds 打印的商品Id
|
|
|
|
|
|
|
+ * @param goodsIds 本次新增的订单商品行ID集合,为空时打印订单全部商品
|
|
|
* @return 包含打印订单和标签结果的Map,key为"order"和"label"
|
|
* @return 包含打印订单和标签结果的Map,key为"order"和"label"
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|