#
Junjie
10 小时以前 8a1f312d9f7842bb02c97b200e017926f0af63a5
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package com.zy.asrs.utils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.core.common.Cools;
import com.zy.asrs.domain.enums.MapExcelNodeType;
import com.zy.common.model.MapNode;
import com.zy.core.enums.MapNodeType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Component
public class MapExcelUtils {
 
    public HashMap<Integer, List<List<MapNode>>> processExcelData(HashMap<Integer, List<List<HashMap<String, Object>>>> dataMap) {
        HashMap<Integer, List<List<MapNode>>> levMap = new HashMap<>();
        for (Map.Entry<Integer, List<List<HashMap<String, Object>>>> entry : dataMap.entrySet()) {
            Integer lev = entry.getKey();
            List<List<HashMap<String, Object>>> excelData = entry.getValue();
 
            HashMap<Integer, JSONObject> rowMap = new HashMap<>();
            HashMap<Integer, JSONObject> bayMap = new HashMap<>();
 
            int finalRow = -1;
            int finalBay = -1;
 
            //获取列数据
            List<HashMap<String, Object>> tmpList0 = excelData.get(0);
            List<HashMap<String, Object>> tmpList1 = excelData.get(1);
            for (int i = 2; i < tmpList1.size(); i++) {
                HashMap<String, Object> tmpBayMap = tmpList1.get(i);
                if (Cools.isEmpty(tmpBayMap.get("value"))) {
                    continue;
                }
                HashMap<String, Object> tmpDistanceMap = tmpList0.get(i);
                int bay = (int) Double.parseDouble(tmpBayMap.get("value").toString());
                JSONObject distanceData = JSON.parseObject(tmpDistanceMap.get("value").toString());
 
                bayMap.put(bay, distanceData);
                finalBay = bay;
            }
 
            //获取排数据
            for (int i = 2; i < excelData.size(); i++) {
                List<HashMap<String, Object>> rowData = excelData.get(i);
                HashMap<String, Object> tmpRowMap = rowData.get(1);
                if (Cools.isEmpty(tmpRowMap.get("value"))) {
                    continue;
                }
                HashMap<String, Object> tmpDistanceMap = rowData.get(0);
                int row = (int) Double.parseDouble(tmpRowMap.get("value").toString());
                JSONObject distanceData = JSON.parseObject(tmpDistanceMap.get("value").toString());
                rowMap.put(row, distanceData);
                finalRow = row;
            }
 
            List<List<MapNode>> mapList = new ArrayList<>();
            //生成二维坐标
            for (int row = 1; row <= finalRow; row++) {
                List<MapNode> rows = new ArrayList<>();
                for (int bay = 1; bay <= finalBay; bay++) {
                    MapNode node = new MapNode();
                    node.setId(row + "-" + bay);
 
                    HashMap<String, Object> nodeData = excelData.get(row + 1).get(bay + 1);
                    String nodeType = nodeData.get("bgColor").toString();
                    String data = "";
                    if (nodeType.equals(MapExcelNodeType.MAIN_PATH.flag)) {
                        node.setValue(MapNodeType.MAIN_PATH.id);
                    } else if (nodeType.equals(MapExcelNodeType.NORMAL_PATH.flag)) {
                        node.setValue(MapNodeType.NORMAL_PATH.id);
                    } else if (nodeType.equals(MapExcelNodeType.LIFT.flag)) {
                        node.setValue(MapNodeType.LIFT.id);
                        JSONObject jsonData = JSON.parseObject(nodeData.get("value").toString());
                        data = jsonData.getString("value");
                    } else if (nodeType.equals(MapExcelNodeType.CHARGE.flag)) {
                        node.setValue(MapNodeType.CHARGE.id);
                        JSONObject jsonData = JSON.parseObject(nodeData.get("value").toString());
                        data = jsonData.getString("value");
                    } else if (nodeType.equals(MapExcelNodeType.STATION.flag)) {
                        node.setValue(MapNodeType.CONVEYOR.id);
                        JSONObject jsonData = JSON.parseObject(nodeData.get("value").toString());
                        data = jsonData.getString("value");
                    }else {
                        node.setValue(MapNodeType.DISABLE.id);
                    }
 
                    node.setData(data);
                    rows.add(node);
                }
                mapList.add(rows);
            }
 
            //生成小车地图坐标间距数据
            for (int row = 1; row <= finalRow; row++) {
                for (int bay = 1; bay <= finalBay; bay++) {
                    MapNode mapNode = mapList.get(row - 1).get(bay - 1);
                    JSONObject rowData = rowMap.get(row);
                    JSONObject bayData = bayMap.get(bay);
 
                    Integer top = rowData.getInteger("top");
                    Integer bottom = rowData.getInteger("bottom");
                    Integer left = bayData.getInteger("left");
                    Integer right = bayData.getInteger("right");
 
                    mapNode.setTop(top);
                    mapNode.setBottom(bottom);
                    mapNode.setLeft(left);
                    mapNode.setRight(right);
                }
            }
 
            //生成牛眼Y坐标基准数据
            for (int bay = 1; bay <= finalBay; bay++) {
                int yBase = 10000;
                for (int row = 1; row <= finalRow; row++) {
                    MapNode mapNode = mapList.get(row - 1).get(bay - 1);
                    HashMap<String, Object> nodeData = excelData.get(row + 1).get(bay + 1);
 
                    JSONObject rowData = rowMap.get(row);
                    Integer bottom = rowData.getInteger("bottom");
                    String nodeValue = nodeData.get("value").toString();
                    if (Utils.isJSON(nodeValue)) {
                        JSONObject jsonData = JSON.parseObject(nodeValue);
                        if (jsonData.containsKey("bottom")) {
                            bottom = jsonData.getInteger("bottom");
                        }
                    }
 
                    if (row != 1) {
                        yBase += bottom;
                    }
                    mapNode.setYBase(yBase);
                }
            }
 
            //生成牛眼X坐标基准数据
            for (int row = 1; row <= finalRow; row++) {
                int xBase = 10000;
                for (int bay = 1; bay <= finalBay; bay++) {
                    MapNode mapNode = mapList.get(row - 1).get(bay - 1);
                    HashMap<String, Object> nodeData = excelData.get(row + 1).get(bay + 1);
 
                    JSONObject bayData = bayMap.get(bay);
                    Integer right = bayData.getInteger("right");
                    String nodeValue = nodeData.get("value").toString();
                    if (Utils.isJSON(nodeValue)) {
                        JSONObject jsonData = JSON.parseObject(nodeValue);
                        if (jsonData.containsKey("right")) {
                            right = jsonData.getInteger("right");
                        }
                    }
 
                    if (bay != 1) {
                        xBase += right;
                    }
                    mapNode.setXBase(xBase);
                }
            }
 
            //生成四向车地图所需格式
            MapNode disableNode = new MapNode();
            disableNode.setId("0-0");
            disableNode.setValue(MapNodeType.DISABLE.id);
            disableNode.setLeft(0);
            disableNode.setRight(0);
            disableNode.setTop(0);
            disableNode.setBottom(0);
 
            List<MapNode> disableRow = new ArrayList<>();
            for (int bay = 1; bay <= finalBay; bay++) {
                disableRow.add(disableNode);
            }
            disableRow.add(0, disableNode);
            disableRow.add(disableNode);
            for (List<MapNode> mapNodes : mapList) {
                mapNodes.add(0, disableNode);
                mapNodes.add(disableNode);
            }
            mapList.add(0, disableRow);
            mapList.add(disableRow);
 
            System.out.println(JSON.toJSONString(mapList, SerializerFeature.DisableCircularReferenceDetect));
            levMap.put(lev, mapList);
        }
 
        return levMap;
    }
 
