Browse Source

update: 卫生考核表导出功能

zhongwei
liwen 3 months ago
parent
commit
5c3e8ae354
  1. 12
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/HygieneEvaluationExportVO.java
  2. 41
      hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/HygieneEvaluationPeopleExportVO.java
  3. 108
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/HygieneRecordServiceImpl.java
  4. 78
      hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/utils/BaseUtil.java
  5. BIN
      hzims-service/safeproduct/src/main/resources/template/卫生考核表(五周).docx
  6. BIN
      hzims-service/safeproduct/src/main/resources/template/卫生考核表(四周).docx
  7. BIN
      hzims-service/safeproduct/src/main/resources/template/卫生考核表.xlsx

12
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/dto/HygieneEvaluationExportDTO.java → hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/HygieneEvaluationExportVO.java

@ -1,6 +1,5 @@
package com.hnac.hzims.safeproduct.dto; package com.hnac.hzims.safeproduct.vo;
import com.hnac.hzims.safeproduct.vo.HygieneEvaluationPeopleVO;
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;
@ -12,8 +11,8 @@ import java.util.List;
* @date 2024-08-20 * @date 2024-08-20
*/ */
@Data @Data
@ApiModel(value = "卫生考核导出DTO类") @ApiModel(value = "卫生考核导出VO类")
public class HygieneEvaluationExportDTO { public class HygieneEvaluationExportVO {
@ApiModelProperty(value = "年份") @ApiModelProperty(value = "年份")
private String year; private String year;
@ -51,9 +50,6 @@ public class HygieneEvaluationExportDTO {
@ApiModelProperty(value = "第五周结束日") @ApiModelProperty(value = "第五周结束日")
private String fifthWeekEnd; private String fifthWeekEnd;
@ApiModelProperty(value = "备注")
private String remark;
@ApiModelProperty(value = "考核人员列表") @ApiModelProperty(value = "考核人员列表")
private List<HygieneEvaluationPeopleVO> evaluationList; private List<HygieneEvaluationPeopleExportVO> evaluationList;
} }

41
hzims-service-api/safeproduct-api/src/main/java/com/hnac/hzims/safeproduct/vo/HygieneEvaluationPeopleExportVO.java

@ -0,0 +1,41 @@
package com.hnac.hzims.safeproduct.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* @author liwen
* @date 2024-08-22
*/
@Data
@ApiModel(value = "卫生考核人员导出VO类")
public class HygieneEvaluationPeopleExportVO {
@ApiModelProperty(value = "单位")
private String unit;
@ApiModelProperty(value = "负责人")
private String principal;
@ApiModelProperty(value = "合计分")
private Long sumScore;
@ApiModelProperty(value = "第一周考核分")
private Long firstWeekScore;
@ApiModelProperty(value = "第二周考核分")
private Long secondWeekScore;
@ApiModelProperty(value = "第三周考核分")
private Long thirdWeekScore;
@ApiModelProperty(value = "第四周考核分")
private Long fourthWeekScore;
@ApiModelProperty(value = "第五周考核分")
private Long fifthWeekScore;
@ApiModelProperty(value = "备注")
private String remark;
}

108
hzims-service/safeproduct/src/main/java/com/hnac/hzims/safeproduct/service/impl/HygieneRecordServiceImpl.java

@ -1,24 +1,19 @@
package com.hnac.hzims.safeproduct.service.impl; package com.hnac.hzims.safeproduct.service.impl;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DatePattern;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hnac.hzims.common.utils.DateUtil; import com.hnac.hzims.common.utils.DateUtil;
import com.hnac.hzims.safeproduct.constants.SafeProductConstant; import com.hnac.hzims.safeproduct.constants.SafeProductConstant;
import com.hnac.hzims.safeproduct.dto.HygieneEvaluationExportDTO; import com.hnac.hzims.safeproduct.vo.HygieneEvaluationExportVO;
import com.hnac.hzims.safeproduct.dto.HygieneRecordDTO; import com.hnac.hzims.safeproduct.dto.HygieneRecordDTO;
import com.hnac.hzims.safeproduct.entity.HygienePlanEntity; import com.hnac.hzims.safeproduct.entity.HygienePlanEntity;
import com.hnac.hzims.safeproduct.entity.HygieneRecordEntity; import com.hnac.hzims.safeproduct.entity.HygieneRecordEntity;
import com.hnac.hzims.safeproduct.entity.HygieneZoneEntity; import com.hnac.hzims.safeproduct.entity.HygieneZoneEntity;
import com.hnac.hzims.safeproduct.enums.RehearsalStatusEnum; import com.hnac.hzims.safeproduct.enums.RehearsalStatusEnum;
import com.hnac.hzims.safeproduct.hazard.util.ExcelConvertPdf;
import com.hnac.hzims.safeproduct.mapper.HygienePlanMapper; import com.hnac.hzims.safeproduct.mapper.HygienePlanMapper;
import com.hnac.hzims.safeproduct.mapper.HygieneRecordMapper; import com.hnac.hzims.safeproduct.mapper.HygieneRecordMapper;
import com.hnac.hzims.safeproduct.service.IHygieneRecordService; import com.hnac.hzims.safeproduct.service.IHygieneRecordService;
@ -26,9 +21,6 @@ import com.hnac.hzims.safeproduct.service.IHygieneZoneService;
import com.hnac.hzims.safeproduct.utils.BaseUtil; import com.hnac.hzims.safeproduct.utils.BaseUtil;
import com.hnac.hzims.safeproduct.vo.*; import com.hnac.hzims.safeproduct.vo.*;
import com.hnac.hzinfo.exception.HzServiceException; import com.hnac.hzinfo.exception.HzServiceException;
import com.itextpdf.text.DocumentException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
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.api.R; import org.springblade.core.tool.api.R;
@ -42,14 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.DayOfWeek; import java.time.DayOfWeek;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
@ -339,12 +324,11 @@ public class HygieneRecordServiceImpl extends ServiceImpl<HygieneRecordMapper, H
throw new HzServiceException(ResultCode.FAILURE, "无卫生考核数据"); throw new HzServiceException(ResultCode.FAILURE, "无卫生考核数据");
} }
// 数据处理 // 数据处理
HygieneEvaluationExportDTO exportDTO = new HygieneEvaluationExportDTO(); HygieneEvaluationExportVO exportVO = new HygieneEvaluationExportVO();
exportDTO.setRemark(page.getRemark());
// 年份、月份 // 年份、月份
String[] time = month.split("-"); String[] time = month.split("-");
exportDTO.setYear(time[0]); exportVO.setYear(time[0]);
exportDTO.setMonth(time[1]); exportVO.setMonth(time[1]);
// 周数据 // 周数据
List<HygieneWeekVO> weekList = page.getWeekList(); List<HygieneWeekVO> weekList = page.getWeekList();
weekList.forEach(week -> { weekList.forEach(week -> {
@ -354,52 +338,70 @@ public class HygieneRecordServiceImpl extends ServiceImpl<HygieneRecordMapper, H
end = end.substring(end.indexOf("-") + 1); end = end.substring(end.indexOf("-") + 1);
switch (week.getWeekNum()) { switch (week.getWeekNum()) {
case 1: case 1:
exportDTO.setFirstWeekStart(start); exportVO.setFirstWeekStart(start);
exportDTO.setFirstWeekEnd(end); exportVO.setFirstWeekEnd(end);
break; break;
case 2: case 2:
exportDTO.setSecondWeekStart(start); exportVO.setSecondWeekStart(start);
exportDTO.setSecondWeekEnd(end); exportVO.setSecondWeekEnd(end);
break; break;
case 3: case 3:
exportDTO.setThirdWeekStart(start); exportVO.setThirdWeekStart(start);
exportDTO.setThirdWeekEnd(end); exportVO.setThirdWeekEnd(end);
break; break;
case 4: case 4:
exportDTO.setFourthWeekStart(start); exportVO.setFourthWeekStart(start);
exportDTO.setFourthWeekEnd(end); exportVO.setFourthWeekEnd(end);
break; break;
case 5: case 5:
exportDTO.setFifthWeekStart(start); exportVO.setFifthWeekStart(start);
exportDTO.setFifthWeekEnd(end); exportVO.setFifthWeekEnd(end);
break; break;
} }
}); });
// 考核人员数据 // 考核人员数据
exportDTO.setEvaluationList(page.getEvaluationList()); List<HygieneEvaluationPeopleExportVO> evaluationExportList = new ArrayList<>();
// 封装参数 List<HygieneEvaluationPeopleVO> evaluationList = page.getEvaluationList();
Map<String, Object> params = BaseUtil.obj2Map(exportDTO); evaluationList.forEach(x -> {
TemplateExportParams templateParams = new TemplateExportParams("template/卫生考核表.xlsx", true); HygieneEvaluationPeopleExportVO peopleExportVO = new HygieneEvaluationPeopleExportVO();
// 导出 peopleExportVO.setUnit(x.getUnit());
try { peopleExportVO.setPrincipal(x.getPrincipal());
String fileName = URLEncoder.encode("卫生考核表.xlsx", "UTF-8"); peopleExportVO.setSumScore(x.getSumScore());
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); peopleExportVO.setRemark(page.getRemark());
response.setContentType("application/vnd.ms-excel;charset=utf-8"); List<HygieneEvaluationScoreVO> scoreList = x.getScoreList();
Workbook workbook = ExcelExportUtil.exportExcel(templateParams, params); scoreList.forEach(score -> {
Sheet sheet = workbook.getSheetAt(0); Long weekScore = score.getScore();
BaseUtil.mergeCell(0, 0, JSONArray.parseArray(JSON.toJSONString(exportDTO)), "unit").forEach(sheet::addMergedRegion); switch (score.getWeekNum()) {
BaseUtil.mergeCell(0, 7, JSONArray.parseArray(JSON.toJSONString(exportDTO)), "remark").forEach(sheet::addMergedRegion); case 1:
String pdfPath = "D://" + "/卫生考核表" + SafeProductConstant.PDF_SUFFIX; peopleExportVO.setFirstWeekScore(weekScore);
OutputStream outputStream = Files.newOutputStream(Paths.get(pdfPath)); break;
ExcelConvertPdf.excelConvertPdf(workbook, outputStream); case 2:
BaseUtil.readPdf(response, pdfPath); peopleExportVO.setSecondWeekScore(weekScore);
} catch (UnsupportedEncodingException e) { break;
log.error("卫生考核表导出异常-UnsupportedEncodingException:" + e.getMessage()); case 3:
} catch (DocumentException e) { peopleExportVO.setThirdWeekScore(weekScore);
log.error("卫生考核表导出异常-DocumentException:" + e.getMessage()); break;
} catch (IOException e) { case 4:
log.error("卫生考核表导出异常-IOException:" + e.getMessage()); peopleExportVO.setFourthWeekScore(weekScore);
break;
case 5:
peopleExportVO.setFifthWeekScore(weekScore);
break;
}
});
evaluationExportList.add(peopleExportVO);
});
exportVO.setEvaluationList(evaluationExportList);
Map<String, Object> params = BaseUtil.obj2Map(exportVO);
String templateFile;
if (weekList.size() == 4) {
templateFile = "template/卫生考核表(四周)" + SafeProductConstant.DOCX_SUFFIX;
} else {
templateFile = "template/卫生考核表(五周)" + SafeProductConstant.DOCX_SUFFIX;
} }
String wordPath = "D:/" + "/卫生考核表" + SafeProductConstant.DOCX_SUFFIX;
String pdfPath = "D:/" + "/卫生考核表" + SafeProductConstant.PDF_SUFFIX;
BaseUtil.exportDocument(templateFile, params, wordPath, pdfPath, "",response);
} }
/** /**

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

@ -153,6 +153,9 @@ public class BaseUtil {
if ("template/车辆检查表.docx".equals(templatePath)) { if ("template/车辆检查表.docx".equals(templatePath)) {
formatCarCheckRecordTable(xwpfDocument); formatCarCheckRecordTable(xwpfDocument);
} }
if ("template/卫生考核表(四周).docx".equals(templatePath) || "template/卫生考核表(五周).docx".equals(templatePath)) {
formatHygieneEvaluationTable(xwpfDocument, params);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -160,6 +163,44 @@ public class BaseUtil {
} }
/** /**
* 卫生考核表格式处理
* @param xwpfDocument word文件类
* @param params 数据参数
*/
private static void formatHygieneEvaluationTable(XWPFDocument xwpfDocument, Map<String, Object> params) {
// 获取文档中所有的表格
List<XWPFTable> tableList = xwpfDocument.getTables();
if (!tableList.isEmpty()) {
// 循环表格
for (XWPFTable table : tableList) {
// 获取表格所有行数
List<XWPFTableRow> rows = table.getRows();
// 第三行到倒数第三行为需合并区域
for (int row = 3; row < rows.size();) {
XWPFTableCell startCell = rows.get(row).getCell(0);
String startText = startCell.getText();
int index = row + 1;
for (; index < rows.size(); index++) {
XWPFTableCell endCell = rows.get(index).getCell(0);
String endText = endCell.getText();
if (!startText.equals(endText)) {
break;
}
}
mergeCellsVertically(table, 0, row, index - 1);
// 判断周数:为五周时,备注在第9列;为四周时,备注在第8列
if (ObjectUtil.isNotEmpty(params.get("fifthWeekStart"))) {
mergeCellsVertically(table, 8, row, index - 1);
} else {
mergeCellsVertically(table, 7, row, index - 1);
}
row = index;
}
}
}
}
/**
* 车辆检查表格式处理 * 车辆检查表格式处理
* @param xwpfDocument word文件类 * @param xwpfDocument word文件类
*/ */
@ -424,41 +465,4 @@ public class BaseUtil {
throw new ServiceException("标准总分值未全部分配"); throw new ServiceException("标准总分值未全部分配");
} }
} }
/**
*
* @param initRow 起始行数
* @param initColumn 起始列数
* @param data 数据
* @param attribute 合并属性
* @return 单元格列表
*/
public static List<CellRangeAddress> mergeCell(int initRow, int initColumn, com.alibaba.fastjson.JSONArray data, String attribute) {
List<CellRangeAddress> result = Lists.newArrayList();
String initAttribute = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(data.get(0))).getString(attribute);
int mergeCount = 1;
for (int index = 0; index < data.size(); index++) {
if (index == 0) {
continue;
}
com.alibaba.fastjson.JSONObject object = com.alibaba.fastjson.JSONObject.parseObject(JSON.toJSONString(data.get(index)));
if (object.getString(attribute).equals(initAttribute)) {
mergeCount++;
}
else {
if (mergeCount > 1) {
result.add(new CellRangeAddress(initRow, initRow+mergeCount-1, initColumn, initColumn));
}
initRow = initRow+mergeCount;
initAttribute = object.getString(attribute);
mergeCount = 1;
}
if (index == data.size() -1) {
if (mergeCount > 1) {
result.add(new CellRangeAddress(initRow, initRow + mergeCount-1, initColumn, initColumn));
}
}
}
return result;
}
} }

BIN
hzims-service/safeproduct/src/main/resources/template/卫生考核表(五周).docx

Binary file not shown.

BIN
hzims-service/safeproduct/src/main/resources/template/卫生考核表(四周).docx

Binary file not shown.

BIN
hzims-service/safeproduct/src/main/resources/template/卫生考核表.xlsx

Binary file not shown.
Loading…
Cancel
Save