package com.ltkj.web.controller.his; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.draw.LineSeparator; import com.ltkj.common.core.domain.AjaxResult; import com.ltkj.common.core.domain.entity.SysDictData; import com.ltkj.common.utils.DateUtils; import com.ltkj.common.utils.StringUtils; import com.ltkj.framework.config.MatchUtils; import com.ltkj.hosp.domain.*; import com.ltkj.hosp.service.*; import com.ltkj.system.service.ISysDictDataService; import com.ltkj.system.service.ISysUserService; import com.ltkj.web.config.pdfutils.PdfUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; 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; import java.io.*; import java.util.*; import java.util.List; import java.util.stream.Collectors; @Slf4j @RestController @RequestMapping("/his/getData") public class TestGetController { @Autowired private HisApiGetMethodService apiGetMethodService; @Autowired private ITjOrderService tjOrderService; @Autowired private ISysDictDataService dictDataService; @Autowired private ITjReportTemplateService reportTemplateService; @Autowired private ITjCustomerService tjCustomerService; @Autowired private IDictCompService dictCompService; @Autowired private ITjProjectService tjProjectService; @Autowired private ITjOrderDetailService tjOrderDetailService; @Autowired private ITjReportService tjReportService; @Autowired private ITjBigPositiveService tjBigPositiveService; @Autowired private ITjProjectService projectService; @Autowired private ISysUserService userService; @Autowired private ITjOrderRemarkService tjOrderRemarkService; @Autowired private ITjStandardService tjStandardService; @PostMapping("test") public AjaxResult test(@RequestBody String json){ HashMap pdfInfoMap = new HashMap<>(); JSONObject jsonObject = JSONUtil.parseObj(json); HashMap hashMap = new HashMap<>(); // 2.4.2 ----- 2.4.7 // 由前面的接口生成 20240604000160 hashMap.put("his_registration_id",jsonObject.getStr("his_registration_id")); hashMap.put("ksbm","0101"); // 由前面的接口生成 20220427000227 hashMap.put("pationid",jsonObject.getStr("pationid")); hashMap.put("ksrq",jsonObject.getStr("ksrq")); hashMap.put("jsrq",jsonObject.getStr("jsrq")); hashMap.put("pagecount",jsonObject.getInt("pagecount")); hashMap.put("page",jsonObject.getInt("page")); // 获取检验报告记录信息 AjaxResult getlabreportinfo = apiGetMethodService.getHISDataNew("Getlabreportinfo", hashMap); log.info("getlabreportinfo ->{}",getlabreportinfo); if (getlabreportinfo.get("code").toString().equals("200")){ makePDFInfo(pdfInfoMap,getlabreportinfo,"检验报告记录信息"); List> data = (List>) getlabreportinfo.get("data"); Map resultMap = data.get(0); String jybgid = (String) resultMap.get("JYBGID"); // 获取检验结果记录信息 hashMap = new HashMap<>(); hashMap.put("jybgid",jybgid); AjaxResult getlabdetailinfo = apiGetMethodService.getHISDataNew("Getlabdetailinfo", hashMap); makePDFInfo(pdfInfoMap,getlabdetailinfo,"检验结果记录"); // 获取微生物报告记录信息 AjaxResult getlabgermrepinfo = apiGetMethodService.getHISDataNew("Getlabgermrepinfo", hashMap); makePDFInfo(pdfInfoMap,getlabgermrepinfo,"微生物报告记录"); // 获取微生物药敏信息 AjaxResult getlabgermdetailinfo = apiGetMethodService.getHISDataNew("Getlabgermdetailinfo", hashMap); makePDFInfo(pdfInfoMap,getlabgermdetailinfo,"微生物药敏信息"); // 获取危急值信息 AjaxResult getlaburgentinfo = apiGetMethodService.getHISDataNew("Getlaburgentinfo", hashMap); makePDFInfo(pdfInfoMap,getlaburgentinfo,"危机值信息"); } return AjaxResult.success(); } // TODO 从2.3.1开始到2.4.7获取报告信息 然后生成pdf // his_registration_id 和 ksbm public List> makePDFInfo(HashMap map,AjaxResult ajaxResult,String key){ List> data = null; if (Integer.parseInt(ajaxResult.get("code").toString()) == 200) { data = (List>) ajaxResult.get("data"); List> parent = (List>) map.get(key); if (parent!=null){ parent.addAll(data); }else { map.put(key,data); } } return data; } // hisID /* 2024-06-01 08:00:00 2024-06-11 19:00:00 * 2024013998 * 2024014026 * 2024014066 * 2024014112 * */ @PostMapping("test_all") public AjaxResult testALL(@RequestBody String json){ // pdf信息集合 HashMap pdfInfoMap = new HashMap<>(); JSONObject jsonObject = JSONUtil.parseObj(json); // 第一次请求 必填his_registration_id、ksbm科室编码、ksrq开始日期、jsrq结束日期 // mzorzy业务类型:门诊、住院、体检、其他、cardtype卡类型, 1 电子健康卡 2 身份证 3诊疗卡 4患者ID 5就诊流水号 默认4 患者ID // String hisRegistrationId = jsonObject.getStr("his_registration_id"); // String ksbm = jsonObject.getStr("ksbm"); Map params = BeanUtil.beanToMap(jsonObject); String hisRegistrationId = params.get("his_registration_id").toString(); String ksrq = params.get("ksrq").toString(); String jsrq = params.get("jsrq").toString(); String mzorzy = params.get("mzorzy").toString(); String cardtype = params.get("cardtype").toString(); String ksbm = params.get("ksbm").toString(); String pationid = ""; // 获取检查申请信息 // AjaxResult getexamapplyinfo = apiGetMethodService.getHISDataNew("Getexamapplyinfo", params); List> list; // List> list = makePDFInfo(pdfInfoMap, getexamapplyinfo, "获取检查申请信息"); // if (list != null){ // pationid = list.get(0).get("pationid").toString(); // params.clear(); // // 获取检查报告信息 // params.put("pationid",pationid); // params.put("his_registration_id",hisRegistrationId); // params.put("page",1); // params.put("pagecount",10); // AjaxResult getexamreportinfo = apiGetMethodService.getHISDataNew("Getexamreportinfo", params); // makePDFInfo(pdfInfoMap,getexamreportinfo,"获取检查报告信息"); // }else { // return AjaxResult.error("未获取到检查申请信息"); // } // params.clear(); // 获取检查危急值信息 // params.put("his_registration_id",hisRegistrationId); // params.put("ksrq",ksrq); // params.put("jsrq",jsrq); // params.put("pagecount",10); // params.put("page",1); // AjaxResult getexamurgentinfo = apiGetMethodService.getHISDataNew("Getexamurgentinfo", params); // makePDFInfo(pdfInfoMap,getexamurgentinfo,"获取检查危急值信息"); // 获取检验申请信息 // params.clear(); // params.put("mzorzy",mzorzy); // params.put("ksbm",ksbm); // params.put("cardtype",cardtype); // params.put("his_registration_id",hisRegistrationId); // AjaxResult getlabapplyinfo = apiGetMethodService.getHISDataNew("Getlabapplyinfo", params); // makePDFInfo(pdfInfoMap,getlabapplyinfo,"获取检验申请信息"); // 获取检验报告记录信息 params.clear(); params.put("his_registration_id",hisRegistrationId); params.put("ksbm",ksbm); params.put("pationid",pationid); params.put("ksrq",ksrq); params.put("jsrq",jsrq); params.put("pagecount",10); params.put("page",1); AjaxResult getlabreportinfo = apiGetMethodService.getHISDataNew("Getlabreportinfo", params); list = makePDFInfo(pdfInfoMap,getlabreportinfo,"获取检验报告记录信息"); JSONArray jsonArray = JSONUtil.createArray(); if (list != null){ params.clear(); for (Map stringObjectMap : list) { JSONObject obj = JSONUtil.createObj(); obj.putOpt("报告记录",stringObjectMap); String jybgid = stringObjectMap.get("JYBGID").toString(); params.put("jybgid",jybgid); // 获取检验结果记录信息 AjaxResult getlabdetailinfo = apiGetMethodService.getHISDataNew("Getlabdetailinfo", params); if (Integer.parseInt(getlabdetailinfo.get("code").toString()) == 200) { List> data = (List>) getlabdetailinfo.get("data"); obj.putOpt("检验结果记录", data); } // 获取微生物报告记录信息 AjaxResult getlabgermrepinfo = apiGetMethodService.getHISDataNew("Getlabgermrepinfo", params); if (Integer.parseInt(getlabgermrepinfo.get("code").toString()) == 200) { List> data = (List>) getlabgermrepinfo.get("data"); obj.putOpt("微生物报告记录", data); } // 获取微生物药敏信息 AjaxResult getlabgermdetailinfo = apiGetMethodService.getHISDataNew("Getlabgermdetailinfo", params); if (Integer.parseInt(getlabgermdetailinfo.get("code").toString()) == 200) { List> data = (List>) getlabgermdetailinfo.get("data"); obj.putOpt("微生物药敏信息", data); } // 获取危急值信息 AjaxResult getlaburgentinfo = apiGetMethodService.getHISDataNew("Getlaburgentinfo", params); if (Integer.parseInt(getlaburgentinfo.get("code").toString()) == 200) { List> data = (List>) getlaburgentinfo.get("data"); obj.putOpt("危机值信息", data); } jsonArray.put(obj); } // System.out.println(JSONUtil.toJsonStr(pdfInfoMap)); System.out.println(JSONUtil.toJsonStr(jsonArray)); // 返回中文字段替换 // {"获取检验报告记录信息":[{"床位号":"3","报告类别代码":"null","门诊住院标志":"1","申请日期时间":"2024-05-22T10:38:00","西医诊断代码":"","病房号":"","检验报告单号":"240522000899","报告单类别":"微生物检验报告单","报告医生代码":"02172","门诊急诊号":"0","申请医生姓名":"袁小海","检验报告 ID":"240522000899","患者姓名":"张海涛","档案保存机构名称":"泾川县人民医院","档案保存机构地址":"泾川县人民医院","医疗机构代码":"43899007-X","个人标识号":"20240522000046","电话号码":"15294040899","采样日期时间":"2024-05-22T11:09:16.51","修改标志":"0","打印日期":"2024-06-03T10:28:28.743","文件链接":"-","审核人员姓名":"王惠惠","申请日期":"2024-05-22T10:38:00","记录医生代码":"02172","检验项目名称":"普通细菌培养+药敏(痰)","审核人员代码":"02172","患者类型代码":"1","标本类型":"痰液","审核人员姓名":"王芳","标本状态":"已采样","标本代码":"null","检验项目代码":"600","报告科室代码":"0016","报告科室名称":"生化室","检验报告结果":"已审","检验备注":"-","报告日期":"2024-06-03T10:28:28.743","报告医生姓名":"王惠惠","申请机构名称":"泾川县人民医院","记录医生姓名":"王芳","申请机构代码":"43899007-X","送检时间":"2024-06-03T10:28:20.203","档案保存机构代码":"43899007-X","行数":"1","年龄":"82","申请科室名称":"内七科","标本编号":"2024060302","申请科室代码":"0072","患者科室名称":"内七科","检验样本编号":"yl2024052200000622","报告医生代码":"02188","申请医生代码":"00123"},{"床位号":"4","报告类别代码":"null","门诊住院标志":"1","申请日期时间":"2024-06-02T09:56:00","西医诊断代码":"","病房号":"","检验报告单号":"240602000236","报告单类别":"微生物检验报告单","报告医生代码":"03089","门诊急诊号":"0","申请医生姓名":"蔺芳芳","检验报告 ID":"240602000236","患者姓名":"张海涛","档案保存机构名称":"泾川县人民医院","档案保存机构地址":"泾川县人民医院","医疗机构代码":"43899007-X","个人标识号":"20240522000046","电话号码":"15294040899","采样日期时间":"2024-06-02T09:58:50","修改标志":"0","打印日期":"2024-06-03T08:34:15.843","文件链接":"-","审核人员姓名":"鲁红娟","申请日期":"2024-06-02T09:56:00","记录医生代码":"03089","检验项目名称":"普通细菌培养+药敏(痰)","审核人员代码":"03089","患者类型代码":"1","标本类型":"痰液","审核人员姓名":"王芳","标本状态":"未采样","标本代码":"null","检验项目代码":"600","报告科室代码":"0016","报告科室名称":"生化室","检验报告结果":"已审","检验备注":"-","报告日期":"2024-06-03T08:34:15.843","报告医生姓名":"鲁红娟","申请机构名称":"泾川县人民医院","记录医生姓名":"王芳","申请机构代码":"43899007-X","送检时间":"2024-06-03T08:34:14.26","档案保存机构代码":"43899007-X","行数":"2","年龄":"82","申请科室名称":"内七科","标本编号":"2024060301","申请科室代码":"0072","患者科室名称":"内七科","检验样本编号":"yl2024060200000185","报告医生代码":"02188","申请医生代码":"00337"}]} // TODO 生成PDF 展示信息 PDF每项信息不换新页 直接连续生成 后期pdf会有排序 比如先输出心电图再是血常规等等... // TODO 不再使用makePDFInfo方法 // 用户信息 // select o.*,c.* from tj_order o,tj_customer c where o.user_id = c.cus_id and o.card_id = 'hisID'; // makePDF(hisRegistrationId); makePDF(hisRegistrationId,jsonArray); return AjaxResult.success(); }else { return AjaxResult.error("未获取到检验报告记录信息"); } } public AjaxResult makePDF(String cardId,JSONArray jsonArray){ LambdaQueryWrapper tjOrderLambdaQueryWrapper = new LambdaQueryWrapper<>(); tjOrderLambdaQueryWrapper.eq(TjOrder::getCardId,cardId); TjOrder tjOrder = tjOrderService.getOne(tjOrderLambdaQueryWrapper); String tjNumber = tjOrder.getTjNumber(); if (null != tjOrder){ LambdaQueryWrapper tjReportTemplateLambdaQueryWrapper = new LambdaQueryWrapper<>(); TjReportTemplate reportTemplate = null; if (tjOrder.getTjCategory() != null) { //查询字典 LambdaQueryWrapper wqq = new LambdaQueryWrapper<>(); wqq.eq(SysDictData::getDictType, "dict_tjtype"); wqq.eq(SysDictData::getDictValue, tjOrder.getTjCategory()); SysDictData one = dictDataService.getOne(wqq); if (one != null) { //查询字典 LambdaQueryWrapper wqq1 = new LambdaQueryWrapper<>(); wqq1.eq(SysDictData::getDictType, "report_template_type"); wqq1.eq(SysDictData::getDictLabel, one.getDictLabel()); SysDictData one1 = dictDataService.getOne(wqq1); if (one1 != null) { tjReportTemplateLambdaQueryWrapper.eq(TjReportTemplate::getFlag, one1.getDictValue()); } else { tjReportTemplateLambdaQueryWrapper.eq(TjReportTemplate::getFlag, 1); } } else { tjReportTemplateLambdaQueryWrapper.eq(TjReportTemplate::getFlag, 1); } tjReportTemplateLambdaQueryWrapper.eq(TjReportTemplate::getOpen, 0); reportTemplate = reportTemplateService.getOne(tjReportTemplateLambdaQueryWrapper); } else { tjReportTemplateLambdaQueryWrapper.eq(TjReportTemplate::getOpen, 0); reportTemplate = reportTemplateService.list(tjReportTemplateLambdaQueryWrapper).get(2); } String template = reportTemplate.getTemplate(); // 生成模版 byte[] decodedBytes = Base64.getDecoder().decode(template); try (FileOutputStream fos = new FileOutputStream("/Users/chacca/开发相关/代码/template.pdf")) { // 将字节数组写入文件 fos.write(decodedBytes); System.out.println("PDF file saved successfully."); } catch (IOException e) { e.printStackTrace(); System.out.println("Failed to save PDF file."); } LambdaQueryWrapper wq1 = new LambdaQueryWrapper<>(); wq1.eq(TjCustomer::getCusId, tjOrder.getUserId()); TjCustomer tjCustomer = tjCustomerService.getOne(wq1); Long userId = tjCustomer.getCusId(); // 生成pdf try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); PdfReader reader = new PdfReader(new ByteArrayInputStream(Base64.getDecoder().decode(template))); PdfStamper pdfStamper = new PdfStamper(reader, byteArrayOutputStream); // 获取pdf表单 AcroFields acroFields = pdfStamper.getAcroFields(); BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); acroFields.addSubstitutionFont(bf); HashMap data = new HashMap<>(); // 设置用户信息 setUserInfo(tjNumber,tjOrder,tjCustomer,data); //异常项目 // 遍历data 给pdf表单表格赋值 for (String key : data.keySet()) { acroFields.setField(key, data.get(key).toString()); } //设置为无法编辑 pdfStamper.setFormFlattening(true); pdfStamper.close(); reader.close(); // 将修改后的PDF内容写入 byte[] bytes = byteArrayOutputStream.toByteArray(); // 创建新的PDF文档 Document document = new Document(PageSize.A4); ByteArrayOutputStream finalOutPut = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(document, finalOutPut); document.open(); reader = new PdfReader(bytes); int numberOfPages = reader.getNumberOfPages(); for (int i = 1; i <= numberOfPages; i++) { PdfImportedPage page = copy.getImportedPage(reader, i); copy.addPage(page); } // 添加详情页 document.newPage(); ByteArrayOutputStream byteArrayOutputStream1 = new ByteArrayOutputStream(); Document document1 = new Document(PageSize.A4); PdfWriter pdfWriter = PdfWriter.getInstance(document1, byteArrayOutputStream1); document1.setMargins(70, 70, 40, 40); document1.open(); for (Object o : jsonArray) { JSONObject jsonObject = (JSONObject) o; JSONObject reportInfo = jsonObject.getJSONObject("报告记录"); String titleName = reportInfo.getStr("JYXMMC").trim(); JSONArray reportDetails = jsonObject.getJSONArray("检验结果记录"); ArrayList list = new ArrayList<>(); for (Object reportDetail : reportDetails) { JSONObject detail = (JSONObject) reportDetail; TjPdfVO tjPdfVO = new TjPdfVO(); tjPdfVO.setProName(detail.getStr("JCZBMC")); tjPdfVO.setProResult(detail.getStr("JCZBJG")); tjPdfVO.setCompany(detail.getStr("JLDW")); tjPdfVO.setStandardValue(detail.getStr("CKZFW")); list.add(tjPdfVO); } String[] titles = {"体检项目", "体检结果", "单位", "参考范围"}; float[] titlesWidth = {120, 120, 120, 120}; addTable(document1,titleName,titles,titlesWidth,list); // 主检医师 String bgysqm = reportInfo.getStr("BGYSQM"); String doctorName = "主检医师:"+bgysqm; Font doctorFonts = PdfUtils.setFont(9); Paragraph df = PdfUtils.setParagraph(doctorFonts, doctorName); df.setAlignment(Element.ALIGN_RIGHT); document1.add(df); //分割线 LineSeparator objectName = new LineSeparator(); document1.add(objectName); } document1.close(); pdfWriter.close(); reader = new PdfReader(byteArrayOutputStream1.toByteArray()); int numberOfPages1 = reader.getNumberOfPages(); for (int i = 1; i <= numberOfPages1 ; i++) { PdfImportedPage page = copy.getImportedPage(reader, i); copy.addPage(page); } byteArrayOutputStream1.close(); document.close(); copy.close(); String outputPath1 = "/Users/chacca/开发相关/代码/"; String outputFileName1 = userId + tjNumber + tjCustomer.getCusName() + "_报告.pdf"; File file = new File(outputPath1 + outputFileName1); BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); outputStream.write(finalOutPut.toByteArray()); outputStream.flush(); outputStream.close(); finalOutPut.close(); reader.close(); } catch (IOException | DocumentException e) { throw new RuntimeException(e); } return AjaxResult.success(); }else { return AjaxResult.error("未查询到体检信息"); } } /** * 设置用户信息 * @param tjNumber * @param tjOrder * @param tjCustomer * @param data */ private void setUserInfo(String tjNumber, TjOrder tjOrder, TjCustomer tjCustomer, Map data) { //填充性别:判断男女 Long cusSex = tjCustomer.getCusSex(); String sex; String name1; String tjtype; if (cusSex == 0) { sex = "男"; name1 = tjCustomer.getCusName() + "先生"; } else { sex = "女"; name1 = tjCustomer.getCusName() + "女士"; } data.put("name", tjCustomer.getCusName()); data.put("cusId", tjCustomer.getCusId()); data.put("name1", name1); if ("1".equals(tjOrder.getTjType())) { tjtype = "团队"; data.put("tjtype", tjtype); } else { tjtype = "个人"; data.put("tjtype", tjtype); } data.put("dstype", "统一"); data.put("sex", sex); data.put("age", DateUtil.ageOfNow(tjCustomer.getCusBrithday())); data.put("tjnumber", tjNumber); data.put("tjdate", DateUtils.parseDateToStr("yyyy/MM/dd", tjOrder.getCreateTime())); data.put("phone", tjCustomer.getCusPhone()); data.put("idcard", MatchUtils.hideIdCardNum(tjCustomer.getCusIdcard())); data.put("num2", 0); data.put("num3", 4); } private static void addTable(Document document,String titleName,String[] titles,float[] titlesWidth,ArrayList list) throws DocumentException { Font titleFont = PdfUtils.setFont(9); Paragraph paragraph = new Paragraph(titleName, titleFont); // 设置文字居中 paragraph.setAlignment(Element.ALIGN_LEFT); // 行间距 // paragraph.setLeading(5f); // 设置段落上空白 paragraph.setSpacingBefore(10f); // 设置段落下空白 paragraph.setSpacingAfter(10f); document.add(paragraph); // 创建一个表格并添加到文档 Font headFont = PdfUtils.setFont(9); Font textFont = PdfUtils.setFont(9); PdfPTable table = new PdfPTable(titlesWidth); table.setTotalWidth(PageSize.A4.getWidth() - 100); // 设置表格总宽度 table.setLockedWidth(true); // 锁定表格宽度 for (String title : titles) { PdfPCell cell = new PdfPCell(new Paragraph(title, headFont)); cell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框 table.addCell(cell); } for (TjPdfVO item : list) { PdfPCell nameCell = new PdfPCell(new Paragraph(StrUtil.isNotBlank(item.getProName()) && !item.getProName().equals("null")?item.getProName():"", textFont)); nameCell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框 table.addCell(nameCell); PdfPCell resultCell = new PdfPCell(new Paragraph(StrUtil.isNotBlank(item.getProResult()) && !item.getProResult().equals("null") ?item.getProResult():"", textFont)); resultCell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框 table.addCell(resultCell); PdfPCell companyCell = new PdfPCell(new Paragraph(StrUtil.isNotBlank(item.getCompany()) && !item.getCompany().equals("null") ?item.getCompany():"", textFont)); companyCell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框 table.addCell(companyCell); PdfPCell standardValueCell = new PdfPCell(new Paragraph(StrUtil.isNotBlank(item.getStandardValue()) && !item.getStandardValue().equals("null") ?item.getStandardValue():"", textFont)); standardValueCell.setBorder(Rectangle.NO_BORDER); // 移除单元格边框 table.addCell(standardValueCell); } // 将表格添加到文档 document.add(table); } }