| | |
| | | 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"); |
| | |
| | | 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"); |
| | |
| | | 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); |
| | | } |
| | | |