From f6abc8c724eed706487b7616bc6c816e5db5c939 Mon Sep 17 00:00:00 2001
From: czt <czt18638530771@163.com>
Date: 星期二, 09 十二月 2025 20:13:48 +0800
Subject: [PATCH] 数量检测页面提交

---
 fzzy-igdss-web/src/main/java/com/fzzy/sys/manager/grain/GrainExportBuilder.java |  346 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 346 insertions(+), 0 deletions(-)

diff --git a/fzzy-igdss-web/src/main/java/com/fzzy/sys/manager/grain/GrainExportBuilder.java b/fzzy-igdss-web/src/main/java/com/fzzy/sys/manager/grain/GrainExportBuilder.java
new file mode 100644
index 0000000..e751048
--- /dev/null
+++ b/fzzy-igdss-web/src/main/java/com/fzzy/sys/manager/grain/GrainExportBuilder.java
@@ -0,0 +1,346 @@
+package com.fzzy.sys.manager.grain;
+
+import com.fzzy.igds.constant.DepotType;
+import com.fzzy.igds.constant.FoodVariety;
+import com.fzzy.igds.data.GrainData;
+import com.fzzy.igds.data.GrainLay;
+import com.fzzy.igds.data.GrainPoint;
+import com.fzzy.igds.domain.Depot;
+import com.fzzy.igds.service.FileService;
+import com.fzzy.igds.utils.ContextUtil;
+import com.fzzy.igds.utils.GrainDataBuilder;
+import com.fzzy.igds.utils.NumberUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.time.DateFormatUtils;
+import org.apache.poi.hssf.usermodel.HSSFRow;
+import org.apache.poi.hssf.usermodel.HSSFSheet;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @Desc: 绮儏椤甸潰鎵归噺瀵煎嚭EXCEL
+ * @author: Andy
+ * @update-time: 2022/12/8
+ */
+@Slf4j
+@Component
+public class GrainExportBuilder {
+
+    @Autowired
+    private FileService fileService;
+
+    /**
+     * 鐢熸垚鐢熸垚
+     *
+     * @param dataMap
+     * @return 濡傛灉杩斿洖淇℃伅浠ワ細ERROR:寮�澶磋〃绀烘墽琛屽嚭閿欙紝姝e父杩斿洖鏂囦欢鍚嶇О
+     */
+    public String exportByMapData(Map<String, GrainData> dataMap, List<Depot> listDepot) {
+
+        String result;
+        try {
+            //鍒涘缓涓�涓柊鐨凟XCEL
+            HSSFWorkbook toWorkbook = createWorkBook();
+            HSSFWorkbook fromWorkBook;
+
+            Map<String, HSSFWorkbook> mapWorkBook = new HashMap<>();
+
+            HSSFSheet newSheet, oldSheet;
+
+            Depot depot = null;
+            String cableKey = "";
+            for (GrainData data : dataMap.values()) {
+                depot = this.getDepot(data.getDepotId(), listDepot);
+
+                if (null == depot) {
+                    log.info("--------绮儏瀵煎嚭EXCEL娌℃湁鑾峰彇鍒颁粨搴撲俊鎭紝鍙栨秷瀵煎嚭----{}", data.getDepotId());
+                    continue;
+                }
+                if (StringUtils.isEmpty(data.getCableCir())) {
+                    cableKey = data.getCable();
+                } else {
+                    cableKey = data.getCable() + "_" + data.getCableCir();
+                }
+
+                fromWorkBook = mapWorkBook.get(cableKey);
+                if (null == fromWorkBook) {
+                    fromWorkBook = this.getFromWorkBook(data, depot);
+                    mapWorkBook.put(cableKey, fromWorkBook);
+                }
+
+                newSheet = toWorkbook.createSheet(depot.getName());
+
+                oldSheet = fromWorkBook.getSheetAt(0);
+
+                this.copySheet(newSheet, oldSheet, toWorkbook, data, depot);
+
+                Thread.sleep(300);
+            }
+
+            result = ContextUtil.getTimeId() + ".xls";
+            String tempPath = fileService.getTempFilePath(depot == null ? ContextUtil.getCompanyId() : depot.getCompanyId());
+            FileOutputStream fos = new FileOutputStream(tempPath + "/" + result);
+            toWorkbook.write(fos);
+            if (null != fos) fos.close();
+
+        } catch (Exception e) {
+            log.info("---------------瀵煎嚭寮傚父淇℃伅----{}", e);
+            result = "ERROR:瀵煎嚭閿欒锛�" + e.getMessage();
+        }
+
+        return result;
+    }
+
+    private HSSFWorkbook getFromWorkBook(GrainData data, Depot depot) throws Exception {
+        FileInputStream fis = null;
+
+        String filePath = fileService.getConfPath();
+        try {
+            //鑾峰彇妯$増鐨勫悕绉帮紝娉ㄦ剰绛掍粨鍚嶇О閰嶇疆瑙勫垯
+            if (StringUtils.isEmpty(data.getCableCir())) {
+                filePath = filePath + data.getCable() + ".xls";
+            } else {
+                filePath = filePath + data.getCable() + "_" + data.getCableCir() + ".xls";
+            }
+
+            fis = new FileInputStream(filePath);
+            HSSFWorkbook wb = new HSSFWorkbook(fis);
+
+            return wb;
+        } catch (Exception e) {
+            log.error("------鏍规嵁绮儏瀵煎嚭EXCEL鎵ц寮傚父锛歿}", e);
+            return null;
+        } finally {
+            if (null != fis) fis.close();
+        }
+    }
+
+    private Depot getDepot(String depotId, List<Depot> listDepot) {
+        if (listDepot == null || listDepot.isEmpty()) return null;
+
+        for (Depot depot : listDepot) {
+            if (depot.getId().equals(depotId)) return depot;
+        }
+        return null;
+    }
+
+
+    private HSSFWorkbook createWorkBook() {
+        return new HSSFWorkbook();
+    }
+
+
+    /**
+     * 澶嶅埗sheet
+     *
+     * @param newSheet
+     * @param oldSheet
+     * @return
+     */
+    public Sheet copySheet(HSSFSheet newSheet, HSSFSheet oldSheet, Workbook workbook, GrainData data, Depot depot) {
+
+        //鍚堝苟鍗曞厓鏍�
+        int numMergedRegions = oldSheet.getNumMergedRegions();
+        for (int i = 0; i < numMergedRegions; i++) {
+            CellRangeAddress mergedRegion = oldSheet.getMergedRegion(i);
+            newSheet.addMergedRegion(mergedRegion);
+        }
+
+
+        // 璁剧疆鍒楀
+        int physicalNumberOfCells = oldSheet.getRow(0).getPhysicalNumberOfCells();
+        for (int i = 0; i < physicalNumberOfCells; i++) {
+            HSSFRow fromRow = oldSheet.getRow(i);
+            if (null == fromRow) continue;
+            for (int j = fromRow.getLastCellNum(); j >= fromRow.getFirstCellNum(); j--) {
+                newSheet.setColumnWidth(j, oldSheet.getColumnWidth(j));
+                newSheet.setColumnHidden(j, false);
+            }
+        }
+
+
+        //姝ゅ闇�瑕佸皢 cellStyle 瀹氫箟鍦ㄩ亶鍘嗚鐨勫湴鏂�,瀹氫箟鍦ㄥ闈㈠彲鑳藉嚭鐜版牱寮忔覆鏌撻敊璇�
+        CellStyle cellStyle = workbook.createCellStyle();
+        cellStyle.setWrapText(true);
+
+
+        //姣忔瀹屾垚涓�涓垹闄や竴涓�
+        List<GrainPoint> listPoints = data.getListPoints();
+
+        //鏈�澶ц幏鍙栬鏁�
+        int maxRowSize = oldSheet.getPhysicalNumberOfRows() - 1;
+        for (int i = 0; i < maxRowSize; i++) {
+            Row newRow = newSheet.createRow(i);
+            Row oldRow = oldSheet.getRow(i);
+            newRow.setHeight((short) (20 * 20));
+
+            int maxColSize = oldRow.getPhysicalNumberOfCells();
+
+            String oldCellValue;
+            for (int j = 0; j < maxColSize; j++) {
+                Cell newCell = newRow.createCell(j);
+                Cell oldCell = oldRow.getCell(j);
+                if (oldCell == null) {
+                    continue;
+                }
+                oldCellValue = oldCell.getStringCellValue();
+
+                newCell.setCellValue(getNewCellValue(oldCellValue, listPoints, data, depot));
+
+                newCell.setCellStyle(cellStyle);
+            }
+        }
+        return newSheet;
+    }
+
+
+    /**
+     * 鏍规嵁鍗曞厓鏍兼爣绛炬浛鎹㈠�硷紝闇�瑕佹浛鎹㈢殑鏍囪涓篶_
+     *
+     * @param oldCellValue
+     * @param data
+     * @return
+     */
+    public String getNewCellValue(String oldCellValue, List<GrainPoint> listPoints, GrainData data, Depot depot) {
+
+        try {
+
+            if (null == oldCellValue) return null;
+
+            if (!oldCellValue.startsWith("c_")) return oldCellValue;
+
+            if ("c_title".equals(oldCellValue)) return "绮儏鎶ヨ〃";
+            if ("c_depot".equals(oldCellValue)) return depot.getName() + "绮儏鎶ヨ〃";
+            if ("c_depot_name".equals(oldCellValue)) return depot.getName();
+            if ("c_weather".equals(oldCellValue)) return data.getWeather();
+            if ("c_checkDate".equals(oldCellValue))
+                return DateFormatUtils.format(data.getReceiveDate(), "yyyy-MM-dd HH:mm");
+            if ("c_checkDateYMD".equals(oldCellValue))
+                return DateFormatUtils.format(data.getReceiveDate(), "yyyy-MM-dd HH:mm:ss");
+            if ("c_ori".equals(oldCellValue)) return "";
+            if ("c_tIn".equals(oldCellValue)) return getStrValue(data.getTempIn());
+            if ("c_tOut".equals(oldCellValue)) return getStrValue(data.getTempOut());
+            if ("c_hIn".equals(oldCellValue)) return getStrValue(data.getHumidityIn());
+            if ("c_hOut".equals(oldCellValue)) return getStrValue(data.getHumidityOut());
+
+            if ("c_tempMax".equals(oldCellValue)) return getStrValue(data.getTempMax());
+            if ("c_tempMin".equals(oldCellValue)) return getStrValue(data.getTempMin());
+            if ("c_tempAve".equals(oldCellValue)) return getStrValue(data.getTempAve());
+            if ("c_depotType".equals(oldCellValue)) return DepotType.getMsg(depot.getDepotType());
+            if ("c_storeDate".equals(oldCellValue))
+                return null == depot.getStoreDate() ? null : DateFormatUtils.format(depot.getStoreDate(), "yyyy-MM-dd");
+            if ("c_foodVariety".equals(oldCellValue)) return FoodVariety.getMsg(depot.getFoodVariety());
+            if ("c_storageReal".equals(oldCellValue)) return getStrValue(NumberUtil.keepPrecision(depot.getStorageReal()/1000, 2));
+            if ("c_foodLocation".equals(oldCellValue)) return depot.getFoodLocation();
+            if ("c_bulkWeight".equals(oldCellValue)) return getStrValue(depot.getBulkWeight());
+            if ("c_checkUser".equals(oldCellValue)) return data.getCheckUser();
+            if ("c_storeKeeper".equals(oldCellValue)) return depot.getStoreKeeperName();
+            if ("c_createDate".equals(oldCellValue)) return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm");
+
+            if (oldCellValue.startsWith("c_lay_")) {
+                String[] tempAttr = oldCellValue.split("_");
+                int layNum = Integer.valueOf(tempAttr[3]);
+                for (GrainLay lay : data.getListLays()) {
+                    if (lay.getFz() == layNum) {
+                        if (oldCellValue.indexOf("max") > 0) return getStrValue(lay.getTempMax());
+                        if (oldCellValue.indexOf("min") > 0) return getStrValue(lay.getTempMin());
+                        if (oldCellValue.indexOf("ave") > 0) return getStrValue(lay.getTempAve());
+                    }
+                }
+                return oldCellValue;
+            }
+
+
+            //骞虫埧浠撶殑鑾峰彇鏂瑰紡
+
+            if (DepotType.TYPE_01.getCode().equals(depot.getDepotType()) || DepotType.TYPE_05.getCode().equals(depot.getDepotType())) {
+                return getNewCellValue01(data, oldCellValue, listPoints);
+            }
+
+            if (DepotType.TYPE_02.getCode().equals(depot.getDepotType())) {
+                return getNewCellValue02(data, oldCellValue, listPoints);
+            }
+
+            if (DepotType.TYPE_03.getCode().equals(depot.getDepotType())) {
+                return getNewCellValue02(data, oldCellValue, listPoints);
+            }
+
+            if (DepotType.TYPE_04.getCode().equals(depot.getDepotType())) {
+                return getNewCellValue02(data, oldCellValue, listPoints);
+            }
+
+        } catch (Exception e) {
+            log.error("---------鎵归噺瀵煎嚭绮儏EXCEL---璧嬪�煎嚭閿�---{}", e);
+            return oldCellValue;
+        }
+
+        return null;
+    }
+
+
+    /**
+     * @Desc: 绛掍粨鐨勭伯娓╃偣瑙勫垯c_x_y_z 鍏朵腑x = 琛ㄧず鍦堟暟锛寉=鎬绘牴鏁帮紝z= 褰撳墠灞�
+     * @author: Andy
+     * @update-time: 2022/12/12
+     */
+    private String getNewCellValue02(GrainData data, String oldCellValue, List<GrainPoint> listPoints) {
+        //鍗曞厓鏍艰祴鍊� c_x_y_z 鍏朵腑x = 琛ㄧず鍦堟暟锛寉=鎬绘牴鏁帮紝z= 褰撳墠灞�
+        String[] attr = oldCellValue.split("_");
+        int x = Integer.valueOf(attr[1]), y = Integer.valueOf(attr[2]), z = Integer.valueOf(attr[3]);
+
+        x = x - 1;
+        y = y - 1;
+        GrainPoint point;
+        for (int i = 0; i < listPoints.size(); i++) {
+            point = listPoints.get(i);
+
+            if (point.getX() == x && point.getY() == y && point.getFz() == z) {
+                listPoints.remove(i);
+                return GrainDataBuilder.renderTempValue(point.getTemp());
+            }
+        }
+
+        return "--";
+    }
+
+
+    private String getNewCellValue01(GrainData data, String oldCellValue, List<GrainPoint> listPoints) {
+        String cable = data.getCable();
+        String[] attrCable = cable.split("-");
+        int numX = Integer.valueOf(attrCable[2]);
+
+
+        //鍗曞厓鏍艰祴鍊� c_灞俖琛宊鍒�
+        String[] attr = oldCellValue.split("_");
+        int lay = Integer.valueOf(attr[1]), row = Integer.valueOf(attr[2]), col = Integer.valueOf(attr[3]);
+
+        row = row - 1;
+        col = numX - col;
+
+        GrainPoint point;
+        for (int i = 0; i < listPoints.size(); i++) {
+            point = listPoints.get(i);
+            if (point.getFz() == lay && point.getY() == row && point.getX() == col) {
+                listPoints.remove(i);
+                return GrainDataBuilder.renderTempValue(point.getTemp());
+            }
+        }
+
+        return "--";
+    }
+
+    private static String getStrValue(Double tempIn) {
+        if (null == tempIn) return null;
+        return tempIn + "";
+    }
+}

--
Gitblit v1.9.3