#
Junjie
2 天以前 34b0c36cf609ba295c8f66a85d479f740964a864
src/main/java/com/zy/asrs/utils/MapExcelUtils.java
@@ -4,10 +4,12 @@
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.core.common.Cools;
import com.core.exception.CoolException;
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.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
@@ -15,10 +17,7 @@
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;
import java.util.*;
@Component
public class MapExcelUtils {
@@ -72,6 +71,8 @@
                for (int bay = 1; bay <= finalBay; bay++) {
                    MapNode node = new MapNode();
                    node.setId(row + "-" + bay);
                    node.setRow(row);
                    node.setBay(bay);
                    HashMap<String, Object> nodeData = excelData.get(row + 1).get(bay + 1);
                    String nodeType = nodeData.get("bgColor").toString();
@@ -102,6 +103,7 @@
                mapList.add(rows);
            }
            MapNode baseMapNode = null;
            //生成小车地图坐标间距数据
            for (int row = 1; row <= finalRow; row++) {
                for (int bay = 1; bay <= finalBay; bay++) {
@@ -114,6 +116,29 @@
                    Integer left = bayData.getInteger("left");
                    Integer right = bayData.getInteger("right");
                    HashMap<String, Object> nodeData = excelData.get(row + 1).get(bay + 1);
                    String nodeValue = nodeData.get("value").toString();
                    if (Utils.isJSON(nodeValue)) {
                        JSONObject jsonData = JSON.parseObject(nodeValue);
                        if (jsonData.containsKey("top")) {
                            top = jsonData.getInteger("top");
                        }
                        if (jsonData.containsKey("bottom")) {
                            bottom = jsonData.getInteger("bottom");
                        }
                        if (jsonData.containsKey("left")) {
                            left = jsonData.getInteger("left");
                        }
                        if (jsonData.containsKey("right")) {
                            right = jsonData.getInteger("right");
                        }
                        if (jsonData.containsKey("xBase")) {
                            //基点
                            baseMapNode = mapNode;
                        }
                    }
                    mapNode.setTop(top);
                    mapNode.setBottom(bottom);
                    mapNode.setLeft(left);
@@ -121,51 +146,58 @@
                }
            }
            //生成牛眼Y坐标基准数据
            if (baseMapNode == null) {
                throw new CoolException("未设置基准点");
            }
            List<Integer> rowList = new ArrayList<>();
            List<Integer> bayList = new ArrayList<>();
            for (int row = 1; row <= finalRow; row++) {
                rowList.add(row);
            }
            for (int bay = 1; bay <= finalBay; bay++) {
                bayList.add(bay);
            }
            if (baseMapNode.getRow() != 1) {
                Collections.reverse(rowList);
            }
            if (baseMapNode.getBay() != 1) {
                Collections.reverse(bayList);
            }
            //生成牛眼Y坐标基准数据
            for (int i = 0; i < bayList.size(); i++) {
                Integer bay = bayList.get(i);
                int yBase = 10000;
                for (int row = 1; row <= finalRow; row++) {
                for (int j = 0; j < rowList.size(); j++) {
                    Integer row = rowList.get(j);
                    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);
                    if (baseMapNode.getRow() == 1) {
                        yBase += mapNode.getBottom();
                    }else {
                        yBase += mapNode.getTop();
                    }
                }
            }
            //生成牛眼X坐标基准数据
            for (int row = 1; row <= finalRow; row++) {
            for (int i = 0; i < rowList.size(); i++) {
                Integer row = rowList.get(i);
                int xBase = 10000;
                for (int bay = 1; bay <= finalBay; bay++) {
                for (int j = 0; j < bayList.size(); j++) {
                    Integer bay = bayList.get(j);
                    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);
                    if (baseMapNode.getBay() == 1) {
                        xBase += mapNode.getRight();
                    }else {
                        xBase += mapNode.getLeft();
                    }
                }
            }
@@ -226,12 +258,74 @@
                String levStr = split[0];
                Integer lev = Integer.parseInt(levStr);
                int idx = 0;
                int rowLength = -1;
                int cellLength = -1;
                Row titleRow = sheet.getRow(1);
                for (Cell cell : titleRow) {
                    if (idx < 2) {
                        idx++;
                        continue;
                    }
                    HashMap<String, Object> map = getCellValue(sheet, titleRow, cell);
                    String value = map.get("value").toString();
                    if (!Cools.isEmpty(value)) {
                        rowLength = (int) Double.parseDouble(value);
                    }
                }
                idx = 0;
                for (Row row : sheet) {
                    if (idx < 2) {
                        idx++;
                        continue;
                    }
                    Cell cell = row.getCell(1);
                    if(cell != null) {
                        HashMap<String, Object> map = getCellValue(sheet, row, cell);
                        String value = map.get("value").toString();
                        if (!Cools.isEmpty(value)) {
                            cellLength = (int) Double.parseDouble(value);
                        }
                    }
                }
                for (int j = 0; j < cellLength; j++) {
                    Row row = sheet.getRow(j + 2);
                    List<HashMap<String, Object>> rowData = new ArrayList<>();
                    for (Cell cell : row) {
                        rowData.add(getCellValue(cell));
                    for (int k = 0; k < rowLength; k++) {
                        Cell cell = row.getCell(k + 2);
                        rowData.add(getCellValue(sheet, row, cell));
                    }
                    data.add(rowData);
                }
                int numMergedRegions = sheet.getNumMergedRegions();
                for (int j = 0; j < numMergedRegions; j++) {
                    CellRangeAddress region = sheet.getMergedRegion(j);
                    int rowSpan = region.getLastRow() - region.getFirstRow() + 1;
                    int colSpan = region.getLastColumn() - region.getFirstColumn() + 1;
                    int rowIdx = region.getFirstRow() - 2;
                    int colIdx = region.getFirstColumn() - 2;
                    HashMap<String, Object> map = data.get(rowIdx).get(colIdx);
                    map.put("rowSpan", rowSpan);
                    map.put("colSpan", colSpan);
                    for (int k = region.getFirstRow(); k <= region.getLastRow(); k++) {
                        for (int l = region.getFirstColumn(); l <= region.getLastColumn(); l++) {
                            if(k == region.getFirstRow() && l == region.getFirstColumn()) {
                                continue;
                            }
                            HashMap<String, Object> mapData = data.get(k - 2).get(l - 2);
                            mapData.put("bgColor", "merge");
                        }
                    }
                }
                dataMap.put(lev, data);
@@ -243,9 +337,16 @@
        return dataMap;
    }
    private HashMap<String, Object> getCellValue(Cell cell) {
    private HashMap<String, Object> getCellValue(Sheet sheet, Row row, Cell cell) {
        if (cell == null) {
            return null;
            HashMap<String, Object> map = new HashMap<>();
            map.put("bgColor", "none");
            map.put("cellWidth", "");
            map.put("cellHeight", "");
            map.put("value", "");
            map.put("rowSpan", 1);
            map.put("colSpan", 1);
            return map;
        }
        HashMap<String, Object> map = new HashMap<>();
@@ -253,10 +354,22 @@
        String bgColor = getCellBackgroundColor(cell);
        map.put("bgColor", bgColor);
        int columnIndex = cell.getColumnIndex();
        int columnWidth = sheet.getColumnWidth(columnIndex);//获取列宽
        short rowHeight = row.getHeight(); //获取行高
        map.put("cellWidth", columnWidth);
        map.put("cellHeight", rowHeight);
        String value = "";
        switch (cell.getCellType()) {
            case Cell.CELL_TYPE_STRING:
                value = cell.getStringCellValue();
                try {
                    JSONObject jsonObject = JSON.parseObject(cell.getStringCellValue());
                    value = JSON.toJSONString(jsonObject);
                } catch (Exception e) {
                    value = cell.getStringCellValue();
                }
                break;
            case Cell.CELL_TYPE_NUMERIC:
                value = String.valueOf(cell.getNumericCellValue());
@@ -275,6 +388,8 @@
        }
        map.put("value", value);
        map.put("rowSpan", 1);
        map.put("colSpan", 1);
        return map;
    }