销售订单增加导出PI功能

This commit is contained in:
Mrking 2024-10-29 23:13:12 +08:00
parent c0cc36772f
commit eb1cd78f07
7 changed files with 160 additions and 56 deletions

View File

@ -173,16 +173,6 @@ public class AppSaleOrderController{
return success(true);
}
/**
* 生成PDF文档并下载
* @param response HTTP响应
* @throws Exception 异常
*/
@GetMapping("/download")
@PermitAll
public void downloadPdf(HttpServletResponse response) throws Exception{
saleOrderService.generatePdf(response);
}
@PutMapping("/update-remark")
@Operation(summary = "订单备注")
public CommonResult<Boolean> updateOrderRemark(@RequestBody SaleOrderRemarkReqVO reqVO) {

View File

@ -164,16 +164,9 @@ public class SaleOrderController {
return success(true);
}
/**
* 生成PDF文档并下载
* @param response HTTP响应
* @throws Exception 异常
*/
@GetMapping("/download")
@PermitAll
public void downloadPdf(HttpServletResponse response) throws Exception{
saleOrderService.generatePdf(response);
}
@PutMapping("/update-remark")
@Operation(summary = "订单备注")
@PreAuthorize("@ss.hasPermission('oms:sale-order:update')")
@ -191,4 +184,23 @@ public class SaleOrderController {
return success(true);
}
/**
* 生成PDF文档并下载
* @param response HTTP响应
* @throws Exception 异常
*/
@GetMapping("/download")
@PermitAll
public void downloadPdf(HttpServletResponse response) throws Exception{
saleOrderService.generatePdf(response,Arrays.asList(1851151636631752704L));
}
@GetMapping("/exportSalePIPDF")
@Operation(summary = "导出PIPDF制单")
@PermitAll
public void exportSalePIPDF(@RequestParam("ids") List<Long> ids,HttpServletResponse response) throws IOException {
saleOrderService.generatePdf(response,ids);
}
}

View File

