hou 1 週間 前
コミット
e3d83eddc2

+ 170 - 0
src/com/mes/component/ProductTypePanel.java

@@ -0,0 +1,170 @@
+package com.mes.component;
+
+import javax.swing.*;
+import javax.swing.border.EmptyBorder;
+import javax.swing.border.LineBorder;
+import java.awt.*;
+import java.awt.event.ActionEvent;
+
+public class ProductTypePanel extends JPanel {
+
+    public static final String TYPE_ZJ = "ZJ";
+    public static final String TYPE_SJ = "SJ";
+
+    private static final Color COLOR_SELECTED = new Color(24, 144, 255);
+    private static final Color COLOR_BG = new Color(245, 247, 250);
+    private static final Color COLOR_BORDER = new Color(217, 217, 217);
+    private static final Color COLOR_TEXT = new Color(89, 89, 89);
+
+    private final String switchPassword;
+    private String result = TYPE_ZJ;
+    private JButton zjButton;
+    private JButton sjButton;
+    private Runnable onChangeListener;
+    private boolean suppressPasswordCheck = false;
+
+    public ProductTypePanel(String switchPassword) {
+        this.switchPassword = switchPassword;
+        setLayout(new GridLayout(1, 2, 12, 0));
+        setOpaque(false);
+        setBorder(BorderFactory.createCompoundBorder(
+                new LineBorder(COLOR_BORDER, 1, true),
+                new EmptyBorder(8, 10, 8, 10)
+        ));
+        setBackground(COLOR_BG);
+
+        zjButton = createTypeButton("智界", TYPE_ZJ);
+        sjButton = createTypeButton("尚界", TYPE_SJ);
+        add(zjButton);
+        add(sjButton);
+        refreshButtonStyle();
+    }
+
+    private JButton createTypeButton(String title, String type) {
+        JButton button = new JButton(title) {
+            @Override
+            protected void paintComponent(Graphics g) {
+                Graphics2D g2 = (Graphics2D) g.create();
+                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+                if (getModel().isPressed()) {
+                    g2.setColor(COLOR_SELECTED.darker());
+                } else if (type.equals(result)) {
+                    g2.setColor(isEnabled() ? COLOR_SELECTED : COLOR_SELECTED.brighter());
+                } else {
+                    g2.setColor(isEnabled() ? Color.WHITE : new Color(250, 250, 250));
+                }
+                g2.fillRoundRect(0, 0, getWidth(), getHeight(), 16, 16);
+                g2.dispose();
+                super.paintComponent(g);
+            }
+        };
+        button.setName(type);
+        button.setFont(new Font("微软雅黑", Font.BOLD, 26));
+        button.setFocusPainted(false);
+        button.setBorderPainted(false);
+        button.setContentAreaFilled(false);
+        button.setPreferredSize(new Dimension(180, 52));
+        button.addActionListener(e -> selectType(type, e));
+        return button;
+    }
+
+    private void selectType(String type, ActionEvent e) {
+        if (!isEnabled()) {
+            return;
+        }
+        if (type.equals(result)) {
+            return;
+        }
+        if (!suppressPasswordCheck && !verifySwitchPassword()) {
+            refreshButtonStyle();
+            return;
+        }
+        result = type;
+        refreshButtonStyle();
+        if (onChangeListener != null) {
+            onChangeListener.run();
+        }
+    }
+
+    private boolean verifySwitchPassword() {
+        JPasswordField passwordField = new JPasswordField(15);
+        JPanel panel = new JPanel(new BorderLayout(8, 8));
+        panel.add(new JLabel("请输入切换密码:"), BorderLayout.NORTH);
+        panel.add(passwordField, BorderLayout.CENTER);
+
+        int option = JOptionPane.showConfirmDialog(
+                SwingUtilities.getWindowAncestor(this),
+                panel,
+                "切换生产类型",
+                JOptionPane.OK_CANCEL_OPTION,
+                JOptionPane.PLAIN_MESSAGE
+        );
+        if (option != JOptionPane.OK_OPTION) {
+            return false;
+        }
+        if (!switchPassword.equals(new String(passwordField.getPassword()))) {
+            JOptionPane.showMessageDialog(
+                    SwingUtilities.getWindowAncestor(this),
+                    "密码错误,无法切换生产类型",
+                    "提示",
+                    JOptionPane.WARNING_MESSAGE
+            );
+            return false;
+        }
+        return true;
+    }
+
+    private void refreshButtonStyle() {
+        styleButton(zjButton, TYPE_ZJ.equals(result));
+        styleButton(sjButton, TYPE_SJ.equals(result));
+        repaint();
+    }
+
+    private void styleButton(JButton button, boolean selected) {
+        if (!button.isEnabled()) {
+            button.setForeground(selected ? Color.WHITE : new Color(191, 191, 191));
+            return;
+        }
+        if (selected) {
+            button.setForeground(Color.WHITE);
+        } else {
+            button.setForeground(COLOR_TEXT);
+        }
+        button.repaint();
+    }
+
+    public void setOnChangeListener(Runnable onChangeListener) {
+        this.onChangeListener = onChangeListener;
+    }
+
+    public void setResult(String type) {
+        suppressPasswordCheck = true;
+        try {
+            if (TYPE_SJ.equals(type)) {
+                result = TYPE_SJ;
+            } else {
+                result = TYPE_ZJ;
+            }
+            refreshButtonStyle();
+        } finally {
+            suppressPasswordCheck = false;
+        }
+    }
+
+    public String getResult() {
+        return result;
+    }
+
+    public String getTypeName() {
+        return TYPE_SJ.equals(result) ? "尚界" : "智界";
+    }
+
+    @Override
+    public void setEnabled(boolean enabled) {
+        super.setEnabled(enabled);
+        zjButton.setEnabled(enabled);
+        sjButton.setEnabled(enabled);
+        refreshButtonStyle();
+        repaint();
+    }
+}

