zxl 2 tygodni temu
rodzic
commit
c3df40fe12

+ 6 - 0
pom.xml

@@ -50,6 +50,12 @@
             <artifactId>javase</artifactId>
             <version>3.5.3</version>
         </dependency>
+        <!-- 本机打印队列持久化 -->
+        <dependency>
+            <groupId>org.xerial</groupId>
+            <artifactId>sqlite-jdbc</artifactId>
+            <version>3.44.1.0</version>
+        </dependency>
         <!-- S7 PLC:大冲 DcPlcClient -->
         <dependency>
             <groupId>com.github.xingshuangs</groupId>

+ 31 - 0
src/main/java/com/mes/entity/DcPrintJob.java

@@ -0,0 +1,31 @@
+package com.mes.entity;
+
+/**
+ * 大冲本机打印队列一行:结案后入队,打印列表页展示与手动送打。
+ */
+public class DcPrintJob {
+
+	/** 批次码 */
+	public String batchCode = "";
+	/** 结案时间 yyyy-MM-dd HH:mm:ss */
+	public String recordTime = "";
+	/** 本批产量 */
+	public int qty;
+	/** 是否已成功送打 */
+	public boolean printed;
+
+	public DcPrintJob() {
+	}
+
+	public DcPrintJob(String batchCode, String recordTime, int qty, boolean printed) {
+		this.batchCode = batchCode == null ? "" : batchCode.trim();
+		this.recordTime = recordTime == null ? "" : recordTime.trim();
+		this.qty = qty;
+		this.printed = printed;
+	}
+
+	/** 列表状态文案 */
+	public String statusText() {
+		return printed ? "已打印" : "未打印";
+	}
+}

+ 33 - 6
src/main/java/com/mes/service/DcBatchPrinter.java

