From cf456d684cadd84a97e8cb417e0613f81a3d47cd Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期四, 23 四月 2026 13:58:19 +0800
Subject: [PATCH] 导出销售台账送货单

---
 src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java |  270 +++++++++++++++++++++++++----------------------------
 1 files changed, 126 insertions(+), 144 deletions(-)

diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index 4bcd2a5..e2a5abd 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -232,178 +232,160 @@
 
     @Override
     public void exportShippingNote(HttpServletResponse response, List<Long> ids) {
-        log.info("寮�濮嬪鍑洪�佽揣鍗曪紝鍙拌处ID鍒楄〃: {}", ids);
-
-        // 鍙傛暟鏍¢獙
-        if (ids == null || ids.isEmpty()) {
-            throw new BaseException("璇烽�夋嫨瑕佸鍑虹殑閿�鍞彴璐�");
-        }
-
-        List<SalesLedger> salesLedgerList = salesLedgerMapper.selectList(
-                Wrappers.<SalesLedger>lambdaQuery()
-                        .in(SalesLedger::getId, ids)
-        );
-
-        if (salesLedgerList.isEmpty()) {
-            throw new BaseException("鏈壘鍒板搴旈攢鍞彴璐︽暟鎹�");
-        }
-
-        // 鏍规嵁閿�鍞彴璐︽暟閲忓喅瀹氬鍑烘柟寮�
-        if (salesLedgerList.size() == 1) {
-            // 鍗曚釜閿�鍞彴璐︼紝鐩存帴瀵煎嚭xlsx鏂囦欢
-            exportSingleShippingNote(response, salesLedgerList.get(0));
-        } else {
-            // 澶氫釜閿�鍞彴璐︼紝鐢熸垚鍘嬬缉鍖�
-            exportBatchShippingNote(response, salesLedgerList);
-        }
-    }
-    
-    /**
-     * 瀵煎嚭鍗曚釜閫佽揣鍗�
-     */
-    private void exportSingleShippingNote(HttpServletResponse response, SalesLedger ledger) {
         try {
-            String fileName = String.format("閫佽揣鍗昣%s.xlsx",
-                    Optional.ofNullable(ledger.getSalesContractNo()).orElse(String.valueOf(ledger.getId())));
+            List<SalesLedger> ledgerList = salesLedgerMapper.selectBatchIds(ids);
+            String fileName = "閫佽揣鍗�.xlsx";
             response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
             response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
             response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
 
-            SalesLedgerProduct cond = new SalesLedgerProduct();
-            cond.setSalesLedgerId(ledger.getId());
-            List<SalesLedgerProduct> productList = salesLedgerProductServiceImpl.selectSalesLedgerProductList(cond);
-            Customer customer = customerMapper.selectById(ledger.getCustomerId());
-
-            BigDecimal totalQuantity = BigDecimal.ZERO;
-            for (SalesLedgerProduct product : productList) {
-                totalQuantity = totalQuantity.add(product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity());
-            }
-
             Workbook workbook = new XSSFWorkbook();
-            Sheet sheet = workbook.createSheet(ledger.getSalesContractNo());
 
-            sheet.setColumnWidth(0, 2500);
-            sheet.setColumnWidth(1, 4000);
-            sheet.setColumnWidth(2, 9000);
-            sheet.setColumnWidth(3, 2500);
-            sheet.setColumnWidth(4, 3000);
-            sheet.setColumnWidth(5, 4500);
-            sheet.setColumnWidth(6, 3500);
+            for (SalesLedger ledger : ledgerList) {
+                // 姣忎釜鍗曟嵁涓�涓� sheet
+                Sheet sheet = workbook.createSheet(ledger.getSalesContractNo());
 
-            // 鏍峰紡
-            CellStyle companyTitle = createBaseStyle(workbook, "瀹嬩綋", (short) 20, true, true);
-            CellStyle billTitle = createBaseStyle(workbook, "瀹嬩綋", (short) 18, true, true);
-            CellStyle headerStyle = createBorderStyle(workbook, "瀹嬩綋", (short) 12, true, true);
-            CellStyle dataCenterStyle = createBorderStyle(workbook, "瀹嬩綋", (short) 11, false, true); // 鍐呭灞呬腑
-            CellStyle noBorder = createNoBorderStyle(workbook);
-            CellStyle fourDash = createDashBottomStyle(workbook);
+                sheet.setColumnWidth(0, 2500);
+                sheet.setColumnWidth(1, 4000);
+                sheet.setColumnWidth(2, 9000);
+                sheet.setColumnWidth(3, 2500);
+                sheet.setColumnWidth(4, 3000);
+                sheet.setColumnWidth(5, 4500);
+                sheet.setColumnWidth(6, 3500);
 
-            // 鏍囬
-            Row row0 = sheet.createRow(0);
-            row0.createCell(0).setCellValue("婀栧崡楣忓垱鐢靛瓙鏈夐檺鍏徃");
-            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
-            fillMergeRange(sheet, 0, 0, 0, 6, companyTitle);
+                // 鏍峰紡
+                CellStyle companyTitle = createBaseStyle(workbook, "瀹嬩綋", (short) 20, true, true);
+                CellStyle billTitle = createBaseStyle(workbook, "瀹嬩綋", (short) 18, true, true);
+                CellStyle headerStyle = createBorderStyle(workbook, "瀹嬩綋", (short) 12, true, true);
+                CellStyle dataCenterStyle = createBorderStyle(workbook, "瀹嬩綋", (short) 11, false, true);
+                CellStyle noBorder = createNoBorderStyle(workbook);
+                CellStyle fourDash = createDashBottomStyle(workbook);
 
-            Row row1 = sheet.createRow(1);
-            row1.createCell(0).setCellValue("閫� 璐� 鍗�");
-            sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
-            fillMergeRange(sheet, 1, 1, 0, 6, billTitle);
+                // 鏍囬
+                Row row0 = sheet.createRow(0);
+                row0.createCell(0).setCellValue("婀栧崡楣忓垱鐢靛瓙鏈夐檺鍏徃");
+                sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
+                fillMergeRange(sheet, 0, 0, 0, 6, companyTitle);
 
-            // 瀹㈡埛淇℃伅
-            Row row2 = sheet.createRow(2);
-            row2.createCell(0).setCellValue("瀹㈡埛鍚嶇О锛�" + ledger.getCustomerName());
-            row2.createCell(4).setCellValue("鍗曟嵁缂栧彿锛�" + ledger.getSalesContractNo());
+                Row row1 = sheet.createRow(1);
+                row1.createCell(0).setCellValue("閫� 璐� 鍗�");
+                sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
+                fillMergeRange(sheet, 1, 1, 0, 6, billTitle);
 
-            Row row3 = sheet.createRow(3);
-            row3.createCell(0).setCellValue("閫佽揣鍦板潃锛�" + customer.getCompanyAddress());
-            row3.createCell(4).setCellValue("閫佽揣鏃ユ湡锛�" + formatDate(LocalDate.now()));
+                // 瀹㈡埛淇℃伅
+                Customer customer = customerMapper.selectById(ledger.getCustomerId());
 
-            Row row4 = sheet.createRow(4);
-            row4.createCell(0).setCellValue("鑱旂郴鏂瑰紡锛�" + customer.getContactPerson() + " " + customer.getContactPhone());
-            row4.createCell(4).setCellValue("鑱旂郴鐢佃瘽锛�" + customer.getCompanyPhone());
+                Row row2 = sheet.createRow(2);
+                row2.createCell(0).setCellValue("瀹㈡埛鍚嶇О锛�" + ledger.getCustomerName());
+                row2.createCell(4).setCellValue("鍗曟嵁缂栧彿锛�" + ledger.getSalesContractNo());
 
-            Row row5 = sheet.createRow(5);
-            row5.createCell(0).setCellValue("閫佽揣鍗曚綅锛氭箹鍗楅箯鍒涚數瀛愭湁闄愬叕鍙�");
-            Row row6 = sheet.createRow(6);
-            row6.createCell(0).setCellValue("鍦板潃锛氭箹鍗楃渷鑰掗槼甯傚垱鏂板垱涓氬洯A1鏍�");
-            Row row7 = sheet.createRow(7);
-            row7.createCell(0).setCellValue("璐х墿璇︾粏淇℃伅锛�");
+                Row row3 = sheet.createRow(3);
+                row3.createCell(0).setCellValue("閫佽揣鍦板潃锛�" + customer.getCompanyAddress());
+                row3.createCell(4).setCellValue("閫佽揣鏃ユ湡锛�" + formatDate(LocalDate.now()));
 
-            // 琛ㄥご
-            int tableStart = 8;
-            Row headerRow = sheet.createRow(tableStart);
-            String[] headers = {"搴忓彿", "鐗╂枡缂栧彿", "鍝佸悕/瑙勬牸", "鍗曚綅", "鏁伴噺", "璁㈠崟鍙�", "鍏卞洓鑱�"};
-            for (int i = 0; i < headers.length; i++) {
-                Cell hc = headerRow.createCell(i);
-                hc.setCellValue(headers[i]);
-                hc.setCellStyle(i == 6 ? noBorder : headerStyle);
-            }
+                Row row4 = sheet.createRow(4);
+                row4.createCell(0).setCellValue("鑱旂郴鏂瑰紡锛�" + customer.getContactPerson() + " " + customer.getContactPhone());
+                row4.createCell(4).setCellValue("鑱旂郴鐢佃瘽锛�" + customer.getCompanyPhone());
 
-            // 鍟嗗搧鏁版嵁锛堝叏閮ㄥ眳涓級
-            int currentRow = tableStart + 1;
-            String[] fourNames = {"瀛樻牴", "鍥炲崟", "瀹㈡埛", "浠撳簱"};
-            for (int i = 0; i < productList.size(); i++) {
-                SalesLedgerProduct p = productList.get(i);
-                if (p == null) continue;
+                Row row5 = sheet.createRow(5);
+                row5.createCell(0).setCellValue("閫佽揣鍗曚綅锛氭箹鍗楅箯鍒涚數瀛愭湁闄愬叕鍙�");
+                Row row6 = sheet.createRow(6);
+                row6.createCell(0).setCellValue("鍦板潃锛氭箹鍗楃渷鑰掗槼甯傚垱鏂板垱涓氬洯A1鏍�");
+                Row row7 = sheet.createRow(7);
+                row7.createCell(0).setCellValue("璐х墿璇︾粏淇℃伅锛�");
 
-                String materialCode = "";
-                if (p.getProductModelId() != null) {
-                    ProductModel m = productModelMapper.selectById(p.getProductModelId());
-                    if (m != null) {
-                        materialCode = m.getMaterialCode();
+                // 琛ㄥご
+                int tableStart = 8;
+                Row headerRow = sheet.createRow(tableStart);
+                String[] headers = {"搴忓彿", "鐗╂枡缂栧彿", "鍝佸悕/瑙勬牸", "鍗曚綅", "鏁伴噺", "璁㈠崟鍙�", "鍏卞洓鑱�"};
+                for (int i = 0; i < headers.length; i++) {
+                    Cell hc = headerRow.createCell(i);
+                    hc.setCellValue(headers[i]);
+                    hc.setCellStyle(i == 6 ? noBorder : headerStyle);
+                }
+
+                // 鏌ヨ褰撳墠鍗曟嵁鍟嗗搧
+                SalesLedgerProduct cond = new SalesLedgerProduct();
+                cond.setSalesLedgerId(ledger.getId());
+                List<SalesLedgerProduct> productList = salesLedgerProductServiceImpl.selectSalesLedgerProductList(cond);
+
+                BigDecimal totalQuantity = BigDecimal.ZERO;
+                for (SalesLedgerProduct product : productList) {
+                    totalQuantity = totalQuantity.add(product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity());
+                }
+
+                // 鍟嗗搧鏁版嵁
+                int currentRow = tableStart + 1;
+                String[] fourNames = {"瀛樻牴", "鍥炲崟", "瀹㈡埛", "浠撳簱"};
+                for (int i = 0; i < productList.size(); i++) {
+                    SalesLedgerProduct p = productList.get(i);
+                    if (p == null) continue;
+
+                    String materialCode = "";
+                    if (p.getProductModelId() != null) {
+                        ProductModel m = productModelMapper.selectById(p.getProductModelId());
+                        if (m != null) {
+                            materialCode = m.getMaterialCode();
+                        }
                     }
+
+                    Row dataRow = sheet.createRow(currentRow);
+                    dataRow.createCell(0).setCellValue(i + 1);
+                    dataRow.createCell(1).setCellValue(materialCode);
+                    dataRow.createCell(2).setCellValue(p.getProductCategory() + "/" + p.getSpecificationModel());
+                    dataRow.createCell(3).setCellValue(p.getUnit());
+                    dataRow.createCell(4).setCellValue(p.getQuantity() == null ? 0 : p.getQuantity().doubleValue());
+                    dataRow.createCell(5).setCellValue(ledger.getSalesContractNo());
+
+                    Cell fourCell = dataRow.createCell(6);
+                    if (i < fourNames.length) {
+                        fourCell.setCellValue(fourNames[i]);
+                    }
+
+                    // 鍙墠3琛屾湁铏氱嚎锛屼粨搴撳強浠ヤ笅鏃犺竟妗�
+                    if (i >= 0 && i <= 2) {
+                        fourCell.setCellStyle(fourDash);
+                    } else {
+                        fourCell.setCellStyle(noBorder);
+                    }
+
+                    for (int c = 0; c <= 5; c++) {
+                        dataRow.getCell(c).setCellStyle(dataCenterStyle);
+                    }
+                    currentRow++;
                 }
 
-                Row dataRow = sheet.createRow(currentRow);
-                dataRow.createCell(0).setCellValue(i + 1);
-                dataRow.createCell(1).setCellValue(materialCode);
-                dataRow.createCell(2).setCellValue(p.getProductCategory() + "/" + p.getSpecificationModel());
-                dataRow.createCell(3).setCellValue(p.getUnit());
-                dataRow.createCell(4).setCellValue(p.getQuantity() == null ? 0 : p.getQuantity().doubleValue());
-                dataRow.createCell(5).setCellValue(ledger.getSalesContractNo());
+                // 鍚堣琛�
+                Row totalRow = sheet.createRow(currentRow);
+                sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 3));
+                totalRow.createCell(0).setCellValue("鍚堣");
+                totalRow.createCell(4).setCellValue(totalQuantity.doubleValue());
+                fillMergeRange(sheet, currentRow, currentRow, 0, 5, dataCenterStyle);
+                totalRow.createCell(6).setCellStyle(noBorder);
 
-                Cell fourCell = dataRow.createCell(6);
-                if (i < fourNames.length) fourCell.setCellValue(fourNames[i]);
-                fourCell.setCellStyle(fourDash);
-
-                for (int c = 0; c <= 5; c++) {
-                    dataRow.getCell(c).setCellStyle(dataCenterStyle); // 鍐呭灞呬腑
-                }
+                // 澶囨敞
                 currentRow++;
+                Row remarkRow = sheet.createRow(currentRow);
+                remarkRow.createCell(0).setCellValue("澶囨敞锛氳吹鍙稿湪鏀惰揣鍚庤鍗冲埢鏍稿疄鏁伴噺鍙婂搧璐紝鑻ユ湁寮傝锛岃鍦�3鏃ュ唴鎻愬嚭锛屽惁鍒欒涓烘敹鍙椼��");
+                sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 6));
+
+                currentRow++;
+                Row sign1 = sheet.createRow(currentRow);
+                sign1.createCell(0).setCellValue("閫佽揣鍗曚綅锛堢绔狅級锛�");
+                sign1.createCell(5).setCellValue("鏀惰揣鍗曚綅锛�");
+
+                currentRow++;
+                Row sign2 = sheet.createRow(currentRow);
+                sign2.createCell(0).setCellValue("閫佽揣浜猴細");
+                sign2.createCell(5).setCellValue("鏀惰揣浜猴細");
             }
-
-            // ===================== 鏈�缁堝悎璁★細鍚堝苟 0-3鍒楋紙4鍒楋級 =====================
-            Row totalRow = sheet.createRow(currentRow);
-            sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 3)); // 璺�4鍒�
-
-            totalRow.createCell(0).setCellValue("鍚堣");
-            totalRow.createCell(4).setCellValue(totalQuantity.doubleValue());
-
-            fillMergeRange(sheet, currentRow, currentRow, 0, 5, dataCenterStyle);
-            totalRow.createCell(6).setCellStyle(noBorder);
-
-            // 澶囨敞
-            currentRow++;
-            Row remarkRow = sheet.createRow(currentRow);
-            remarkRow.createCell(0).setCellValue("澶囨敞锛氳吹鍙稿湪鏀惰揣鍚庤鍗冲埢鏍稿疄鏁伴噺鍙婂搧璐紝鑻ユ湁寮傝锛岃鍦�3鏃ュ唴鎻愬嚭锛屽惁鍒欒涓烘敹鍙椼��");
-            sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 6));
-
-            currentRow++;
-            Row sign1 = sheet.createRow(currentRow);
-            sign1.createCell(0).setCellValue("閫佽揣鍗曚綅锛堢绔狅級锛�");
-            sign1.createCell(5).setCellValue("鏀惰揣鍗曚綅锛�");
-
-            currentRow++;
-            Row sign2 = sheet.createRow(currentRow);
-            sign2.createCell(0).setCellValue("閫佽揣浜猴細");
-            sign2.createCell(5).setCellValue("鏀惰揣浜猴細");
 
             workbook.write(response.getOutputStream());
             workbook.close();
 
         } catch (Exception e) {
-            log.error("瀵煎嚭寮傚父", e);
-            throw new BaseException("瀵煎嚭澶辫触");
+            log.error("鎵归噺瀵煎嚭閫佽揣鍗曞紓甯�", e);
+            throw new BaseException("鎵归噺瀵煎嚭澶辫触");
         }
     }
 

--
Gitblit v1.9.3