Browse Source

#施工日志

zhongwei
yang_shj 11 months ago
parent
commit
fe00060b45
  1. 21
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/access/service/impl/ConstructionServiceImpl.java
  2. 322
      hzims-service/operational/src/main/java/com/hnac/hzims/operational/util/PdfUtils.java

21
hzims-service/operational/src/main/java/com/hnac/hzims/operational/access/service/impl/ConstructionServiceImpl.java

@ -9,7 +9,6 @@ import com.hnac.hzims.operational.access.service.ConstructionService;
import com.hnac.hzims.operational.access.vo.ConstructionVo; import com.hnac.hzims.operational.access.vo.ConstructionVo;
import com.hnac.hzims.operational.access.wrapper.ConstructionWrapper; import com.hnac.hzims.operational.access.wrapper.ConstructionWrapper;
import com.hnac.hzims.operational.util.ExcelUtil; import com.hnac.hzims.operational.util.ExcelUtil;
import com.hnac.hzims.operational.util.PdfUtils;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.Workbook;
@ -74,11 +73,11 @@ public class ConstructionServiceImpl extends BaseServiceImpl<ConstructionMapper,
//准备数据 //准备数据
ConstructionVo construction = this.record(id); ConstructionVo construction = this.record(id);
Map<String,Object> params = new HashMap<>(); Map<String,Object> params = new HashMap<>();
try { /*try {
params = PdfUtils.objectToMap(construction); params = PdfUtils.objectToMap(construction);
}catch (Exception e) { }catch (Exception e) {
log.error("转换对象失败!"); log.error("转换对象失败!");
} }*/
TemplateExportParams templateExportParams = new TemplateExportParams("template/constructio_template.xlsx", true); TemplateExportParams templateExportParams = new TemplateExportParams("template/constructio_template.xlsx", true);
Workbook workbook = null; Workbook workbook = null;
try { try {
@ -87,19 +86,19 @@ public class ConstructionServiceImpl extends BaseServiceImpl<ConstructionMapper,
e.printStackTrace(); e.printStackTrace();
} }
//上传xlsx至服务器 //上传xlsx至服务器
String fileName = DateUtil.format(construction.getConstructionTime(),DateUtil.PATTERN_DATETIME) + "_" + construction.getOverhaulName() + "_施工日志" + PdfUtils.XLSX_SUFFIX; //String fileName = DateUtil.format(construction.getConstructionTime(),DateUtil.PATTERN_DATETIME) + "_" + construction.getOverhaulName() + "_施工日志" + PdfUtils.XLSX_SUFFIX;
try { /* try {
ExcelUtil.upload(workbook,constructionSaveXlsxPath,fileName); ExcelUtil.upload(workbook,constructionSaveXlsxPath,fileName);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }*/
//将docx文件转换为pdf并保存 //将docx文件转换为pdf并保存
String pdfFileName = DateUtil.format(construction.getConstructionTime(),DateUtil.PATTERN_DATETIME) + "_" + construction.getOverhaulName() + "_施工日志" + PdfUtils.PDF_SUFFIX; //String pdfFileName = DateUtil.format(construction.getConstructionTime(),DateUtil.PATTERN_DATETIME) + "_" + construction.getOverhaulName() + "_施工日志" + PdfUtils.PDF_SUFFIX;
String xlsxFileName = DateUtil.format(construction.getConstructionTime(),DateUtil.PATTERN_DATETIME) + "_" + construction.getOverhaulName() + "_施工日志" + PdfUtils.XLSX_SUFFIX; //String xlsxFileName = DateUtil.format(construction.getConstructionTime(),DateUtil.PATTERN_DATETIME) + "_" + construction.getOverhaulName() + "_施工日志" + PdfUtils.XLSX_SUFFIX;
PdfUtils.convertPdf(constructionSaveXlsxPath, xlsxFileName, constructionSavePdfPath, pdfFileName); //PdfUtils.convertPdf(constructionSaveXlsxPath, xlsxFileName, constructionSavePdfPath, pdfFileName);
String savePath = constructionSavePdfPath + pdfFileName; //String savePath = constructionSavePdfPath + pdfFileName;
// 设置response参数,可以打开下载页面 // 设置response参数,可以打开下载页面
PdfUtils.readPdf(response,savePath); //PdfUtils.readPdf(response,savePath);
} }
/** /**

322
hzims-service/operational/src/main/java/com/hnac/hzims/operational/util/PdfUtils.java

@ -1,322 +0,0 @@
package com.hnac.hzims.operational.util;
import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;
import com.hnac.hzims.ticket.annotation.DefaultValue;
import com.hnac.hzims.ticket.constants.TicketConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.tool.utils.ObjectUtil;
import org.springblade.core.tool.utils.StringUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;
/**
* @author hx
*/
@Slf4j
public class PdfUtils {
/** 文件后缀名 **/
public static String DOC_SUFFIX = ".doc";
public static String DOCX_SUFFIX = ".docx";
public static String XLS_SUFFIX = ".xls";
public static String XLSX_SUFFIX = ".xlsx";
public static String PDF_SUFFIX = ".pdf";
/***
* 各文件转换为pdf
* @param templatePath 待转换的文件路径
* @param templateFileName 待转换的文件名
* @param savePath 转换为pdf后的文件路径
* @param saveFileName 转换为pdf后的文件名称
* @return
*/
public static void convertPdf(String templatePath, String templateFileName, String savePath, String saveFileName) {
String templateFilePath = templatePath + templateFileName;
String saveFilePath = savePath + saveFileName;
log.info("转换前的文件为:{};转换后的路径为:{}", templateFilePath, saveFilePath);
File templateFile = new File(templateFilePath);
File saveFile = new File(saveFilePath);
//获取文件类型
String fileType = templateFileName.substring(templateFileName.lastIndexOf("."), templateFileName.length());
InputStream inputStream = null;
OutputStream outputStream = null;
try {
try {
inputStream = new FileInputStream(templateFile);
outputStream = new FileOutputStream(saveFile);
//document4j 转换 pdf linux环境下不支持
if (DOCX_SUFFIX.equals(fileType)) {
IConverter converter = LocalConverter.builder().build();
converter.convert(inputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
converter.shutDown();
} else if (DOC_SUFFIX.equals(fileType)) {
IConverter converter = LocalConverter.builder().build();
converter.convert(inputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();
converter.shutDown();
} else if (XLS_SUFFIX.equals(fileType)) {
IConverter converter = LocalConverter.builder().build();
converter.convert(inputStream).as(DocumentType.XLS).to(outputStream).as(DocumentType.PDF).execute();
converter.shutDown();
} else if (XLSX_SUFFIX.equals(fileType)) {
//此方式行不通
//converter.convert(inputStream).as(DocumentType.XLSX).to(outputStream).as(DocumentType.PDF).execute();
Workbook wb = new Workbook();
wb.loadFromFile(templateFilePath);
wb.saveToFile(saveFilePath, FileFormat.PDF);
}
} catch (Exception e) {
log.error("转换失败,错误信息为:{}", e.getMessage());
throw new ServiceException(e.getMessage());
} finally {
inputStream.close();
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 对象转化为Map 并设置默认值
*
* @param obj
* @return
* @throws Exception
*/
public static Map<String, Object> objectToMap(Object obj) {
Map<String, Object> result = new HashMap<>();
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) {
Class clazz = obj.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
clazz = clazz.getSuperclass();
}
fieldList.forEach(field -> {
field.setAccessible(true);
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
Object value;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
throw new ServiceException("获取属性性出错");
}
//若为list则不处理
if (value instanceof List) {
return;
}
//若为时间格式则进行格式化
if (value instanceof LocalDateTime) {
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN);
}
if (value instanceof Date) {
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN);
}
//如果value为空直接跳出
// if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){
// return;
// }
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
if (ObjectUtil.isNotEmpty(defaultValue)) {
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(),
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value);
} else {
result.put(field.getName(), Optional.ofNullable(value).orElse(" "));
}
});
}
return result;
}
/**
* 对象转化为Map 并设置默认值
*
* @param obj
* @return
* @throws Exception
*/
public static Map<String, Object> objectToMap(Object obj, boolean isWordFlag) {
Map<String, Object> result = new HashMap<>();
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) {
Class clazz = obj.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
clazz = clazz.getSuperclass();
}
fieldList.forEach(field -> {
field.setAccessible(true);
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
Object value;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
throw new ServiceException("获取属性性出错");
}
//若为list则不处理
if (value instanceof List) {
return;
}
//若为时间格式则进行格式化
if (value instanceof LocalDateTime) {
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN);
}
if (value instanceof Date) {
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN);
}
//如果value为空直接跳出
// if ((ObjectUtils.isEmpty(value) || value == "") && StringUtil.isNoneBlank(field.getName())){
// return;
// }
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
if (ObjectUtil.isNotEmpty(defaultValue)) {
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(),
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value);
} else {
if (isWordFlag) {
//如果导出word为null会出现{{ ,value设置"\u00A0"
result.put(field.getName(), Optional.ofNullable(value).orElse("\u00A0"));
} else {
result.put(field.getName(), Optional.ofNullable(value).orElse(" "));
}
}
});
}
return result;
}
/**
* 对象转化为Map 并设置默认值
*
* @param obj
* @return
* @throws Exception
*/
public static Map<String, Object> objectToMapResult(Object obj, Map<String, Object> map) {
Map<String, Object> result = new HashMap<>();
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) {
Class clazz = obj.getClass();
List<Field> fieldList = new ArrayList<>();
while (clazz != null) {
fieldList.addAll(Arrays.asList(clazz.getDeclaredFields()));
clazz = clazz.getSuperclass();
}
fieldList.forEach(field -> {
field.setAccessible(true);
DefaultValue defaultValue = field.getAnnotation(DefaultValue.class);
Object value;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
throw new ServiceException("获取属性性出错");
}
//若为list则不处理
if (value instanceof List) {
return;
}
//若为时间格式则进行格式化
if (value instanceof LocalDateTime) {
value = DateUtil.format((LocalDateTime) value, TicketConstants.TICKET_DATE_PATTERN);
}
if (value instanceof Date) {
value = DateUtil.format((Date) value, TicketConstants.TICKET_DATE_PATTERN);
}
if (map.containsKey(field.getName())) {
return;
}
//属性上是否加入DefaultValue注解 若加入 则判断是否定义属性名以及值 若未定义则取原属性名及值
if (ObjectUtil.isNotEmpty(defaultValue)) {
result.put(StringUtil.isNoneBlank(defaultValue.name()) ? defaultValue.name() : field.getName(),
ObjectUtil.isNotEmpty(defaultValue.value()) ? defaultValue.value() : value);
} else {
//如果导出word为null会出现{{ ,value设置"\u00A0"
result.put(field.getName(), Optional.ofNullable(value).orElse(""));
}
});
}
return result;
}
/**
* 读取本地pdf,这里设置的是预览
*/
public static void readPdf(HttpServletResponse response, String filePath) {
response.reset();
response.setContentType("application/pdf");
FileInputStream fileInputStream = null;
OutputStream outputStream = null;
try {
File file = new File(filePath);
fileInputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
response.setHeader("Content-Disposition",
"inline; filename= " + URLEncoder.encode(file.getName(), "UTF-8"));
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 读取本地pdf,这里设置的是导出
*/
public static void exportPdf(HttpServletResponse response, String filePath) {
response.reset();
response.setContentType("application/pdf");
FileInputStream fileInputStream = null;
OutputStream outputStream = null;
try {
File file = new File(filePath);
fileInputStream = new FileInputStream(file);
outputStream = response.getOutputStream();
IOUtils.write(IOUtils.toByteArray(fileInputStream), outputStream);
response.setHeader("Content-Disposition", "inline; filename= " + URLEncoder.encode(file.getName(), "UTF-8"));
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Loading…
Cancel
Save