huminmin
2026-04-23 cf456d684cadd84a97e8cb417e0613f81a3d47cd
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -232,54 +232,17 @@
    @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();
            for (SalesLedger ledger : ledgerList) {
                // 每个单据一个 sheet
            Sheet sheet = workbook.createSheet(ledger.getSalesContractNo());
            sheet.setColumnWidth(0, 2500);
@@ -294,7 +257,7 @@
            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 dataCenterStyle = createBorderStyle(workbook, "宋体", (short) 11, false, true);
            CellStyle noBorder = createNoBorderStyle(workbook);
            CellStyle fourDash = createDashBottomStyle(workbook);
@@ -310,6 +273,8 @@
            fillMergeRange(sheet, 1, 1, 0, 6, billTitle);
            // 客户信息
                Customer customer = customerMapper.selectById(ledger.getCustomerId());
            Row row2 = sheet.createRow(2);
            row2.createCell(0).setCellValue("客户名称:" + ledger.getCustomerName());
            row2.createCell(4).setCellValue("单据编号:" + ledger.getSalesContractNo());
@@ -339,7 +304,17 @@
                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++) {
@@ -363,22 +338,28 @@
                dataRow.createCell(5).setCellValue(ledger.getSalesContractNo());
                Cell fourCell = dataRow.createCell(6);
                if (i < fourNames.length) fourCell.setCellValue(fourNames[i]);
                    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); // 内容居中
                        dataRow.getCell(c).setCellStyle(dataCenterStyle);
                }
                currentRow++;
            }
            // ===================== 最终合计:合并 0-3列(4列) =====================
                // 合计行
            Row totalRow = sheet.createRow(currentRow);
            sheet.addMergedRegion(new CellRangeAddress(currentRow, currentRow, 0, 3)); // 跨4列
                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);
@@ -397,13 +378,14 @@
            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("批量导出失败");
        }
    }