From 833a285ef00a84fc1a88464eb3d7d1934cf71ff6 Mon Sep 17 00:00:00 2001
From: yuan <123@>
Date: 星期四, 23 四月 2026 14:45:27 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev_衡阳_鹏创电子' into dev_衡阳_鹏创电子
---
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java | 188 ++++++++++++++--------------------------------
1 files changed, 59 insertions(+), 129 deletions(-)
diff --git a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
index e2a5abd..6ebc49c 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -53,6 +53,7 @@
import org.apache.commons.io.FilenameUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@@ -239,11 +240,10 @@
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
- Workbook workbook = new XSSFWorkbook();
+ XSSFWorkbook workbook = new XSSFWorkbook();
for (SalesLedger ledger : ledgerList) {
- // 姣忎釜鍗曟嵁涓�涓� sheet
- Sheet sheet = workbook.createSheet(ledger.getSalesContractNo());
+ XSSFSheet sheet = workbook.createSheet(ledger.getSalesContractNo());
sheet.setColumnWidth(0, 2500);
sheet.setColumnWidth(1, 4000);
@@ -251,15 +251,22 @@
sheet.setColumnWidth(3, 2500);
sheet.setColumnWidth(4, 3000);
sheet.setColumnWidth(5, 4500);
- sheet.setColumnWidth(6, 3500);
+ sheet.setColumnWidth(6, 2000);
- // 鏍峰紡
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);
+
+ // 鏍峰紡1锛氬甫铏氱嚎 + 灞呬腑
+ CellStyle dashStyle = createDashBottomStyle(workbook);
+ dashStyle.setAlignment(HorizontalAlignment.CENTER);
+ dashStyle.setVerticalAlignment(VerticalAlignment.CENTER);
+
+ // 鏍峰紡2锛氭棤杈规 + 灞呬腑锛堢粰浠撳簱鐢級
+ CellStyle centerNoBorder = createNoBorderStyle(workbook);
+ centerNoBorder.setAlignment(HorizontalAlignment.CENTER);
+ centerNoBorder.setVerticalAlignment(VerticalAlignment.CENTER);
// 鏍囬
Row row0 = sheet.createRow(0);
@@ -272,7 +279,6 @@
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 6));
fillMergeRange(sheet, 1, 1, 0, 6, billTitle);
- // 瀹㈡埛淇℃伅
Customer customer = customerMapper.selectById(ledger.getCustomerId());
Row row2 = sheet.createRow(2);
@@ -294,42 +300,43 @@
Row row7 = sheet.createRow(7);
row7.createCell(0).setCellValue("璐х墿璇︾粏淇℃伅锛�");
- // 琛ㄥご
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);
+ if (i == 6) {
+ hc.setCellStyle(dashStyle);
+ } else {
+ hc.setCellStyle(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());
+ for (SalesLedgerProduct p : productList) {
+ if (p.getQuantity() != null) totalQuantity = totalQuantity.add(p.getQuantity());
}
- // 鍟嗗搧鏁版嵁
int currentRow = tableStart + 1;
String[] fourNames = {"瀛樻牴", "鍥炲崟", "瀹㈡埛", "浠撳簱"};
+
+ // 1. 鎵�鏈変骇鍝�
for (int i = 0; i < productList.size(); i++) {
SalesLedgerProduct p = productList.get(i);
- if (p == null) continue;
+ Row dataRow = sheet.createRow(currentRow++);
String materialCode = "";
if (p.getProductModelId() != null) {
ProductModel m = productModelMapper.selectById(p.getProductModelId());
- if (m != null) {
- materialCode = m.getMaterialCode();
- }
+ 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());
@@ -337,31 +344,50 @@
dataRow.createCell(4).setCellValue(p.getQuantity() == null ? 0 : p.getQuantity().doubleValue());
dataRow.createCell(5).setCellValue(ledger.getSalesContractNo());
+ for (int c = 0; c <= 5; c++) dataRow.getCell(c).setCellStyle(dataCenterStyle);
+
Cell fourCell = dataRow.createCell(6);
if (i < fourNames.length) {
fourCell.setCellValue(fourNames[i]);
- }
-
- // 鍙墠3琛屾湁铏氱嚎锛屼粨搴撳強浠ヤ笅鏃犺竟妗�
- if (i >= 0 && i <= 2) {
- fourCell.setCellStyle(fourDash);
+ if (i <= 2) {
+ fourCell.setCellStyle(dashStyle);
+ } else {
+ fourCell.setCellStyle(centerNoBorder);
+ }
} else {
- fourCell.setCellStyle(noBorder);
+ fourCell.setCellStyle(centerNoBorder);
}
-
- for (int c = 0; c <= 5; c++) {
- dataRow.getCell(c).setCellStyle(dataCenterStyle);
- }
- currentRow++;
}
- // 鍚堣琛�
+ // 2. 涓嶈冻6琛岃ˉ绌�
+ int minRow = 6;
+ int needEmpty = Math.max(0, minRow - productList.size());
+ for (int i = 0; i < needEmpty; i++) {
+ int seq = productList.size() + i;
+ Row dataRow = sheet.createRow(currentRow++);
+
+ for (int c = 0; c <= 5; c++) dataRow.createCell(c).setCellStyle(dataCenterStyle);
+
+ Cell fourCell = dataRow.createCell(6);
+ if (seq < fourNames.length) {
+ fourCell.setCellValue(fourNames[seq]);
+ if (seq <= 2) {
+ fourCell.setCellStyle(dashStyle);
+ } else {
+ fourCell.setCellStyle(centerNoBorder);
+ }
+ } else {
+ fourCell.setCellStyle(centerNoBorder);
+ }
+ }
+
+ // 鍚堣
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);
+ totalRow.createCell(6).setCellStyle(centerNoBorder);
// 澶囨敞
currentRow++;
@@ -385,7 +411,7 @@
} catch (Exception e) {
log.error("鎵归噺瀵煎嚭閫佽揣鍗曞紓甯�", e);
- throw new BaseException("鎵归噺瀵煎嚭澶辫触");
+ throw new RuntimeException("瀵煎嚭澶辫触");
}
}
@@ -430,102 +456,6 @@
Cell cell = row.getCell(c) == null ? row.createCell(c) : row.getCell(c);
cell.setCellStyle(style);
}
- }
- }
- /**
- * 鎵归噺瀵煎嚭閫佽揣鍗曚负鍘嬬缉鍖�
- */
- 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");
-
- try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
-
- for (SalesLedger ledger : salesLedgerList) {
- log.info("澶勭悊鍚堝悓鍙�: {}", ledger.getSalesContractNo());
-
- // 鏌ヨ鏄庣粏
- 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);
-
- 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();
-
- // 鍏堝~鍏呮暟鎹紙鍖呮嫭鍚堣锛�
- writer.fill(dataMap, sheet);
-
- // 鍐嶅~鍏呬骇鍝佸垪琛�
- if (!productList.isEmpty()) {
- 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())));
- ZipEntry entry = new ZipEntry(excelName);
- zos.putNextEntry(entry);
- zos.write(bos.toByteArray());
- zos.closeEntry();
- bos.close();
- }
-
- zos.finish();
- log.info("鎵归噺瀵煎嚭瀹屾垚锛屽叡{}浠介�佽揣鍗�", salesLedgerList.size());
-
- } catch (BaseException e) {
- log.error("瀵煎嚭澶辫触:{}", e.getMessage(), e);
- throw e;
- } catch (Exception e) {
- log.error("瀵煎嚭寮傚父", e);
- throw new BaseException("瀵煎嚭澶辫触锛�" + e.getMessage());
}
}
--
Gitblit v1.9.3