Browse Source

fix: 解决演练模块导出文件图片不展示的问题

zhongwei
liwen 1 year ago
parent
commit
6cbfbeb8ac
  1. 10
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/dto/RehearsalExportDTO.java
  2. 5
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/entity/RehearsalPlanEntity.java
  3. 11
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailVO.java
  4. 10
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalPlanServiceImpl.java
  5. 21
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalRecordServiceImpl.java
  6. 120
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java
  7. BIN
      hzims-service/safeproduct/src/main/resources/template/演练记录表.docx

10
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/dto/RehearsalExportDTO.java

@ -26,11 +26,9 @@ public class RehearsalExportDTO {
private String subject;
@ApiModelProperty("演练计划开始时间")
@ExcelProperty(value = "计划开始时间", index = 2)
private Date scheduledStartTime;
@ApiModelProperty("演练计划结束时间")
@ExcelProperty(value = "计划结束时间", index = 3)
private Date scheduledEndTime;
@ApiModelProperty("演练地点")
@ -40,4 +38,12 @@ public class RehearsalExportDTO {
@ApiModelProperty("演练状态")
@ExcelProperty(value = "演练状态", index = 5)
private String rehearsalStatus;
@ApiModelProperty("计划开始时间")
@ExcelProperty(value = "计划开始时间", index = 2)
private String startTime;
@ApiModelProperty("计划结束时间")
@ExcelProperty(value = "计划结束时间", index = 3)
private String endTime;
}

5
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/entity/RehearsalPlanEntity.java

@ -7,6 +7,7 @@ import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.tool.utils.DateUtil;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
@ -33,12 +34,12 @@ public class RehearsalPlanEntity extends BaseEntity {
private String subject;
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = DateUtil.PATTERN_DATE)
@ApiModelProperty("演练计划开始时间")
private Date scheduledStartTime;
@NotNull
@JsonFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = DateUtil.PATTERN_DATE)
@ApiModelProperty("演练计划结束时间")
private Date scheduledEndTime;

11
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/RehearsalRecordDetailVO.java