@@ -28,30 +28,36 @@ import java.util.Map;
 /**
  * 批次结案小票:上方居中批次码二维码,下方居中批次码文字。
  * 打印机名由 yml {@code mes.printer-name} 指定;空则用系统默认打印机。
- * 界面「设置 → 打印」开关控制是否真正送打;勾选状态本地记住。
+ * 界面「设置 → 打印」:总开关,关则自动/列表都不送打。
+ * 界面「设置 → 手动打印」:开则结案只入打印列表,工人点选确认后再打。
  */
 public final class DcBatchPrinter {
 
 	private static final Logger log = LoggerFactory.getLogger(DcBatchPrinter.class);
 
-	/** Preferences 节点:记住「打印」开关 */
+	/** Preferences 节点:记住「打印」「手动打印」开关 */
 	private static final String PREF_NODE = "com.mes.dachong";
 	private static final String PREF_PRINT_ENABLED = "printEnabled";
+	private static final String PREF_MANUAL_PRINT = "manualPrintMode";
 
 	/** 二维码边长(像素,按可打印宽度再裁) */
 	private static final int QR_PREF_PX = 200;
 
 	/** 目标打印机名称(空=默认打印机) */
 	private static volatile String printerName = "";
-	/** 界面「打印」开关;默认开 */
+	/** 界面「打印」总开关;默认开 */
 	private static volatile boolean printEnabled = true;
+	/** 界面「手动打印」:开=结案入队不自动打;默认关(结案自动打) */
+	private static volatile boolean manualPrintMode = false;
 
 	static {
 		try {
-			printEnabled = java.util.prefs.Preferences.userRoot()
-					.node(PREF_NODE).getBoolean(PREF_PRINT_ENABLED, true);
+			java.util.prefs.Preferences pref = java.util.prefs.Preferences.userRoot().node(PREF_NODE);
+			printEnabled = pref.getBoolean(PREF_PRINT_ENABLED, true);
+			manualPrintMode = pref.getBoolean(PREF_MANUAL_PRINT, false);
 		} catch (Exception ignored) {
 			printEnabled = true;
+			manualPrintMode = false;
 		}
 	}
 
@@ -82,9 +88,30 @@ public final class DcBatchPrinter {
 		log.info("打印开关={}", Boolean.valueOf(enabled));
 	}
 
+	/** 当前是否手动打印模式(结案入列表、不自动打) */
+	public static boolean isManualPrintMode() {
+		return manualPrintMode;
+	}
+
+	/**
+	 * 设置手动打印模式,并写入本机 Preferences。
+	 *
+	 * @param manual true=结案入队待打;false=结案按「打印」开关自动送打
+	 */
+	public static void setManualPrintMode(boolean manual) {
+		manualPrintMode = manual;
+		try {
+			java.util.prefs.Preferences.userRoot().node(PREF_NODE)
+					.putBoolean(PREF_MANUAL_PRINT, manual);
+		} catch (Exception e) {
+			log.warn("保存手动打印开关失败: {}", e.getMessage());
+		}
+		log.info("手动打印模式={}", Boolean.valueOf(manual));
+	}
+
 	/**
 	 * 连接打印机并送打批次码小票(上二维码、下批次码,均居中)。
-	 * 开关关闭时直接跳过并返回 false。
+	 * 「打印」总开关关闭时直接跳过并返回 false。
 	 *
 	 * @param batchCode 批次码(二维码内容与下方文字)
 	 * @param produceTime 保留入参兼容调用方(本版小票不印)

+ 262 - 0
src/main/java/com/mes/service/DcPrintQueueStore.java

@@ -0,0 +1,262 @@
+package com.mes.service;
+
+import com.mes.entity.DcPrintJob;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * 大冲本机打印队列(SQLite):结案入队未打印,手动送打成功后标已打印。
+ * 库文件:工作目录下 {@code mes_db.db}(与小冲等客户端一致);同批次码再入队重置为未打印并置顶;最多 200 条。
+ */
+public final class DcPrintQueueStore {
+
+	private static final Logger log = LoggerFactory.getLogger(DcPrintQueueStore.class);
+
+	private static final int MAX_SIZE = 200;
+	/** 本机库:mesclient-dachong/mes_db.db(相对进程工作目录) */
+	private static final String JDBC_URL = "jdbc:sqlite:mes_db.db";
+
+	private static final Object LOCK = new Object();
+	private static volatile boolean inited = false;
+
+	private DcPrintQueueStore() {
+	}
+
+	/**
+	 * 结案后入队为未打印;同批次码已存在则更新并置顶。
+	 *
+	 * @param batchCode  批次码
+	 * @param recordTime 结案时间
+	 * @param qty        本批产量
+	 */
+	public static void addPending(String batchCode, String recordTime, int qty) {
+		String code = batchCode == null ? "" : batchCode.trim();
+		if (code.isEmpty()) {
+			return;
+		}
+		String time = recordTime == null ? "" : recordTime.trim();
+		long sortMs = System.currentTimeMillis();
+		synchronized (LOCK) {
+			ensureInit();
+			Connection conn = null;
+			PreparedStatement ps = null;
+			try {
+				conn = open();
+				ps = conn.prepareStatement(
+						"INSERT INTO print_job(batch_code, record_time, qty, printed, sort_ms) "
+								+ "VALUES(?,?,?,0,?) "
+								+ "ON CONFLICT(batch_code) DO UPDATE SET "
+								+ "record_time=excluded.record_time, qty=excluded.qty, "
+								+ "printed=0, sort_ms=excluded.sort_ms");
+				ps.setString(1, code);
+				ps.setString(2, time);
+				ps.setInt(3, qty);
+				ps.setLong(4, sortMs);
+				ps.executeUpdate();
+				trimOverflow(conn);
+			} catch (Exception e) {
+				log.warn("打印队列入队失败 batch={}: {}", code, e.getMessage());
+			} finally {
+				closeQuietly(ps, conn);
+			}
+		}
+	}
+
+	/** 当前队列快照(新→旧) */
+	public static List<DcPrintJob> list() {
+		synchronized (LOCK) {
+			ensureInit();
+			List<DcPrintJob> out = new ArrayList<DcPrintJob>();
+			Connection conn = null;
+			PreparedStatement ps = null;
+			ResultSet rs = null;
+			try {
+				conn = open();
+				ps = conn.prepareStatement(
+						"SELECT batch_code, record_time, qty, printed FROM print_job ORDER BY sort_ms DESC");
+				rs = ps.executeQuery();
+				while (rs.next()) {
+					out.add(new DcPrintJob(
+							rs.getString(1),
+							rs.getString(2),
+							rs.getInt(3),
+							rs.getInt(4) == 1));
+				}
+			} catch (Exception e) {
+				log.warn("打印队列查询失败: {}", e.getMessage());
+			} finally {
+				closeQuietly(rs, ps, conn);
+			}
+			return out;
+		}
+	}
+
+	/**
+	 * 按批次码标记已打印;找不到则忽略。
+	 *
+	 * @param batchCode 批次码
+	 * @return 是否更新到行
+	 */
+	public static boolean markPrinted(String batchCode) {
+		String code = batchCode == null ? "" : batchCode.trim();
+		if (code.isEmpty()) {
+			return false;
+		}
+		synchronized (LOCK) {
+			ensureInit();
+			Connection conn = null;
+			PreparedStatement ps = null;
+			try {
+				conn = open();
+				ps = conn.prepareStatement("UPDATE print_job SET printed=1 WHERE batch_code=?");
+				ps.setString(1, code);
+				return ps.executeUpdate() > 0;
+			} catch (Exception e) {
+				log.warn("打印队列标已打印失败 batch={}: {}", code, e.getMessage());
+				return false;
+			} finally {
+				closeQuietly(ps, conn);
+			}
+		}
+	}
+
+	/**
+	 * 按批次码取一条(确认打印时读数量/时间)。
+	 *
+	 * @param batchCode 批次码
+	 * @return 任务;没有则 null
+	 */
+	public static DcPrintJob findByBatchCode(String batchCode) {
+		String code = batchCode == null ? "" : batchCode.trim();
+		if (code.isEmpty()) {
+			return null;
+		}
+		synchronized (LOCK) {
+			ensureInit();
+			Connection conn = null;
+			PreparedStatement ps = null;
+			ResultSet rs = null;
+			try {
+				conn = open();
+				ps = conn.prepareStatement(
+						"SELECT batch_code, record_time, qty, printed FROM print_job WHERE batch_code=?");
+				ps.setString(1, code);
+				rs = ps.executeQuery();
+				if (rs.next()) {
+					return new DcPrintJob(
+							rs.getString(1),
+							rs.getString(2),
+							rs.getInt(3),
+							rs.getInt(4) == 1);
+				}
+			} catch (Exception e) {
+				log.warn("打印队列点查失败 batch={}: {}", code, e.getMessage());
+			} finally {
+				closeQuietly(rs, ps, conn);
+			}
+			return null;
+		}
+	}
+
+	public static int size() {
+		synchronized (LOCK) {
+			ensureInit();
+			Connection conn = null;
+			PreparedStatement ps = null;
+			ResultSet rs = null;
+			try {
+				conn = open();
+				ps = conn.prepareStatement("SELECT COUNT(1) FROM print_job");
+				rs = ps.executeQuery();
+				if (rs.next()) {
+					return rs.getInt(1);
+				}
+			} catch (Exception e) {
+				log.warn("打印队列计数失败: {}", e.getMessage());
+			} finally {
+				closeQuietly(rs, ps, conn);
+			}
+			return 0;
+		}
+	}
+
+	/** 建表(只做一次);库文件为工作目录 mes_db.db */
+	private static void ensureInit() {
+		if (inited) {
+			return;
+		}
+		try {
+			Class.forName("org.sqlite.JDBC");
+			Connection conn = null;
+			Statement st = null;
+			try {
+				conn = open();
+				st = conn.createStatement();
+				st.executeUpdate(
+						"CREATE TABLE IF NOT EXISTS print_job ("
+								+ "batch_code TEXT PRIMARY KEY NOT NULL,"
+								+ "record_time TEXT,"
+								+ "qty INTEGER NOT NULL DEFAULT 0,"
+								+ "printed INTEGER NOT NULL DEFAULT 0,"
+								+ "sort_ms INTEGER NOT NULL"
+								+ ")");
+				st.executeUpdate(
+						"CREATE INDEX IF NOT EXISTS idx_print_job_sort ON print_job(sort_ms DESC)");
+			} finally {
+				closeQuietly(st, conn);
+			}
+			inited = true;
+			File db = new File("mes_db.db");
+			log.info("打印队列 SQLite 就绪: {}", db.getAbsolutePath());
+		} catch (Exception e) {
+			log.error("初始化打印队列 SQLite 失败: {}", e.getMessage(), e);
+			throw new IllegalStateException("打印队列库初始化失败: " + e.getMessage(), e);
+		}
+	}
+
+	private static Connection open() throws SQLException {
+		return DriverManager.getConnection(JDBC_URL);
+	}
+
+	/** 超出上限删最旧的 */
+	private static void trimOverflow(Connection conn) throws SQLException {
+		PreparedStatement ps = null;
+		try {
+			ps = conn.prepareStatement(
+					"DELETE FROM print_job WHERE batch_code NOT IN ("
+							+ "SELECT batch_code FROM print_job ORDER BY sort_ms DESC LIMIT ?"
+							+ ")");
+			ps.setInt(1, MAX_SIZE);
+			ps.executeUpdate();
+		} finally {
+			closeQuietly(ps);
+		}
+	}
+
+	private static void closeQuietly(AutoCloseable... closeables) {
+		if (closeables == null) {
+			return;
+		}
+		for (int i = 0; i < closeables.length; i++) {
+			AutoCloseable c = closeables[i];
+			if (c == null) {
+				continue;
+			}
+			try {
+				c.close();
+			} catch (Exception ignored) {
+				// ignore
+			}
+		}
+	}
+}

+ 43 - 3
src/main/java/com/mes/ui/MainFrame.java

@@ -22,8 +22,8 @@ import java.util.TimerTask;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 /**
- * 自动化大冲主窗:工作面板(DB260 取批)+ 五台压机过程页 + 工作记录。
- * 结案打印由压机5冲次达标触发;各页独立调度,一台 PLC 挂死不影响其它页。
+ * 自动化大冲主窗:工作面板(DB260 取批)+ 五台压机过程页 + 工作记录 + 打印列表。
+ * 结案打印由压机5冲次达标触发(手动打印模式则入列表待工人确认);各页独立调度。
  */
 public class MainFrame extends JFrame {
 
@@ -41,6 +41,8 @@ public class MainFrame extends JFrame {
 	private static final List<PressPanel> pressPanels = new ArrayList<PressPanel>();
 	/** 工作记录页(结案后刷新) */
 	private static WorkRecordPanel workRecordPanel;
+	/** 打印列表页(结案入队 / 手动送打后刷新) */
+	private static PrintListPanel printListPanel;
 	private static Timer lineTickTimer;
 	private static final List<Timer> pressTickTimers = new ArrayList<Timer>();
 	/** 取码队列 SSE */
@@ -286,6 +288,7 @@ public class MainFrame extends JFrame {
 			break;
 		}
 		workRecordPanel = new WorkRecordPanel();
+		printListPanel = new PrintListPanel();
 
 		int sampleMs = props != null && props.getPressSampleMs() > 0 ? props.getPressSampleMs() : 1000;
 		int uploadMax = props != null && props.getPressUploadMax() > 0 ? props.getPressUploadMax() : 20;
@@ -306,6 +309,7 @@ public class MainFrame extends JFrame {
 			cards.add(pp, "press-" + pp.getPressNo());
 		}
 		cards.add(workRecordPanel, "record");
+		cards.add(printListPanel, "print");
 
 		final JButton btnSettings = navBtn("设置", false);
 		final JButton btnWork = navBtn("工作面板", true);
@@ -314,11 +318,13 @@ public class MainFrame extends JFrame {
 			btnPresses.add(navBtn(pp.tabTitle(), false));
 		}
 		final JButton btnRecord = navBtn("工作记录", false);
+		final JButton btnPrintList = navBtn("打印列表", false);
 
 		navButtons.add(btnSettings);
 		navButtons.add(btnWork);
 		navButtons.addAll(btnPresses);
 		navButtons.add(btnRecord);
+		navButtons.add(btnPrintList);
 
 		// 重量级弹出,避免被卡片层盖住像点了没反应
 		final JPopupMenu settingsMenu = new JPopupMenu();
@@ -346,7 +352,7 @@ public class MainFrame extends JFrame {
 		settingsMenu.add(miNext);
 		settingsMenu.add(miManual);
 		settingsMenu.addSeparator();
-		// 设置 → 打印:勾选=结案送打,取消=不打印;状态本机记住
+		// 设置 → 打印:总开关;关则自动/列表都不送打
 		final JCheckBoxMenuItem miPrint = new JCheckBoxMenuItem("打印");
 		miPrint.setFont(new Font("微软雅黑", Font.PLAIN, 14));
 		miPrint.setSelected(DcBatchPrinter.isPrintEnabled());
@@ -357,10 +363,22 @@ public class MainFrame extends JFrame {
 			}
 		});
 		settingsMenu.add(miPrint);
+		// 设置 → 手动打印:开=结案入列表;关=结案自动打
+		final JCheckBoxMenuItem miManualPrint = new JCheckBoxMenuItem("手动打印");
+		miManualPrint.setFont(new Font("微软雅黑", Font.PLAIN, 14));
+		miManualPrint.setSelected(DcBatchPrinter.isManualPrintMode());
+		miManualPrint.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				DcBatchPrinter.setManualPrintMode(miManualPrint.isSelected());
+			}
+		});
+		settingsMenu.add(miManualPrint);
 		btnSettings.addActionListener(new ActionListener() {
 			@Override
 			public void actionPerformed(ActionEvent e) {
 				miPrint.setSelected(DcBatchPrinter.isPrintEnabled());
+				miManualPrint.setSelected(DcBatchPrinter.isManualPrintMode());
 				settingsMenu.show(btnSettings, 0, btnSettings.getHeight());
 			}
 		});
