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; private String subject;
@ApiModelProperty("演练计划开始时间") @ApiModelProperty("演练计划开始时间")
@ExcelProperty(value = "计划开始时间", index = 2)
private Date scheduledStartTime; private Date scheduledStartTime;
@ApiModelProperty("演练计划结束时间") @ApiModelProperty("演练计划结束时间")
@ExcelProperty(value = "计划结束时间", index = 3)
private Date scheduledEndTime; private Date scheduledEndTime;
@ApiModelProperty("演练地点") @ApiModelProperty("演练地点")
@ -40,4 +38,12 @@ public class RehearsalExportDTO {
@ApiModelProperty("演练状态") @ApiModelProperty("演练状态")
@ExcelProperty(value = "演练状态", index = 5) @ExcelProperty(value = "演练状态", index = 5)
private String rehearsalStatus; 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.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity; import org.springblade.core.mp.base.BaseEntity;
import org.springblade.core.tool.utils.DateUtil;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -33,12 +34,12 @@ public class RehearsalPlanEntity extends BaseEntity {
private String subject; private String subject;
@NotNull @NotNull
@JsonFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = DateUtil.PATTERN_DATE)
@ApiModelProperty("演练计划开始时间") @ApiModelProperty("演练计划开始时间")
private Date scheduledStartTime; private Date scheduledStartTime;
@NotNull @NotNull
@JsonFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = DateUtil.PATTERN_DATE)
@ApiModelProperty("演练计划结束时间") @ApiModelProperty("演练计划结束时间")
private Date scheduledEndTime; 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; package com.hnac.hzims.safeproduct.vo;
import cn.afterturn.easypoi.entity.ImageEntity;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Data; import lombok.Data;
import java.util.Date; import java.util.Date;
import java.util.List;
/** /**
* @author liwen * @author liwen
@ -52,4 +54,13 @@ public class RehearsalRecordDetailVO {
@ApiModelProperty("演练科目") @ApiModelProperty("演练科目")
private String subject; 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 com.hnac.hzims.safeproduct.vo.RehearsalYearVO;
import org.springblade.core.log.exception.ServiceException; import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Query; import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -45,8 +46,7 @@ import java.util.stream.Collectors;
* @date 2023-12-27 * @date 2023-12-27
*/ */
@Service @Service
public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, RehearsalPlanEntity> public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, RehearsalPlanEntity> implements IRehearsalPlanService {
implements IRehearsalPlanService {
@Autowired @Autowired
IRehearsalRecordService rehearsalRecordService; IRehearsalRecordService rehearsalRecordService;
@ -190,8 +190,9 @@ public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, R
String startTime = String.valueOf(param.get("scheduledStartTime")); String startTime = String.valueOf(param.get("scheduledStartTime"));
String endTime = String.valueOf(param.get("scheduledEndTime")); String endTime = String.valueOf(param.get("scheduledEndTime"));
List<RehearsalExportDTO> rehearsalList = getRehearsalByUnitAndDate(unit, startTime, endTime); List<RehearsalExportDTO> rehearsalList = getRehearsalByUnitAndDate(unit, startTime, endTime);
// 处理演练状态信息 // 数据处理
rehearsalList.forEach(record -> { rehearsalList.forEach(record -> {
// 演练状态
String status; String status;
if (record.getRehearsalStatus().equals(RehearsalStatusEnum.WAITING.getValue())) { if (record.getRehearsalStatus().equals(RehearsalStatusEnum.WAITING.getValue())) {
status = RehearsalStatusEnum.WAITING.getDesc(); status = RehearsalStatusEnum.WAITING.getDesc();
@ -201,6 +202,9 @@ public class RehearsalPlanServiceImpl extends ServiceImpl<RehearsalPlanMapper, R
status = RehearsalStatusEnum.FINISHED.getDesc(); status = RehearsalStatusEnum.FINISHED.getDesc();
} }
record.setRehearsalStatus(status); record.setRehearsalStatus(status);
// 时间格式
record.setStartTime(DateUtil.format(record.getScheduledStartTime(), DateUtil.PATTERN_DATE));
record.setEndTime(DateUtil.format(record.getScheduledEndTime(), DateUtil.PATTERN_DATE));
}); });
// 设置响应头 // 设置响应头
// URLEncoder.encode防止中文乱码 // 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; package com.hnac.hzims.safeproduct.service.impl;
import cn.afterturn.easypoi.entity.ImageEntity;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import com.alibaba.excel.util.CollectionUtils; import com.alibaba.excel.util.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 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 lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -43,6 +45,7 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
/** /**
* 新增演练 * 新增演练
*/ */
@Transactional(rollbackFor = Exception.class)
@Override @Override
public boolean saveRehearsalRecord(RehearsalRecordEntity rehearsalRecord) { public boolean saveRehearsalRecord(RehearsalRecordEntity rehearsalRecord) {
// 获取当月时间 // 获取当月时间
@ -132,7 +135,23 @@ public class RehearsalRecordServiceImpl extends ServiceImpl<RehearsalRecordMappe
@Override @Override
public void exportRehearsalRecordData(Long id, HttpServletResponse response) { public void exportRehearsalRecordData(Long id, HttpServletResponse response) {
RehearsalRecordDetailVO rehearsalRecordDetailVO = this.getRecordDetailVOById(id); 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(); String rehearsalMethod = rehearsalRecordDetailVO.getRehearsalMethod();
if (rehearsalMethod.equals(RehearsalMethodEnum.MIXED.getValue())) { if (rehearsalMethod.equals(RehearsalMethodEnum.MIXED.getValue())) {
rehearsalRecordDetailVO.setRehearsalMethod(RehearsalMethodEnum.MIXED.getDesc()); 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 cn.afterturn.easypoi.word.WordExportUtil;
import com.hnac.hzims.common.logs.utils.StringUtils; 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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
@ -13,10 +13,12 @@ import org.springblade.core.tool.utils.ObjectUtil;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.file.Files;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* 公用工具类 * 公用工具类
@ -156,8 +158,8 @@ public class BaseUtil {
* @return map参数 * @return map参数
*/ */
public static Map<String, Object> obj2Map(Object obj) { public static Map<String, Object> obj2Map(Object obj) {
Map<String, Object> result = new HashMap<>();
if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) { if (ObjectUtil.isNotEmpty(obj) && null != obj.getClass()) {
Map<String, Object> result = new HashMap<>();
List<Field> fieldList = new ArrayList<>(); List<Field> fieldList = new ArrayList<>();
Class<?> clazz = obj.getClass(); Class<?> clazz = obj.getClass();
while (clazz != null) { while (clazz != null) {
@ -172,59 +174,81 @@ public class BaseUtil {
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
throw new ServiceException("获取属性性出错"); throw new ServiceException("获取属性性出错");
} }
if (value instanceof List) { if (value instanceof LocalDateTime) {
List<Map<String, Object>> list = ((List<?>) value).stream().map(BaseUtil::obj2Map).collect(Collectors.toList()); value = DateUtil.format((LocalDateTime) value, SafeProductConstant.SAFEPRODUCT_EXPORT_DATE);
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()));
}
} }
else if (value instanceof Date) { else if (value instanceof Date) {
DateTimeToMap dateTimeToMap = field.getAnnotation(DateTimeToMap.class); value = DateUtil.format((Date) value, SafeProductConstant.SAFEPRODUCT_EXPORT_DATE);
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);
} }
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 fileUrl 文件路径
* @param prefix 转换格式Key前缀 * @param downloadFileDir 下载路径
* @param split 转换格式Key分隔符 * @return 下载文件路径
* @return map对象
*/ */
public static Map<String, Object> LocalDateConvertMap(LocalDateTime dateTime,String prefix,String split) { public static String downloadFileByUrl(String fileUrl, String downloadFileDir){
return new HashMap<String, Object>() {{ URL url;
put(prefix + split + "year", dateTime.getYear()); String fileName = null;
put(prefix + split + "month", dateTime.getMonthValue()); try {
put(prefix + split + "day", dateTime.getDayOfMonth()); url = new URL(fileUrl);
put(prefix + split + "hour", dateTime.getHour()); HttpURLConnection connection = (HttpURLConnection)url.openConnection();
put(prefix + split + "minuter", dateTime.getMinute()); connection.setConnectTimeout(5000);
put(prefix + split + "second", dateTime.getSecond()); 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