ZengTao
2024-03-18 e76f203f76b934d2f49dcea89649b2f616878056
springboot-vue3/src/main/java/com/example/springboot/service/HomeService.java
@@ -1,9 +1,150 @@
package com.example.springboot.service;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.CellValue;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springboot.entity.GlassInfo;
import com.example.springboot.entity.MeasureSetting;
import com.example.springboot.mapper.GlassInfoMapper;
import com.example.springboot.mapper.MeasureSettingMapper;
import com.example.springboot.mapper.QueueMapper;
@Service
public class HomeService {
    @Autowired
    MeasureSettingMapper MeasureSettingMapper;
    @Autowired
    GlassInfoMapper GlassInfoMapper;
    @Autowired
    QueueMapper QueueMapper;
    // 匹配玻璃 宽,高,线路
    public List<GlassInfo> NormalGlassInfo(double width, double height, String line) {
        //
        List<GlassInfo> Results = new ArrayList<GlassInfo>();
        // 获取匹配设置
        MeasureSetting MeasureSetting = MeasureSettingMapper.SelectMeasureSetting(line);
        if (height > 0 && width > 0 && MeasureSetting != null) {
            // 有效参数 请求=1 长/宽>0
            double maxheight = height + MeasureSetting.getErrorHeight();
            double minheight = height - MeasureSetting.getErrorHeight();
            double maxwidth = width + MeasureSetting.getErrorWidth();
            double minwidth = width - MeasureSetting.getErrorWidth();
            // 查询当前测量数据
            List<GlassInfo> GlassInfos = GlassInfoMapper.selectGlassInfos(maxwidth, minwidth, maxheight, minheight);
            List types = new ArrayList<>();
            for (int i = 0; i < GlassInfos.size(); i++) {
                Integer glasstype = GlassInfos.get(i).getGlasstype();
                if (!types.contains(glasstype)) {
                    types.add(glasstype);
                    Results.add(GlassInfos.get(i));
                }
            }
        }
        return Results;
    }
    // 匹配逻辑
    public boolean Normal(double width, double height, String line) {
        List<GlassInfo> Result = NormalGlassInfo(width, height, "1");
        if (Result.size() == 1) {
            // 匹配成功 就一种类型 添加数据
            GlassInfo GlassInfo = Result.get(0);
            QueueMapper.insert(GlassInfo.getGlassid(), width, height, 1);
            GlassInfoMapper.updatemeasurenumber(GlassInfo.getId());
            System.out.println("匹配成功");
            // S7control.getinstance().WriteWord(plcmes.getPlcParameter("GaToMES").getAddress(),
            // (short) 1);
            System.out.println(GlassInfo.getGlassid());
            return true;
        } else if (Result.size() > 1) {
            // 匹配失败 匹配到多条符合的数据 添加数据
            QueueMapper.insert(null, width, height, 2);
            System.out.println("匹配失败");
            return false;
        } else {
            // 匹配失败 未找到符合的数据 添加数据
            QueueMapper.insert(null, width, height, 3);
            System.out.println("未找到符合的数据");
            return false;
        }
    }
    // Execl表格 传入文件路径
    public List ReadExecl(String filename) {
        try {
            // 创建文件对象
            File file = new File(filename);
            // 创建文件输入流对象
            FileInputStream inputStream = new FileInputStream(file);
            // 创建工作簿对象
            Workbook workbook = WorkbookFactory.create(inputStream);
            // 获取第一个工作表对象
            Sheet sheet = workbook.getSheetAt(0);
            // 创建一个实体类集合,用于存储Excel数据
            List Results = new ArrayList();
            int i = 0;
            // 遍历行
            for (Row row : sheet) {
                // 遍历单元格
                List ResultRow = new ArrayList();
                // System.out.println();
                for (Cell cell : row) {
                    String LastCellvalue = "";
                    // 判断单元格类型是否为公式类型
                    if (cell.getCellType() == CellType.FORMULA) {
                        // 使用公式计算器计算单元格的值
                        FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
                        CellValue cellValue = evaluator.evaluate(cell);
                        // 计算后的单元格值
                        LastCellvalue = cellValue.getNumberValue() + "";
                    } else if (cell.getCellType() == CellType.NUMERIC) {
                        double value = cell.getNumericCellValue();
                        if (value % 1 == 0) {
                            LastCellvalue = Math.round(value) + "";
                        } else {
                            LastCellvalue = value + "";
                        }
                    } else {
                        // 单元格值
                        LastCellvalue = cell.toString();
                    }
                    ResultRow.add(LastCellvalue);
                    // System.out.print(LastCellvalue+" ");
                }
                Results.add(ResultRow);
                i++;
            }
            System.out.println(i);
            // 关闭工作簿和输入流对象
            workbook.close();
            inputStream.close();
            return Results;
        } catch (Exception e) {
            // TODO: handle exception
            System.out.println("异常");
            return new ArrayList();
        }
    }
}