huminmin
2026-04-23 7403a0bb5b6c69a830c326aa7bc4fa720d23477a
导出销售台账送货单
已修改3个文件
112 ■■■■■ 文件已修改
src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/ISalesLedgerService.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/sales/controller/SalesLedgerController.java
@@ -193,12 +193,11 @@
     */
    @Log(title = "导出销售台账发货单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportShippingNote")
    public void exportShippingNote(HttpServletResponse response, @RequestParam("ids") String idsStr) {
    public void exportShippingNote(HttpServletResponse response, @RequestParam("ids") String idsStr) throws Exception {
        // 将逗号分隔的字符串转换为List<Long>
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong)
                .collect(Collectors.toList());
        System.out.println("导出销售台账发货单,ID列表: " + ids);
        salesLedgerService.exportShippingNote(response, ids);
    }
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);
    void exportShippingNote(HttpServletResponse response, List<Long> ids) throws Exception;
}
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -244,6 +244,105 @@
            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())));
            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);
            // 自动序号+物料编码赋值
            for (int i = 0; i < productList.size(); i++) {
                SalesLedgerProduct product = productList.get(i);
                if (product != null) {
                    product.setSerialNumber(i + 1);
                    product.setOrderNo(ledger.getSalesContractNo());
                    Long productModelId = product.getProductModelId();
                    if (productModelId != null) {
                        ProductModel productModel = productModelMapper.selectById(productModelId);
                        if (productModel != null) {
                            product.setMaterialCode(productModel.getMaterialCode());
                        }
                    }
                }
            }
            // 关联客户信息
            Customer customer = customerMapper.selectById(ledger.getCustomerId());
            // 计算总合计
            BigDecimal totalQuantity = BigDecimal.ZERO;
            for (SalesLedgerProduct product : productList) {
                totalQuantity = totalQuantity.add(product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity());
            }
            // 组装顶部静态数据
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("customerName", ledger.getCustomerName());
            dataMap.put("companyAddress", customer.getCompanyAddress());
            dataMap.put("contactPerson", customer.getContactPerson());
            dataMap.put("contactPhone", customer.getContactPhone());
            dataMap.put("companyPhone", customer.getCompanyPhone());
            dataMap.put("salesContractNo", ledger.getSalesContractNo());
            dataMap.put("deliveryDate", formatDate(LocalDate.now()));
            dataMap.put("totalQuantity", totalQuantity);
            // 使用模板生成Excel
            InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx");
            if (templateIs == null) {
                throw new BaseException("模板文件不存在");
            }
            ExcelWriter writer = EasyExcel.write(response.getOutputStream()).withTemplate(templateIs).build();
            WriteSheet sheet = EasyExcel.writerSheet().build();
            // 先填充数据(包括合计)
            writer.fill(dataMap, sheet);
            // 再填充产品列表
            if (!productList.isEmpty()) {
                FillConfig fillConfig = FillConfig.builder()
                        .forceNewRow(Boolean.TRUE)
                        .build();
                writer.fill(productList, fillConfig, sheet);
            }
            writer.finish();
            templateIs.close();
        } catch (BaseException e) {
            log.error("导出失败:{}", e.getMessage(), e);
            throw e;
        } catch (Exception e) {
            log.error("导出异常", e);
            throw new BaseException("导出失败:" + e.getMessage());
        }
    }
    /**
     * 批量导出送货单为压缩包
     */
    private void exportBatchShippingNote(HttpServletResponse response, List<SalesLedger> salesLedgerList) {
        // 设置响应头
        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment;filename=送货单.zip");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
@@ -292,6 +391,7 @@
                dataMap.put("companyPhone", customer.getCompanyPhone());
                dataMap.put("salesContractNo", ledger.getSalesContractNo());
                dataMap.put("deliveryDate", formatDate(LocalDate.now()));
                dataMap.put("totalQuantity", totalQuantity);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx");
@@ -300,16 +400,13 @@
                WriteSheet sheet = EasyExcel.writerSheet().build();
                // 先填充数据(包括合计)
                Map<String, Object> totalMap = new HashMap<>();
                totalMap.put("totalQuantity", totalQuantity);
                dataMap.putAll(totalMap);
                writer.fill(dataMap, sheet);
                
                // 再填充产品列表
                if (!productList.isEmpty()) {
                    FillConfig fillConfig = FillConfig.builder()
                        .forceNewRow(Boolean.TRUE)
                        .build();
                            .forceNewRow(Boolean.TRUE)
                            .build();
                    writer.fill(productList, fillConfig, sheet);
                }