ZengTao
2024-05-10 73263f94cf46a409b0d6fd329b1caa645422379b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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.entity.Queue;
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<Queue> ErrowQueues= QueueMapper.selectErrorQueues();
        if (ErrowQueues.size()>0) {
            //System.out.println("有匹配失败数据未去除");
            return false;
        }
        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("匹配成功");
            System.out.println(GlassInfo.getGlassid());
            return true;
        } else if (Result.size() > 1) {
            // 匹配失败 匹配到多条符合的数据 添加数据
            QueueMapper.insertMatchFailure(width, height, 0);
            System.out.println("匹配失败");
            
        } else {
            // 匹配失败 未找到符合的数据 添加数据
            QueueMapper.insertMatchFailure(width, height, -1);
            System.out.println("未找到符合的数据");
        }
        return false;
    }
 
    // Execl表格 传入文件路径
    public List ReadExecl(String filename) {
        try {
            // 创建文件对象
            File file = new File(filename);
            if (!file.exists()) {
                System.out.println("文件不存在! "+filename);
                return new ArrayList<>();
            }
            // 创建文件输入流对象
            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();
        }
 
    }
 
}