Browse Source

fix:[资源库管理]麻精药导出

master
15881625488@163.com 3 months ago
parent
commit
029cc69974
9 changed files with 709 additions and 684 deletions
  1. +2
    -2
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java
  2. +0
    -152
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java
  3. +23
    -15
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java
  4. +2
    -2
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java
  5. +170
    -0
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java
  6. +2
    -3
      hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java

+ 2
- 2
hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/controller/MjyController.java View File

@ -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<MjyTz> 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,

+ 0
- 152
hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Resource/Drug.java View File

@ -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<MjyTz> mjyTzList, List<MjyJcgj> 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<String, String> 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<String, String> 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;
}
}

hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfBaseUtil.java → hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfBaseUtil.java View File

@ -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,

hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/PdfExportUtil.java → hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/PdfExportUtil.java View File

@ -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"
);

+ 170
- 0
hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/resource/Drug.java View File

@ -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<MjyTz> mjyTzList, List<MjyJcgj> 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<String, String> 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<String, String> 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<MjyTz> 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<MjyJcgj> 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);
}
}
}

hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/Pdf/Template/Sp/Sp001.java → hxhq-modules/hxhq-system/src/main/java/com/hxhq/business/utils/pdf/template/sp/Sp001.java View File

@ -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;

Loading…
Cancel
Save