package org.jeecg.modules.msi.webapi.service.impl; 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; import org.jeecg.modules.msi.webapi.mapper.MsiWebapiJsonMapper; 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.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; @Service public class MsiWebapiJsonServiceImpl extends ServiceImpl implements IMsiWebapiJsonService { @Autowired private JdbcTemplate jdbcTemplate; /** * 解析单条数据保存到单表 * @param data * @return */ @Override public boolean saveTableAutomationOne(List data) { if (data ==null || data.isEmpty()) { return false; } List listStatus = new ArrayList<>(); List listParameter = new ArrayList<>(); for (MachineEquipentInfo machineInfo : data) { System.out.println(machineInfo); 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); return true; } catch (Exception e) { log.error("执行SQL批量插入失败", e); TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return false; } } /** * 批量执行SQL语句 * @param sqlList SQL语句列表 * @throws SQLException */ private void executeBatchSql(List 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()); } } /** * 插入到EquipmentLog * @param machineInfo * @return */ public String listSqlParameter(MachineEquipentInfo machineInfo) { String listSqls = null; List itemList = machineInfo.getItemList(); Map itemMap = new HashMap(); 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("")) { return null; } else { sql = sql + "'" + machineInfo.getMachineNo() + "', "; } //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) + "', "; } else { sql = sql + "'" + DateUtils.format(DateUtils.getDate(),DateUtils.STR_DATE_TIME) + "', "; } //runningStatus if (itemMap.containsKey("runningStatus")) { sql = sql + "'" + itemMap.get("runningStatus").getItemValue() + "', "; } else { sql = sql + "NULL, "; } //spindleSpeed if (itemMap.containsKey("spindleSpeed")) { sql = sql + "'" + itemMap.get("spindleSpeed").getItemValue() + "', "; } else { 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, "; } //spindleDuration if (itemMap.containsKey("spindleDuration")) { sql = sql + "'" + itemMap.get("spindleDuration").getItemValue() + "', "; } else { sql = sql + "NULL, "; } //progName if (itemMap.containsKey("progName")) { // 修正为 "progName" sql = sql + "'" + itemMap.get("progName").getItemValue() + "', "; } else { sql = sql + "NULL, "; } //progStatus if (itemMap.containsKey("progStatus")) { sql = sql + "'" + itemMap.get("progStatus").getItemValue() + "', "; } else { sql = sql + "NULL, "; } //toolNo if (itemMap.containsKey("toolNo")) { sql = sql + "'" + itemMap.get("toolNo").getItemValue() + "' "; } else { sql = sql + "NULL"; } sql = sql + "); \n"; if (StringUtils.isBlank(listSqls) ) { listSqls = sql; } else { listSqls += sql; } return listSqls; } /** * 插入到EquipmentLog * @param machineInfo * @return */ public String listSqls(MachineEquipentInfo machineInfo) { String listSqls = null; List itemList = machineInfo.getItemList(); String sql = "insert into EquipmentLog (EquipmentID,EquipmentName,CollectTime,Oporation,Alarm) values ( "; if (machineInfo.getMachineNo() == null) { return null; } else { sql = sql + "'" + machineInfo.getMachineNo() + "', "; } sql = sql + null + ", "; if (machineInfo.getEndTime() != null) { sql = sql + "'" + DateUtils.formattedDate(machineInfo.getEndTime(),DateUtils.STR_DD_MM_YYYY,DateUtils.STR_DATE_TIME_SMALL) + "', "; } else { return null; } Map itemMap = new HashMap(); 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; } sql = sql + " " + oporation + ", "; } else { return null; } sql = sql + null + " ); \n"; if (StringUtils.isBlank(listSqls) ) { listSqls = sql; } return listSqls; } }