From 709e07347ac86c766582518345352af2fa286f22 Mon Sep 17 00:00:00 2001
From: lyh <925863403@qq.com>
Date: 星期三, 04 六月 2025 09:12:20 +0800
Subject: [PATCH] 修改自动化添加日志代码

---
 lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java |  140 ++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 120 insertions(+), 20 deletions(-)

diff --git a/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java b/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java
index 72e2db6..5364928 100644
--- a/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java
+++ b/lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java
@@ -1,7 +1,8 @@
 package org.jeecg.modules.msi.webapi.service.impl;
 
-import cn.hutool.core.date.DateUtil;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import liquibase.util.JdbcUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.jeecg.common.util.DateUtils;
 import org.jeecg.modules.msi.webapi.entity.MsiWebapiJsonEntity;
@@ -9,17 +10,30 @@
 import org.jeecg.modules.msi.webapi.service.IMsiWebapiJsonService;
 import org.jeecg.modules.msi.webapi.vo.DetailedListVo;
 import org.jeecg.modules.msi.webapi.vo.MachineEquipentInfo;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.jdbc.core.JdbcTemplate;
+import org.springframework.jdbc.datasource.DataSourceUtils;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 @Service
 public class MsiWebapiJsonServiceImpl extends ServiceImpl<MsiWebapiJsonMapper, MsiWebapiJsonEntity> implements IMsiWebapiJsonService {
 
+    @Autowired
+    private JdbcTemplate jdbcTemplate;
 
+    /**
+     * 瑙f瀽鍗曟潯鏁版嵁淇濆瓨鍒板崟琛�
+     * @param data
+     * @return
+     */
     @Override
     public boolean saveTableAutomationOne(List<MachineEquipentInfo> data) {
         if (data ==null || data.isEmpty()) {
@@ -27,8 +41,31 @@
         }
         List<String> listStatus = new ArrayList<>();
         List<String> listParameter = new ArrayList<>();
+
+        // 鐢ㄤ簬璁板綍鍓嶄竴鏉¤褰曠殑鐘舵�佸��
+        String prevRunningStatus = null;
+
         for (MachineEquipentInfo machineInfo : data) {
             System.out.println(machineInfo);
+
+            // 鑾峰彇褰撳墠璁板綍鐨剅unningStatus鍊�
+            String currentRunningStatus = null;
+            for (DetailedListVo item : machineInfo.getItemList()) {
+                if ("runningStatus".equals(item.getItemName())) {
+                    currentRunningStatus = item.getItemValue();
+                    break;
+                }
+            }
+
+            // 濡傛灉褰撳墠鐘舵�佷笌鍓嶄竴鏉$浉鍚岋紝鍒欒烦杩�
+            if (currentRunningStatus != null && currentRunningStatus.equals(prevRunningStatus)) {
+                continue;
+            }
+
+            // 鏇存柊鍓嶄竴鏉¤褰曠殑鐘舵�佸��
+            prevRunningStatus = currentRunningStatus;
+
+            // 鐢熸垚SQL
             String stutus = listSqls(machineInfo);
             if (stutus != null && !stutus.equals("")) {
                 listStatus.add(stutus);
@@ -38,9 +75,53 @@
                 listParameter.add(parameter);
             }
         }
-        //鐘舵�佹暟鎹�
+        // 鎵ц鐘舵�佹暟鎹甋QL
+        try {
+            executeBatchSql(listStatus);
+            if (!listParameter.isEmpty()) {
+                executeBatchSql(listParameter);
+            }
+            return true;
+        } catch (Exception e) {
+            log.error("鎵цSQL鎵归噺鎻掑叆澶辫触", e);
+            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
+            return false;
+        }
+    }
 
-        return false;
+    /**
+     * 鎵归噺鎵цSQL璇彞
+     * @param sqlList SQL璇彞鍒楄〃
+     * @throws SQLException
+     */
+    private void executeBatchSql(List<String> sqlList) throws SQLException {
+        if (CollectionUtils.isEmpty(sqlList)) {
+            return;
+        }
+
+        Connection connection = null;
+        Statement statement = null;
+
+        try {
+            connection = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());
+            connection.setAutoCommit(false);
+            statement = connection.createStatement();
+
+            for (String sql : sqlList) {
+                statement.addBatch(sql);
+            }
+
+            statement.executeBatch();
+            connection.commit();
+        } catch (SQLException e) {
+            if (connection != null) {
+                connection.rollback();
+            }
+            throw e;
+        } finally {
+            JdbcUtils.closeStatement(statement);
+            DataSourceUtils.releaseConnection(connection, jdbcTemplate.getDataSource());
+        }
     }
 
 
