批量打印
This commit is contained in:
parent
7e1e257c19
commit
449b0173b0
@ -0,0 +1,23 @@
|
||||
package com.czlis.interfaceCommon.pojo.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class PrintReportProgressDTO {
|
||||
private String taskId;
|
||||
private Integer progress;
|
||||
private String desc;
|
||||
// progress / finish / error
|
||||
private String status;
|
||||
// 完成后pdf base64
|
||||
private String pdfBase64;
|
||||
private String errorMsg;
|
||||
// 成功/失败数量,前端展示统计
|
||||
private Integer succCount;
|
||||
private Integer failCount;
|
||||
private String ybh;
|
||||
}
|
||||
@ -0,0 +1,141 @@
|
||||
package com.czlis.interfaceCommon.service;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.czlis.common.annotation.Async;
|
||||
import com.czlis.common.core.domain.Result;
|
||||
import cn.hutool.core.codec.Base64;
|
||||
import com.czlis.common.utils.file.FileUtils;
|
||||
import com.czlis.common.utils.file.ImageUtils;
|
||||
import com.czlis.common.utils.spring.SpringUtils;
|
||||
import com.czlis.interfaceCommon.pojo.dto.PrintReportProgressDTO;
|
||||
import com.czlis.interfaceCommon.pojo.dto.BatchPrintDTO;
|
||||
import com.czlis.interfaceCommon.utils.LisUtil;
|
||||
import com.czlis.interfaceCommon.utils.ReportUtil;
|
||||
import com.czlis.interfaceCommon.websocket.ChatWebSocketServer;
|
||||
import com.czlis.interfaceCommon.websocket.RetryMsgService;
|
||||
import com.czlis.interfaceCommon.websocket.WebSocketMessage;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
@Slf4j
|
||||
@Service
|
||||
public class PrintReportService {
|
||||
@Autowired
|
||||
ReportUtil reportUtil;
|
||||
/**
|
||||
* 组装标准WebSocket消息推送 printReport 类型
|
||||
* @param sid 前端浏览器指纹SID
|
||||
* @param progressDTO 进度数据
|
||||
*/
|
||||
private void sendPrintWsMsg(String sid, PrintReportProgressDTO progressDTO) {
|
||||
long ts = System.currentTimeMillis();
|
||||
String dataJson = JSON.toJSONString(progressDTO);
|
||||
WebSocketMessage wsMsg = new WebSocketMessage();
|
||||
wsMsg.setMsgType("printReport");
|
||||
wsMsg.setData(dataJson);
|
||||
wsMsg.setSenderId("SERVER_PRINT");
|
||||
wsMsg.setReceiverId(sid);
|
||||
wsMsg.setTimestamp(ts);
|
||||
|
||||
boolean sendSuccess = ChatWebSocketServer.sendToUser(sid, JSON.toJSONString(wsMsg));
|
||||
// 发送失败加入重试队列
|
||||
if (!sendSuccess) {
|
||||
RetryMsgService retryMsgService = SpringUtils.getBean(RetryMsgService.class);
|
||||
retryMsgService.sendToRetryQueue(wsMsg);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 批量打印入口,同步返回任务ID,后台异步执行
|
||||
* @param clientSid 前端浏览器指纹SID ws连接标识
|
||||
* @param bgmap 打印列表
|
||||
* @return taskId
|
||||
*/
|
||||
public String startBatchPrint(String clientSid, List<BatchPrintDTO> bgmap) {
|
||||
String taskId = UUID.randomUUID().toString();
|
||||
// 提交异步打印任务
|
||||
batchPrintAsync(taskId, clientSid, bgmap);
|
||||
return taskId;
|
||||
}
|
||||
// 异步批量打印任务
|
||||
@Async
|
||||
public void batchPrintAsync(String taskId, String clientSid, List<BatchPrintDTO> reportlist) {
|
||||
PrintReportProgressDTO progressDTO = new PrintReportProgressDTO();
|
||||
progressDTO.setTaskId(taskId);
|
||||
int total = reportlist.size();
|
||||
int fall = 0, ok = 0;
|
||||
List<String> mergeBgdhList = new ArrayList<>();
|
||||
String staticPath = ImageUtils.getStaticPath() + File.separator + "printPDF" + File.separator;
|
||||
|
||||
try {
|
||||
// 循环单份打印
|
||||
for (int i = 0; i < total; i++) {
|
||||
BatchPrintDTO bg = reportlist.get(i);
|
||||
Result res = reportUtil.printReport(bg.getJyrq(), bg.getYq(), bg.getYbh(), true);
|
||||
|
||||
// 单份打印失败
|
||||
if (res == null || !"0".equals(res.getCode())) {
|
||||
fall++;
|
||||
bg.setFlag("-1");
|
||||
bg.setMsg(res == null ? "生成报告失败" : res.getMsg());
|
||||
} else {
|
||||
ok++;
|
||||
bg.setFlag("1");
|
||||
bg.setMsg("生成报告成功");
|
||||
mergeBgdhList.add(reportUtil.getStringValue(res.getData()));
|
||||
}
|
||||
|
||||
// // 每5份或最后一份推送一次进度,减少消息风暴
|
||||
// if ((i + 1) % 5 == 0 || i == total - 1) {
|
||||
int currProgress = (i + 1) * 100 / total;
|
||||
progressDTO.setProgress(currProgress);
|
||||
progressDTO.setStatus(bg.getFlag());
|
||||
progressDTO.setDesc("正在生成第" + (i + 1) + "/" + total + "份报告");
|
||||
progressDTO.setSuccCount(ok);
|
||||
progressDTO.setFailCount(fall);
|
||||
progressDTO.setPdfBase64(null);
|
||||
progressDTO.setErrorMsg(bg.getMsg());
|
||||
progressDTO.setYbh(bg.getYbh());
|
||||
sendPrintWsMsg(clientSid, progressDTO);
|
||||
// }
|
||||
}
|
||||
|
||||
// 全部单份PDF生成完成,开始合并
|
||||
progressDTO.setDesc("正在合并全部PDF文件...");
|
||||
progressDTO.setProgress(98);
|
||||
sendPrintWsMsg(clientSid, progressDTO);
|
||||
|
||||
String pdfName = UUID.randomUUID().toString();
|
||||
String path = staticPath + pdfName + ".pdf";
|
||||
// 合并文件
|
||||
LisUtil.mergeToFile(mergeBgdhList, path);
|
||||
File file = new File(path);
|
||||
String pdfBase64 = Base64.encode(file);
|
||||
// 删除临时文件
|
||||
FileUtils.deleteFile(path);
|
||||
|
||||
// 更新数据库打印标识
|
||||
reportUtil.pdatePrintStatus(reportlist);
|
||||
// 推送完成消息
|
||||
progressDTO.setStatus("finish");
|
||||
progressDTO.setProgress(100);
|
||||
progressDTO.setDesc("批量打印任务完成,成功:" + ok + "份,失败:" + fall + "份");
|
||||
progressDTO.setPdfBase64(pdfBase64);
|
||||
progressDTO.setErrorMsg(null);
|
||||
progressDTO.setSuccCount(ok);
|
||||
progressDTO.setFailCount(fall);
|
||||
sendPrintWsMsg(clientSid, progressDTO);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("批量打印任务异常 taskId:{}", taskId, e);
|
||||
// 推送异常消息
|
||||
progressDTO.setStatus("error");
|
||||
progressDTO.setErrorMsg("任务执行失败:" + e.getMessage());
|
||||
progressDTO.setDesc("批量打印出现异常");
|
||||
sendPrintWsMsg(clientSid, progressDTO);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -337,9 +337,8 @@ public class ReportUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public Result printReport(Date jyrq, String yq, String ybh) {
|
||||
commonUtil.getLabPatOne(jyrq, yq, ybh);
|
||||
return printReport(commonUtil.getLabPatOne(jyrq, yq, ybh));
|
||||
public Result printReport(Date jyrq, String yq, String ybh,boolean base64) {
|
||||
return printReport(commonUtil.getLabPatOne(jyrq, yq, ybh),base64);
|
||||
}
|
||||
public Result printReport(LabPat labPat) {
|
||||
return printReport(labPat,true);
|
||||
@ -555,10 +554,8 @@ public class ReportUtil {
|
||||
*/
|
||||
public Result printReport(List<BatchPrintDTO> bgmap) {
|
||||
String staticpath = ImageUtils.getStaticPath() + File.separator+"printPDF"+ File.separator;
|
||||
// List<Map<String,Object>> bgdhBase64List = new ArrayList<>();
|
||||
List<String> mergeBgdhList = new ArrayList<>();
|
||||
String brdh = "";
|
||||
int j = 0;
|
||||
int fall=0,succ=0;
|
||||
for (BatchPrintDTO bg : bgmap) {
|
||||
String yq = bg.getYq();
|
||||
@ -580,22 +577,9 @@ public class ReportUtil {
|
||||
}
|
||||
bg.setFlag("1");
|
||||
bg.setMsg("打印成功");
|
||||
String pageType=res.getMsg();
|
||||
// Map<String,Object> map = new HashMap<>();
|
||||
// map.put("data", res.getData());
|
||||
// map.put("pageType", pageType);
|
||||
// map.put("brdh",labPatOne.getBrdh());
|
||||
// map.put("confirmdt",labPatOne.getConfirmdt());
|
||||
// map.put("jyrq", jyrq);
|
||||
// map.put("yq", yq);
|
||||
// map.put("ybh", ybh);
|
||||
// bgdhBase64List.add(map);
|
||||
mergeBgdhList.add(getStringValue(res.getData()));
|
||||
j++;
|
||||
succ++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
String encode = "";
|
||||
try {
|
||||
//合并成一个pdf交给前端
|
||||
@ -614,6 +598,10 @@ public class ReportUtil {
|
||||
return new Result("-1",e.getMessage());
|
||||
}
|
||||
//修改打印标志
|
||||
pdatePrintStatus(bgmap);
|
||||
return new Result("0","打印成功!",encode);
|
||||
}
|
||||
public void pdatePrintStatus(List<BatchPrintDTO> bgmap) {
|
||||
for (BatchPrintDTO list : bgmap) {
|
||||
String flag=list.getFlag();
|
||||
if(flag!=null&&"1".equals(flag)) {
|
||||
@ -623,10 +611,8 @@ public class ReportUtil {
|
||||
commonUtil.updateLabPatByColumn(jyrq, yq, ybh, "dybz", "1");
|
||||
}
|
||||
}
|
||||
|
||||
return new Result("0","打印成功!",encode);
|
||||
}
|
||||
private String getStringValue(Object obj) {
|
||||
public String getStringValue(Object obj) {
|
||||
return obj == null ? null : obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,8 +2,10 @@ package com.czlis.liswork.service.impl.work;
|
||||
|
||||
import com.czlis.common.core.domain.Result;
|
||||
import com.czlis.common.core.domain.ResultT;
|
||||
import com.czlis.common.utils.SecurityUtils;
|
||||
import com.czlis.common.utils.bean.BeanUtils;
|
||||
import com.czlis.interfaceCommon.pojo.dto.BatchPrintDTO;
|
||||
import com.czlis.interfaceCommon.service.PrintReportService;
|
||||
import com.czlis.interfaceCommon.utils.ReportUtil;
|
||||
import com.czlis.liswork.mapper.work.BatchPrintMapper;
|
||||
import com.czlis.liswork.pojo.VO.BatchPrintQueryVO;
|
||||
@ -17,6 +19,7 @@ import org.springframework.stereotype.Service;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@ -26,7 +29,8 @@ public class BatchPrintServiceImpl implements BatchPrintService {
|
||||
@Autowired
|
||||
BatchPrintMapper batchPrintMapper;
|
||||
@Autowired
|
||||
ReportUtil reportUtil;
|
||||
PrintReportService printReportService;
|
||||
|
||||
@Override
|
||||
public Result printAll(BatchPrintQueryVO batchPrintQueryVO) {
|
||||
//TODO 直接过滤掉未审核的报告了,如果需要后面再加吧
|
||||
@ -37,23 +41,27 @@ public class BatchPrintServiceImpl implements BatchPrintService {
|
||||
BeanUtils.copyProperties(item2, item1);
|
||||
return item1;
|
||||
}).collect(Collectors.toList());
|
||||
Result result =reportUtil.printReport(list1);
|
||||
if(result==null&&!result.getCode().equals("0")) return null;
|
||||
Map<String, BatchPrintDTO> table1Map = list1.stream().collect(Collectors.toMap(BatchPrintDTO::getYbh, t -> t));
|
||||
|
||||
// 2. 遍历目标list2,匹配覆盖字段
|
||||
for (BatchPrintVO t2 : batchPrintVOList) {
|
||||
BatchPrintDTO t1 = table1Map.get(t2.getYbh());
|
||||
if (t1 != null) {
|
||||
// 两个字段覆盖
|
||||
t2.setCp_msg(t1.getMsg());
|
||||
t2.setCp_cnt(Integer.getInteger(t1.getFlag()));
|
||||
}
|
||||
}
|
||||
Map<String, Object> map=new HashMap<>();
|
||||
map.put("base64data",result.getData());
|
||||
map.put("printflag",batchPrintVOList);
|
||||
return new Result("0","打印成功!",map);
|
||||
String sid= SecurityUtils.getLoginUser().getSysLoginParam().getLocalid();
|
||||
String taskId = printReportService.startBatchPrint(sid, list1);
|
||||
return new Result("0", "任务已开始执行", taskId);
|
||||
//
|
||||
// Result result =reportUtil.printReport(list1);
|
||||
// if(result==null&&!result.getCode().equals("0")) return null;
|
||||
// Map<String, BatchPrintDTO> table1Map = list1.stream().collect(Collectors.toMap(BatchPrintDTO::getYbh, t -> t));
|
||||
//
|
||||
//// 2. 遍历目标list2,匹配覆盖字段
|
||||
// for (BatchPrintVO t2 : batchPrintVOList) {
|
||||
// BatchPrintDTO t1 = table1Map.get(t2.getYbh());
|
||||
// if (t1 != null) {
|
||||
// // 两个字段覆盖
|
||||
// t2.setCp_msg(t1.getMsg());
|
||||
// t2.setCp_cnt(Integer.getInteger(t1.getFlag()));
|
||||
// }
|
||||
// }
|
||||
// Map<String, Object> map=new HashMap<>();
|
||||
// map.put("base64data",result.getData());
|
||||
// map.put("printflag",batchPrintVOList);
|
||||
// return new Result("0","打印成功!",map);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user