    public HashMap<Integer, List<List<HashMap<String, Object>>>> readExcel(String filePath) throws IOException {
        HashMap<Integer, List<List<HashMap<String, Object>>>> dataMap = new HashMap<>();
 
        FileInputStream inputStream = new FileInputStream(new File(filePath));
        Workbook workbook;
 
        if (filePath.endsWith(".xlsx")) {
            workbook = new XSSFWorkbook(inputStream);
        } else if (filePath.endsWith(".xls")) {
            throw new IllegalArgumentException("文件格式不支持");
        } else {
            throw new IllegalArgumentException("文件格式不支持");
        }
 
        int numberOfSheets = workbook.getNumberOfSheets();
        System.out.println("总共有 " + numberOfSheets + " 个Sheet");
 
        // 遍历所有Sheet
        for (int i = 0; i < numberOfSheets; i++) {
            Sheet sheet = workbook.getSheetAt(i);
            String sheetName = sheet.getSheetName();
 
            List<List<HashMap<String, Object>>> data = new ArrayList<>();
            if (sheetName.contains("F")) {
                String[] split = sheetName.split("F");
                String levStr = split[0];
                Integer lev = Integer.parseInt(levStr);
 
                for (Row row : sheet) {
                    List<HashMap<String, Object>> rowData = new ArrayList<>();
                    for (Cell cell : row) {
                        rowData.add(getCellValue(cell));
                    }
                    data.add(rowData);
                }
 
                dataMap.put(lev, data);
            }
        }
 
        workbook.close();
        inputStream.close();
        return dataMap;
    }
 
    private HashMap<String, Object> getCellValue(Cell cell) {
        if (cell == null) {
            return null;
        }
 
        HashMap<String, Object> map = new HashMap<>();
 
        String bgColor = getCellBackgroundColor(cell);
        map.put("bgColor", bgColor);
 
        String value = "";
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                break;
            case Cell.CELL_TYPE_NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
                break;
            case Cell.CELL_TYPE_BOOLEAN:
                value = String.valueOf(cell.getBooleanCellValue());
                break;
            case Cell.CELL_TYPE_FORMULA:
                value = cell.getCellFormula();
                break;
            case Cell.CELL_TYPE_BLANK:
                value = "";
                break;
            default:
                value = "";
        }
 
        map.put("value", value);
        return map;
    }
 
 
    public String getCellBackgroundColor(Cell cell) {
        if (cell == null) {
            return "无单元格";
        }
 
        CellStyle style = cell.getCellStyle();
        short bgColorIndex = style.getFillForegroundColor(); // 获取前景色(实际是底色)
 
        return getXSSFBackgroundColor(style);
    }
 
    // 处理XSSF (.xlsx) 颜色
    private String getXSSFBackgroundColor(CellStyle style) {
        XSSFColor color = (XSSFColor) style.getFillForegroundColorColor();
        if (color == null) {
            return "default";
        }
 
        byte[] rgb = color.getRGB();
        if (rgb == null) {
            return "default";
        }
 
        return String.format("RGB(%d,%d,%d)",
                rgb[0] & 0xFF,
                rgb[1] & 0xFF,
                rgb[2] & 0xFF);
    }
 
}