feat:新增门诊缴费-T2模板为完全动态生成

This commit is contained in:
石崇礼 2025-03-26 10:53:04 +08:00
parent b2f2e8cd49
commit 6c126a6757
3 changed files with 84 additions and 18 deletions

View File

@ -2,7 +2,6 @@ package com.dpkj.common.config;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
/** /**
@ -29,4 +28,15 @@ public class PrinterConfig {
* 波特率 串口连接下使用 * 波特率 串口连接下使用
*/ */
private Integer baudRate; private Integer baudRate;
/**
* 终端号
*/
private String terminalNumber;
/**
* 时间格式
*/
private String timeType;
} }

View File

@ -1,7 +1,9 @@
package com.dpkj.modules.print.service.impl; package com.dpkj.modules.print.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.dpkj.common.config.PrinterConfig;
import com.dpkj.common.dto.LexMarkDTO; import com.dpkj.common.dto.LexMarkDTO;
import com.dpkj.common.dto.LexMarkResultDTO; import com.dpkj.common.dto.LexMarkResultDTO;
import com.dpkj.common.exception.RRException; import com.dpkj.common.exception.RRException;
@ -19,6 +21,9 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/** /**
* 挂号服务打印 * 挂号服务打印
@ -31,20 +36,72 @@ import java.io.IOException;
@Service("registerService") @Service("registerService")
public class RegisterServiceImpl implements PrintService { public class RegisterServiceImpl implements PrintService {
@Resource
private PrinterConfig printerConfig;
@Resource @Resource
private ThirdService thirdService; private ThirdService thirdService;
private static final int MAX_HEIGHT = 1000; private static final int MAX_HEIGHT = 1000;
private static final int FIXED_WIDTH = 690; private static final int FIXED_WIDTH = 730;
@Override @Override
public LexMarkResultDTO<LexMarkResultDTO.Param> printImage(JSONObject data, String template, int width, int height, String saveDir) { public LexMarkResultDTO<LexMarkResultDTO.Param> printImage(JSONObject data, String template, int width, int height, String saveDir) {
if ( width > 690 || height <= 0){ if ( height <= 0){
width = 690; width = FIXED_WIDTH;
} }
// 强行设置终端号
data.put("terminalNumber", printerConfig.getTerminalNumber());
// 强行设置终端号和打印时间
SimpleDateFormat sdf = new SimpleDateFormat(printerConfig.getTimeType());
String formattedDate = sdf.format( new Date());
data.put("printTime", formattedDate);
// 获取模板 // 获取模板
String templateName = ReceiptTemplateEnum.getTemplateName(template); String templateName = ReceiptTemplateEnum.getTemplateName(template);
int dinyHeight = 0;
if (templateName.equals("department")) {
// 由于是使用的门诊小票-T2那么默认的高度为1000强行设置通过动态修改渲染的图片的高度
height = 1000;
// 单行最大长度为10
int singleLineMaxLength = 10;
// 这里的长度取自于department.html模板中的项目单个tr高度并且略高于该高度
int singleLineHeight = 40;
// 动态计算长度
JSONArray items = data.getJSONArray("items");
for (Object item : items) {
JSONObject itemEntity = (JSONObject) item;
String projectName = String.valueOf(itemEntity.get("name"));
int length = projectName.length();
int count = (int)Math.ceil((double) length / singleLineMaxLength);
dinyHeight += count * singleLineHeight;
}
// 计算是否有门诊号
String outpatientNumber = data.getString("outpatientNumber");
if ( !StringUtils.isEmpty(outpatientNumber)){
dinyHeight += 35;
}
// 计算是否有就诊医生
String doctor = data.getString("doctor");
if ( !StringUtils.isEmpty(doctor)){
dinyHeight += 35;
}
// 计算是否有就诊科室
String department = data.getString("department");
if ( !StringUtils.isEmpty(department)){
dinyHeight += 35;
}
}
height += dinyHeight;
this.getStatus(); this.getStatus();
StringBuilder filePath = new StringBuilder(saveDir); StringBuilder filePath = new StringBuilder(saveDir);
// 校验是否选中了模板,如果没选中模板的话则不需要另外生成了 // 校验是否选中了模板,如果没选中模板的话则不需要另外生成了
@ -54,18 +111,18 @@ public class RegisterServiceImpl implements PrintService {
throw new RRException("模板渲染错误"); throw new RRException("模板渲染错误");
} }
String[] deletePathList = new String[(int) Math.ceil((double) height / MAX_HEIGHT) + 1]; // 计算切割的块数
int numPieces = (int) Math.ceil((double) height / MAX_HEIGHT);
String[] deletePathList = new String[numPieces + 1];
deletePathList[0] = filePath.toString(); deletePathList[0] = filePath.toString();
String[] filePathList = new String[(int) Math.ceil((double) height / MAX_HEIGHT)]; String[] filePathList = new String[numPieces];
// 对图片进行分块处理当前台式打印机最大参数配置 宽度690高度1200 // 对图片进行分块处理当前台式打印机最大参数配置 宽度690高度1200
if ( height > MAX_HEIGHT){ if ( height > MAX_HEIGHT){
try { try {
// 读取输入图片 // 读取输入图片
BufferedImage originalImage = ImageIO.read(new File(filePath.toString())); BufferedImage originalImage = ImageIO.read(new File(filePath.toString()));
// 计算切割的块数
int numPieces = (int) Math.ceil((double) height / MAX_HEIGHT);
// 循环切割图片并保存每一块 // 循环切割图片并保存每一块
for (int i = 0; i < numPieces; i++) { for (int i = 0; i < numPieces; i++) {
int startY = i * MAX_HEIGHT; int startY = i * MAX_HEIGHT;
@ -83,7 +140,7 @@ public class RegisterServiceImpl implements PrintService {
} }
} catch (IOException e) { } catch (IOException e) {
log.error("图片分段错误"); log.error("模板切割失败");
e.printStackTrace(); e.printStackTrace();
} }
}else { }else {
@ -124,7 +181,6 @@ public class RegisterServiceImpl implements PrintService {
} }
} }
// this.thirdService.close("ReceiptPrinter");
return paramLexMarkResultDTO; return paramLexMarkResultDTO;
} }

View File

@ -7,16 +7,16 @@
<body> <body>
<div style="font-weight: 700;"> <div style="font-weight: 700;">
<div style="text-align: center;margin-top: 40px;"> <div style="text-align: center;margin-top: 40px;">
<div><span style="font-size: 28px;">******<span th:text="${hospitalName}"></span>******</span></div> <div><span style="font-size: 28px;">******<span th:text="${registeTerminalName}"></span>******</span></div>
<div style="font-size: 38px; margin-top: 20px;"><span th:text="${registeTerminalName}"></span></div> <div style="font-size: 38px; margin-top: 20px;"><span th:text="${hospitalName}"></span></div>
<div style="font-size: 38px; margin-top: 10px;"><span th:text="${registeType}"></span></div> <div style="font-size: 38px; margin-top: 10px;"><span th:text="${registeType}"></span></div>
</div> </div>
<div style="text-align: center; font-weight: 700; font-size: 32px;">-----------------------------------------------------------------------------</div> <div style="text-align: center; font-weight: 700; font-size: 32px;">-----------------------------------------------------------------------------</div>
<div style="margin-left: 20px; font-size: 35px; margin-bottom: 10px; display: flex"> <div style="margin-left: 20px; font-size: 35px; margin-bottom: 10px; display: flex">
<div style="flex: 1; margin-top: 10px; "> <div style="flex: 1; margin-top: 10px; ">
姓名:<span style="margin-right: 40px;" th:text="${name}"></span> 姓名:<span style="margin-right: 25px;" th:text="${name}"></span><br>
性别:<span style="margin-right: 40px;" th:text="${gender}"></span> 性别:<span style="margin-right: 25px;" th:text="${gender}"></span>
年龄:<span style="margin-right: 40px;" th:text="${age}"></span> 年龄:<span style="margin-right: 25px;" th:text="${age}"></span>
</div> </div>
<div th:if="${outpatientNumber}" style="flex: 1; margin-top: 10px; margin-right: 15px;">门诊号:<span th:text="${outpatientNumber}"></span></div> <div th:if="${outpatientNumber}" style="flex: 1; margin-top: 10px; margin-right: 15px;">门诊号:<span th:text="${outpatientNumber}"></span></div>
<div th:if="${doctor}" style="flex: 1; margin-top: 10px; margin-right: 15px;">就诊医生:<span th:text="${doctor}"></span></div> <div th:if="${doctor}" style="flex: 1; margin-top: 10px; margin-right: 15px;">就诊医生:<span th:text="${doctor}"></span></div>
@ -38,8 +38,8 @@
<th >小计</th> <th >小计</th>
</tr> </tr>
<tr th:each="item : ${items}" style="height: 50px;"> <tr th:each="item : ${items}" style="height: 50px;">
<td style=" width: 310px; word-break: break-all;" th:text="${item.name}"></td> <td style=" width: 310px; word-break: break-all; margin-right: 10px;" th:text="${item.name}"></td>
<td th:text="${item.quantity}"></td> <td style="margin-left: 5px;" th:text="${item.quantity}"></td>
<td th:text="${item.unitPrice}"></td> <td th:text="${item.unitPrice}"></td>
<td th:text="${item.subtotal}"></td> <td th:text="${item.subtotal}"></td>
</tr> </tr>