| @ -0,0 +1,170 @@ | |||||
| package com.hxhq.business.utils.pdf.resource; | |||||
| import com.fasterxml.jackson.databind.JsonNode; | |||||
| import com.fasterxml.jackson.databind.ObjectMapper; | |||||
| import com.hxhq.business.domain.GyzjJcgj; | |||||
| import com.hxhq.business.domain.GyzjTz; | |||||
| import com.hxhq.business.dto.gyzj.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.List; | |||||
| import java.util.*; | |||||
| /** | |||||
| * 给药制剂详情 | |||||
| * | |||||
| * @author tanfei | |||||
| */ | |||||
| public class Dosage { | |||||
| private static final Logger logger = LoggerFactory.getLogger(Dosage.class.getName()); | |||||
| /** | |||||
| * 导出 | |||||
| * | |||||
| * @param gyzj | |||||
| * @return | |||||
| */ | |||||
| public String exportDetail(DetailDto gyzj, List<GyzjTz> gyzjTzList, List<GyzjJcgj> gyzjJcgjList, 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()), null); | |||||
| // 基本信息 | |||||
| PdfBaseUtil.addUnderlinedTitle(document, "基本信息", 10,true); | |||||
| Map<String, String> formData1 = new LinkedHashMap<>(); | |||||
| formData1.put("名称", gyzj.getMc()); | |||||
| formData1.put("编号", gyzj.getBh()); | |||||
| formData1.put("浓度", gyzj.getNd() + gyzj.getNddw()); | |||||
| formData1.put("库存量", gyzj.getKc() + gyzj.getKcdw()); | |||||
| formData1.put("失效日期", sdf.format(gyzj.getSxrq())); | |||||
| formData1.put("存储条件", gyzj.getCctj()); | |||||
| formData1.put("存储位置", gyzj.getCcwz()); | |||||
| PdfBaseUtil.addFormTableColumns(document, formData1, 2); | |||||
| // 表单信息 | |||||
| PdfBaseUtil.addUnderlinedTitle(document, "表单信息", 10,true); | |||||
| Map<String, String> formData3 = new LinkedHashMap<>(); | |||||
| formData3.put("所属表单", gyzj.getBdMc()); | |||||
| formData3.put("所属试验信息", gyzj.getStudyMc()); | |||||
| formData3.put("表单所属人", gyzj.getBdgsrMc()); | |||||
| PdfBaseUtil.addFormTableColumns(document, formData3, 2); | |||||
| PdfBaseUtil.addUnderlinedTitle(document, "麻精药台账", 10,true); | |||||
| // 台账 | |||||
| addTz(document,gyzjTzList); | |||||
| PdfBaseUtil.addUnderlinedTitle(document, "稽查轨迹", 10,true); | |||||
| //稽查轨迹 | |||||
| addJcgj(document,gyzjJcgjList); | |||||
| 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 gyzjTzList | |||||
| * @throws IOException | |||||
| * @throws DocumentException | |||||
| */ | |||||
| public void addTz(Document document,List<GyzjTz> gyzjTzList) 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 (GyzjTz gyzjTz : gyzjTzList) { | |||||
| // 交替行颜色 | |||||
| if (rowNum % 2 == 0) { | |||||
| table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); | |||||
| } else { | |||||
| table.getDefaultCell().setBackgroundColor(BaseColor.WHITE); | |||||
| } | |||||
| table.addCell(PdfBaseUtil.createCell(gyzjTz.getQmrMc(), contentFont)); | |||||
| table.addCell(PdfBaseUtil.createCell(StringUtils.isNoneBlank(gyzjTz.getLqrMc())?gyzjTz.getLqrMc():""+(StringUtils.isNoneBlank(gyzjTz.getGhrMc())?gyzjTz.getGhrMc():""), contentFont)); | |||||
| table.addCell(PdfBaseUtil.createCell(StringUtils.isNoneBlank(gyzjTz.getFfrMc())?gyzjTz.getFfrMc():""+(StringUtils.isNoneBlank(gyzjTz.getJsrMc())?gyzjTz.getJsrMc():""), contentFont)); | |||||
| table.addCell(PdfBaseUtil.createCell(gyzjTz.getQmyy(), contentFont)); | |||||
| table.addCell(PdfBaseUtil.createCell(gyzjTz.getCzl()+gyzjTz.getCzldw(), contentFont)); | |||||
| table.addCell(PdfBaseUtil.createCell(gyzjTz.getRemark(), contentFont)); | |||||
| table.addCell(PdfBaseUtil.createCell(sdf.format(gyzjTz.getCreateTime()), contentFont)); | |||||
| rowNum++; | |||||
| } | |||||
| document.add(table); | |||||
| } | |||||
| /** | |||||
| * 稽查轨迹 | |||||
| * @param document | |||||
| * @param gyzjJcgjList | |||||
| * @throws IOException | |||||
| * @throws DocumentException | |||||
| */ | |||||
| public void addJcgj(Document document,List<GyzjJcgj> gyzjJcgjList) throws IOException, DocumentException { | |||||
| SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | |||||
| for (GyzjJcgj jcgj : gyzjJcgjList) { | |||||
| PdfBaseUtil.addUnderlinedTitle(document, sdf.format(jcgj.getCreateTime())+" "+jcgj.getJcmc() , 10,false); | |||||
| StringJoiner result = new StringJoiner(", "); | |||||
| if(StringUtils.isNoneBlank(jcgj.getJcnr())){ | |||||
| ObjectMapper mapper = new ObjectMapper(); | |||||
| JsonNode jsonArray = mapper.readTree(jcgj.getJcnr()); | |||||
| for (JsonNode node : jsonArray) { | |||||
| String name = node.get("name").asText(); | |||||
| String value = node.get("value").asText(); | |||||
| result.add(name + ":" + value); | |||||
| } | |||||
| } | |||||
| if(StringUtils.isNoneBlank(jcgj.getRemark())){ | |||||
| result.add("备注:"+jcgj.getRemark()); | |||||
| } | |||||
| if(StringUtils.isNoneBlank(jcgj.getRemark())){ | |||||
| result.add("签名人:"+jcgj.getQmrMc()); | |||||
| } | |||||
| PdfBaseUtil.addUnderlinedTitle(document, result.toString(), 10,false); | |||||
| } | |||||
| } | |||||
| } | |||||