From b7b7c0a641c7bf53d3d31449dc8c31efba58ea97 Mon Sep 17 00:00:00 2001
From: huminmin <mac@MacBook-Pro.local>
Date: 星期三, 22 四月 2026 17:58:34 +0800
Subject: [PATCH] 导出销售台账送货单
---
src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java | 174 ++++++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 145 insertions(+), 29 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 fe3e20b..c09ccd0 100644
--- a/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/sales/service/impl/SalesLedgerServiceImpl.java
@@ -1,5 +1,8 @@
package com.ruoyi.sales.service.impl;
+import com.alibaba.excel.EasyExcel;
+import com.alibaba.excel.ExcelWriter;
+import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -8,17 +11,23 @@
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.deepoove.poi.XWPFTemplate;
+import com.deepoove.poi.config.Configure;
+import com.ruoyi.account.pojo.AccountIncome;
import com.ruoyi.account.service.AccountIncomeService;
import com.ruoyi.basic.mapper.CustomerMapper;
import com.ruoyi.basic.mapper.ProductMapper;
import com.ruoyi.basic.mapper.ProductModelMapper;
import com.ruoyi.basic.pojo.Customer;
+import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.common.enums.FileNameType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
+import com.ruoyi.common.utils.HackLoopTableRenderPolicy;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.device.execl.DeviceMaintenanceExeclDto;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.other.mapper.TempFileMapper;
@@ -33,8 +42,8 @@
import com.ruoyi.sales.dto.*;
import com.ruoyi.sales.mapper.*;
import com.ruoyi.sales.pojo.*;
+import com.ruoyi.sales.service.ISalesLedgerProductService;
import com.ruoyi.sales.service.ISalesLedgerService;
-import com.ruoyi.stock.dto.StockInventoryDto;
import com.ruoyi.stock.mapper.StockInventoryMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@@ -48,11 +57,13 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
-import java.io.IOException;
-import java.io.InputStream;
+import javax.servlet.http.HttpServletResponse;
+import java.io.*;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -60,11 +71,14 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
+import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
/**
* 閿�鍞彴璐ervice涓氬姟灞傚鐞�
@@ -130,6 +144,8 @@
private final StockInventoryMapper stockInventoryMapper;
+ private ISalesLedgerProductService salesLedgerProductService;
+
@Autowired
private SysDeptMapper sysDeptMapper;
@@ -186,6 +202,9 @@
if (shippingInfo != null) {
product.setShippingStatus(shippingInfo.getStatus());
}
+
+ ProductModel productModel = productModelMapper.selectById(product.getProductModelId());
+ product.setMaterialCode(productModel.getMaterialCode());
}
// 3.鏌ヨ涓婁紶鏂囦欢
@@ -203,6 +222,99 @@
resultDto.setSalesLedgerFiles(salesLedgerFiles);
}
return resultDto;
+ }
+
+ @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("鏈壘鍒板搴旈攢鍞彴璐︽暟鎹�");
+ }
+
+ 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++) {
+ productList.get(i).setSerialNumber(i + 1);
+ }
+
+ // 鍏宠仈鏌ヨ瀹㈡埛淇℃伅
+ Customer customer = customerMapper.selectById(ledger.getCustomerId());
+ // 缁勮鏁版嵁
+ 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("products", productList);
+
+ // 姣忔寰幆鍏ㄦ柊璇诲彇妯℃澘锛屽交搴曡В鍐虫祦闂
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ try (InputStream templateIs = this.getClass().getResourceAsStream("/static/shipping-note.xlsx")) {
+ if (templateIs == null) throw new BaseException("妯℃澘鏂囦欢涓嶅瓨鍦�");
+
+ // EasyExcel濉厖
+ ExcelWriter writer = EasyExcel.write(bos).withTemplate(templateIs).build();
+ WriteSheet sheet = EasyExcel.writerSheet().build();
+ writer.fill(dataMap, sheet);
+ writer.finish();
+ }
+
+ // 鎵撳寘杩沍IP
+ 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());
+ }
+ }
+
+ public static String formatDate(LocalDate date) {
+ if (date == null) {
+ return null;
+ }
+ return date.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
@Override
@@ -602,7 +714,7 @@
// 4. 澶勭悊瀛愯〃鏁版嵁
List<SalesLedgerProduct> productList = salesLedgerDto.getProductData();
if (productList != null && !productList.isEmpty()) {
- handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType());
+ handleSalesLedgerProducts(salesLedger.getId(), productList, salesLedgerDto.getType(), salesLedgerDto.isProduce());
updateMainContractAmount(
salesLedger.getId(),
productList,
@@ -700,7 +812,7 @@
}
- private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type) {
+ private void handleSalesLedgerProducts(Long salesLedgerId, List<SalesLedgerProduct> products, Integer type, boolean isProduce) {
// 鎸塈D鍒嗙粍锛屽尯鍒嗘柊澧炲拰鏇存柊鐨勮褰�
Map<Boolean, List<SalesLedgerProduct>> partitionedProducts = products.stream()
.peek(p -> p.setSalesLedgerId(salesLedgerId))
@@ -709,26 +821,26 @@
List<SalesLedgerProduct> updateList = partitionedProducts.get(true);
List<SalesLedgerProduct> insertList = partitionedProducts.get(false);
- List<StockInventoryDto> stockInventoryDtos = stockInventoryMapper.selectProductList();
+// List<StockInventoryDto> stockInventoryDtos = stockInventoryMapper.selectProductList();
// 鎵ц鏇存柊鎿嶄綔
if (!updateList.isEmpty()) {
for (SalesLedgerProduct product : updateList) {
- // 鏌ヨ搴撳瓨鏁伴噺
- SalesLedgerProduct oldProduct = salesLedgerProductMapper.selectById(product.getId());
- BigDecimal quantityChange = product.getQuantity().subtract(oldProduct.getQuantity());
-
- // 濡傛灉鏁伴噺澧炲姞浜嗭紝妫�鏌ュ簱瀛�
- if (quantityChange.compareTo(BigDecimal.ZERO) > 0) {
- for (StockInventoryDto stockInventoryDto : stockInventoryDtos) {
- if (stockInventoryDto.getProductId().equals(product.getId())) {
- if (quantityChange.compareTo(stockInventoryDto.getQualitity()) > 0) {
- throw new RuntimeException("搴撳瓨涓嶈冻");
- }
- break;
- }
- }
- }
+// // 鏌ヨ搴撳瓨鏁伴噺
+// SalesLedgerProduct oldProduct = salesLedgerProductMapper.selectById(product.getId());
+// BigDecimal quantityChange = product.getQuantity().subtract(oldProduct.getQuantity());
+//
+// // 濡傛灉鏁伴噺澧炲姞浜嗭紝妫�鏌ュ簱瀛�
+// if (quantityChange.compareTo(BigDecimal.ZERO) > 0) {
+// for (StockInventoryDto stockInventoryDto : stockInventoryDtos) {
+// if (stockInventoryDto.getProductId().equals(product.getId())) {
+// if (quantityChange.compareTo(stockInventoryDto.getQualitity()) > 0) {
+// throw new RuntimeException("搴撳瓨涓嶈冻");
+// }
+// break;
+// }
+// }
+// }
product.setType(type);
salesLedgerProductMapper.updateById(product);
}
@@ -736,18 +848,22 @@
// 鎵ц鎻掑叆鎿嶄綔
if (!insertList.isEmpty()) {
for (SalesLedgerProduct salesLedgerProduct : insertList) {
- for (StockInventoryDto stockInventoryDto : stockInventoryDtos) {
- if (stockInventoryDto.getProductId().equals(salesLedgerProduct.getId())) {
- if (salesLedgerProduct.getQuantity().compareTo(stockInventoryDto.getQualitity()) > 0) {
- throw new RuntimeException("搴撳瓨涓嶈冻");
- }
- }
- }
+// for (StockInventoryDto stockInventoryDto : stockInventoryDtos) {
+// if (stockInventoryDto.getProductId().equals(salesLedgerProduct.getId())) {
+// if (salesLedgerProduct.getQuantity().compareTo(stockInventoryDto.getQualitity()) > 0) {
+// throw new RuntimeException("搴撳瓨涓嶈冻");
+// }
+// }
+// }
salesLedgerProduct.setType(type);
salesLedgerProduct.setNoInvoiceNum(salesLedgerProduct.getQuantity());
salesLedgerProduct.setNoInvoiceAmount(salesLedgerProduct.getTaxInclusiveTotalPrice());
salesLedgerProduct.setPendingInvoiceTotal(salesLedgerProduct.getTaxInclusiveTotalPrice());
salesLedgerProductMapper.insert(salesLedgerProduct);
+ // 娣诲姞鐢熶骇鏁版嵁
+ if (isProduce) {
+ salesLedgerProductServiceImpl.addProductionData(salesLedgerProduct);
+ }
}
}
}
@@ -861,4 +977,4 @@
throw new RuntimeException("鍔ㄦ�佹洿鏂颁富琛ㄩ噾棰濆け璐�", e);
}
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3