Lius
2025-06-28 39f62fa03a2463652e971edfabab56313db6af10
lxzn-module-msi/src/main/java/org/jeecg/modules/msi/webapi/service/impl/MsiWebapiJsonServiceImpl.java
@@ -3,8 +3,10 @@
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import liquibase.util.JdbcUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.msi.webapi.entity.MsiWebapiJsonEntity;
import org.jeecg.modules.msi.webapi.mapper.MsiWebapiJsonMapper;
import org.jeecg.modules.msi.webapi.service.IMsiWebapiJsonService;
@@ -15,48 +17,93 @@
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
@Slf4j
public class MsiWebapiJsonServiceImpl extends ServiceImpl<MsiWebapiJsonMapper, MsiWebapiJsonEntity> implements IMsiWebapiJsonService {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Resource
    private RedisUtil redisUtil;
    private final static String ZDHKEY = "ZDHKEY_";
    /**
     * 解析单条数据保存到单表
     *
     * @param data
     * @return
     */
    @Override
    public boolean saveTableAutomationOne(List<MachineEquipentInfo> data) {
        if (data ==null || data.isEmpty()) {
        if (data == null || data.isEmpty()) {
            return false;
        }
        List<String> listStatus = new ArrayList<>();
        List<String> listParameter = new ArrayList<>();
        for (MachineEquipentInfo machineInfo : data) {
            System.out.println(machineInfo);
//            log.info("接收自动化数据:" + machineInfo);
            // 获取当前记录的runningStatus值
            String currentRunningStatus = null;
            for (DetailedListVo item : machineInfo.getItemList()) {
                if ("runningStatus".equals(item.getItemName())) {
                    currentRunningStatus = item.getItemValue();
                    break;
                }
            }
            // 获取前一条状态
            if (redisUtil.hasKey(ZDHKEY + machineInfo.getMachineNo())) {
                String prevRunningStatus = (String) redisUtil.get(ZDHKEY + machineInfo.getMachineNo());
                // 如果当前状态与前一条相同,则只写入单表数据
                if (currentRunningStatus != null && currentRunningStatus.equals(prevRunningStatus)) {
                    String parameter = listSqlParameter(machineInfo);
                    if (parameter != null && !parameter.equals("")) {
                        listParameter.add(parameter);
                    }
                    continue;
                }
                // 更新前一条记录的状态值
                redisUtil.set(ZDHKEY + machineInfo.getMachineNo(), currentRunningStatus);
            } else {
                if (currentRunningStatus != null) {
                    redisUtil.set(ZDHKEY + machineInfo.getMachineNo(), currentRunningStatus);
                }
            }
            // 生成SQL
            String stutus = listSqls(machineInfo);
            if (stutus != null && !stutus.equals("")) {
                listStatus.add(stutus);
            }
            String parameter = listSqlParameter(machineInfo);
            if (parameter != null && !parameter.equals("")) {
                listParameter.add(parameter);
            }
        }
        // 执行状态数据SQL
        try {
            executeBatchSql(listStatus);
            executeBatchSql(listParameter);
            if (!listParameter.isEmpty()) {
                executeBatchSql(listParameter);
            }
            return true;
        } catch (Exception e) {
            log.error("执行SQL批量插入失败", e);
@@ -67,6 +114,7 @@
    /**
     * 批量执行SQL语句
     *
     * @param sqlList SQL语句列表
     * @throws SQLException
     */
@@ -102,22 +150,23 @@
    /**
     *  插入到EquipmentLog
     * 插入到设备单表
     *
     * @param machineInfo
     * @return
     */
    public String listSqlParameter(MachineEquipentInfo machineInfo) {
        String listSqls = null;
        List<DetailedListVo> itemList = machineInfo.getItemList();
        Map<String ,DetailedListVo> itemMap = new HashMap<String ,DetailedListVo>();
        for (DetailedListVo item: itemList) {
            itemMap.put(item.getItemName(),item);
        Map<String, DetailedListVo> itemMap = new HashMap<String, DetailedListVo>();
        for (DetailedListVo item : itemList) {
            itemMap.put(item.getItemName(), item);
        }
        String sql = "insert into ";
        sql = sql + "ZDH_" + machineInfo.getMachineNo() + " ";
        sql = sql + "(EquipmentID,EquipmentName,CollectTime,runningStatus,spindleSpeed,feedRatio,runDuration,spindleDuration,progName,progStatus,toolNo) values (  ";
        //EquipmentID
        if (machineInfo.getMachineNo() == null ||  machineInfo.getMachineNo().equals("")) {
        if (machineInfo.getMachineNo() == null || machineInfo.getMachineNo().equals("")) {
            return null;
        } else {
            sql = sql + "'" + machineInfo.getMachineNo() + "', ";
@@ -125,56 +174,64 @@
        //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()) {
            sql = getTime(machineInfo, sql);
        } 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";
        if (StringUtils.isBlank(listSqls) ) {
        if (StringUtils.isBlank(listSqls)) {
            listSqls = sql;
        } else {
            listSqls += sql;
@@ -184,7 +241,8 @@
    /**
     *  插入到EquipmentLog
     * 插入到EquipmentLog
     *
     * @param machineInfo
     * @return
     */
@@ -199,37 +257,72 @@
        }
        sql = sql + null + ", ";
        if (machineInfo.getEndTime() != null) {
            sql = sql + "'" + DateUtils.formattedDate(machineInfo.getEndTime(),DateUtils.STR_DD_MM_YYYY,DateUtils.STR_DATE_TIME_SMALL) + "', ";
            sql = getTime(machineInfo, sql);
        } else {
            return null;
        }
        Map<String ,DetailedListVo> itemMap = new HashMap<String ,DetailedListVo>();
        for (DetailedListVo item: itemList) {
            itemMap.put(item.getItemName(),item);
        Map<String, DetailedListVo> itemMap = new HashMap<String, DetailedListVo>();
        for (DetailedListVo item : itemList) {
            itemMap.put(item.getItemName(), item);
        }
        if (itemMap.containsKey("runningStatus")) {
            int oporation = 0;
            switch (itemMap.get("runningStatus").getItemValue()) {
                case "0" : oporation =0;break;
                case "1" : oporation =2;break;
                case "2" : oporation =3;break;
                case "3" : oporation =2;break;
                case "4" : oporation =2;break;
                case "10" : oporation =2;break;
                case "13" : oporation =2;break;
                case "14" : oporation =2;break;
                case "10001" : oporation =2;break;
                default: break;
                case "0":
                    oporation = 0;
                    break;
                case "1":
                    oporation = 2;
                    break;
                case "2":
                    oporation = 3;
                    break;
                case "3":
                    oporation = 2;
                    break;
                case "4":
                    oporation = 2;
                    break;
                case "10":
                    oporation = 2;
                    break;
                case "13":
                    oporation = 2;
                    break;
                case "14":
                    oporation = 2;
                    break;
                case "10001":
                    oporation = 2;
                    break;
                default:
                    break;
            }
            sql = sql + " " + oporation  + ", ";
            sql = sql + " " + oporation + ", ";
        } else {
            return null;
        }
        sql = sql + null +   " ); \n";
        if (StringUtils.isBlank(listSqls) ) {
        sql = sql + null + " ); \n";
        if (StringUtils.isBlank(listSqls)) {
            listSqls = sql;
        }
        return listSqls;
    }
    @NotNull
    private String getTime(MachineEquipentInfo machineInfo, String sql) {
        try {
            // 1. 将字符串解析为 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) {
            // 处理解析失败的情况(如日志记录或抛出异常)
            throw new RuntimeException("Invalid end time format: " + machineInfo.getEndTime(), e);
        }
        return sql;
    }
}