zouyu
3 天以前 625c4f2816d171bbe8cf264e23d3bec0c11e3b86
src/main/java/com/chinaztt/mes/docx/util/TakeWords.java
@@ -7,6 +7,7 @@
import com.opencsv.CSVReader;
import com.opencsv.CSVReaderBuilder;
import com.opencsv.exceptions.CsvValidationException;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.lang3.ObjectUtils;
@@ -17,7 +18,6 @@
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
@@ -28,6 +28,7 @@
import java.util.*;
import java.util.regex.Pattern;
@Slf4j
public class TakeWords {
    private static final String splitIdentifier = "@-@"; // 自定义唯一标识符
@@ -36,6 +37,11 @@
    private static final Pattern SCIENTIFIC_PATTERN = Pattern.compile(
            "^[+-]?\\d+(\\.\\d+)?[eE][+-]?\\d+$"
    );
    /**
     * 处理mdb数据库排除字段类型
     */
    private final static List<String> MDB_EXCLUDE_TYPES = Arrays.asList("java.sql.Blob");
    public static Object readWordFile(File file) {
        String result = "";
@@ -236,7 +242,7 @@
            if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){
                s+=" and " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'";
            }
            if(StringUtils.isNotBlank(getFileDto.getBushingColorField())){
            if(ObjectUtils.allNotNull(getFileDto.getBushingColorField(), getFileDto.getBushingColor()) && !getFileDto.getBushingColorField().equals("null")){
                s+=" and " + getFileDto.getBushingColorField() + " = '" + getFileDto.getBushingColor() + "'";
            }
@@ -244,11 +250,17 @@
            rs = preparedStatement.executeQuery();
            ResultSetMetaData data = rs.getMetaData();
            while (rs.next()) {
                Map<String, String> map = new HashMap<>();
                Map<String, Object> map = new HashMap<>();
                for (int i = 1; i <= data.getColumnCount(); i++) {
                    //列名
                    String columnName = data.getColumnName(i);
                    map.put(columnName, rs.getString(i));
                    //列字段类型
                    String columnClassName = data.getColumnClassName(i);
                    Object columnValue = null;
                    if(!MDB_EXCLUDE_TYPES.contains(columnClassName)){
                        columnValue = rs.getObject(i);
                    }
                    map.put(columnName, columnValue);
                }
                list.add(map);
            }
@@ -286,7 +298,7 @@
        String table = getFileDto.getDbTable();
        // 检查数据库名和表名是否为空
        if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) {
            return R.failed("数据库名或表名不能为空");
            return Result.failed("数据库名或表名不能为空");
        }
        // 数据库连接信息
        String url = "jdbc:mysql://localhost:3306/"+dbName+"?useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true&characterEncoding=utf8";
@@ -332,7 +344,7 @@
        } catch (Exception e) {
            e.printStackTrace();
            // 假设 R 类有 failed 方法,若没有需补充实现
            return R.failed("数据库查询出错: " + e.getMessage());
            return Result.failed("数据库查询出错: " + e.getMessage());
        } finally {
            try {
                if (resultSet != null) {
@@ -371,9 +383,13 @@
            List<Object> list = new ArrayList<>();
            //转义中文
            String sampleCode = changeCharset(getFileDto.getSampleCode(), "GBK");
            String sql = "select * from " + getFileDto.getDbFileName() +
                    " where " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode() +
                    "' and " + getFileDto.getMdbSampleCode() + " = '" + sampleCode + "'";
            String sql = "select * from " + getFileDto.getDbFileName() + " where 1=1";
            if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode())){
                sql+=" and " + getFileDto.getMdbEntrustCode() + " = '" + getFileDto.getEntrustCode() +"'";
            }
            if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){
                sql+=" and " + getFileDto.getMdbSampleCode() + " = '" + sampleCode + "'";
            }
            if (ObjectUtils.allNotNull(getFileDto.getFiberOpticRibbonCode(), getFileDto.getFiberOpticRibbon()) && !getFileDto.getFiberOpticRibbonCode().equals("null")) {
                sql = sql + " and " + getFileDto.getFiberOpticRibbonCode() + " = '" + getFileDto.getFiberOpticRibbon() + "'";
            }
@@ -456,4 +472,82 @@
        }
    }
    /**
     * 读取sqlserver数据
     * @param getFileDto 请求参数对象
     * @return
     */
    public static Object readSqlServerDataBase(GetFileDto getFileDto) {
        Map<String, Object> tableMap = new HashMap<>(16);
        // 从 GetFileDto 获取数据库名,对应【文件名称】字段
        String dbName = getFileDto.getDbFileName();
        String user = getFileDto.getDbUserName();
        String password = getFileDto.getDbPassword();
        // 从 GetFileDto 获取数据表名,对应【数据库表名】字段
        String table = getFileDto.getDbTable();
        // 检查数据库名和表名是否为空
        if (dbName == null || dbName.isEmpty() || table == null || table.isEmpty()) {
            return Result.failed("数据库名或表名不能为空");
        }
        // 数据库连接信息
        String url = "jdbc:sqlserver://localhost:1433;DatabaseName="+dbName+";encrypt=true;trustServerCertificate=true";
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Map<String, Object>> dataList = new ArrayList<>();
        try {
            // 建立连接
            connection = DriverManager.getConnection(url, user, password);
            // 构建基础 SQL
            String sql = "SELECT * FROM "+table+" WHERE 1=1";
            if(StringUtils.isNotBlank(getFileDto.getMdbEntrustCode()) ){
                sql+=" AND (" + getFileDto.getMdbEntrustCode() + " = TRIM('" + getFileDto.getEntrustCode()+ "')";
                if(StringUtils.isNotBlank(getFileDto.getLotBatchNo())){
                    sql+=" OR "+ getFileDto.getMdbEntrustCode() + " = TRIM('" + getFileDto.getLotBatchNo()+ "')";
                }
                sql+=")";
            }
            if(StringUtils.isNotBlank(getFileDto.getMdbSampleCode())){
                sql+=" AND " + getFileDto.getMdbSampleCode() + " = '" + getFileDto.getSampleCode() + "'";
            }
            if(StringUtils.isNotBlank(getFileDto.getCableTag())){
                sql+=" AND Color = '" + getFileDto.getCableTag() + "'";
            }
            // 创建 PreparedStatement 对象执行 SQL
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            // 遍历结果集获取数据
            while (resultSet.next()) {
                Map<String, Object> rowData = new HashMap<>();
                for (int i = 1; i <= columnCount; i++) {
                    String columnName = metaData.getColumnName(i);
                    rowData.put(columnName, resultSet.getObject(i));
                }
                dataList.add(rowData);
            }
            tableMap.put("data", dataList);
        } catch (Exception e) {
            e.printStackTrace();
            // 假设 R 类有 failed 方法,若没有需补充实现
            return Result.failed("数据库查询出错: " + e.getMessage());
        } finally {
            try {
                if (resultSet != null) {
                    resultSet.close();
                }
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return tableMap;
    }
}