+ 173 - 2
src/com/mes/ui/MesClient.java

@@ -8,6 +8,7 @@ import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
 import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
 import com.mes.component.ConfigPanel;
 import com.mes.component.MesRadio;
+import com.mes.component.ProductTypePanel;
 import com.mes.component.MesWebView;
 import com.mes.netty.NettyClient;
 import com.mes.util.DateLocalUtils;
@@ -68,6 +69,16 @@ public class MesClient extends JFrame {
 
     public static Boolean check_quality_result = false;
     public static Integer work_status = 0;
+    public static ProductTypePanel productTypePanel;
+    public static JLabel productTypeCurrentLabel;
+
+    public static final int PRODUCT_SN_LENGTH = 32;
+    public static final String PREFIX_ZJ = "000020021815-01000016";  //智界
+    public static final String PREFIX_SJ = "000020022029-01000016";  //尚界
+    public static final boolean PRODUCT_TYPE_SWITCH_ENABLED = false;
+    public static final String PRODUCT_TYPE_SWITCH_PASSWORD = "503833";
+    public static final String PRODUCT_TYPE_ZJ = ProductTypePanel.TYPE_ZJ;
+    public static final String PRODUCT_TYPE_SJ = ProductTypePanel.TYPE_SJ;
 
     public static JButton heart_beat_menu;
     public static JButton status_menu;
@@ -568,6 +579,7 @@ public class MesClient extends JFrame {
     }
 
     public static void initWarehouseData(){
+        loadProductTypeFromDb();
         resetScanA();
     }
 
@@ -607,7 +619,12 @@ public class MesClient extends JFrame {
         if (f_scan_data_bt_2 != null) {
             f_scan_data_bt_2.setEnabled(false);
         }
-        MesClient.setMenuStatus("请扫工件码",0);
+        updateProductTypePanelEnabled(true);
+        if (PRODUCT_TYPE_SWITCH_ENABLED) {
+            MesClient.setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0);
+        } else {
+            MesClient.setMenuStatus("请扫工件码",0);
+        }
 
         // 不再重置为固定值,而是保留加载的配置值
         // MesClient.aMax = 11;
@@ -697,9 +714,18 @@ public class MesClient extends JFrame {
                         if (hgB) {
                             sn = MesClient.s7PLC.readString("DB32.268.0");
                         }
+                        if (sn != null) {
+                            sn = sn.trim();
+                        }
+                        String validateMsg = validateProductSnMessage(sn);
+                        if (validateMsg != null) {
+                            setMenuStatus(validateMsg, -1);
+                            return;
+                        }
                         curSn = sn;
                         product_sn.setText(sn);
                         work_status_jqb = 1;
+                        updateProductTypePanelEnabled(false);
                     }
                 }else if (work_status_jqb == 1){
                     // 已扫码,等待允许加工信号(只发一次,等回包)
@@ -737,6 +763,85 @@ public class MesClient extends JFrame {
         },100,1000);
     }
 
