2025-03-12 17:12:48 +08:00
package com.dpkj.modules.autoReplyPrint.controller ;
2025-03-26 23:21:19 +08:00
import com.alibaba.fastjson.JSONArray ;
2025-03-14 16:31:53 +08:00
import com.alibaba.fastjson.JSONObject ;
2025-03-26 23:21:19 +08:00
import com.dpkj.common.config.PrinterConfig ;
2025-03-14 16:31:53 +08:00
import com.dpkj.common.exception.RRException ;
2025-03-12 17:12:48 +08:00
import com.dpkj.common.vo.Result ;
2025-03-24 10:27:51 +08:00
import com.dpkj.modules.autoReplyPrint.enums.ReceiptTemplateEnum ;
2025-03-12 17:12:48 +08:00
import com.dpkj.modules.autoReplyPrint.request.ReceiptPrintRequest ;
import com.dpkj.modules.autoReplyPrint.service.ImagePrintService ;
import com.dpkj.modules.autoReplyPrint.service.impl.TemplateService ;
2025-03-31 17:41:48 +08:00
import com.dpkj.modules.autoReplyPrint.utils.FolderUtils ;
2025-03-17 10:28:17 +08:00
import com.dpkj.modules.autoReplyPrint.vo.PrinterStatus ;
import lombok.extern.slf4j.Slf4j ;
2025-03-12 17:12:48 +08:00
import org.springframework.validation.annotation.Validated ;
2025-05-19 20:11:33 +08:00
import org.springframework.web.bind.annotation.PostMapping ;
import org.springframework.web.bind.annotation.RequestBody ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RestController ;
2025-03-14 16:31:53 +08:00
import org.thymeleaf.util.StringUtils ;
2025-05-19 20:11:33 +08:00
import javax.annotation.Resource ;
2025-03-17 10:28:17 +08:00
import java.io.File ;
2025-03-19 13:48:33 +08:00
import java.io.UnsupportedEncodingException ;
import java.net.URLDecoder ;
2025-03-26 23:21:19 +08:00
import java.text.SimpleDateFormat ;
import java.util.Date ;
2025-03-12 17:12:48 +08:00
/ * *
* 小票打印机控制层
*
* @author < a href = " https://gitee.com/shi-chongli " > 石头人 < / a >
* @version 1 . 0
* @since 2025 - 03 - 10 9 : 29 : 22
* /
2025-03-17 10:28:17 +08:00
@Slf4j
2025-03-12 17:12:48 +08:00
@RestController
@RequestMapping ( " /receipt " )
public class ReceiptPrintController {
2025-03-26 23:21:19 +08:00
@Resource
private PrinterConfig printerConfig ;
private static final int MAX_HEIGHT = 1000 ;
private static final int FIXED_WIDTH = 730 ;
2025-03-12 17:12:48 +08:00
@Resource ( name = " USBImagePrint " )
private ImagePrintService usbImagePrintService ;
2025-03-14 16:31:53 +08:00
@Resource
private TemplateService templateService ;
2025-03-12 17:12:48 +08:00
@PostMapping ( " /print " )
2025-03-19 13:48:33 +08:00
private Result < Void > print ( @Validated @RequestBody ReceiptPrintRequest request ) throws UnsupportedEncodingException {
2025-03-31 17:41:48 +08:00
FolderUtils . checkFolderAndCreate ( request . getFileDir ( ) ) ;
2025-03-12 17:12:48 +08:00
String devName = " VID:0x0FE6,PID:0x811E " ; // 采用默认的devName, 不进行入参传值了
2025-03-24 10:27:51 +08:00
String templateName = ReceiptTemplateEnum . getTemplateName ( request . getTemplateName ( ) ) ;
2025-03-26 23:21:19 +08:00
// 宽度固定
request . setWidth ( request . getWidth ( ) > 610 ? 600 : request . getWidth ( ) ) ;
JSONObject data = JSONObject . parseObject ( URLDecoder . decode ( request . getTemplateFillData ( ) , " UTF-8 " ) ) ;
2025-05-19 20:11:33 +08:00
// 强行设置终端号 不需要固定,前端自己穿
// data.put("terminalNumber", printerConfig.getTerminalNumber());
2025-03-26 23:21:19 +08:00
2025-05-19 20:11:33 +08:00
// 强行设置打印时间
2025-03-26 23:21:19 +08:00
SimpleDateFormat sdf = new SimpleDateFormat ( printerConfig . getTimeType ( ) ) ;
2025-05-19 20:23:53 +08:00
String formattedDate = sdf . format ( new Date ( ) ) ;
2025-03-26 23:21:19 +08:00
data . put ( " printTime " , formattedDate ) ;
Integer height = request . getHeight ( ) ;
int dinyHeight = 0 ;
if ( templateName . equals ( " department " ) ) {
// 由于是使用的门诊小票-T2, 那么默认的高度为1000, 强行设置, 通过动态修改渲染的图片的高度
height = 1100 ;
// 单行最大长度为10
int singleLineMaxLength = 10 ;
// 这里的长度取自于department.html模板中的项目单个tr高度, 并且略高于该高度
int singleLineHeight = 30 ;
// 动态计算长度
JSONArray items = data . getJSONArray ( " items " ) ;
for ( Object item : items ) {
JSONObject itemEntity = ( JSONObject ) item ;
String projectName = String . valueOf ( itemEntity . get ( " name " ) ) ;
int length = projectName . length ( ) ;
2025-05-19 20:23:53 +08:00
int count = ( int ) Math . ceil ( ( double ) length / singleLineMaxLength ) ;
2025-03-26 23:21:19 +08:00
dinyHeight + = count * singleLineHeight ;
}
// 计算是否有门诊号
String outpatientNumber = data . getString ( " outpatientNumber " ) ;
2025-05-19 20:23:53 +08:00
if ( ! StringUtils . isEmpty ( outpatientNumber ) ) {
2025-03-26 23:21:19 +08:00
dinyHeight + = 28 ;
}
// 计算是否有就诊医生
String doctor = data . getString ( " doctor " ) ;
2025-05-19 20:23:53 +08:00
if ( ! StringUtils . isEmpty ( doctor ) ) {
2025-03-26 23:21:19 +08:00
dinyHeight + = 28 ;
}
// 计算是否有就诊科室
String department = data . getString ( " department " ) ;
2025-05-19 20:23:53 +08:00
if ( ! StringUtils . isEmpty ( department ) ) {
2025-03-26 23:21:19 +08:00
dinyHeight + = 28 ;
}
}
height + = dinyHeight ;
2025-03-17 10:28:17 +08:00
// 进行模板填充
2025-03-17 10:29:19 +08:00
// String testData = "{\"hospitalName\":\"澜沧县中医医院\",\"registeTerminalName\":\"中国农业银行自助终端\",\"registeType\":\"自助挂号\",\"name\":\"刘博雅\",\"gender\":\"男\",\"age\":28,\"birthDate\":\"1996-06-31\",\"cardNumber\":\"6221**********0731\",\"outpatientNumber\":\"2501150038\",\"department\":\"普外科门诊\",\"visitLevel\":\"普通号\",\"doctor\":\"普通门诊\",\"sequence\":\"1\",\"registerDate\":\"2025-01-15\",\"totalFee\":4.00,\"paymentMethod\":\"微信扫码支付\",\"orderNumber\":\"\",\"transactionNumber\":\"2025011513090412092794szztzzj\",\"qrCodeBase64_2base64Type_1_250_250\":\"maby this is a Base64 code data if has\",\"terminalNumber\":\"12092794\",\"printTime\":\"2025-01-15 13:10:08\"}";
2025-03-14 16:31:53 +08:00
StringBuilder filePath = new StringBuilder ( request . getFileDir ( ) ) ;
// 校验是否选中了模板,如果没选中模板的话则不需要另外生成了
2025-05-19 20:23:53 +08:00
if ( ! StringUtils . isEmpty ( request . getTemplateName ( ) ) & & ! StringUtils . isEmpty ( request . getFileDir ( ) ) ) {
2025-03-19 13:48:33 +08:00
byte [ ] image = templateService . generateReceiptImage (
2025-03-26 23:21:19 +08:00
data ,
2025-03-24 10:27:51 +08:00
templateName ,
2025-03-19 13:48:33 +08:00
request . getWidth ( ) ,
2025-03-26 23:21:19 +08:00
height ,
2025-03-19 13:48:33 +08:00
filePath ) ;
2025-05-19 20:23:53 +08:00
} else {
2025-03-14 16:31:53 +08:00
throw new RRException ( " 模板渲染错误 " ) ;
}
2025-03-12 17:12:48 +08:00
usbImagePrintService . imagePrintFromPath ( devName ,
request . getWidth ( ) ,
request . getHeight ( ) ,
2025-03-14 16:31:53 +08:00
filePath . toString ( ) ,
2025-03-12 17:12:48 +08:00
1 ,
0 ) ;
2025-03-17 10:28:17 +08:00
// 删除图片
File file = new File ( filePath . toString ( ) ) ;
// 检查文件是否存在
2025-05-19 20:23:53 +08:00
if ( file . exists ( ) ) {
2025-03-17 10:28:17 +08:00
// 尝试删除文件
if ( file . delete ( ) ) {
log . info ( " 文件删除成功: " + filePath ) ;
} else {
log . info ( " 文件删除失败: " + filePath ) ;
}
} else {
log . info ( " 文件不存在: " + filePath ) ;
}
2025-03-12 17:12:48 +08:00
return Result . ok ( ) ;
}
2025-03-17 10:28:17 +08:00
@PostMapping ( " /getStatus " )
2025-05-19 20:23:53 +08:00
public Result < PrinterStatus > print ( ) {
2025-03-17 10:28:17 +08:00
String devName = " VID:0x0FE6,PID:0x811E " ; // 采用默认的devName, 不进行入参传值了
return Result . ok ( this . usbImagePrintService . getStatus ( devName ) ) ;
}
2025-03-12 17:12:48 +08:00
}