diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java index dd9f32e..dc24bc4 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java @@ -8,7 +8,7 @@ import com.hxhq.business.dto.mjy.ListDto; import com.hxhq.business.form.mjy.*; import com.hxhq.business.service.IMjyJcgjService; import com.hxhq.business.service.IMjyTzService; -import com.hxhq.business.utils.Pdf.PdfExportUtil; +import com.hxhq.business.utils.pdf.PdfExportUtil; import com.hxhq.common.security.annotation.RequiresPermissions; import com.hxhq.common.security.utils.SecurityUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -112,7 +112,7 @@ public class MjyController extends BaseController { mjyTz.setMjyId(id); List mjyTzList = mjyTzService.queryList(mjyTz); return AjaxResult.success(localFilePrefix + PdfExportUtil.export( - "com.hxhq.business.utils.Pdf.Resource.Drug", + "com.hxhq.business.utils.pdf.resource.Drug", "exportDetail", mjyService.queryInfo(id), mjyTzList, diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java deleted file mode 100644 index 03025fd..0000000 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.hxhq.business.utils.Pdf.Resource; - -import com.alibaba.fastjson2.JSONObject; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.hxhq.business.domain.Mjy; -import com.hxhq.business.domain.MjyJcgj; -import com.hxhq.business.domain.MjyTz; -import com.hxhq.business.domain.StudyMethodRead; -import com.hxhq.business.utils.Pdf.PdfBaseUtil; -import com.hxhq.common.core.utils.StringUtils; -import com.hxhq.common.security.utils.SecurityUtils; -import com.hxhq.system.api.domain.SysUser; -import com.hxhq.system.api.model.LoginUser; -import com.itextpdf.text.*; -import com.itextpdf.text.pdf.BaseFont; -import com.itextpdf.text.pdf.PdfPCell; -import com.itextpdf.text.pdf.PdfPTable; -import com.itextpdf.text.pdf.draw.LineSeparator; -import org.apache.http.client.utils.DateUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.FileOutputStream; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.*; -import java.util.List; - -/** - * 麻精药详情 - * - * @author tanfei - */ -public class Drug { - private static final Logger logger = LoggerFactory.getLogger(Drug.class.getName()); - - /** - * 导出 - * - * @param mjy - * @return - */ - public String exportDetail(Mjy mjy, List mjyTzList, List mjyJcgjList, String localFilePath) { - Document document = null; - FileOutputStream fos = null; - SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - String filePath = PdfBaseUtil.getFilePath(localFilePath); - try { - String sign="华西海圻"; - LoginUser loginUser=SecurityUtils.getLoginUser(); - if(loginUser!=null){ - sign=loginUser.getSysUser().getNickName(); - } - document = PdfBaseUtil.init(document, fos, filePath, sign+sdf.format(new Date()), mjy.getMc()); - // 基本信息 - PdfBaseUtil.addUnderlinedTitle(document, "基本信息", 10,true); - Map formData1 = new LinkedHashMap<>(); - formData1.put("名称", mjy.getMc()); - formData1.put("编号", mjy.getBh()); - formData1.put("浓度", mjy.getNd() + mjy.getNddw()); - formData1.put("库存量", mjy.getKc() + mjy.getKcdw()); - formData1.put("失效日期", sdf.format(mjy.getSxrq())); - formData1.put("存储条件", mjy.getCctj()); - formData1.put("存储位置", mjy.getCcwz()); - PdfBaseUtil.addFormTableColumns(document, formData1, 2); - // 表单信息 - PdfBaseUtil.addUnderlinedTitle(document, "表单信息", 10,true); - Map formData3 = new LinkedHashMap<>(); - formData3.put("所属表单", ""); - formData3.put("所属试验信息", mjy.getMdIds()); - formData3.put("表单所属人", mjy.getBdId().toString()); - PdfBaseUtil.addFormTableColumns(document, formData3, 2); - PdfBaseUtil.addUnderlinedTitle(document, "麻精药台账", 10,true); - // 9. 表头 - BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); - Font headerFont = new Font(bfChinese, 10, Font.NORMAL); - Font contentFont = new Font(bfChinese, 10, Font.NORMAL); - // 8. 创建表格 - PdfPTable table = new PdfPTable(7); - table.setWidthPercentage(100); - String[] headers = {"使用人", "领取/归还人", "发放/接收人", "操作类型", "操作量", "备注/原因", "操作时间"}; - for (String header : headers) { - PdfPCell cell = new PdfPCell(new Phrase(header, headerFont)); - cell.setHorizontalAlignment(Element.ALIGN_CENTER); - cell.setBackgroundColor(BaseColor.WHITE); - cell.setPadding(8); - cell.setBorderWidth(1); - table.addCell(cell); - } - - // 台账 - int rowNum = 0; - for (MjyTz mjyTz : mjyTzList) { - // 交替行颜色 - if (rowNum % 2 == 0) { - table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); - } else { - table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); - } - table.addCell(PdfBaseUtil.createCell(mjyTz.getQmrMc(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getLqrMc(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getGhrMc(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getQmyy(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getCzl()+mjyTz.getCzldw(), contentFont)); - table.addCell(PdfBaseUtil.createCell(mjyTz.getRemark(), contentFont)); - table.addCell(PdfBaseUtil.createCell(sdf.format(mjyTz.getCreateTime()), contentFont)); - rowNum++; - } - document.add(table); - PdfBaseUtil.addUnderlinedTitle(document, "稽查轨迹", 10,true); - - //稽查轨迹 - for (MjyJcgj mjyJcgj : mjyJcgjList) { - PdfBaseUtil.addUnderlinedTitle(document, sdf.format(mjyJcgj.getCreateTime())+" "+mjyJcgj.getJcmc() , 10,false); - StringJoiner result = new StringJoiner(", "); - ObjectMapper mapper = new ObjectMapper(); - JsonNode jsonArray = mapper.readTree(mjyJcgj.getJcnr()); - for (JsonNode node : jsonArray) { - String name = node.get("name").asText(); - String value = node.get("value").asText(); - result.add(name + ":" + value); - } - if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ - result.add("备注:"+mjyJcgj.getRemark()); - } - if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ - result.add("签名人:"+mjyJcgj.getQmrMc()); - } - PdfBaseUtil.addUnderlinedTitle(document, result.toString(), 10,false); - } - logger.info("生成成功:{}", filePath); - } catch (Exception e) { - logger.error("生成失败", e); - throw new RuntimeException("生成失败: " + e.getMessage()); - } finally { - if (document != null) { - document.close(); - } - if (fos != null) { - try { - fos.close(); - } catch (IOException e) { - logger.error("关闭文件流失败", e); - } - } - } - return filePath; - } - - -} diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java similarity index 93% rename from hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java rename to hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java index 3079ec7..da75c30 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java @@ -1,14 +1,12 @@ -package com.hxhq.business.utils.Pdf; +package com.hxhq.business.utils.pdf; import com.hxhq.common.core.exception.ServiceException; import com.hxhq.common.core.text.Convert; -import com.hxhq.common.core.utils.DateUtils; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.draw.LineSeparator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.File; @@ -90,13 +88,15 @@ public class PdfBaseUtil { if (hasLine) { // 添加下划线 LineSeparator underline = new LineSeparator(linFont); - underline.setPercentage(100); // 宽度占50% - underline.setLineWidth(1); // 线粗 + // 宽度占50% + underline.setPercentage(100); + // 线粗 + underline.setLineWidth(0.5f); underline.setAlignment(Element.ALIGN_LEFT); // 添加到文档 document.add(underline); // 添加一些间距 - document.add(new Paragraph(titleText)); // 空行 + document.add(new Paragraph(titleText)); } } @@ -111,10 +111,14 @@ public class PdfBaseUtil { public static Document init(Document document, FileOutputStream fos, String filePath, String signText, String headerText) { try { // 创建PDF文档 设置文档边距,避免内容遮挡页眉页脚 - float topMargin = 70; // 顶部边距(为页眉留出空间) - float bottomMargin = 60; // 底部边距(为页脚留出空间) - float leftMargin = 50; // 左边距 - float rightMargin = 50; // 右边距 + // 顶部边距(为页眉留出空间) + float topMargin = 70; + // 底部边距(为页脚留出空间) + float bottomMargin = 60; + // 左边距 + float leftMargin = 50; + // 右边距 + float rightMargin = 50; document = new Document(PageSize.A4, leftMargin, rightMargin, topMargin, bottomMargin); @@ -156,7 +160,8 @@ public class PdfBaseUtil { // 设置等宽列 float[] widths = new float[count]; for (int i = 0; i < count; i++) { - widths[i] = 1.0f; // 等宽 + // 等宽 + widths[i] = 1.0f; } table.setWidths(widths); @@ -183,11 +188,13 @@ public class PdfBaseUtil { cellContent.add(new Chunk(com.hxhq.common.core.utils.StringUtils.isBlank(entry.getValue()) ? "" : entry.getValue(), valueFont)); PdfPCell cell = new PdfPCell(cellContent); - cell.setBorder(Rectangle.NO_BORDER); // 无边框 + // 无边框 + cell.setBorder(Rectangle.NO_BORDER); cell.setPadding(4); cell.setHorizontalAlignment(Element.ALIGN_LEFT); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); - cell.setMinimumHeight(20); // 最小高度 + // 最小高度 + cell.setMinimumHeight(20); table.addCell(cell); } else { @@ -371,8 +378,9 @@ public class PdfBaseUtil { float stepY = textHeight + 100; // 添加重复水印 - for (float x = textWidth / 2; x < width; x += stepX) { - for (float y = textHeight / 2; y < height; y += stepY) { + Integer two=2; + for (float x = textWidth / two; x < width; x += stepX) { + for (float y = textHeight / two; y < height; y += stepY) { content.beginText(); content.showTextAligned( Element.ALIGN_CENTER, diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java similarity index 94% rename from hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java rename to hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java index 6d8eab8..679dc40 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java @@ -1,4 +1,4 @@ -package com.hxhq.business.utils.Pdf; +package com.hxhq.business.utils.pdf; import com.hxhq.common.core.exception.ServiceException; import org.slf4j.Logger; @@ -39,7 +39,7 @@ public class PdfExportUtil { public static void main(String[] args) throws Exception { String result = export( - "com.hxhq.business.utils.Pdf.Template.Sp.Sp001", + "com.hxhq.business.utils.pdf.template.sp.Sp001", "exportDetail", "D:/hxhq/uploadPath" ); diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java new file mode 100644 index 0000000..59c3d07 --- /dev/null +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java @@ -0,0 +1,170 @@ +package com.hxhq.business.utils.pdf.resource; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.hxhq.business.domain.Mjy; +import com.hxhq.business.domain.MjyJcgj; +import com.hxhq.business.domain.MjyTz; +import com.hxhq.business.dto.mjy.DetailDto; +import com.hxhq.business.utils.pdf.PdfBaseUtil; +import com.hxhq.common.core.utils.StringUtils; +import com.hxhq.common.security.utils.SecurityUtils; +import com.hxhq.system.api.model.LoginUser; +import com.itextpdf.text.*; +import com.itextpdf.text.pdf.BaseFont; +import com.itextpdf.text.pdf.PdfPCell; +import com.itextpdf.text.pdf.PdfPTable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.List; + +/** + * 麻精药详情 + * + * @author tanfei + */ +public class Drug { + private static final Logger logger = LoggerFactory.getLogger(Drug.class.getName()); + + /** + * 导出 + * + * @param mjy + * @return + */ + public String exportDetail(DetailDto mjy, List mjyTzList, List mjyJcgjList, String localFilePath) { + Document document = null; + FileOutputStream fos = null; + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String filePath = PdfBaseUtil.getFilePath(localFilePath); + try { + String sign="华西海圻"; + LoginUser loginUser=SecurityUtils.getLoginUser(); + if(loginUser!=null){ + sign=loginUser.getSysUser().getNickName(); + } + document = PdfBaseUtil.init(document, fos, filePath, sign+sdf.format(new Date()), mjy.getMc()); + // 基本信息 + PdfBaseUtil.addUnderlinedTitle(document, "基本信息", 10,true); + Map formData1 = new LinkedHashMap<>(); + formData1.put("名称", mjy.getMc()); + formData1.put("编号", mjy.getBh()); + formData1.put("浓度", mjy.getNd() + mjy.getNddw()); + formData1.put("库存量", mjy.getKc() + mjy.getKcdw()); + formData1.put("失效日期", sdf.format(mjy.getSxrq())); + formData1.put("存储条件", mjy.getCctj()); + formData1.put("存储位置", mjy.getCcwz()); + PdfBaseUtil.addFormTableColumns(document, formData1, 2); + // 表单信息 + PdfBaseUtil.addUnderlinedTitle(document, "表单信息", 10,true); + Map formData3 = new LinkedHashMap<>(); + formData3.put("所属表单", mjy.getBdMc()); + formData3.put("所属试验信息", mjy.getStudyMc()); + formData3.put("表单所属人", mjy.getBdgsrMc()); + PdfBaseUtil.addFormTableColumns(document, formData3, 2); + PdfBaseUtil.addUnderlinedTitle(document, "麻精药台账", 10,true); + // 台账 + addTz(document,mjyTzList); + //稽查轨迹 + addJcgj(document,mjyJcgjList); + logger.info("生成成功:{}", filePath); + } catch (Exception e) { + logger.error("生成失败", e); + throw new RuntimeException("生成失败: " + e.getMessage()); + } finally { + if (document != null) { + document.close(); + } + if (fos != null) { + try { + fos.close(); + } catch (IOException e) { + logger.error("关闭文件流失败", e); + } + } + } + return filePath; + } + + /** + * 台账 + * @param document + * @param mjyTzList + * @throws IOException + * @throws DocumentException + */ + public void addTz(Document document,List mjyTzList) throws IOException, DocumentException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + // 9. 表头 + BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); + Font headerFont = new Font(bfChinese, 10, Font.NORMAL); + Font contentFont = new Font(bfChinese, 10, Font.NORMAL); + // 8. 创建表格 + PdfPTable table = new PdfPTable(7); + table.setWidthPercentage(100); + String[] headers = {"使用人", "领取/归还人", "发放/接收人", "操作类型", "操作量", "备注/原因", "操作时间"}; + for (String header : headers) { + PdfPCell cell = new PdfPCell(new Phrase(header, headerFont)); + cell.setHorizontalAlignment(Element.ALIGN_CENTER); + cell.setPadding(8); + cell.setBorderWidth(1); + table.addCell(cell); + } + int rowNum = 0; + for (MjyTz mjyTz : mjyTzList) { + // 交替行颜色 + if (rowNum % 2 == 0) { + table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); + } else { + table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); + } + table.addCell(PdfBaseUtil.createCell(mjyTz.getQmrMc(), contentFont)); + table.addCell(PdfBaseUtil.createCell(StringUtils.isNoneBlank(mjyTz.getLqrMc())?mjyTz.getLqrMc():""+(StringUtils.isNoneBlank(mjyTz.getGhrMc())?mjyTz.getGhrMc():""), contentFont)); + table.addCell(PdfBaseUtil.createCell(StringUtils.isNoneBlank(mjyTz.getFfrMc())?mjyTz.getFfrMc():""+(StringUtils.isNoneBlank(mjyTz.getJsrMc())?mjyTz.getJsrMc():""), contentFont)); + table.addCell(PdfBaseUtil.createCell(mjyTz.getQmyy(), contentFont)); + table.addCell(PdfBaseUtil.createCell(mjyTz.getCzl()+mjyTz.getCzldw(), contentFont)); + table.addCell(PdfBaseUtil.createCell(mjyTz.getRemark(), contentFont)); + table.addCell(PdfBaseUtil.createCell(sdf.format(mjyTz.getCreateTime()), contentFont)); + rowNum++; + } + document.add(table); + PdfBaseUtil.addUnderlinedTitle(document, "稽查轨迹", 10,true); + } + + /** + * 稽查轨迹 + * @param document + * @param mjyJcgjList + * @throws IOException + * @throws DocumentException + */ + public void addJcgj(Document document,List mjyJcgjList) throws IOException, DocumentException { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + for (MjyJcgj mjyJcgj : mjyJcgjList) { + PdfBaseUtil.addUnderlinedTitle(document, sdf.format(mjyJcgj.getCreateTime())+" "+mjyJcgj.getJcmc() , 10,false); + StringJoiner result = new StringJoiner(", "); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonArray = mapper.readTree(mjyJcgj.getJcnr()); + for (JsonNode node : jsonArray) { + String name = node.get("name").asText(); + String value = node.get("value").asText(); + result.add(name + ":" + value); + } + if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ + result.add("备注:"+mjyJcgj.getRemark()); + } + if(StringUtils.isNoneBlank(mjyJcgj.getRemark())){ + result.add("签名人:"+mjyJcgj.getQmrMc()); + } + PdfBaseUtil.addUnderlinedTitle(document, result.toString(), 10,false); + } + } + + +} diff --git a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java similarity index 93% rename from hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java rename to hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java index eeac752..125e2d8 100644 --- a/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java +++ b/hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java @@ -1,8 +1,7 @@ -package com.hxhq.business.utils.Pdf.Template.Sp; +package com.hxhq.business.utils.pdf.template.sp; -import com.hxhq.business.utils.Pdf.PdfBaseUtil; +import com.hxhq.business.utils.pdf.PdfBaseUtil; import com.hxhq.common.security.utils.SecurityUtils; -import com.hxhq.system.api.domain.SysUser; import com.hxhq.system.api.model.LoginUser; import com.itextpdf.text.Document; import org.slf4j.Logger;