@@ -387,6 +405,15 @@ public class MainFrame extends JFrame {
 				showCard(cards, "record", btnRecord);
 			}
 		});
+		btnPrintList.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				showCard(cards, "print", btnPrintList);
+				if (printListPanel != null) {
+					printListPanel.reload();
+				}
+			}
+		});
 
 		JPanel nav = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
 		nav.setOpaque(true);
@@ -398,6 +425,7 @@ public class MainFrame extends JFrame {
 			nav.add(b);
 		}
 		nav.add(btnRecord);
+		nav.add(btnPrintList);
 
 		JPanel page = new JPanel(new BorderLayout(0, 0));
 		page.setOpaque(false);
@@ -460,6 +488,18 @@ public class MainFrame extends JFrame {
 		});
 	}
 
+	/** 结案入队 / 送打后刷新打印列表 */
+	public static void refreshPrintList() {
+		SwingUtilities.invokeLater(new Runnable() {
+			@Override
+			public void run() {
+				if (printListPanel != null) {
+					printListPanel.reload();
+				}
+			}
+		});
+	}
+
 	/** 线体与各压机各自 Timer,互不阻塞 */
 	private static void startTickTimers() {
 		stopTickTimers();

+ 215 - 0
src/main/java/com/mes/ui/PrintListPanel.java

@@ -0,0 +1,215 @@
+package com.mes.ui;
+
+import com.mes.entity.DcPrintJob;
+import com.mes.service.DcBatchPrinter;
+import com.mes.service.DcPrintQueueStore;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import javax.swing.table.DefaultTableCellRenderer;
+import javax.swing.table.DefaultTableModel;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+import java.util.List;
+
+/**
+ * 打印列表页:批次码 / 时间 / 数量 / 状态;点选确认后手动送打。
+ */
+public class PrintListPanel extends JPanel {
+
+	private static final long serialVersionUID = 1L;
+	private static final Logger log = LoggerFactory.getLogger(PrintListPanel.class);
+
+	private final DefaultTableModel model;
+	private final JTable table;
+	private final JLabel lblStatus;
+	private final JButton btnRefresh;
+	private final JButton btnPrint;
+
+	public PrintListPanel() {
+		setLayout(new BorderLayout(0, 0));
+		setBackground(UiTheme.BG);
+		setBorder(new EmptyBorder(0, 0, 0, 0));
+
+		JPanel header = UiTheme.gradientHeader();
+		header.setBorder(new EmptyBorder(12, 16, 12, 16));
+		JLabel title = new JLabel("打印列表");
+		title.setFont(UiTheme.FONT_TITLE);
+		title.setForeground(Color.WHITE);
+
+		// 顶栏按钮:刷新 + 打印
+		JPanel right = new JPanel(new FlowLayout(FlowLayout.RIGHT, 8, 0));
+		right.setOpaque(false);
+		btnRefresh = headerBtn("刷新");
+		btnPrint = headerBtn("打印");
+		btnRefresh.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				reload();
+			}
+		});
+		btnPrint.addActionListener(new ActionListener() {
+			@Override
+			public void actionPerformed(ActionEvent e) {
+				printSelected();
+			}
+		});
+		right.add(btnRefresh);
+		right.add(btnPrint);
+		header.add(title, BorderLayout.WEST);
+		header.add(right, BorderLayout.EAST);
+		add(header, BorderLayout.NORTH);
+
+		String[] cols = new String[] { "批次码", "记录时间", "数量", "状态" };
+		model = new DefaultTableModel(cols, 0) {
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			public boolean isCellEditable(int row, int column) {
+				return false;
+			}
+		};
+		table = new JTable(model);
+		table.setFont(UiTheme.FONT_BODY);
+		table.setRowHeight(28);
+		table.getTableHeader().setFont(new Font("微软雅黑", Font.BOLD, 13));
+		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+		table.setAutoCreateRowSorter(true);
+		DefaultTableCellRenderer center = new DefaultTableCellRenderer();
+		center.setHorizontalAlignment(SwingConstants.CENTER);
+		for (int i = 0; i < table.getColumnCount(); i++) {
+			table.getColumnModel().getColumn(i).setCellRenderer(center);
+		}
+		table.getColumnModel().getColumn(0).setPreferredWidth(220);
+		table.getColumnModel().getColumn(1).setPreferredWidth(160);
+		table.getColumnModel().getColumn(2).setPreferredWidth(70);
+		table.getColumnModel().getColumn(3).setPreferredWidth(80);
+		// 双击等同点「打印」
+		table.addMouseListener(new MouseAdapter() {
+			@Override
+			public void mouseClicked(MouseEvent e) {
+				if (e.getClickCount() >= 2) {
+					printSelected();
+				}
+			}
+		});
+
+		JScrollPane scroll = new JScrollPane(table);
+		scroll.setBorder(BorderFactory.createEmptyBorder(12, 16, 8, 16));
+		scroll.getViewport().setBackground(UiTheme.CARD);
+		add(scroll, BorderLayout.CENTER);
+
+		lblStatus = new JLabel(" ");
+		lblStatus.setFont(UiTheme.FONT_BODY);
+		lblStatus.setBorder(new EmptyBorder(8, 16, 12, 16));
+		lblStatus.setForeground(UiTheme.MUTED);
+		add(lblStatus, BorderLayout.SOUTH);
+
+		reload();
+	}
+
+	/** 顶栏描边按钮(深绿底白字) */
+	private static JButton headerBtn(String text) {
+		JButton btn = new JButton(text);
+		btn.setFont(new Font("微软雅黑", Font.PLAIN, 14));
+		btn.setForeground(Color.WHITE);
+		btn.setFocusPainted(false);
+		btn.setContentAreaFilled(false);
+		btn.setOpaque(false);
+		btn.setBorder(BorderFactory.createCompoundBorder(
+				BorderFactory.createLineBorder(new Color(140, 210, 220)),
+				new EmptyBorder(6, 14, 6, 14)));
+		btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
+		return btn;
+	}
+
+	/** 从本机打印队列刷新表格 */
+	public void reload() {
+		List<DcPrintJob> list = DcPrintQueueStore.list();
+		model.setRowCount(0);
+		int pending = 0;
+		for (int i = 0; i < list.size(); i++) {
+			DcPrintJob job = list.get(i);
+			if (job == null) {
+				continue;
+			}
+			if (!job.printed) {
+				pending++;
+			}
+			model.addRow(new Object[] {
+					job.batchCode,
+					job.recordTime,
+					Integer.valueOf(job.qty),
+					job.statusText()
+			});
+		}
+		lblStatus.setText("共 " + list.size() + " 条,未打印 " + pending + " 条");
+		lblStatus.setForeground(UiTheme.MUTED);
+	}
+
+	/**
+	 * 对当前选中行确认后送打;成功则标已打印并刷新。
+	 */
+	private void printSelected() {
+		int viewRow = table.getSelectedRow();
+		if (viewRow < 0) {
+			JOptionPane.showMessageDialog(this, "请先选中一条批次记录", "打印",
+					JOptionPane.INFORMATION_MESSAGE);
+			return;
+		}
+		int modelRow = table.convertRowIndexToModel(viewRow);
+		Object codeObj = model.getValueAt(modelRow, 0);
+		final String batchCode = codeObj == null ? "" : String.valueOf(codeObj).trim();
+		if (batchCode.isEmpty()) {
+			return;
+		}
+		if (!DcBatchPrinter.isPrintEnabled()) {
+			JOptionPane.showMessageDialog(this,
+					"打印总开关已关闭,请先在「设置 → 打印」中打开。",
+					"打印", JOptionPane.WARNING_MESSAGE);
+			return;
+		}
+		int ok = JOptionPane.showConfirmDialog(this,
+				"是否打印批次 " + batchCode + "?",
+				"确认打印", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
+		if (ok != JOptionPane.YES_OPTION) {
+			return;
+		}
+		final DcPrintJob job = DcPrintQueueStore.findByBatchCode(batchCode);
+		final String time = job == null ? "" : job.recordTime;
+		final int qty = job == null ? 0 : job.qty;
+		btnPrint.setEnabled(false);
+		lblStatus.setText("正在送打 " + batchCode + " …");
+		new Thread(new Runnable() {
+			@Override
+			public void run() {
+				final boolean success = DcBatchPrinter.printBatch(batchCode, time, qty);
+				SwingUtilities.invokeLater(new Runnable() {
+					@Override
+					public void run() {
+						btnPrint.setEnabled(true);
+						if (success) {
+							DcPrintQueueStore.markPrinted(batchCode);
+							reload();
+							lblStatus.setText("已送打:" + batchCode);
+							lblStatus.setForeground(new Color(40, 140, 80));
+							log.info("打印列表手动送打成功 batch={}", batchCode);
+						} else {
+							lblStatus.setText("送打失败或无打印机:" + batchCode);
+							lblStatus.setForeground(new Color(200, 70, 70));
+							JOptionPane.showMessageDialog(PrintListPanel.this,
+									"送打失败,请检查打印机与「设置 → 打印」开关。",
+									"打印", JOptionPane.WARNING_MESSAGE);
+							log.warn("打印列表手动送打失败 batch={}", batchCode);
+						}
+					}
+				});
+			}
+		}, "dc-manual-print").start();
+	}
+}

