|
@@ -0,0 +1,289 @@
|
|
|
+package org.springblade.business.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.lowagie.text.Document;
|
|
|
+import com.lowagie.text.DocumentException;
|
|
|
+import com.lowagie.text.Font;
|
|
|
+import com.lowagie.text.Paragraph;
|
|
|
+import com.lowagie.text.pdf.BaseFont;
|
|
|
+import com.lowagie.text.pdf.PdfPTable;
|
|
|
+import com.lowagie.text.pdf.PdfWriter;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.commons.lang.time.DateUtils;
|
|
|
+import org.springblade.business.entity.TrialDeviceClassification;
|
|
|
+import org.springblade.business.excel.TrialDeviceUseExcel;
|
|
|
+import org.springblade.business.mapper.TrialDeviceClassificationMapper;
|
|
|
+import org.springblade.business.utils.PDFUtil;
|
|
|
+import org.springblade.business.utils.SystemUtils;
|
|
|
+import org.springblade.business.vo.TrialDeviceUsePageVO;
|
|
|
+import org.springblade.business.entity.TrialDeviceUse;
|
|
|
+import org.springblade.business.mapper.TrialDeviceUseMapper;
|
|
|
+import org.springblade.business.service.ITrialDeviceUseService;
|
|
|
+import org.springblade.business.wrapper.TrialDeviceUseWarpper;
|
|
|
+import org.springblade.core.mp.base.BaseServiceImpl;
|
|
|
+import org.springblade.core.mp.support.Condition;
|
|
|
+import org.springblade.core.oss.model.BladeFile;
|
|
|
+import org.springblade.core.tool.utils.BeanUtil;
|
|
|
+import org.springblade.core.tool.utils.DateUtil;
|
|
|
+import org.springblade.core.tool.utils.Func;
|
|
|
+import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springblade.manager.entity.WbsTreePrivate;
|
|
|
+import org.springblade.resource.feign.NewIOSSClient;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+public class TrialDeviceUseServiceImpl extends BaseServiceImpl<TrialDeviceUseMapper, TrialDeviceUse> implements ITrialDeviceUseService {
|
|
|
+
|
|
|
+ private final NewIOSSClient newIOSSClient;
|
|
|
+ private final TrialDeviceClassificationMapper trialDeviceClassificationMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public TrialDeviceUse useDetail(Long id) {
|
|
|
+ return baseMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean useSubmit(TrialDeviceUse obj) {
|
|
|
+ return this.saveOrUpdate(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IPage<TrialDeviceUsePageVO> usePage(IPage<TrialDeviceUse> page, TrialDeviceUsePageVO dto, Long projectId, Long contractId) {
|
|
|
+ //获取设备分类数据
|
|
|
+ List<TrialDeviceClassification> deviceClassificationList = trialDeviceClassificationMapper.selectList(Wrappers.<TrialDeviceClassification>query().lambda().eq(TrialDeviceClassification::getContractId, contractId));
|
|
|
+ //获取当前试验树
|
|
|
+ List<WbsTreePrivate> wbsTreePrivates = baseMapper.selectTrialTreeAll(String.valueOf(projectId), 2);
|
|
|
+
|
|
|
+ QueryWrapper<TrialDeviceUse> queryWrapper = Condition.getQueryWrapper(dto);
|
|
|
+ if (ObjectUtil.isNotEmpty(dto.getDeviceClassId())) {
|
|
|
+ queryWrapper.lambda().eq(TrialDeviceUse::getDeviceClassId, dto.getDeviceClassId());
|
|
|
+ }
|
|
|
+ if (ObjectUtil.isNotEmpty(dto.getQueryValue())) {
|
|
|
+ queryWrapper.lambda().like(TrialDeviceUse::getDeviceName, dto.getDeviceName());
|
|
|
+ queryWrapper.lambda().or().like(TrialDeviceUse::getDeviceModel, dto.getDeviceModel());
|
|
|
+ }
|
|
|
+ if (StringUtils.isNotEmpty(dto.getStartTime()) && StringUtils.isNotEmpty(dto.getEndTime())) {
|
|
|
+ String endTime = dto.getEndTime();
|
|
|
+ endTime = DateUtil.format(DateUtils.addDays(DateUtil.parse(endTime, "yyyy-MM-dd"), 1), "yyyy-MM-dd");
|
|
|
+ queryWrapper.lambda().between(TrialDeviceUse::getStartDate, dto.getStartTime(), endTime);
|
|
|
+ }
|
|
|
+ IPage<TrialDeviceUse> resultPages = this.page(page, queryWrapper.lambda().orderBy(true, true, TrialDeviceUse::getCreateTime));
|
|
|
+ IPage<TrialDeviceUsePageVO> trialDeviceUsePageDTOIPage = TrialDeviceUseWarpper.build().pageVO(resultPages);
|
|
|
+ List<TrialDeviceUsePageVO> records = trialDeviceUsePageDTOIPage.getRecords();
|
|
|
+ for (TrialDeviceUsePageVO record : records) {
|
|
|
+ for (TrialDeviceClassification classification : deviceClassificationList) {
|
|
|
+ if (record.getDeviceClassId().equals(classification.getId())) {
|
|
|
+ record.setDeviceClassName(classification.getClassName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for (WbsTreePrivate wbsTreePrivate : wbsTreePrivates) {
|
|
|
+ if (record.getNodeId().equals(wbsTreePrivate.getPKeyId())) {
|
|
|
+ record.setNodeName(wbsTreePrivate.getNodeName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return trialDeviceUsePageDTOIPage.setRecords(records);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String usePrintPdf(String ids, String projectId, HttpServletResponse response, Long contractId) {
|
|
|
+ try {
|
|
|
+ String fileName = java.net.URLEncoder.encode(DateUtil.time() + ".pdf", "UTF-8");
|
|
|
+ response.reset();
|
|
|
+ response.setHeader("Content-Disposition", "inline; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
|
|
|
+
|
|
|
+ //构造数据
|
|
|
+ List<TrialDeviceUse> pdfData = baseMapper.selectBatchIds(Func.toLongList(ids));
|
|
|
+ //获取设备分类数据
|
|
|
+ List<TrialDeviceClassification> deviceClassificationList = trialDeviceClassificationMapper.selectList(Wrappers.<TrialDeviceClassification>query().lambda().eq(TrialDeviceClassification::getContractId, contractId));
|
|
|
+ //获取当前试验树
|
|
|
+ List<WbsTreePrivate> wbsTreePrivates = baseMapper.selectTrialTreeAll(projectId, 2);
|
|
|
+
|
|
|
+ //删除旧PDF
|
|
|
+ List<String> pdfUrls = pdfData.stream().map(TrialDeviceUse::getPdfUrl).distinct().collect(Collectors.toList());
|
|
|
+ for (String pdfUrl : pdfUrls) {
|
|
|
+ //格式 https: //xxx.com//upload/20221101/xxx.pdf
|
|
|
+ if (StringUtils.isNotEmpty(pdfUrl)) {
|
|
|
+ String pdfName = pdfUrl.split("//")[2].split("/")[2];
|
|
|
+ newIOSSClient.removeFile(pdfName);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //创建文档,设置页面大小、左右上下边距
|
|
|
+ Document document = new Document();
|
|
|
+
|
|
|
+ BaseFont bfChinese = null;
|
|
|
+ String pdfUrl = null;
|
|
|
+ //处理中文显示问题,使用资源字体
|
|
|
+ if (SystemUtils.isWindows()) {
|
|
|
+ //windows
|
|
|
+ pdfUrl = "C:\\pdfFiles\\";
|
|
|
+ File file = new File(pdfUrl);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
|
|
+ }
|
|
|
+ if (SystemUtils.isLinux()) {
|
|
|
+ //linux
|
|
|
+ pdfUrl = "/home/pdfFiles";
|
|
|
+ File file = new File(pdfUrl);
|
|
|
+ if (!file.exists()) {
|
|
|
+ file.mkdirs();
|
|
|
+ }
|
|
|
+ bfChinese = BaseFont.createFont("/usr/share/fonts/my-fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
|
|
+ }
|
|
|
+
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(pdfUrl + fileName);
|
|
|
+
|
|
|
+ //Font dateTitle = new Font(bfChinese, 7, Font.NORMAL);
|
|
|
+ //Font title = new Font(bfChinese, 12, Font.BOLD);//文字加粗
|
|
|
+ Font textFont = new Font(bfChinese, 7, Font.NORMAL);//文字正常
|
|
|
+
|
|
|
+ //设置分页
|
|
|
+ PdfWriter.getInstance(document, fileOutputStream);
|
|
|
+
|
|
|
+ //打开文档
|
|
|
+ document.open();
|
|
|
+
|
|
|
+ //标题
|
|
|
+ PdfPTable tableTitle = new PdfPTable(3);
|
|
|
+ //生成一个13列的表格
|
|
|
+ PdfPTable table = new PdfPTable(13);
|
|
|
+
|
|
|
+ //定义每个单元格的宽度
|
|
|
+ float[] widthsHeaderTitle = {1f, 1f, 1f};
|
|
|
+ float[] widthsHeader = {1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f, 1f};
|
|
|
+
|
|
|
+ float lineHeight = (float) 20.0;
|
|
|
+
|
|
|
+ //设置表格每一格的宽度
|
|
|
+ tableTitle.setWidths(widthsHeaderTitle);
|
|
|
+ table.setWidths(widthsHeader);
|
|
|
+
|
|
|
+ //设置表格总体宽度
|
|
|
+ tableTitle.setWidthPercentage(100);
|
|
|
+ table.setWidthPercentage(100);
|
|
|
+
|
|
|
+ int colSpan = 1;
|
|
|
+
|
|
|
+ /*SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ Date date = new Date(System.currentTimeMillis());
|
|
|
+ //添加标题
|
|
|
+ PDFUtil.createTableCellLeft("导出人:XXX", textFont, tableTitle, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCellCenter("XXX表", title, tableTitle, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCellRight(formatter.format(date), dateTitle, tableTitle, lineHeight, colSpan);*/
|
|
|
+
|
|
|
+ document.add(tableTitle);
|
|
|
+ document.add(new Paragraph("\n"));
|
|
|
+
|
|
|
+ String[] array = {"序号", "设备名称", "设备分类", "设备编号", "设备型号", "出厂编号", "使用日期(起)", "使用日期(止)",
|
|
|
+ "检测试验项目", "样品编号", "样品名称", "使用人", "备注"};
|
|
|
+ for (String s : array) {
|
|
|
+ PDFUtil.createTableCell(s, textFont, table, lineHeight, colSpan);
|
|
|
+ }
|
|
|
+ for (TrialDeviceUse pdfDatum : pdfData) {
|
|
|
+ PDFUtil.createTableCell(String.valueOf(pdfDatum.getId()), textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(pdfDatum.getDeviceName(), textFont, table, lineHeight, colSpan);
|
|
|
+ for (TrialDeviceClassification classification : deviceClassificationList) {
|
|
|
+ if (classification.getId().equals(pdfDatum.getDeviceClassId())) {
|
|
|
+ PDFUtil.createTableCell(classification.getClassName(), textFont, table, lineHeight, colSpan);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getDeviceNumber()) ? pdfDatum.getDeviceNumber() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getDeviceModel()) ? pdfDatum.getDeviceModel() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getFactoryNumber()) ? pdfDatum.getFactoryNumber() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getStartDate()) ? pdfDatum.getStartDate().toString() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getEndDate()) ? pdfDatum.getEndDate().toString() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ for (WbsTreePrivate wbsTreePrivate : wbsTreePrivates) {
|
|
|
+ if (pdfDatum.getNodeId().equals(wbsTreePrivate.getPKeyId())) {
|
|
|
+ PDFUtil.createTableCell(wbsTreePrivate.getNodeName(), textFont, table, lineHeight, colSpan);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getSpecificationNumber()) ? pdfDatum.getSpecificationNumber() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getMaterialName()) ? pdfDatum.getMaterialName() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getManagerName()) ? pdfDatum.getManagerName() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ PDFUtil.createTableCell(ObjectUtil.isNotEmpty(pdfDatum.getRemarks()) ? pdfDatum.getRemarks() : "", textFont, table, lineHeight, colSpan);
|
|
|
+ }
|
|
|
+
|
|
|
+ document.add(table);
|
|
|
+ document.close();
|
|
|
+ BladeFile bladeFile;
|
|
|
+ try {
|
|
|
+ document.close();
|
|
|
+ fileOutputStream.close();
|
|
|
+
|
|
|
+ //存储OSS
|
|
|
+ File file = new File(pdfUrl + fileName);
|
|
|
+ String canonicalPath = file.getCanonicalPath();
|
|
|
+ String name = file.getName();
|
|
|
+ bladeFile = newIOSSClient.uploadFile(name, canonicalPath);
|
|
|
+
|
|
|
+ //修改URL
|
|
|
+ for (TrialDeviceUse trialDeviceInfo : pdfData) {
|
|
|
+ trialDeviceInfo.setPdfUrl(bladeFile.getLink());
|
|
|
+ }
|
|
|
+ this.updateBatchById(pdfData);
|
|
|
+
|
|
|
+ //删除本地缓存
|
|
|
+ if (file.exists()) {
|
|
|
+ file.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ return bladeFile.getLink();
|
|
|
+
|
|
|
+ } catch (ClassCastException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (DocumentException | IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean importBatchDeviceUse(List<TrialDeviceUseExcel> list, Long projectId, Long contractId) {
|
|
|
+ //获取当前合同段的设备分类
|
|
|
+ List<TrialDeviceClassification> deviceClassifications = trialDeviceClassificationMapper.selectList(Wrappers.<TrialDeviceClassification>query().lambda().eq(TrialDeviceClassification::getContractId, contractId));
|
|
|
+ //获取当前项目试验树
|
|
|
+ List<WbsTreePrivate> wbsTreePrivates = baseMapper.selectTrialTreeAll(String.valueOf(projectId), 2);
|
|
|
+
|
|
|
+ List<TrialDeviceUse> listData = new ArrayList<>();
|
|
|
+ for (TrialDeviceUseExcel trialDeviceUseExcel : list) {
|
|
|
+ TrialDeviceUse trialDeviceUse = BeanUtil.copyProperties(trialDeviceUseExcel, TrialDeviceUse.class);
|
|
|
+ //设备分类id
|
|
|
+ for (TrialDeviceClassification classification : deviceClassifications) {
|
|
|
+ if (trialDeviceUseExcel.getDeviceClassName().equals(classification.getClassName())) {
|
|
|
+ if (trialDeviceUse != null) {
|
|
|
+ trialDeviceUse.setDeviceClassId(classification.getId());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //检测试验项目id
|
|
|
+ for (WbsTreePrivate wbsTreePrivate : wbsTreePrivates) {
|
|
|
+ if (trialDeviceUseExcel.getNodeName().equals(wbsTreePrivate.getNodeName())) {
|
|
|
+ if (trialDeviceUse != null) {
|
|
|
+ trialDeviceUse.setNodeId(wbsTreePrivate.getId());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ listData.add(trialDeviceUse);
|
|
|
+ }
|
|
|
+ return this.saveBatch(listData, 1000);
|
|
|
+ }
|
|
|
+}
|