Compare commits

...

3 Commits

Author SHA1 Message Date
曹鹏飞 30c726d080 一些优化 2025-09-25 17:55:47 +08:00
曹鹏飞 05b9005120 feat: 将pdf使用的字体外挂,减少打包的jar体积 2025-09-25 17:55:33 +08:00
曹鹏飞 f1345a3249 feat: 优化钢构包查询 2025-09-25 17:52:50 +08:00
9 changed files with 85 additions and 29 deletions

View File

@ -0,0 +1,22 @@
package com.nflg.wms.admin.util;
import java.io.File;
public class PathUtils {
public static String getPath() {
String classPath = PathUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println("classPath:" + classPath);
// 如果是jar包运行获取jar包所在目录
if (classPath.contains(".jar")) {
File jarFile = new File(classPath);
return jarFile.getParent();
}
// 如果是开发环境获取target/classes目录的父目录
File classDir = new File(classPath);
if (classDir.getName().equals("classes")) {
return classDir.getParentFile().getParentFile().getAbsolutePath();
}
return new File(classPath).getAbsolutePath();
}
}

View File

@ -1,20 +1,33 @@
package com.nflg.wms.admin.util;
import com.lowagie.text.pdf.BaseFont;
import com.nflg.wms.common.util.VUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class PdfGeneratorUtil {
private static final String fontPath = "fonts/simsun.ttc";
private static final Set<String> SUPPORTED_EXTENSIONS = new HashSet<>(Arrays.asList(
"ttf", "ttc", "otf", "pfb"
));
public static void generatePdf(String name,String html, HttpServletResponse response) throws Exception {
URL baseUrl = new ClassPathResource("template/").getURL();
@ -23,7 +36,7 @@ public class PdfGeneratorUtil {
public static void generatePdf(String name,String html,String baseUrl, HttpServletResponse response) throws Exception {
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
loadFonts(renderer);
renderer.setDocumentFromString(html,baseUrl);
renderer.layout();
renderer.createPDF(response.getOutputStream());
@ -39,9 +52,21 @@ public class PdfGeneratorUtil {
public static void generatePdf(String name,String html,String baseUrl,OutputStream output) throws Exception {
ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
loadFonts(renderer);
renderer.setDocumentFromString(html,baseUrl);
renderer.layout();
renderer.createPDF(output);
}
private static void loadFonts(ITextRenderer renderer) throws IOException {
Path fontsDir = Paths.get(PathUtils.getPath(), "fonts");
VUtil.trueThrowBusinessError(!Files.exists(fontsDir) || !Files.isDirectory(fontsDir))
.throwMessage("fonts文件夹不存在: " + fontsDir);
File directory = fontsDir.toFile();
File[] fonts = directory.listFiles((dir, name) -> SUPPORTED_EXTENSIONS.contains(FilenameUtils.getExtension(name)));
VUtil.trueThrowBusinessError(Objects.isNull(fonts) || fonts.length == 0).throwMessage("未找到有效字体");
for (File font : fonts) {
renderer.getFontResolver().addFont(font.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
}
}

View File

@ -35,6 +35,8 @@ public class DeployTest {
sshUtil.connect("192.168.163.84", 22, "root", "CMP2025nf");
//处理主jar包
handleFile(sshUtil, localPath + jarName, remotePath + jarName);
//处理字体目录
handleDir(sshUtil, localPath, remotePath, "fonts");
//处理lib目录
handleDir(sshUtil, localPath, remotePath, "lib");
//执行脚本启动服务

View File

@ -2,8 +2,10 @@ package com.nflg.wms.common.pojo;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nflg.wms.common.constant.Constant;
import com.nflg.wms.common.constant.STATE;
import lombok.Data;
import org.slf4j.MDC;
import java.io.Serializable;
import java.time.LocalDateTime;
@ -15,7 +17,8 @@ public class ApiResult<T> implements Serializable {
private String type;
private String message;
private Object extras;
private LocalDateTime time=LocalDateTime.now();
private LocalDateTime time = LocalDateTime.now();
private String traceId = MDC.get(Constant.TRACE_ID);
private T result;
public static <T> ApiResult<T> success(T value) {

View File

@ -1,5 +1,7 @@
package com.nflg.wms.common.pojo.dto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.experimental.Accessors;
@ -13,6 +15,7 @@ public class PackageMaterialDTO {
/**
* 编号
*/
@NotBlank
private String no;
/**
@ -28,16 +31,19 @@ public class PackageMaterialDTO {
/**
* 工位
*/
@NotBlank
private String station;
/**
* 托盘
*/
@NotBlank
private String tray;
/**
* 数量
*/
@NotNull
private BigDecimal num;
/**
@ -52,6 +58,7 @@ public class PackageMaterialDTO {
/**
* 版本
*/
@NotNull
private Integer version;
/**

View File

@ -1,6 +1,7 @@
package com.nflg.wms.common.pojo.qo;
import com.nflg.wms.common.pojo.dto.PackageMaterialDTO;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
@ -71,6 +72,7 @@ public class PackageAddQO {
/**
* 零件清单
*/
@Valid
@NotEmpty
private List<PackageMaterialDTO> materials;
}

View File

@ -37,7 +37,7 @@ public class TrayVO {
private String tray;
/**
* 状态0未打包1已打包2-在途
* 状态0未打包1已打包2-在途3已收货
*/
private Short state;

View File

@ -3,30 +3,25 @@
<mapper namespace="com.nflg.wms.repository.mapper.WmsStructuralPackageMapper">
<select id="search" resultType="com.nflg.wms.common.pojo.vo.PackageVO">
SELECT *
FROM (
SELECT DISTINCT ON ("no") id,"no","order_no","name",drawing_no,weight,cate,eco,version,remark,enable,create_by
,create_time,update_by,update_time,get_modelnos(model_ids) as "modelNos"
FROM wms_structural_package
<where>
<if test="request.no!=null and request.no!=''">
and ("no" ilike concat('%', #{request.no}, '%') or "name" ilike concat('%', #{request.no}, '%'))
</if>
<if test="request.eco!=null and request.eco!=''">
and eco ilike concat('%', #{request.eco}, '%')
</if>
<if test="request.drawingNo!=null and request.drawingNo!=''">
and drawing_no like concat('%', #{request.drawingNo}, '%')
</if>
<if test="request.modelId!=null">
and find_in_set(#{request.modelId},model_ids)>0
</if>
<if test="request.type==0">
and latest=true
</if>
</where>
ORDER BY "no",id DESC
) t
SELECT *,get_modelnos(model_ids) as "modelNos"
FROM wms_structural_package
<where>
<if test="request.no!=null and request.no!=''">
and ("no" ilike concat('%', #{request.no}, '%') or "name" ilike concat('%', #{request.no}, '%'))
</if>
<if test="request.eco!=null and request.eco!=''">
and eco ilike concat('%', #{request.eco}, '%')
</if>
<if test="request.drawingNo!=null and request.drawingNo!=''">
and drawing_no like concat('%', #{request.drawingNo}, '%')
</if>
<if test="request.modelId!=null">
and find_in_set(#{request.modelId},model_ids)>0
</if>
<if test="request.type==0">
and latest=true
</if>
</where>
ORDER BY id DESC
</select>