| | |
| | | import com.alibaba.excel.EasyExcel; |
| | | import com.alibaba.excel.ExcelWriter; |
| | | import com.alibaba.excel.write.metadata.WriteSheet; |
| | | import com.alibaba.excel.write.metadata.fill.FillConfig; |
| | | import com.alibaba.excel.write.metadata.fill.FillWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | |
| | | 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); |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | // 关联查询客户信息 |
| | | // 关联客户信息 |
| | | 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("companyPhone", customer.getCompanyPhone()); |
| | | dataMap.put("salesContractNo", ledger.getSalesContractNo()); |
| | | dataMap.put("deliveryDate", formatDate(LocalDate.now())); |
| | | BigDecimal totalQuantity = new BigDecimal(0); |
| | | for (SalesLedgerProduct product : productList) { |
| | | totalQuantity = totalQuantity.add(product.getQuantity() == null ? BigDecimal.ZERO : product.getQuantity()); |
| | | } |
| | | dataMap.put("totalQuantity", totalQuantity); |
| | | |
| | | // 生成Excel |
| | | ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| | | InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx"); |
| | | |
| | | ExcelWriter writer = EasyExcel.write(bos).withTemplate(templateIs).build(); |
| | | 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()) { |
| | | writer.fill(productList, sheet); |
| | | FillConfig fillConfig = FillConfig.builder() |
| | | .forceNewRow(Boolean.TRUE) |
| | | .build(); |
| | | writer.fill(productList, fillConfig, sheet); |
| | | } |
| | | |
| | | writer.finish(); |
| | | templateIs.close(); |
| | | |
| | | // 写入ZIP |
| | | String excelName = String.format("送货单_%s.xlsx", Optional.ofNullable(ledger.getSalesContractNo()).orElse(String.valueOf(ledger.getId()))); |
| | | // 打包ZIP |
| | | String excelName = String.format("送货单_%s.xlsx", |
| | | Optional.ofNullable(ledger.getSalesContractNo()).orElse(String.valueOf(ledger.getId()))); |
| | | ZipEntry entry = new ZipEntry(excelName); |
| | | zos.putNextEntry(entry); |
| | | zos.write(bos.toByteArray()); |