@@ -68,52 +149,71 @@
         //EquipmentName
         sql = sql + null + ", ";
         //CollectTime
-        if (machineInfo.getEndTime() != null) {
-            sql = sql + "'" + DateUtils.formattedDate(machineInfo.getEndTime(),DateUtils.STR_DD_MM_YYYY,DateUtils.STR_DATE_TIME_SMALL) + "', ";
+        // 鍋囪 endTime 鏄� "MM/dd/yyyy HH:mm:ss" 鏍煎紡鐨勫瓧绗︿覆
+        if (machineInfo.getEndTime() != null && !machineInfo.getEndTime().isEmpty()) {
+            try {
+                // 1. 灏嗗瓧绗︿覆瑙f瀽涓� Date 瀵硅薄
+                SimpleDateFormat inputFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
+                Date endDate = inputFormat.parse(machineInfo.getEndTime());
+
+                // 2. 灏� Date 鏍煎紡鍖栦负鐩爣 SQL 鏍煎紡锛堝 "yyyy-MM-dd HH:mm:ss"锛�
+                SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+                sql = sql + "'" + outputFormat.format(endDate) + "', ";
+            } catch (ParseException e) {
+                // 澶勭悊瑙f瀽澶辫触鐨勬儏鍐碉紙濡傛棩蹇楄褰曟垨鎶涘嚭寮傚父锛�
+                throw new RuntimeException("Invalid end time format: " + machineInfo.getEndTime(), e);
+            }
         } else {
-            sql = sql + "'" + DateUtils.format(DateUtils.getDate(),DateUtils.STR_DATE_TIME) + "', ";
+            // 濡傛灉 endTime 涓� null 鎴栫┖锛屼娇鐢ㄥ綋鍓嶆椂闂�
+            sql = sql + "'" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "', ";
         }
         //runningStatus
         if (itemMap.containsKey("runningStatus")) {
             sql = sql + "'" + itemMap.get("runningStatus").getItemValue() + "', ";
         } else {
-            sql = sql + null + ", ";
+            sql = sql + "NULL, ";
         }
         //spindleSpeed
         if (itemMap.containsKey("spindleSpeed")) {
             sql = sql + "'" + itemMap.get("spindleSpeed").getItemValue() + "', ";
         } else {
-            sql = sql + null + ", ";
+            sql = sql + "NULL, ";
+        }
+        //feedRatio
+        if (itemMap.containsKey("feedRatio")) {
+            sql = sql + "'" + itemMap.get("feedRatio").getItemValue() + "', ";
+        }else {
+            sql = sql + "NULL, ";
         }
         //runDuration
         if (itemMap.containsKey("runDuration")) {
             sql = sql + "'" + itemMap.get("runDuration").getItemValue() + "', ";
         } else {
-            sql = sql + null + ", ";
+            sql = sql + "NULL, ";
         }
         //spindleDuration
         if (itemMap.containsKey("spindleDuration")) {
             sql = sql + "'" + itemMap.get("spindleDuration").getItemValue() + "', ";
         } else {
-            sql = sql + null + ", ";
+            sql = sql + "NULL, ";
         }
         //progName
-        if (itemMap.containsKey("spindleSpeed")) {
-            sql = sql + "'" + itemMap.get("spindleSpeed").getItemValue() + "', ";
+        if (itemMap.containsKey("progName")) {  // 淇涓� "progName"
+            sql = sql + "'" + itemMap.get("progName").getItemValue() + "', ";
         } else {
-            sql = sql + null + ", ";
+            sql = sql + "NULL, ";
         }
         //progStatus
         if (itemMap.containsKey("progStatus")) {
             sql = sql + "'" + itemMap.get("progStatus").getItemValue() + "', ";
         } else {
-            sql = sql + null + ", ";
+            sql = sql + "NULL, ";
         }
         //toolNo
         if (itemMap.containsKey("toolNo")) {
-            sql = sql + "'" + itemMap.get("toolNo").getItemValue() + "'  ";
+            sql = sql + "'" + itemMap.get("toolNo").getItemValue() + "' ";
         } else {
-            sql = sql + null;
+            sql = sql + "NULL";
         }
 
         sql = sql + "); \n";

--
Gitblit v1.9.3