|
@@ -11,6 +11,7 @@ import com.qmth.boot.tools.excel.model.DataMap;
|
|
|
import com.qmth.distributed.print.business.bean.dto.professional.MatrixDto;
|
|
|
import com.qmth.distributed.print.business.bean.dto.professional.MatrixRequirementDto;
|
|
|
import com.qmth.distributed.print.business.bean.dto.professional.MatrixSubRequirementDto;
|
|
|
+import com.qmth.distributed.print.business.bean.dto.professional.ProfessionalExportHead;
|
|
|
import com.qmth.distributed.print.business.entity.TPCourse;
|
|
|
import com.qmth.distributed.print.business.entity.TPMatrix;
|
|
|
import com.qmth.distributed.print.business.entity.TPProfessional;
|
|
@@ -25,14 +26,19 @@ import com.qmth.teachcloud.common.entity.SysUser;
|
|
|
import com.qmth.teachcloud.common.util.ServletUtil;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.hssf.usermodel.*;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -76,8 +82,7 @@ public class TPMatrixServiceImpl extends ServiceImpl<TPMatrixMapper, TPMatrix> i
|
|
|
TPRequirement tpRequirement = tpRequirementService.getById(requirementEntry.getKey());
|
|
|
MatrixRequirementDto matrixRequirementDto = new MatrixRequirementDto();
|
|
|
matrixRequirementDto.setName(tpRequirement.getName());
|
|
|
- // 排序
|
|
|
- List<MatrixSubRequirementDto> matrixSubRequirementDtoList = requirementEntry.getValue().stream().map(tpMatrix -> {
|
|
|
+ List<MatrixSubRequirementDto> matrixSubRequirementDtoList = requirementEntry.getValue().stream().sorted(Comparator.comparing(TPMatrix::getSubName)).map(tpMatrix -> {
|
|
|
MatrixSubRequirementDto matrixSubRequirementDto = new MatrixSubRequirementDto();
|
|
|
matrixSubRequirementDto.setId(tpMatrix.getId());
|
|
|
matrixSubRequirementDto.setName(StringUtils.isNotBlank(tpMatrix.getSubName()) ? tpMatrix.getRequirementSortNum() + "-" + tpMatrix.getSubName() : tpMatrix.getSubName());
|
|
@@ -105,48 +110,131 @@ public class TPMatrixServiceImpl extends ServiceImpl<TPMatrixMapper, TPMatrix> i
|
|
|
|
|
|
@Override
|
|
|
public void downloadMatrix(Long professionalId, HttpServletResponse response) {
|
|
|
- TPProfessional tpProfessional = tpProfessionalService.getById(professionalId);
|
|
|
- List<TPMatrix> tpMatrixList = this.list(new QueryWrapper<TPMatrix>().lambda().eq(TPMatrix::getProfessionalId, professionalId));
|
|
|
+ List<TPMatrix> tpMatrixList = this.baseMapper.listMatrix(professionalId);
|
|
|
if (CollectionUtils.isNotEmpty(tpMatrixList)) {
|
|
|
- List<DataMap> matrixMapList = new ArrayList<>();
|
|
|
- Map<Long, List<TPMatrix>> courseMap = tpMatrixList.stream().collect(Collectors.groupingBy(TPMatrix::getCourseId));
|
|
|
- List<String> columnNameList = Arrays.asList("课程名称");
|
|
|
- // 是否添加表头列
|
|
|
- boolean addColumn = true;
|
|
|
- for (Map.Entry<Long, List<TPMatrix>> entry : courseMap.entrySet()) {
|
|
|
+ TPProfessional tpProfessional = tpProfessionalService.getById(professionalId);
|
|
|
+ // 表头
|
|
|
+ List<ProfessionalExportHead> professionalExportHeadList = new ArrayList<>();
|
|
|
+ // 默认课程
|
|
|
+ professionalExportHeadList.add(new ProfessionalExportHead("", 0, Arrays.asList("")));
|
|
|
+ List<TPRequirement> tpRequirementList = tpRequirementService.listRequirement(professionalId);
|
|
|
+ for (TPRequirement tpRequirement : tpRequirementList) {
|
|
|
+ List<String> subNameList = new ArrayList<>();
|
|
|
+ if (tpRequirement.getNodeCount() == null) {
|
|
|
+ subNameList.add("");
|
|
|
+ } else {
|
|
|
+ for (Integer i = 1; i <= tpRequirement.getNodeCount(); i++) {
|
|
|
+ subNameList.add(tpRequirement.getSortNum() + "-" + i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ professionalExportHeadList.add(new ProfessionalExportHead(tpRequirement.getName(), tpRequirement.getSortNum(), subNameList));
|
|
|
+ }
|
|
|
+
|
|
|
+ // 内容
|
|
|
+ List<List<String>> contentList = new ArrayList<>();
|
|
|
+ LinkedMultiValueMap<Long, TPMatrix> courseMap = new LinkedMultiValueMap<>();
|
|
|
+ for (TPMatrix tpMatrix : tpMatrixList) {
|
|
|
+ courseMap.add(tpMatrix.getCourseId(), tpMatrix);
|
|
|
+ }
|
|
|
+ courseMap.entrySet().stream().forEach(entry -> {
|
|
|
+ List<String> content = new ArrayList<>();
|
|
|
TPCourse tpCourse = tpCourseService.getById(entry.getKey());
|
|
|
- DataMap dataMap = new DataMap();
|
|
|
- dataMap.put("courseName", tpCourse.getCourseName());
|
|
|
- Map<Long, List<TPMatrix>> requirementMap = entry.getValue().stream().collect(Collectors.groupingBy(TPMatrix::getRequirementId));
|
|
|
- for (Map.Entry<Long, List<TPMatrix>> requirementEntry : requirementMap.entrySet()) {
|
|
|
- TPRequirement tpRequirement = tpRequirementService.getById(requirementEntry.getKey());
|
|
|
- // 排序
|
|
|
- boolean finalAddColumn = addColumn;
|
|
|
- requirementEntry.getValue().stream().sorted(Comparator.comparing(TPMatrix::getSubName)).forEach(tpMatrix -> {
|
|
|
- if (finalAddColumn) {
|
|
|
- columnNameList.add(tpMatrix.getSubName());
|
|
|
- }
|
|
|
- dataMap.put(tpMatrix.getSubName(), tpMatrix.getContent() == null ? "" : tpMatrix.getContent().toString());
|
|
|
- });
|
|
|
+ content.add(tpCourse.getCourseName());
|
|
|
+ LinkedMultiValueMap<Long, TPMatrix> requirementMap = new LinkedMultiValueMap<>();
|
|
|
+ for (TPMatrix tpMatrix : entry.getValue()) {
|
|
|
+ requirementMap.add(tpMatrix.getRequirementId(), tpMatrix);
|
|
|
}
|
|
|
- addColumn = false;
|
|
|
+ requirementMap.entrySet().stream().forEach(requirementEntry -> requirementEntry.getValue().stream().forEach(tpMatrix -> content.add(tpMatrix.getContent() == null ? "" : String.valueOf(tpMatrix.getContent()))));
|
|
|
+ contentList.add(content);
|
|
|
+ });
|
|
|
+
|
|
|
+ // 生成excel
|
|
|
+ exportExcel(response, professionalExportHeadList, contentList, tpProfessional.getName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void exportExcel(HttpServletResponse response, List<ProfessionalExportHead> professionalExportHeadList, List<List<String>> contentList, String professionalName) {
|
|
|
+ HSSFWorkbook wb = new HSSFWorkbook();
|
|
|
+ HSSFSheet sheet = wb.createSheet(professionalName);
|
|
|
+
|
|
|
+ // 表对样式
|
|
|
+ HSSFCellStyle styleHead = wb.createCellStyle(); // 样式对象
|
|
|
+ styleHead.setVerticalAlignment(VerticalAlignment.CENTER);// 垂直
|
|
|
+ styleHead.setAlignment(HorizontalAlignment.CENTER);// 水平
|
|
|
+ Font font = wb.createFont();
|
|
|
+ font.setBold(true);
|
|
|
+ //设置字体样式
|
|
|
+ font.setFontHeightInPoints((short) 12);//设置字体大小
|
|
|
+ styleHead.setFont(font);
|
|
|
+
|
|
|
+ //表格第一行(一级表头)
|
|
|
+ HSSFRow row1 = sheet.createRow(0);
|
|
|
+ // 计算一级表头合并单元格数量
|
|
|
+ Integer requirementCount = professionalExportHeadList.stream().filter(m -> m.getSubName() != null).map(m -> m.getSubName().size()).collect(Collectors.summingInt(Integer::intValue));
|
|
|
+
|
|
|
+ for (int i = 0; i < requirementCount; i++) {
|
|
|
+ sheet.autoSizeColumn(i, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, requirementCount));
|
|
|
+ HSSFCell cell0 = row1.createCell(0);
|
|
|
+ cell0.setCellStyle(styleHead);
|
|
|
+ cell0.setCellValue("课程名称");
|
|
|
+ HSSFCell cell1 = row1.createCell(1);
|
|
|
+ cell1.setCellStyle(styleHead);
|
|
|
+ cell1.setCellValue("毕业要求");
|
|
|
+
|
|
|
+ //表格第二行(二级表头)
|
|
|
+ HSSFRow row2 = sheet.createRow(1);
|
|
|
+ HSSFCell cell2;
|
|
|
+ int num = 0;
|
|
|
+ for (int i = 0; i < professionalExportHeadList.size(); i++) {
|
|
|
+ ProfessionalExportHead professionalExportHead = professionalExportHeadList.get(i);
|
|
|
+ num = num + professionalExportHead.getSubName().size();
|
|
|
+ int firstCol = num - professionalExportHead.getSubName().size();
|
|
|
+ int lastCol = firstCol + professionalExportHead.getSubName().size() - 1;
|
|
|
+ cell2 = row2.createCell(firstCol);
|
|
|
+ cell2.setCellStyle(styleHead);
|
|
|
+ cell2.setCellValue(professionalExportHead.getRequirementName());
|
|
|
+ if (firstCol < lastCol) {
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(1, 1, firstCol, lastCol));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //表格第三行(三级表头)
|
|
|
+ HSSFRow row3 = sheet.createRow(2);
|
|
|
+ HSSFCell cell3;
|
|
|
+ AtomicInteger atomicInteger = new AtomicInteger(0);
|
|
|
+ for (ProfessionalExportHead professionalExportHead : professionalExportHeadList) {
|
|
|
+ for (String s : professionalExportHead.getSubName()) {
|
|
|
+ cell3 = row3.createCell(atomicInteger.getAndIncrement());
|
|
|
+ cell3.setCellStyle(styleHead);
|
|
|
+ cell3.setCellValue(s);
|
|
|
}
|
|
|
- String[] columnNames = columnNameList.toArray(new String[0]);
|
|
|
- try {
|
|
|
- log.debug("导出Excel开始...");
|
|
|
- response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(tpProfessional.getName() + "毕业要求支撑矩阵", SystemConstant.CHARSET_NAME) + ".xlsx");
|
|
|
- response.setContentType("application/vnd.ms-excel");
|
|
|
- ServletOutputStream outputStream = response.getOutputStream();
|
|
|
- ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
|
|
|
- writer.writeDataMaps("毕业要求支撑矩阵", null, columnNames, matrixMapList.listIterator());
|
|
|
- writer.output(outputStream);
|
|
|
- outputStream.flush();
|
|
|
- outputStream.close();
|
|
|
- log.debug("导出Excel结束");
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ // 合并课程名称
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(0, 2, 0, 0));
|
|
|
+
|
|
|
+ AtomicInteger i = new AtomicInteger(3);
|
|
|
+ HSSFRow row;
|
|
|
+ for (List<String> list : contentList) {
|
|
|
+ row = sheet.createRow(i.getAndIncrement());
|
|
|
+ HSSFCell cell;
|
|
|
+ for (int j = 0; j < list.size(); j++) {
|
|
|
+ cell = row.createCell(j);
|
|
|
+ cell.setCellValue(list.get(j));
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ ServletOutputStream outputStream = null;
|
|
|
+ try {
|
|
|
+ response.setHeader("Content-Disposition", "inline;filename=" + URLEncoder.encode(professionalName + "毕业要求支撑矩阵", SystemConstant.CHARSET_NAME) + SystemConstant.EXCEL_PREFIX);
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ wb.write(outputStream);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@Override
|