yinyitong-zhongyuyuan-dll-hang/src/main/java/com/dpkj/modules/autoReplyPrint/controller/ReceiptPrintController.java

87 lines
3.7 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.dpkj.modules.autoReplyPrint.controller;
import com.alibaba.fastjson.JSONObject;
import com.dpkj.common.exception.RRException;
import com.dpkj.common.vo.Result;
import com.dpkj.modules.autoReplyPrint.request.ReceiptPrintRequest;
import com.dpkj.modules.autoReplyPrint.service.ImagePrintService;
import com.dpkj.modules.autoReplyPrint.service.impl.TemplateService;
import com.dpkj.modules.autoReplyPrint.vo.PrinterStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
/**
* 小票打印机控制层
*
* @author <a href="https://gitee.com/shi-chongli">石头人</a>
* @version 1.0
* @since 2025-03-10 9:29:22
*/
@Slf4j
@RestController
@RequestMapping("/receipt")
public class ReceiptPrintController {
@Resource(name = "USBImagePrint")
private ImagePrintService usbImagePrintService;
@Resource
private TemplateService templateService;
@PostMapping("/print")
private Result<Void> print(@Validated @RequestBody ReceiptPrintRequest request) throws UnsupportedEncodingException {
String devName = "VID:0x0FE6,PID:0x811E"; // 采用默认的devName不进行入参传值了
// 进行模板填充
// 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\"}";
StringBuilder filePath = new StringBuilder(request.getFileDir());
// 校验是否选中了模板,如果没选中模板的话则不需要另外生成了
if ( !StringUtils.isEmpty(request.getTemplateName()) && !StringUtils.isEmpty(request.getFileDir())){
byte[] image = templateService.generateReceiptImage(
JSONObject.parseObject(URLDecoder.decode(request.getTemplateFillData(), "UTF-8")),
request.getTemplateName(),
request.getWidth(),
request.getHeight(),
filePath);
}else {
throw new RRException("模板渲染错误");
}
usbImagePrintService.imagePrintFromPath(devName,
request.getWidth(),
request.getHeight(),
filePath.toString(),
1,
0);
// 删除图片
File file = new File(filePath.toString());
// 检查文件是否存在
if (file.exists() ) {
// 尝试删除文件
if (file.delete()) {
log.info("文件删除成功: " + filePath);
} else {
log.info("文件删除失败: " + filePath);
}
} else {
log.info("文件不存在: " + filePath);
}
return Result.ok();
}
@PostMapping("/getStatus")
public Result<PrinterStatus> print(){
String devName = "VID:0x0FE6,PID:0x811E"; // 采用默认的devName不进行入参传值了
return Result.ok(this.usbImagePrintService.getStatus(devName));
}
}