@ -1,10 +1,12 @@
package com.hnac.hzims.safeproduct.vo;
import cn.afterturn.easypoi.entity.ImageEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.Date;
import java.util.List;
/**
* @author liwen
@ -52,4 +54,13 @@ public class RehearsalRecordDetailVO {
@ApiModelProperty("演练科目")
private String subject;
@ApiModelProperty("图片列表")
private List<ImageEntity> images;
@ApiModelProperty("年份")
private String year;
@ApiModelProperty("月份")
private String month;
}

10
hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalPlanServiceImpl.java

@ -21,6 +21,7 @@ import com.hnac.hzims.safeproduct.vo.RehearsalMonthVO;
import com.hnac.hzims.safeproduct.vo.RehearsalYearVO;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -45,8 +46,7 @@ import java.util.stream.Collectors;
* @date 2023-12-27
*/
@Service
public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, RehearsalPlanEntity>
implements IRehearsalPlanService {
public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, RehearsalPlanEntity> implements IRehearsalPlanService {
@Autowired
IRehearsalRecordService rehearsalRecordService;
@ -190,8 +190,9 @@ public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, R
String startTime = String.valueOf(param.get("scheduledStartTime"));
String endTime = String.valueOf(param.get("scheduledEndTime"));
List<RehearsalExportDTO> rehearsalList = getRehearsalByUnitAndDate(unit, startTime, endTime);
// 处理演练状态信息
// 数据处理
rehearsalList.forEach(record -> {
// 演练状态
String status;
if (record.getRehearsalStatus().equals(RehearsalStatusEnum.WAITING.getValue())) {
status = RehearsalStatusEnum.WAITING.getDesc();
@ -201,6 +202,9 @@ public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, R
status = RehearsalStatusEnum.FINISHED.getDesc();
}
record.setRehearsalStatus(status);
// 时间格式
record.setStartTime(DateUtil.format(record.getScheduledStartTime(), DateUtil.PATTERN_DATE));
record.setEndTime(DateUtil.format(record.getScheduledEndTime(), DateUtil.PATTERN_DATE));
});
// 设置响应头
// URLEncoder.encode防止中文乱码

21
hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/RehearsalRecordServiceImpl.java

@ -1,5 +1,6 @@
package com.hnac.hzims.safeproduct.service.impl;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.hutool.core.date.DatePattern;
import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@ -18,6 +19,7 @@ import com.hnac.hzims.safeproduct.vo.RehearsalRecordDetailVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
@ -43,6 +45,7 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
/**
* 新增演练
*/
@Transactional(rollbackFor = Exception.class)
@Override
public boolean saveRehearsalRecord(RehearsalRecordEntity rehearsalRecord) {
// 获取当月时间
@ -132,7 +135,23 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
@Override
public void exportRehearsalRecordData(Long id, HttpServletResponse response) {
RehearsalRecordDetailVO rehearsalRecordDetailVO = this.getRecordDetailVOById(id);
// 演练方式字段
// 数据处理
// 图片
if (StringUtils.isNotEmpty(rehearsalRecordDetailVO.getImgPath())) {
String[] imgArr = rehearsalRecordDetailVO.getImgPath().split(",");
List<ImageEntity> list = new ArrayList<>();
for (String img : imgArr) {
ImageEntity imageEntity = new ImageEntity(BaseUtil.imgToByte(savePath +
BaseUtil.downloadFileByUrl(img, savePath)), 180, 150);
list.add(imageEntity);
}
rehearsalRecordDetailVO.setImages(list);
}
// 表头年月
String code = rehearsalRecordDetailVO.getCode();
rehearsalRecordDetailVO.setYear(code.substring(4, 8));
rehearsalRecordDetailVO.setMonth(code.substring(8, 10));
// 演练方式
String rehearsalMethod = rehearsalRecordDetailVO.getRehearsalMethod();
if (rehearsalMethod.equals(RehearsalMethodEnum.MIXED.getValue())) {
rehearsalRecordDetailVO.setRehearsalMethod(RehearsalMethodEnum.MIXED.getDesc());

120
hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java

@ -2,7 +2,7 @@ package com.hnac.hzims.safeproduct.utils;
import cn.afterturn.easypoi.word.WordExportUtil;
import com.hnac.hzims.common.logs.utils.StringUtils;
import com.hnac.hzims.ticket.annotation.DateTimeToMap;
import com.hnac.hzims.safeproduct.constants.SafeProductConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
@ -13,10 +13,12 @@ import org.springblade.core.tool.utils.ObjectUtil;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
/**
* 公用工具类
@ -156,8 +158,8 @@ public class BaseUtil {
* @return map参数
*/
public static Map<String, Object> obj2Map(Object obj) {
Map<String, Object> result = new HashMap<>();
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) {
Map<String, Object> result = new HashMap<>();
List<Field> fieldList = new ArrayList<>();
Class<?> clazz = obj.getClass();
while (clazz != null) {
@ -172,59 +174,81 @@ public class BaseUtil {
} catch (IllegalAccessException e) {
throw new ServiceException("获取属性性出错");
}
if (value instanceof List) {
List<Map<String, Object>> list = ((List<?>) value).stream().map(BaseUtil::obj2Map).collect(Collectors.toList());
result.put(field.getName(),list);
}
else if (value instanceof LocalDateTime) {
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class);
LocalDateTime time = (LocalDateTime) value;
if(ObjectUtil.isEmpty(dateTimeToMap)) {
result.put(field.getName(),time.format(DateUtil.TIME_FORMATTER));
}
else {
result.putAll(LocalDateConvertMap(time,dateTimeToMap.prefix(),dateTimeToMap.split()));
}
if (value instanceof LocalDateTime) {
value = DateUtil.format((LocalDateTime) value, SafeProductConstant.SAFEPRODUCT_EXPORT_DATE);
}
else if (value instanceof Date) {
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class);
Date time = (Date) value;
if (ObjectUtil.isEmpty(dateTimeToMap)) {
result.put(field.getName(),DateUtil.format(time,DateUtil.PATTERN_DATETIME));
}
else {
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"year", time.getYear());
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"month", time.getMonth());
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"day", time.getDay());
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"hour", time.getHours());
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"minuter", time.getMinutes());
result.put(dateTimeToMap.prefix()+dateTimeToMap.split()+"second", time.getSeconds());
}
}
else {
result.put(field.getName(),value);
value = DateUtil.format((Date) value, SafeProductConstant.SAFEPRODUCT_EXPORT_DATE);
}
result.put(field.getName(),value);
});
return result;
}
return null;
return result;
}
/**
* 图片转字节
* @param tempImgPath 图片路径
* @return 图片字节
*/
public static byte[] imgToByte(String tempImgPath) {
File file = new File(tempImgPath);
byte[] buffer = null;
try {
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
byte[] b = new byte[1000];
int n;
while ((n = fis.read(b)) != -1) {
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
} catch (IOException e) {
log.error(e.getMessage());
}
//删除临时文件
file.delete();
return buffer;
}
/**
* 将时间按格式转化为map对象
* @param dateTime 待转换时间
* @param prefix 转换格式Key前缀
* @param split 转换格式Key分隔符
* @return map对象
* 下载文件
* @param fileUrl 文件路径
* @param downloadFileDir 下载路径
* @return 下载文件路径
*/
public static Map<String, Object> LocalDateConvertMap(LocalDateTime dateTime,String prefix,String split) {
return new HashMap<String, Object>() {{
put(prefix + split + "year", dateTime.getYear());
put(prefix + split + "month", dateTime.getMonthValue());
put(prefix + split + "day", dateTime.getDayOfMonth());
put(prefix + split + "hour", dateTime.getHour());
put(prefix + split + "minuter", dateTime.getMinute());
put(prefix + split + "second", dateTime.getSecond());
}};
public static String downloadFileByUrl(String fileUrl, String downloadFileDir){
URL url;
String fileName = null;
try {
url = new URL(fileUrl);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == 200) {
InputStream inputStream = connection.getInputStream();
int lastSlashIndex = fileUrl.lastIndexOf("/");
if (lastSlashIndex > 0) {
fileName = fileUrl.substring(lastSlashIndex+1);
String filePath = downloadFileDir + fileName;
File file = new File(filePath);
if (file.exists()){
file.delete();
}
OutputStream outputStream = Files.newOutputStream(file.toPath());
// 将文件流拷贝到本地处理
IOUtils.copy(inputStream, outputStream);
} else {
throw new ServiceException("下载文件路径异常:" + downloadFileDir);
}
}
} catch (Exception e) {
throw new ServiceException("文件图片下载失败!");
}
return fileName;
}
}

BIN
hzims-service/safeproduct/src/main/resources/template/演练记录表.docx

Binary file not shown.
Loading…
Cancel
Save