@ -78,7 +78,7 @@ public interface SaleOrderService {
void generateProduceOrder(List<Long> ids);
void generatePdf(HttpServletResponse response) throws IOException;
void generatePdf(HttpServletResponse response,List<Long> ids) throws IOException;
default DataComparisonRespVO<TradeOrderSummaryRespVO> getOrderComparison(){
return getOrderComparison(null);

View File

@ -59,6 +59,7 @@ import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.ZipUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
@ -71,13 +72,12 @@ import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
@ -417,6 +417,16 @@ public class SaleOrderServiceImpl implements SaleOrderService {
private String generateHtmlContent(Long saleContractId) {
SaleContractDO saleContract = saleContractService.getSaleContract(saleContractId);
List<SaleContractEntryDO> saleContractEntrys = saleContractService.getSaleContractEntryListByParentId(saleContractId);
Context context = generateHtmlContents(saleContract, saleContractEntrys);
return templateEngine.process("pdf_template", context);
}
private String generateHtmlContent(SaleContractDO saleContract,List<SaleContractEntryDO> saleContractEntrys) {
Context context = generateHtmlContents(saleContract,saleContractEntrys);
return templateEngine.process("pdf_template", context);
}
private Context generateHtmlContents(SaleContractDO saleContract,List<SaleContractEntryDO> saleContractEntrys){
CustomerDO customer = customerService.getCustomer(saleContract.getCustomerId());
Context context = new Context();
context.setVariable("title", "销售合约");
@ -458,48 +468,53 @@ public class SaleOrderServiceImpl implements SaleOrderService {
context.setVariable("explain"+j+"2", details);
context.setVariable("explain"+j+"3", spec);
}
return templateEngine.process("pdf_template", context);
return context;
}
@Override
public void generatePdf(HttpServletResponse response) throws IOException {
public void generatePdf(HttpServletResponse response,List<Long> ids) throws IOException {
List<File> files = new ArrayList<>();
List<SaleOrderDO> saleOrders = saleOrderMapper.selectList(SaleOrderDO::getId, ids);
for (SaleOrderDO saleOrder : saleOrders) {
List<SaleOrderEntryDO> entrys = getSaleOrderEntryListByParentId(saleOrder.getId());
SaleContractSaveReqVO saleContractVO = toSaleContractVO(saleOrder, entrys);
SaleContractDO saleContract = BeanUtils.toBean(saleContractVO, SaleContractDO.class);
List<SaleContractEntryDO> saleContractEntrys = BeanUtils.toBean(saleContractVO.getSaleContractEntrys(), SaleContractEntryDO.class);
String htmlContent = generateHtmlContent(saleContract,saleContractEntrys);
String fileName = StrUtil.format(pdfPath+"/销售合约_{}", saleOrder.getBillno());
String htmlContent = generateHtmlContent(0L);
String templatePath = fileName + ".html";
String pdfPath = fileName + ".pdf";
FileUtil.writeString(htmlContent,templatePath, "UTF-8");
WKHtmlToPdfUtil.convert(templatePath, pdfPath);
File file = FileUtil.file(pdfPath);
files.add(file);
}
String zipFileName = StrUtil.format(pdfPath + "/销售合约_{}", new Date().getTime());
// 压缩到的位置
File zipFile = new File(zipFileName+".zip");
ZipUtil.zip(zipFile,false,files.toArray(new File[files.size()]));
//downloadZip(zipFile,response);
// 设置响应类型
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=generated.pdf");
String fileName = StrUtil.format("C:\\Users\\Admin\\Desktop\\1111111\\test\\test_{}",
new Date().getTime());
String templatePath = fileName + ".html";
String pdfPath = fileName + ".pdf";
FileUtil.writeString(htmlContent,templatePath, "UTF-8");
WKHtmlToPdfUtil.convert(templatePath, pdfPath);
File file = FileUtil.file(pdfPath);
//response.setContentType("application/pdf");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(zipFile.getName(),"UTF-8"));
// 输出PDF到响应输出流
// 将文件内容写入响应流
try (InputStream inputStream = new FileInputStream(file)) {
try (InputStream inputStream = new FileInputStream(zipFile)) {
IoUtil.copy(inputStream, response.getOutputStream());
} catch (IOException e) {
// 异常处理
log.info("导出销售合约写入流失败,{}", e.getMessage());
}
// 导出完删除
//FileUtil.del(file);
//FileUtil.del(templatePath);
/*
// 设置输出PDF文件
OutputStream os = new FileOutputStream("index.pdf");
renderer.createPDF(os);
os.flush();
os.close();*/
/* FileUtil.del(file);
FileUtil.del(templatePath);*/
}
@Override
@ -788,8 +803,13 @@ public class SaleOrderServiceImpl implements SaleOrderService {
private Long generateSaleContract(SaleOrderDO saleOrder, List<SaleOrderEntryDO> entrys){
CustomerDO customer = customerService.getCustomer(saleOrder.getCustomerId());
SaleContractSaveReqVO saveReqVO = toSaleContractVO(saleOrder, entrys);
return saleContractService.createSaleContract(saveReqVO);
}
private SaleContractSaveReqVO toSaleContractVO(SaleOrderDO saleOrder, List<SaleOrderEntryDO> entrys){
CustomerDO customer = customerService.getCustomer(saleOrder.getCustomerId());
SaleContractSaveReqVO saveReqVO = new SaleContractSaveReqVO();
saveReqVO.setBillno(getNewOrderCode2());
saveReqVO.setCustomerId(saleOrder.getCustomerId());
@ -820,12 +840,49 @@ public class SaleOrderServiceImpl implements SaleOrderService {
list.add(saleContractEntryDO);
}
saveReqVO.setSaleContractEntrys(list);
return saleContractService.createSaleContract(saveReqVO);
return saveReqVO;
}
@Override
public List<SaleOrderSkuDO> getSaleOrderSkuEntryListByEntryId(Long entryId) {
return skuOrderSkuMapper.selectListByEntryId(entryId);
}
/**
* 下载ZIP压缩包(会对下载后的压缩包进行删除)
*
* @param file zip压缩包文件
* @param response 响应
*/
public static void downloadZip(File file, HttpServletResponse response) {
OutputStream toClient = null;
try {
// 以流的形式下载文件
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
toClient = new BufferedOutputStream(response.getOutputStream());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(),"UTF-8"));
toClient.write(buffer);
toClient.flush();
} catch (Exception e) {
log.error("下载zip压缩包过程发生异常:", e);
} finally {
if (toClient != null) {
try {
toClient.close();
} catch (IOException e) {
log.error("zip包下载关流失败:", e);
}
}
//删除改临时zip包(此zip包任何时候都不需要保留,因为源文件随时可以再次进行压缩生成zip包)
file.delete();
}
}
}

View File

@ -103,6 +103,13 @@ export const SaleOrderApi = {
// 更新分录数据
updateOrderEntrys: async (data: SaleOrderVO) => {
return await request.put({ url: `/oms/sale-order/update-entrys`, data })
}
},
// 导出PIPDF
exportSalePIPdf: async (ids: number[]) => {
const params = {
ids: ids.join(',')
}
return await request.download({ url: `/oms/sale-order/exportSalePIPDF`, params})
}
}

View File

@ -32,6 +32,10 @@ const download = {
// 下载 Markdown 方法
markdown: (data: Blob, fileName: string) => {
download0(data, fileName, 'text/markdown')
},
// 下载 pdf 方法
pdf: (data: Blob, fileName: string) => {
download0(data, fileName, 'application/pdf')
}
}

View File

@ -149,10 +149,19 @@
type="primary"
plain
@click="generateProduceOrder()"
:disabled="selectionList.length === 0 || queryParams.tabType === 0"
:disabled="selectionList.length === 0"
>
生成制单
</el-button>
<el-button
type="primary"
plain
:loading="exportLoading2"
@click="exportSalePIPdf()"
:disabled="selectionList.length === 0"
>
导出PI
</el-button>
<el-button
type="success"
plain
@ -343,6 +352,7 @@ const queryParams = reactive({
})
const queryFormRef = ref() //
const exportLoading = ref(false) //
const exportLoading2 = ref(false) //
//
@ -492,6 +502,30 @@ const generateProduceOrder = async () => {
}
/** 导出PIpdf文件 */
const exportSalePIPdf = async () => {
try {
const ids = selectionList.value.map((item) => item.id)
const billStatus = selectionList.value.map((item) => item.billStatus)
for(let vals of billStatus) {
if(vals!='C' && vals!='E'){
message.error("请选择单据状态待排产/已排产的数据行");
return;
}
}
//
await message.exportConfirm("是否确认导出PI")
//
exportLoading2.value = true
//
const data = await SaleOrderApi.exportSalePIPdf(ids)
download.zip(data, '销售订单PI.zip')
selectionList.value = selectionList.value.filter((item) => !ids.includes(item.id))
} catch {
} finally {
exportLoading2.value = false
}
}
/** 导出按钮操作 */
const handleExport = async () => {