import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.PdfWriter;
import com.sydata.fmimp.entity.enums.basicdata.SjlxEnum;
import com.sydata.fmimp.entity.enums.stock.FumigationZtEnum;
import com.sydata.fmimp.entity.po.*;
import com.sydata.fmimp.entity.util.CheckVerifyUtil;
import com.sydata.fmimp.stock.infrastructure.enums.CzbzEnum;
import com.sydata.zt.common.common.api.PageResult;
import com.sydata.zt.common.data.bind.annotation.DataBindFieldConvert;
import one.util.streamex.StreamEx;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.ObjectUtils.isNotEmpty;
@Override
public void download(FumigationDTO fumigationDTO, HttpServletResponse response) throws IOException {
//I removed the data query part to be downloaded. If you need to get it according to the business, I will get it myself.
FumigationDowloadVO fumigationDowloadVO = new FumigationDowloadVO();
// Define global font static variables
Font titlefont;
Font headfont;
Font keyfont = null;
Font textfont = null;
Font content = null;
// Maximum width
try {
// Different fonts (defined here as the same font: including different font sizes and different styles)
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
titlefont = new Font(bfChinese, 16, Font.BOLD);
headfont = new Font(bfChinese, 14, Font.BOLD);
keyfont = new Font(bfChinese, 10, Font.BOLD);
textfont = new Font(bfChinese, 15, Font.NORMAL);
content = new Font(bfChinese, 10, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
BaseFont bf;
Font font = null;
try {
//Create font
bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
//Use font and give color
font = new Font(bf,20,Font.BOLD,BaseColor.BLACK);
} catch (Exception e) {
e.printStackTrace();
}
Document document = new Document(new RectangleReadOnly(842F, 595F));
//Set the page margin 60: left margin, 60: right margin, 72: top margin, 72: bottom margin
document.setMargins(60, 60, 72, 72);
try {
PdfWriter writer = PdfWriter.getInstance(document,response.getOutputStream());
//Add page number
writer.setPageEvent(new PdfPageUtil());
//Open the generated pdf file
document.open();
//Set content
Paragraph paragraph = new Paragraph("Fumigation filing receipt",font);
paragraph.setAlignment(1);
//Quoted font
document.add(paragraph);
// Set the column width and number of columns of the table
float[] widths = {25f,25f,25f,25f,25f,25f};
PdfPTable table = new PdfPTable(widths);
table.setSpacingBefore(20f);
// Set the table width to 100%
table.setWidthPercentage(100.0F);
table.setHeaderRows(1);
table.getDefaultCell().setHorizontalAlignment(1);
PdfPCell cell = null;
//The first line
cell = new PdfPCell(new Paragraph("Fumigation registration code",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getXzbm()));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Fumigation filing time",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(fumigationDowloadVO.getSqxzrq())));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Apply for filing unit",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getDwmc(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
//Line 2
cell = new PdfPCell(new Paragraph("Fumigation Work Library Point",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getKdmc(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Person in charge",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getFzr(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Contact Number",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(fumigationDowloadVO.getFzrdh(),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
//The third line
cell = new PdfPCell(new Paragraph("Document Status",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(30);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(shzt(fumigationDowloadVO.getShzt()),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph("Audit time",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString5(fumigationDowloadVO.getShsj()),content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" ",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Paragraph(" ",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
// Set the column width and number of columns of the table
float[] widths2 = {25f,25f,25f,25f,25f,25f};
PdfPTable table2 = new PdfPTable(widths2);
table2.setSpacingBefore(20f);
// Set the table width to 100%
table2.setWidthPercentage(100.0F);
table2.setHeaderRows(1);
table2.getDefaultCell().setHorizontalAlignment(1);
//People List - Line 4
cell = new PdfPCell(new Paragraph("Name",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(20);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("Position",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("Professional Qualification",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("Physical condition",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("Fumigation Task Division",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
cell = new PdfPCell(new Paragraph("Whether to outsource",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table2.addCell(cell);
//People list data - line 5
if(fumigationDowloadVO.getProples().size() > 0){
for (RecordFumigationPeople prople : fumigationDowloadVO.getProples()) {
PdfPCell cell1 = new PdfPCell(new Paragraph(prople.getXm(), content));
PdfPCell cell2 = new PdfPCell(new Paragraph(prople.getZw(), content));
PdfPCell cell3 = new PdfPCell(new Paragraph(prople.getZyzg(), content));
PdfPCell cell4 = new PdfPCell(new Paragraph(prople.getStzk(), content));
PdfPCell cell5 = new PdfPCell(new Paragraph(prople.getXzrwfg(), content));
PdfPCell cell6 = new PdfPCell(new Paragraph(prople.getSfwb(), content));
//Cell alignment
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setFixedHeight(20);
//Cell vertical alignment
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell6.setHorizontalAlignment(Element.ALIGN_CENTER);
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
table2.addCell(cell1);
table2.addCell(cell2);
table2.addCell(cell3);
table2.addCell(cell4);
table2.addCell(cell5);
table2.addCell(cell6);
}
}
// Set the column width and number of columns of the table
float[] widths3 = {25f,25f,25f,25f,25f};
PdfPTable table3 = new PdfPTable(widths3);
table3.setSpacingBefore(20f);
// Set the table width to 100%
table3.setWidthPercentage(100.0F);
table3.setHeaderRows(1);
table3.getDefaultCell().setHorizontalAlignment(1);
//Implementation of grain storage information
cell = new PdfPCell(new Paragraph("Store House",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(20);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("Cargo space",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("Grain and food",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("Planning fumigation start time",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
cell = new PdfPCell(new Paragraph("Planned fumigation end time",content));
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table3.addCell(cell);
if(fumigationDowloadVO.getDtls().size() > 0){
for (RecordFumigationDtlVO dtl : fumigationDowloadVO.getDtls()) {
PdfPCell cell1 = new PdfPCell(new Paragraph(dtl.getCfmc(), content));
PdfPCell cell2 = new PdfPCell(new Paragraph(dtl.getHwmc(), content));
PdfPCell cell3 = new PdfPCell(new Paragraph(dtl.getLspzmc(), content));
PdfPCell cell4 = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(dtl.getJhxzksrq()), content));
PdfPCell cell5 = new PdfPCell(new Paragraph(CheckVerifyUtil.dateToString4(dtl.getJhxzjsrq()), content));
//Set centered
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell1.setFixedHeight(20);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
table3.addCell(cell1);
table3.addCell(cell2);
table3.addCell(cell3);
table3.addCell(cell4);
table3.addCell(cell5);
}
}
document.add(new Paragraph("\n"));
document.add(new Paragraph("▋ Basic Information",content));
document.add(new Paragraph("\n"));
document.add(table);
document.add(new Paragraph("\n"));
document.add(new Paragraph("▋ Basic Information",content));
document.add(new Paragraph("\n"));
document.add(table2);
document.add(new Paragraph("\n"));
document.add(new Paragraph("▋ Fumigation operation grain storage situation",content));
document.add(new Paragraph("\n"));
document.add(table3);
//Close the document
document.close();
} catch (DocumentException e) {
e.printStackTrace();
log.error("Export pdf failed: {}",e);
}
}