From 1d915922d8197aa5d5dc3e40e3088d983dd6e141 Mon Sep 17 00:00:00 2001
From: liding <756868258@qq.com>
Date: 星期五, 13 三月 2026 17:57:59 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_New_kthg' into dev_New_kthg

---
 src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java |  268 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 268 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java b/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java
new file mode 100644
index 0000000..feafdd0
--- /dev/null
+++ b/src/main/java/com/ruoyi/stock/word/WeighbridgeDocGenerator.java
@@ -0,0 +1,268 @@
+package com.ruoyi.stock.word;
+
+import com.ruoyi.basic.pojo.Product;
+import com.ruoyi.basic.service.IProductService;
+import com.ruoyi.stock.dto.StockInRecordDto;
+import org.apache.poi.xwpf.usermodel.*;
+import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * 纾呭崟Word鏂囨。鐢熸垚鍣�
+ */
+@Component
+public class WeighbridgeDocGenerator {
+
+    @Value("${file.upload-dir}")
+    private String uploadDir;
+
+    @Autowired
+    private IProductService productService;
+
+    /**
+     * 鐢熸垚纾呭崟Word鏂囨。
+     * @param dto 鍏ュ簱璁板綍DTO
+     * @return 鏂囦欢淇濆瓨鐨勭粷瀵硅矾寰勶紝鐢熸垚澶辫触杩斿洖null
+     */
+    public String generateWeighbridgeDoc(StockInRecordDto dto) {
+        // 鍙傛暟鏍¢獙
+        if (dto == null) {
+            return null;
+        }
+
+        FileOutputStream out = null;
+        XWPFDocument document = null;
+
+        try {
+            // 鍒涘缓鏂版枃妗�
+            document = new XWPFDocument();
+
+            // 鏋勫缓鏂囨。鍐呭
+            buildDocumentContent(document, dto);
+
+            // 鏋勫缓鏂囦欢淇濆瓨璺緞锛堢粷瀵硅矾寰勶級
+            String absolutePath = buildAbsoluteFilePath(dto);
+            File file = new File(absolutePath);
+
+            // 纭繚鐩綍瀛樺湪
+            file.getParentFile().mkdirs();
+
+            // 鍐欏叆鏂囦欢
+            out = new FileOutputStream(file);
+            document.write(out);
+            out.flush();
+
+            // 杩斿洖缁濆璺緞锛堢敤浜庢暟鎹簱瀛樺偍锛�
+            return absolutePath;
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            return null;
+        } finally {
+            // 鍏抽棴璧勬簮
+            try {
+                if (out != null) {
+                    out.close();
+                }
+                if (document != null) {
+                    document.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+        }
+    }
+
+    /**
+     * 鏋勫缓鏂囨。鍐呭
+     */
+    private void buildDocumentContent(XWPFDocument document, StockInRecordDto dto) {
+        // 1. 娣诲姞鏍囬 "纾呯爜鍗�" - 浣跨敤榛戜綋
+        XWPFParagraph titlePara = document.createParagraph();
+        titlePara.setAlignment(ParagraphAlignment.CENTER);
+        titlePara.setSpacingAfter(200);
+
+        XWPFRun titleRun = titlePara.createRun();
+        titleRun.setText("纾呯爜鍗�");
+        titleRun.setBold(true);
+        titleRun.setFontSize(28);
+        titleRun.setFontFamily("榛戜綋");
+
+        // 2. 绌轰竴琛�
+        document.createParagraph();
+
+        // 3. 澶撮儴淇℃伅锛堝勾鏈堟棩鍜岃閲忓崟浣嶅悎骞跺湪涓�琛岋級- 浣跨敤瀹嬩綋
+        XWPFParagraph headerPara = document.createParagraph();
+        headerPara.setSpacingAfter(200);
+        XWPFRun headerRun = headerPara.createRun();
+
+        // 鏍煎紡鍖栨棩鏈熶负 yyyy-MM-dd
+        String weighDate = "";
+        if (dto.getWeighingDate() != null) {
+            try {
+                // 鍋囪 weighingDate 鏄� LocalDateTime 绫诲瀷
+                weighDate = dto.getWeighingDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
+            } catch (Exception e) {
+                // 濡傛灉鏄瓧绗︿覆绫诲瀷锛屽皾璇曡浆鎹�
+                weighDate = dto.getWeighingDate().toString();
+                if (weighDate.length() > 10) {
+                    weighDate = weighDate.substring(0, 10);
+                }
+            }
+        }
+
+        headerRun.setText("骞存湀鏃ワ細" + weighDate + "    璁¢噺鍗曚綅锛氾紙鍚級");
+        headerRun.setFontSize(12);
+        headerRun.setFontFamily("瀹嬩綋");
+
+        // 4. 鍒涘缓涓昏〃鏍� - 4琛�6鍒楋紙琛ㄥご1琛�+鏁版嵁1琛�+鍚堣1琛�+绌鸿1琛岋級
+        XWPFTable table = document.createTable(4, 6);
+
+        // 璁剧疆琛ㄦ牸瀹藉害
+        table.setWidth("100%");
+
+        // 璁剧疆琛ㄦ牸杈规鍔犵矖锛堣竟妗嗗搴﹁涓�2锛�
+        table.setInsideHBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
+        table.setInsideVBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
+        table.setTopBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
+        table.setBottomBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
+        table.setLeftBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
+        table.setRightBorder(XWPFBorderType.SINGLE, 2, 0, "000000");
+
+        // 璁剧疆琛岄珮
+        table.getRow(0).setHeight(400);
+        table.getRow(1).setHeight(400);
+        table.getRow(2).setHeight(400);
+        table.getRow(3).setHeight(400);
+
+        // 璁剧疆琛ㄥご - 浣跨敤榛戜綋鍔犵矖
+        String[] headers = {"杞﹀彿", "鍝佸悕", "姣涢噸", "鐨噸", "鍑�閲�", "澶囨敞"};
+        XWPFTableRow headerRow = table.getRow(0);
+        for (int i = 0; i < headers.length; i++) {
+            XWPFTableCell cell = headerRow.getCell(i);
+            cell.setText(headers[i]);
+            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+
+            XWPFParagraph cellPara = cell.getParagraphs().get(0);
+            cellPara.setAlignment(ParagraphAlignment.CENTER);
+            XWPFRun cellRun = cellPara.getRuns().get(0);
+            cellRun.setBold(true);
+            cellRun.setFontSize(12);
+            cellRun.setFontFamily("榛戜綋");
+        }
+
+        // 璁剧疆鏁版嵁琛� - 浣跨敤瀹嬩綋
+        XWPFTableRow dataRow = table.getRow(1);
+        String netWeight = dto.getNetWeight() != null ? dto.getNetWeight().toString() : "";
+        String grossWeight = dto.getGrossWeight() != null ? dto.getGrossWeight().toString() : "";
+        String tareWeight = dto.getTareWeight() != null ? dto.getTareWeight().toString() : "";
+
+        String[] data = {
+                dto.getLicensePlateNo() != null ? dto.getLicensePlateNo() : "",
+                getProductModelName(dto.getProductId()),
+                grossWeight + "t",
+                tareWeight + "t",
+                netWeight + "t",
+                dto.getRemark() != null ? dto.getRemark() : ""
+        };
+
+        for (int i = 0; i < data.length; i++) {
+            XWPFTableCell cell = dataRow.getCell(i);
+            cell.setText(data[i]);
+            cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+
+            XWPFParagraph cellPara = cell.getParagraphs().get(0);
+            cellPara.setAlignment(ParagraphAlignment.CENTER);
+            XWPFRun cellRun = cellPara.getRuns().get(0);
+            cellRun.setFontSize(12);
+            cellRun.setFontFamily("瀹嬩綋");
+        }
+
+        // 鍚堣琛� - 绗�3琛�
+        XWPFTableRow totalRow = table.getRow(2);
+
+        // 绗�1鍒楁樉绀�"鍚堣锛堝ぇ鍐欙級"
+        XWPFTableCell totalLabelCell = totalRow.getCell(0);
+        totalLabelCell.setText("鍚堣锛堝ぇ鍐欙級");
+        totalLabelCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+        XWPFParagraph totalLabelPara = totalLabelCell.getParagraphs().get(0);
+        totalLabelPara.setAlignment(ParagraphAlignment.CENTER);
+        XWPFRun totalLabelRun = totalLabelPara.getRuns().get(0);
+        totalLabelRun.setBold(true);
+        totalLabelRun.setFontSize(12);
+        totalLabelRun.setFontFamily("瀹嬩綋");
+
+
+        XWPFTableCell contentCell = totalRow.getCell(1);
+        String netWeightChinese = ChineseNumberUtil.numberToChinese(
+                dto.getNetWeight() != null ? dto.getNetWeight() : BigDecimal.ZERO
+        );
+        contentCell.setText(netWeightChinese + "鍚�");
+        contentCell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
+        XWPFParagraph contentPara = contentCell.getParagraphs().get(0);
+        contentPara.setAlignment(ParagraphAlignment.LEFT);
+        XWPFRun contentRun = contentPara.getRuns().get(0);
+        contentRun.setFontSize(12);
+        contentRun.setFontFamily("瀹嬩綋");
+
+        // 鍚堝苟绗�3-6鍒楀埌绗�2鍒�
+        for (int i = 2; i < 6; i++) {
+            XWPFTableCell cell = totalRow.getCell(i);
+            // 璁剧疆杩欎簺鍗曞厓鏍间负鍚堝苟鐘舵�侊紙缁ф壙鑷2鍒楋級
+            cell.getCTTc().addNewTcPr().addNewHMerge().setVal(org.openxmlformats.schemas.wordprocessingml.x2006.main.STMerge.CONTINUE);
+        }
+
+        // 6. 鍒涘缓杩囩鍛樿锛堝崟鐙竴琛岋紝宸﹀榻愶級
+        XWPFParagraph operatorPara = document.createParagraph();
+        operatorPara.setSpacingAfter(100);
+        operatorPara.setAlignment(ParagraphAlignment.LEFT);
+        XWPFRun operatorRun = operatorPara.createRun();
+        String operator = dto.getWeighingOperator() != null ? dto.getWeighingOperator() : "";
+        operatorRun.setText("杩囩鍛橈細" + operator);
+        operatorRun.setFontSize(8);
+        operatorRun.setFontFamily("瀹嬩綋");
+
+    }
+
+    /**
+     * 鏋勫缓鏂囦欢淇濆瓨鐨勭粷瀵硅矾寰�
+     */
+    private String buildAbsoluteFilePath(StockInRecordDto dto) {
+        LocalDateTime now = LocalDateTime.now();
+        String year = String.valueOf(now.getYear());
+        String month = String.format("%02d", now.getMonthValue());
+        String day = String.format("%02d", now.getDayOfMonth());
+
+        // 鏂囦欢鍚嶆牸寮忥細纾呭崟_杞︾墝鍙穇骞存湀鏃ユ椂鍒嗙.docx
+        String fileName = String.format("纾呭崟_%s_%s.docx",
+                dto.getLicensePlateNo() != null ? dto.getLicensePlateNo() : "鏈煡杞︾墝",
+                now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")));
+
+        return uploadDir + File.separator + year + File.separator + month + File.separator + day + File.separator + fileName;
+    }
+
+    /**
+     * 鑾峰彇浜у搧鍨嬪彿鍚嶇О
+     */
+    private String getProductModelName(Long productModelId) {
+        if (productModelId == null) {
+            return "鏈煡浜у搧";
+        }
+        try {
+            Product product = productService.getById(productModelId);
+            return product != null ? product.getProductName() : "鏈煡浜у搧";
+        } catch (Exception e) {
+            e.printStackTrace();
+            return "鏈煡浜у搧";
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.3