huminmin
2026-04-23 cf456d684cadd84a97e8cb417e0613f81a3d47cd
导出销售台账送货单
已修改2个文件
272 ■■■■ 文件已修改
src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java 270 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java
@@ -44,5 +44,5 @@
    List<LossProductModelDto> getSalesLedgerWithProductsLoss(Long salesLedgerId);
    void exportShippingNote(HttpServletResponse response, List<Long> ids) throws Exception;
    void exportShippingNote(HttpServletResponse response, List<Long> ids);
}
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("批量导出失败");
        }
    }