+ 35 - 12
src/main/java/com/mes/ui/StationPanel.java

@@ -6,6 +6,7 @@ import com.mes.entity.DcRunSnapshot;
 import com.mes.service.DcBatchPrinter;
 import com.mes.service.DcLineBatchContext;
 import com.mes.service.DcPlcClient;
+import com.mes.service.DcPrintQueueStore;
 import com.mes.service.DcWorkRecordStore;
 import com.mes.service.MesDcApiClient;
 import org.slf4j.Logger;
@@ -695,19 +696,33 @@ public class StationPanel extends JPanel {
 					pendingQty = qty;
 					pendingPrinted = false;
 					fieldsReady = true;
-					// 打印异步:打印机卡死不能挡 MES 结案;小票打批次码
+					// 手动打印:只入列表不自动打;自动模式:异步送打(卡死不能挡结案)
 					final String printTime = time;
 					final String printBatch = codeSnap;
-					new Thread(new Runnable() {
-						@Override
-						public void run() {
-							boolean ok = DcBatchPrinter.printBatch(printBatch, printTime, qty);
-							pendingPrinted = ok;
-							if (!ok) {
-								log.warn("工位[{}] 异步打印失败/无打印机 batch={}", cfg.getStationSn(), printBatch);
+					final int printQty = qty;
+					if (DcBatchPrinter.isManualPrintMode()) {
+						DcPrintQueueStore.addPending(printBatch, printTime, printQty);
+						MainFrame.refreshPrintList();
+						pendingPrinted = false;
+						log.info("工位[{}] 手动打印模式:批次已入打印列表 batch={}", cfg.getStationSn(), printBatch);
+						setStatusUi("已入打印列表,请到「打印列表」确认送打");
+					} else {
+						new Thread(new Runnable() {
+							@Override
+							public void run() {
+								boolean ok = DcBatchPrinter.printBatch(printBatch, printTime, printQty);
+								pendingPrinted = ok;
+								if (ok) {
+									// 自动打成功也记一笔已打印,便于打印列表追溯
+									DcPrintQueueStore.addPending(printBatch, printTime, printQty);
+									DcPrintQueueStore.markPrinted(printBatch);
+									MainFrame.refreshPrintList();
+								} else {
+									log.warn("工位[{}] 异步打印失败/无打印机 batch={}", cfg.getStationSn(), printBatch);
+								}
 							}
-						}
-					}, "dc-print-" + cfg.getStationSn()).start();
+						}, "dc-print-" + cfg.getStationSn()).start();
+					}
 					// 先放 busy:phase=DONE,tick 可刷灯并可走续传
 					busy.set(false);
 					uploadPendingComplete(true);
@@ -774,9 +789,17 @@ public class StationPanel extends JPanel {
 			}
 			if (uploaded) {
 				pendingCompleteFields = null;
+				// 手动模式:未自动送打,提示去打印列表;自动模式按是否真正送打成功区分
+				String printTip;
+				if (DcBatchPrinter.isManualPrintMode()) {
+					printTip = "  待手动打印";
+				} else if (pendingPrinted) {
+					printTip = "  已送打印";
+				} else {
+					printTip = "  打印已提交/待确认";
+				}
 				final String tip = how + "  批次码=" + batch + "  数量=" + qty
-						+ (pendingPrinted ? "  已送打印" : "  打印已提交/待确认")
-						+ "  已上传";
+						+ printTip + "  已上传";
 				resetToIdle(tip);
 				MainFrame.setMenuStatus(cfg.getStationSn() + " 结案 " + batch + " x" + qty, 0);
 				log.info("工位[{}] 结案成功 batch={} qty={}", cfg.getStationSn(), batch, Integer.valueOf(qty));

+ 1 - 1
src/main/resources/config/application.yml

@@ -23,7 +23,7 @@ mes:
   # 结案小票连接的系统打印机名;空=用 Windows 默认打印机
   printer-name:
   # 是否写 PLC MES允许运行点。false=开批/结案/清码都不写,本地仍取批盯产量
-  mes-allow-enabled: false
+  mes-allow-enabled: true
 
   # 线体主站(唯一取码工位;五台压机共用此工位号)
   stations: