修改物资采购联系单PDF

This commit is contained in:
lcj
2025-08-21 16:17:47 +08:00
parent 2b99eb8d76
commit e9b7d03fc1
5 changed files with 75 additions and 28 deletions

View File

@ -1,12 +1,8 @@
package org.dromara.bigscreen.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotNull;
import org.dromara.common.core.domain.R;
import org.dromara.tender.domain.vo.BusBiddingPlanAnnexVo;
import jakarta.annotation.Resource;
import org.dromara.bigscreen.service.ProjectBigScreenService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -19,6 +15,9 @@ import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/project/big/screen")
public class ProjectBigScreenController {
@Resource
private ProjectBigScreenService projectBigScreenService;
// @GetMapping("/{projectId}")
// public R<BusBiddingPlanAnnexVo> getInfo(@NotNull(message = "主键不能为空")

View File

@ -57,6 +57,7 @@ public class BusPurchaseDocController extends BaseController {
private final IBusBillofquantitiesVersionsService busBillofquantitiesVersionsService;
private final IBusBillofquantitiesService busBillofquantitiesService;
/**
* 查询物资-采购联系单列表
*/
@ -101,12 +102,12 @@ public class BusPurchaseDocController extends BaseController {
}
/**
* 获取物资-采购联系单详细信息图片
* 获取物资-采购联系单详细PDF
*
* @param id 主键
*/
@SaCheckPermission("cailiaoshebei:purchaseDoc:querPic")
@GetMapping("/pic/{id}")
@SaCheckPermission("cailiaoshebei:purchaseDoc:queryPdf")
@GetMapping("/pdf/{id}")
public R<String> getPic(@NotNull(message = "主键不能为空")
@PathVariable Long id) {
return R.ok("操作成功", busPurchaseDocService.queryPicBase64ById(id));
@ -179,8 +180,8 @@ public class BusPurchaseDocController extends BaseController {
.eq(BusBillofquantitiesVersions::getStatus, BusinessStatusEnum.FINISH.getStatus())
.last("limit 1")
);
if (one == null){
throw new ServiceException("请先完成物资工程量清单");
if (one == null) {
throw new ServiceException("请先完成物资工程量清单");
}
List<BusBillofquantities> list = busBillofquantitiesService.list(Wrappers.<BusBillofquantities>lambdaQuery()
.eq(BusBillofquantities::getVersions, one.getVersions())

View File

@ -4,6 +4,7 @@ import org.dromara.cailiaoshebei.domain.BusPurchaseDoc;
import org.dromara.common.core.utils.DateUtils;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @Author 铁憨憨
@ -27,6 +28,14 @@ public class constant {
return String.format("%s%s/%s", PURCHASE_DOC_FILE_URL, purchaseDoc.getId(), timestamp);
}
/**
* 获取物资采购联系单文件名
*/
public static String getBusPurchaseDocFileUrl(BusPurchaseDoc purchaseDoc, Date updateTime) {
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(updateTime);
return String.format("%s%s/%s", PURCHASE_DOC_FILE_URL, purchaseDoc.getId(), timestamp);
}
/**
* 获取物资采购联系单文件名
*/

View File

@ -91,10 +91,10 @@ public interface IBusPurchaseDocService extends IService<BusPurchaseDoc> {
void exportWordById(Long id, HttpServletResponse response);
/**
* 根据主键查询详情图片base64
* 根据主键查询详情PDF地址
*
* @param id 主键id
* @return 详情图片base64
* @return 详情PDF地址
*/
String queryPicBase64ById(Long id);
}

View File

@ -36,6 +36,7 @@ import org.dromara.common.core.domain.event.ProcessEvent;
import org.dromara.common.core.domain.event.ProcessTaskEvent;
import org.dromara.common.core.enums.BusinessStatusEnum;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.file.FileUtils;
@ -60,6 +61,7 @@ import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@ -287,10 +289,12 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
* @param purchaseDoc 采购联系单对象
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void createWord(BusPurchaseDoc purchaseDoc) {
Path targetDir = Paths.get(constant.getBusPurchaseDocFileUrl(purchaseDoc));
Path targetDir = Paths.get(System.getProperty("user.dir"), constant.getBusPurchaseDocFileUrl(purchaseDoc));
// 如果存在目录则直接返回,不存在则生成文件并返回
if (!Files.exists(targetDir)) {
Date now = new Date();
// 清理旧文件
String baseUrl = constant.PURCHASE_DOC_FILE_URL + purchaseDoc.getId();
try {
@ -301,6 +305,10 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
} catch (IOException e) {
log.error("文件目录:{},清理失败", baseUrl, e);
}
Long pdfFileId = purchaseDoc.getPdfFileId();
if (pdfFileId != null) {
ossService.deleteWithValidByIds(List.of(pdfFileId), true);
}
// 准备数据
List<BusMaterialbatchdemandplan> items = new ArrayList<>();
List<BusPlanDocAssociation> planDocAssociationList = planDocAssociationService.lambdaQuery()
@ -311,6 +319,7 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
items = materialbatchdemandplanService.listByIds(planIds);
}
BusPurchaseDocWordDto data = this.getReplacementDto(purchaseDoc, items);
Path newTargetDir = Paths.get(System.getProperty("user.dir"), constant.getBusPurchaseDocFileUrl(purchaseDoc, now));
// 生成文件
try (InputStream is = getClass().getClassLoader().getResourceAsStream(constant.PURCHASE_DOC_TEMPLATE_PATH)) {
if (is == null) {
@ -321,19 +330,51 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
XWPFTemplate template = XWPFTemplate.compile(is, config);
template.render(data);
// 创建目标目录
if (!Files.exists(targetDir)) {
Files.createDirectories(targetDir);
if (!Files.exists(newTargetDir)) {
Files.createDirectories(newTargetDir);
}
// 组合目标文件名
String fileName = constant.getBusPurchaseDocFileName(purchaseDoc);
// 保存修改后的文件
try (FileOutputStream fos = new FileOutputStream(targetDir.resolve(fileName).toFile())) {
try (FileOutputStream fos = new FileOutputStream(newTargetDir.resolve(fileName).toFile())) {
template.write(fos);
}
template.close();
} catch (IOException e) {
throw new OssException("生成Word文件失败错误信息: " + e.getMessage());
}
String filePath = constant.getBusPurchaseDocFileUrl(purchaseDoc, now) + "/";
String fileName = constant.getBusPurchaseDocFileName(purchaseDoc);
File file = new File(filePath + fileName);
try {
// Word → PDF
ByteArrayOutputStream pdfOut = new ByteArrayOutputStream();
WordprocessingMLPackage wordML = WordprocessingMLPackage.load(file);
Docx4J.toPDF(wordML, pdfOut);
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String createDate = DateUtils.formatDate(purchaseDoc.getCreateTime());
String pdfFileName = String.format("物资采购联系单(%s.pdf", createDate);
String contentType = fileNameMap.getContentTypeFor(pdfFileName);
// 保存到对象存储中
SysOssVo upload = ossService.upload(new ByteArrayInputStream(pdfOut.toByteArray()), pdfFileName, contentType, pdfOut.size());
BusPurchaseDoc update = new BusPurchaseDoc();
update.setId(purchaseDoc.getId());
update.setPdfFileId(upload.getOssId());
boolean b = this.updateById(update);
if (!b) {
throw new ServiceException("保存PDF文件失败");
}
purchaseDoc.setPdfFileId(upload.getOssId());
// 修改文件夹名
BusPurchaseDoc newPurchaseDoc = this.getById(purchaseDoc.getId());
Path target = Paths.get(System.getProperty("user.dir"), constant.getBusPurchaseDocFileUrl(newPurchaseDoc));
// 重命名目录
Files.move(newTargetDir, target, StandardCopyOption.ATOMIC_MOVE);
} catch (Docx4JException e) {
throw new ServiceException("获取物资采购联系单详情失败,错误信息: " + e.getMessage());
} catch (IOException e) {
throw new ServiceException("保存PDF文件失败错误信息: " + e.getMessage());
}
}
}
@ -343,6 +384,7 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
* @param id 主键id
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void exportWordById(Long id, HttpServletResponse response) {
BusPurchaseDoc purchaseDoc = this.getById(id);
if (purchaseDoc == null) {
@ -353,12 +395,13 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
}
/**
* 根据主键查询详情图片base64
* 根据主键查询详情PDF地址
*
* @param id 主键id
* @return 详情图片base64
* @return 详情PDF地址
*/
@Override
@Transactional(rollbackFor = Exception.class)
public String queryPicBase64ById(Long id) {
BusPurchaseDoc purchaseDoc = this.getById(id);
if (purchaseDoc == null) {
@ -366,16 +409,11 @@ public class BusPurchaseDocServiceImpl extends ServiceImpl<BusPurchaseDocMapper,
}
// 获取物料单Word
this.createWord(purchaseDoc);
String filePath = constant.getBusPurchaseDocFileUrl(purchaseDoc) + "/";
String fileName = constant.getBusPurchaseDocFileName(purchaseDoc);
File file = new File(filePath + fileName);
String base64;
try (FileInputStream inputStream = new FileInputStream(file)) {
base64 = wordToPdfToImg.wordToImgBase64(inputStream);
} catch (IOException | Docx4JException e) {
throw new ServiceException("获取物资采购联系单详情失败,错误信息: " + e.getMessage());
Long pdfFileId = purchaseDoc.getPdfFileId();
if (pdfFileId == null) {
throw new ServiceException("请先生成Word文件");
}
return "data:image/png;base64," + base64;
return ossService.getById(pdfFileId).getUrl();
}
/**