|
@@ -0,0 +1,134 @@
|
|
|
|
|
+package shop.alien.store.controller;
|
|
|
|
|
+
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestParam;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
+import shop.alien.entity.store.PrinterDO;
|
|
|
|
|
+import shop.alien.entity.store.StoreReceiptTemplateConfig;
|
|
|
|
|
+import shop.alien.mapper.PrinterMapper;
|
|
|
|
|
+import shop.alien.store.service.StoreReceiptTemplateConfigService;
|
|
|
|
|
+import shop.alien.store.service.impl.PrintService;
|
|
|
|
|
+import shop.alien.store.service.impl.PrintTemplate;
|
|
|
|
|
+
|
|
|
|
|
+import javax.annotation.Resource;
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+
|
|
|
|
|
+@RestController
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@RequestMapping("/print")
|
|
|
|
|
+public class PrintController {
|
|
|
|
|
+ // 注入数据库Mapper
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private PrinterMapper printerMapper;
|
|
|
|
|
+
|
|
|
|
|
+ // 注入打印服务
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private PrintService printService;
|
|
|
|
|
+
|
|
|
|
|
+ // 门店票据模板服务
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private StoreReceiptTemplateConfigService storeReceiptTemplateConfigService;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 一键打印所有打印机
|
|
|
|
|
+ * 访问地址:http://localhost:8080/print/all
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("/all")
|
|
|
|
|
+ public String printAll(@RequestParam(value = "storeId", required = false) Integer storeId,
|
|
|
|
|
+ @RequestParam(value = "receiptType", required = false) Integer receiptType) {
|
|
|
|
|
+ // 默认店铺与票据类型(1:客单, 2:结账单)
|
|
|
|
|
+ if (storeId == null) storeId = 1;
|
|
|
|
|
+ if (receiptType == null) receiptType = 2;
|
|
|
|
|
+
|
|
|
|
|
+ // 查询数据库所有打印机
|
|
|
|
|
+ List<PrinterDO> list = printerMapper.selectList(new LambdaQueryWrapper<PrinterDO>().isNotNull(PrinterDO::getSn).eq(PrinterDO::getStoreId, storeId).eq(PrinterDO::getDelFlag, 0));
|
|
|
|
|
+
|
|
|
|
|
+ // 获取当前启用的模板(无启用则回落到默认模板)
|
|
|
|
|
+ List<StoreReceiptTemplateConfig> templates = storeReceiptTemplateConfigService.listByStoreAndReceiptType(storeId, receiptType);
|
|
|
|
|
+ StoreReceiptTemplateConfig enabled = null;
|
|
|
|
|
+ for (StoreReceiptTemplateConfig t : templates) {
|
|
|
|
|
+ if (t != null && t.getEnabled() != null && t.getEnabled() == 1) {
|
|
|
|
|
+ enabled = t;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (enabled == null) {
|
|
|
|
|
+ enabled = storeReceiptTemplateConfigService.getDetail(storeId, receiptType, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 组装渲染所需数据(示例数据,可替换为真实订单数据)
|
|
|
|
|
+ Map<String, Object> ctx = new HashMap<>();
|
|
|
|
|
+ ctx.put("ticketName", receiptType != null && receiptType == 2 ? "结账单" : "客单");
|
|
|
|
|
+ ctx.put("storeName", "测试门店");
|
|
|
|
|
+ ctx.put("orderNo", "ORDER20260406001");
|
|
|
|
|
+ ctx.put("tableNo", "A01");
|
|
|
|
|
+ ctx.put("peopleCount", 4);
|
|
|
|
|
+ ctx.put("diningTime", "2026-04-06 10:00");
|
|
|
|
|
+ ctx.put("cashierTime", "2026-04-06 13:00");
|
|
|
|
|
+ ctx.put("name", "张大大");
|
|
|
|
|
+ ctx.put("phone", "18000000000");
|
|
|
|
|
+ ctx.put("beginRemark", "无");
|
|
|
|
|
+
|
|
|
|
|
+ // 品项
|
|
|
|
|
+ List<Map<String, Object>> items = new ArrayList<>();
|
|
|
|
|
+ Map<String, Object> i1 = new HashMap<>();
|
|
|
|
|
+ i1.put("name", "可乐");
|
|
|
|
|
+ i1.put("unitPrice", "3.00");
|
|
|
|
|
+ i1.put("quantity", 2);
|
|
|
|
|
+ i1.put("subtotal", "6.00");
|
|
|
|
|
+ items.add(i1);
|
|
|
|
|
+ Map<String, Object> i2 = new HashMap<>();
|
|
|
|
|
+ i2.put("name", "方便面");
|
|
|
|
|
+ i2.put("unitPrice", "5.00");
|
|
|
|
|
+ i2.put("quantity", 1);
|
|
|
|
|
+ i2.put("subtotal", "5.00");
|
|
|
|
|
+ items.add(i2);
|
|
|
|
|
+ ctx.put("items", items);
|
|
|
|
|
+
|
|
|
|
|
+ // 订单价格合计
|
|
|
|
|
+ ctx.put("dishPriceTotal", "11.00");
|
|
|
|
|
+ ctx.put("serviceFeeTotal", "0.00");
|
|
|
|
|
+ ctx.put("otherServiceFeeTotal", "0.00");
|
|
|
|
|
+ ctx.put("chargeReason", "");
|
|
|
|
|
+ ctx.put("orderTotal", "11.00");
|
|
|
|
|
+
|
|
|
|
|
+ // 支付与结算信息
|
|
|
|
|
+ ctx.put("coupon", "0.00");
|
|
|
|
|
+ ctx.put("manualReduction", "0.00");
|
|
|
|
|
+ ctx.put("reductionReason", "");
|
|
|
|
|
+ ctx.put("settlementMethod", "在线支付");
|
|
|
|
|
+ ctx.put("paymentMethod", "微信支付");
|
|
|
|
|
+ ctx.put("paymentTotal", "11.00");
|
|
|
|
|
+
|
|
|
|
|
+ // 底栏信息
|
|
|
|
|
+ ctx.put("orderBy", "路通");
|
|
|
|
|
+ ctx.put("orderAt", "2026-04-06 10:00");
|
|
|
|
|
+ ctx.put("printBy", "系统");
|
|
|
|
|
+ ctx.put("printAt", String.valueOf(java.time.LocalDateTime.now()));
|
|
|
|
|
+ ctx.put("storeAddress", "XX市XX区XX路100号");
|
|
|
|
|
+ ctx.put("storeTel", "020-12345678");
|
|
|
|
|
+
|
|
|
|
|
+ // 动态渲染小票
|
|
|
|
|
+ String content = PrintTemplate.buildFromConfig(enabled != null ? enabled.getTemplateConfigJson() : "{}", ctx);
|
|
|
|
|
+
|
|
|
|
|
+ // 批量打印
|
|
|
|
|
+ log.info("打印内容:"+ content );
|
|
|
|
|
+ printService.printAll(list, content);
|
|
|
|
|
+ return "已下发打印指令,共" + list.size() + "台打印机";
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 查询所有打印机
|
|
|
|
|
+ * 访问地址:http://localhost:8080/print/list
|
|
|
|
|
+ */
|
|
|
|
|
+ @GetMapping("/list")
|
|
|
|
|
+ public Object list() {
|
|
|
|
|
+ return printerMapper.selectList(null);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|