+    public static void loadProductTypeFromDb() {
+        if (productTypePanel == null) {
+            return;
+        }
+        String savedType = JdbcUtils.getClientConfig(
+                JdbcUtils.getProductTypeConfigKey(mes_gw),
+                PRODUCT_TYPE_ZJ
+        );
+        if (PRODUCT_TYPE_SJ.equals(savedType)) {
+            productTypePanel.setResult(PRODUCT_TYPE_SJ);
+        } else {
+            productTypePanel.setResult(PRODUCT_TYPE_ZJ);
+        }
+        refreshProductTypeCurrentLabel();
+    }
+
+    public static void refreshProductTypeCurrentLabel() {
+        if (productTypeCurrentLabel != null) {
+            productTypeCurrentLabel.setText("当前选择:" + getProductTypeName() + "(前缀 " + getProductSnPrefix() + ")");
+        }
+    }
+
+    public static void saveProductTypeToDb() {
+        if (productTypePanel == null) {
+            return;
+        }
+        JdbcUtils.saveClientConfig(
+                JdbcUtils.getProductTypeConfigKey(mes_gw),
+                productTypePanel.getResult()
+        );
+    }
+
+    public static String getProductSnPrefix() {
+        if (productTypePanel != null && PRODUCT_TYPE_SJ.equals(productTypePanel.getResult())) {
+            return PREFIX_SJ;
+        }
+        return PREFIX_ZJ;
+    }
+
+    public static String getProductTypeName() {
+        if (productTypePanel != null) {
+            return productTypePanel.getTypeName();
+        }
+        return "智界";
+    }
+
+    private static String getProductTypeNameBySn(String sn) {
+        if (sn.startsWith(PREFIX_SJ)) {
+            return "尚界";
+        }
+        if (sn.startsWith(PREFIX_ZJ)) {
+            return "智界";
+        }
+        return "未知";
+    }
+
+    private static String validateProductSnMessage(String sn) {
+        if (sn == null || sn.isEmpty()) {
+            return "工件码为空,请重试";
+        }
+        if (sn.length() != PRODUCT_SN_LENGTH) {
+            return "工件码长度须为32位,当前" + sn.length() + "位";
+        }
+        if (!PRODUCT_TYPE_SWITCH_ENABLED) {
+            return null;
+        }
+        String prefix = getProductSnPrefix();
+        if (!sn.startsWith(prefix)) {
+            return "工件码前缀与当前类型(" + getProductTypeName() + ")不匹配,当前工件码为" + getProductTypeNameBySn(sn);
+        }
+        return null;
+    }
+
+    public static void updateProductTypePanelEnabled(boolean enabled) {
+        if (productTypePanel != null) {
+            productTypePanel.setEnabled(enabled);
+        }
+    }
+
     public static void scanBarcode() {
         if (work_status != 0) {
             status_menu.setText("工件加工中,请勿重复扫码");
@@ -788,6 +893,7 @@ public class MesClient extends JFrame {
         //弹窗扫工件码
         String scanBarcode = JOptionPane.showInputDialog(null, scanBarcodeTitle);
         if(scanBarcode!=null&&!scanBarcode.equalsIgnoreCase("")) {
+            scanBarcode = scanBarcode.trim();
             //获取用户
             getUser();
             //获取扫码内容36位
@@ -803,8 +909,17 @@ public class MesClient extends JFrame {
             //刷新界面
             mesClientFrame.repaint();
 
+            String validateMsg = validateProductSnMessage(scanBarcode);
+            if (validateMsg != null) {
+                updateProductTypePanelEnabled(true);
+                MesClient.setMenuStatus(validateMsg, -1);
+                return;
+            }
+            updateProductTypePanelEnabled(false);
+
             if(!tcp_connect_flag) {
                 JOptionPane.showMessageDialog(mesClientFrame,"设备未连接Mes服务器","提示窗口", JOptionPane.INFORMATION_MESSAGE);
+                updateProductTypePanelEnabled(true);
                 return;
             }
 
@@ -812,6 +927,7 @@ public class MesClient extends JFrame {
             Boolean sendret = DataUtil.checkQuality(nettyClient,barcode36,user20);
             if(!sendret){
                 JOptionPane.showMessageDialog(mesClientFrame,"消息发送失败,请重试","提示窗口", JOptionPane.INFORMATION_MESSAGE);
+                updateProductTypePanelEnabled(true);
                 return;
             }
         }else {
@@ -1153,6 +1269,11 @@ public class MesClient extends JFrame {
                 if(work_status == 1 && check_quality_result && (bindLbStatus || manualSubmitFlag)){
 
                     String snSrc = curSn != null && !curSn.trim().isEmpty() ? curSn : product_sn.getText();
+                    String validateMsg = validateProductSnMessage(snSrc == null ? "" : snSrc.trim());
+                    if (validateMsg != null) {
+                        MesClient.setMenuStatus(validateMsg, -1);
+                        return;
+                    }
                     String sn = getBarcode(snSrc);
                     getUser();
 
@@ -1188,6 +1309,11 @@ public class MesClient extends JFrame {
                 if(work_status == 1 && check_quality_result && (bindLbStatus || manualSubmitFlag)){
 
                     String snSrc = curSn != null && !curSn.trim().isEmpty() ? curSn : product_sn.getText();
+                    String validateMsg = validateProductSnMessage(snSrc == null ? "" : snSrc.trim());
+                    if (validateMsg != null) {
+                        MesClient.setMenuStatus(validateMsg, -1);
+                        return;
+                    }
                     String sn = getBarcode(snSrc);
                     getUser();
                     String qret = "NG";
@@ -1295,6 +1421,51 @@ public class MesClient extends JFrame {
         tabbedPane.addTab("工作面板", new ImageIcon(MesClient.class.getResource("/bg/a_side.png")), indexScrollPaneA, null);
         tabbedPane.setEnabledAt(0, true);
 
+        if (PRODUCT_TYPE_SWITCH_ENABLED) {
+        JPanel indexPanelProductType = new JPanel();
+        indexPanelProductType.setLayout(null);
+        JScrollPane productTypeScrollPane = new JScrollPane(indexPanelProductType);
+
+        JLabel productTypeTitle = new JLabel("生产类型设置");
+        productTypeTitle.setFont(new Font("微软雅黑", Font.BOLD, 28));
+        productTypeTitle.setForeground(new Color(64, 64, 64));
+        productTypeTitle.setHorizontalAlignment(SwingConstants.CENTER);
+        productTypeTitle.setBounds(81, 80, 810, 50);
+        indexPanelProductType.add(productTypeTitle);
+
+        JLabel productTypeHint = new JLabel("请选择当前生产的工件类型,切换后将保存到本地并用于扫码校验");
+        productTypeHint.setFont(new Font("微软雅黑", Font.PLAIN, 20));
+        productTypeHint.setForeground(new Color(140, 140, 140));
+        productTypeHint.setHorizontalAlignment(SwingConstants.CENTER);
+        productTypeHint.setBounds(81, 140, 810, 40);
+        indexPanelProductType.add(productTypeHint);
+
+        productTypePanel = new ProductTypePanel(PRODUCT_TYPE_SWITCH_PASSWORD);
+        productTypePanel.setBounds(276, 210, 420, 72);
+        productTypePanel.setOnChangeListener(() -> {
+            if (work_status == 0) {
+                saveProductTypeToDb();
+                if (PRODUCT_TYPE_SWITCH_ENABLED) {
+            setMenuStatus("当前生产类型:" + getProductTypeName() + ",请扫工件码",0);
+        } else {
+            setMenuStatus("请扫工件码",0);
+        }
+            }
+            refreshProductTypeCurrentLabel();
+        });
+        indexPanelProductType.add(productTypePanel);
+
+        productTypeCurrentLabel = new JLabel();
+        productTypeCurrentLabel.setFont(new Font("微软雅黑", Font.PLAIN, 22));
+        productTypeCurrentLabel.setForeground(new Color(64, 64, 64));
+        productTypeCurrentLabel.setHorizontalAlignment(SwingConstants.CENTER);
+        productTypeCurrentLabel.setBounds(81, 310, 810, 40);
+        refreshProductTypeCurrentLabel();
+        indexPanelProductType.add(productTypeCurrentLabel);
+
+        tabbedPane.addTab("生产类型", new ImageIcon(MesClient.class.getResource("/bg/menu_setting.png")), productTypeScrollPane, null);
+        }
+
 //		searchScrollPane = new JScrollPane((Component) null);
 
         indexPanelC = new JPanel(new BorderLayout());
@@ -1319,7 +1490,7 @@ public class MesClient extends JFrame {
                 int selectedIndex = tabbedPane.getSelectedIndex();
                 System.out.println("selectedIndex:"+selectedIndex);
 
-                if (selectedIndex == 3) { // 假设设备管理是第4个标签(索引3)
+                if ("设备管理".equals(tabbedPane.getTitleAt(selectedIndex))) {
                     JPasswordField pf = new JPasswordField();
                     int okCansel = JOptionPane.showConfirmDialog(null, pf, "请输入管理员密码", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
                     if (okCansel == JOptionPane.OK_OPTION) {

+ 1 - 0
src/com/mes/ui/MesRevice.java

@@ -86,6 +86,7 @@ public class MesRevice {
                 }
                 MesClient.work_status = 1;
                 MesClient.f_scan_data_bt_1.setEnabled(false);
+                MesClient.updateProductTypePanelEnabled(false);
                 MesClient.tjStatus = 0;
                 MesClient.work_status_jqb = 2;
                 MesClient.startWorkRequestSent = true;

+ 51 - 0
src/com/mes/util/JdbcUtils.java

@@ -193,10 +193,61 @@ public class JdbcUtils {
                 + "info_01 VARCHAR(200),info_02 VARCHAR(200),info_03 VARCHAR(200))";
 //        statement.executeUpdate("drop table if exists bw_record");//判断是否有表tables的存在。有则删除
         statement.executeUpdate(sqlEquipment);
+        String clientConfig = "CREATE TABLE if not exists client_config("
+                + "config_key VARCHAR(100) PRIMARY KEY,"
+                + "config_value VARCHAR(500),"
+                + "update_time DATETIME"
+                + ")";
+        statement.executeUpdate(clientConfig);
         System.out.println("表record创建成功!");
         statement.close();
     }
 
+    public static void saveClientConfig(String key, String value) {
+        try {
+            if (conn == null || conn.isClosed()) {
+                getConn();
+            }
+            String sql = "INSERT INTO client_config (config_key, config_value, update_time) VALUES (?, ?, ?) "
+                    + "ON CONFLICT(config_key) DO UPDATE SET config_value = excluded.config_value, update_time = excluded.update_time";
+            PreparedStatement ps = conn.prepareStatement(sql);
+            ps.setString(1, key);
+            ps.setString(2, value);
+            ps.setString(3, DateLocalUtils.getCurrentTime());
+            ps.executeUpdate();
+            ps.close();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static String getClientConfig(String key, String defaultValue) {
+        try {
+            if (conn == null || conn.isClosed()) {
+                getConn();
+            }
+            String sql = "SELECT config_value FROM client_config WHERE config_key = ?";
+            PreparedStatement ps = conn.prepareStatement(sql);
+            ps.setString(1, key);
+            ResultSet rs = ps.executeQuery();
+            if (rs.next()) {
+                String value = rs.getString("config_value");
+                rs.close();
+                ps.close();
+                return value;
+            }
+            rs.close();
+            ps.close();
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+        return defaultValue;
+    }
+
+    public static String getProductTypeConfigKey(String gw) {
+        return "product_type_" + gw;
+    }
+
     //插入数据
     public static boolean insertData(String gw, String gy, String bw, String message_type, String sn) {
         boolean ret = false;