|
@@ -1,498 +1,34 @@
|
|
|
package org.springblade.business.controller;
|
|
|
|
|
|
-import com.aspose.cells.PageSetup;
|
|
|
-import com.aspose.cells.PaperSizeType;
|
|
|
-import com.aspose.cells.PdfSaveOptions;
|
|
|
-import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
-import org.apache.commons.lang.StringUtils;
|
|
|
-import org.apache.poi.ss.usermodel.*;
|
|
|
-import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
-import org.apache.poi.xssf.usermodel.*;
|
|
|
-import org.springblade.business.dto.TrialSummaryMonthlyEditDTO;
|
|
|
-import org.springblade.business.dto.TrialSummaryMonthlyPageDTO;
|
|
|
-import org.springblade.business.entity.TrialSelfInspectionRecord;
|
|
|
-import org.springblade.business.entity.TrialSummaryMonthlyRemarks;
|
|
|
-import org.springblade.common.utils.SnowFlakeUtil;
|
|
|
-import org.springblade.core.log.exception.ServiceException;
|
|
|
-import org.springblade.core.oss.model.BladeFile;
|
|
|
import org.springblade.core.tool.api.R;
|
|
|
-import org.springblade.core.tool.utils.ObjectUtil;
|
|
|
+import org.springblade.manager.entity.TrialSummaryClassificationConfiguration;
|
|
|
import org.springblade.resource.feign.NewIOSSClient;
|
|
|
-import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.jdbc.core.BeanPropertyRowMapper;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
-import org.springframework.mock.web.MockMultipartFile;
|
|
|
-import org.springframework.web.bind.annotation.*;
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import javax.servlet.ServletOutputStream;
|
|
|
-import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.*;
|
|
|
-import java.net.URLDecoder;
|
|
|
-import java.util.*;
|
|
|
import java.util.List;
|
|
|
-import java.util.function.Function;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
@RestController
|
|
|
@AllArgsConstructor
|
|
|
@RequestMapping("/trial/summary")
|
|
|
-@Api(value = "试验、月报汇总", tags = "试验、月报汇总接口")
|
|
|
+@Api(value = "试验汇总", tags = "试验汇总接口")
|
|
|
public class TrialSummaryController {
|
|
|
|
|
|
private final JdbcTemplate jdbcTemplate;
|
|
|
private final NewIOSSClient newIOSSClient;
|
|
|
|
|
|
- @PostMapping("/monthly/page")
|
|
|
+ @PostMapping("/list")
|
|
|
@ApiOperationSupport(order = 1)
|
|
|
- @ApiOperation(value = "月报汇总分页查询", notes = "传入TrialSummaryMonthlyPageDTO")
|
|
|
- public R<IPage<Map<String, Object>>> monthlyPage(@RequestBody TrialSummaryMonthlyPageDTO dto) {
|
|
|
- if (ObjectUtil.isEmpty(dto.getContractId()) || ObjectUtil.isEmpty(dto.getType()) || ObjectUtil.isEmpty(dto.getIds()) || StringUtils.isBlank(dto.getStartTime()) || StringUtils.isBlank(dto.getEndTime())) {
|
|
|
- throw new ServiceException("入参异常");
|
|
|
- }
|
|
|
- List<Object> params = new ArrayList<>();
|
|
|
- StringBuilder sqlString = new StringBuilder("SELECT * FROM u_trial_self_inspection_record WHERE 1=1 AND is_deleted = 0");
|
|
|
- if (ObjectUtil.isNotEmpty(dto.getContractId())) {
|
|
|
- sqlString.append(" AND contract_id = ?");
|
|
|
- params.add(dto.getContractId());
|
|
|
- }
|
|
|
- if (ObjectUtil.isNotEmpty(dto.getType())) {
|
|
|
- sqlString.append(" AND detection_category = ?");
|
|
|
- params.add(dto.getType());
|
|
|
- } else {
|
|
|
- sqlString.append(" AND detection_category = 1");
|
|
|
- }
|
|
|
- if (StringUtils.isNotBlank(dto.getStartTime()) && StringUtils.isNotBlank(dto.getEndTime())) {
|
|
|
- sqlString.append(" AND report_date BETWEEN ? AND ?");
|
|
|
- params.add(dto.getStartTime());
|
|
|
- params.add(dto.getEndTime());
|
|
|
- }
|
|
|
- if (StringUtils.isNotEmpty(dto.getIds())) {
|
|
|
- sqlString.append(" AND node_id in(?)");
|
|
|
- params.add(dto.getIds());
|
|
|
- }
|
|
|
-
|
|
|
- String sqlPage = sqlString.append(" ORDER BY create_time;").toString();
|
|
|
- List<TrialSelfInspectionRecord> resultList = jdbcTemplate.query(
|
|
|
- sqlPage,
|
|
|
- new BeanPropertyRowMapper<>(TrialSelfInspectionRecord.class),
|
|
|
- params.toArray()
|
|
|
- );
|
|
|
-
|
|
|
- Map<String, List<TrialSelfInspectionRecord>> groupTOTrialProjectName = resultList.stream()
|
|
|
- .collect(Collectors.groupingBy(TrialSelfInspectionRecord::getTrialProjectName, LinkedHashMap::new, Collectors.toList()));
|
|
|
-
|
|
|
- int current = dto.getCurrent();
|
|
|
- int size = dto.getSize();
|
|
|
- int start = (current - 1) * size;
|
|
|
- Map<String, List<TrialSelfInspectionRecord>> paginatedMap = groupTOTrialProjectName.entrySet()
|
|
|
- .stream()
|
|
|
- .skip(start)
|
|
|
- .limit(size)
|
|
|
- .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
|
|
|
-
|
|
|
- if (paginatedMap.size() <= 0) {
|
|
|
- return R.data(null);
|
|
|
- }
|
|
|
-
|
|
|
- Set<String> names = paginatedMap.keySet();
|
|
|
- String commaSeparatedQuotedNames = names.stream()
|
|
|
- .map(name -> "'" + name + "'")
|
|
|
- .collect(Collectors.joining(","));
|
|
|
- String sql = "SELECT * FROM u_trial_self_inspection_record WHERE is_deleted = 0 AND contract_id = " + dto.getContractId() + " AND trial_project_name IN (" + commaSeparatedQuotedNames + ")";
|
|
|
- List<TrialSelfInspectionRecord> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(TrialSelfInspectionRecord.class));
|
|
|
- Map<String, List<TrialSelfInspectionRecord>> groupAllTOTrialProjectName = query.stream().collect(Collectors.groupingBy(TrialSelfInspectionRecord::getTrialProjectName));
|
|
|
-
|
|
|
- String sqlRemarks = "SELECT record_id,remarks FROM m_trial_summary_monthly_remarks WHERE contract_id = ?";
|
|
|
- List<TrialSummaryMonthlyRemarks> trialSummaryMonthlyRemarks = jdbcTemplate.query(sqlRemarks, new Object[]{dto.getContractId()}, new BeanPropertyRowMapper<>(TrialSummaryMonthlyRemarks.class));
|
|
|
- Map<Long, TrialSummaryMonthlyRemarks> trialSummaryMonthlyRemarksMaps = trialSummaryMonthlyRemarks.stream().collect(Collectors.toMap(TrialSummaryMonthlyRemarks::getRecordId, Function.identity()));
|
|
|
-
|
|
|
- IPage<Map<String, Object>> page = new Page<>();
|
|
|
- List<Map<String, Object>> result = new LinkedList<>();
|
|
|
-
|
|
|
- for (Map.Entry<String, List<TrialSelfInspectionRecord>> stringListEntry : paginatedMap.entrySet()) {
|
|
|
- String key = stringListEntry.getKey();
|
|
|
- if (ObjectUtil.isEmpty(key)) {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- Map<String, Object> map = new LinkedHashMap<>();
|
|
|
- map.put("trialProjectName", key);
|
|
|
-
|
|
|
- List<TrialSelfInspectionRecord> currentMonthValue = stringListEntry.getValue();
|
|
|
- Map<String, Object> currentMonthValueMap = new HashMap<>();
|
|
|
- Optional<TrialSelfInspectionRecord> minIdRecord = currentMonthValue.stream()
|
|
|
- .min(Comparator.comparingLong(TrialSelfInspectionRecord::getId));
|
|
|
- TrialSelfInspectionRecord minIdRecordOrNull = minIdRecord.orElse(null);
|
|
|
- if (minIdRecordOrNull != null) {
|
|
|
- long recordId = minIdRecordOrNull.getId() + 1; //防止与累加id重复,+1
|
|
|
- currentMonthValueMap.put("recordId", recordId);
|
|
|
- TrialSummaryMonthlyRemarks orDefault = trialSummaryMonthlyRemarksMaps.getOrDefault(recordId, null);
|
|
|
- currentMonthValueMap.put("remarks", ObjectUtil.isNotEmpty(orDefault) ? ObjectUtil.isNotEmpty(orDefault.getRemarks()) ? orDefault.getRemarks() : "" : "");
|
|
|
- }
|
|
|
- currentMonthValueMap.put("qualifiedTotal", currentMonthValue.stream().filter(f -> f.getDetectionResult() == 1).count());
|
|
|
- currentMonthValueMap.put("unQualifiedTotal", currentMonthValue.stream().filter(f -> f.getDetectionResult() == 0).count());
|
|
|
- map.put("currentMonth", currentMonthValueMap);
|
|
|
-
|
|
|
- List<TrialSelfInspectionRecord> totalMonthValue = groupAllTOTrialProjectName.getOrDefault(key, null);
|
|
|
- Map<String, Object> totalMonthValueMap = new HashMap<>();
|
|
|
- Optional<TrialSelfInspectionRecord> minIdRecordAll = totalMonthValue.stream()
|
|
|
- .min(Comparator.comparingLong(TrialSelfInspectionRecord::getId));
|
|
|
- TrialSelfInspectionRecord minIdRecordOrNullAll = minIdRecordAll.orElse(null);
|
|
|
- if (minIdRecordOrNullAll != null) {
|
|
|
- long recordId = minIdRecordOrNullAll.getId() + 2; //防止与本月id重复,+2
|
|
|
- totalMonthValueMap.put("recordId", recordId);
|
|
|
- TrialSummaryMonthlyRemarks orDefault = trialSummaryMonthlyRemarksMaps.getOrDefault(recordId, null);
|
|
|
- totalMonthValueMap.put("remarks", ObjectUtil.isNotEmpty(orDefault) ? ObjectUtil.isNotEmpty(orDefault.getRemarks()) ? orDefault.getRemarks() : "" : "");
|
|
|
- }
|
|
|
- totalMonthValueMap.put("qualifiedTotal", totalMonthValue.stream().filter(f -> f.getDetectionResult() == 1).count());
|
|
|
- totalMonthValueMap.put("unQualifiedTotal", totalMonthValue.stream().filter(f -> f.getDetectionResult() == 0).count());
|
|
|
- map.put("totalMonth", totalMonthValueMap);
|
|
|
-
|
|
|
- result.add(map);
|
|
|
- }
|
|
|
-
|
|
|
- page.setRecords(result);
|
|
|
- page.setSize(size);
|
|
|
- page.setCurrent(current);
|
|
|
- page.setTotal(groupTOTrialProjectName.size());
|
|
|
- page.setPages((page.getTotal() + size - 1) / size);
|
|
|
- return R.data(page);
|
|
|
+ @ApiOperation(value = "汇总分类列表", notes = "")
|
|
|
+ public R<List<TrialSummaryClassificationConfiguration>> list() {
|
|
|
+ return R.data(jdbcTemplate.query("SELECT * FROM m_trial_summary_classification_configuration WHERE is_deleted = 0 AND status = 1 ORDER BY sort,create_time", new BeanPropertyRowMapper<>(TrialSummaryClassificationConfiguration.class)));
|
|
|
}
|
|
|
|
|
|
- @PostMapping("/monthly/edit")
|
|
|
- @ApiOperationSupport(order = 2)
|
|
|
- @ApiOperation(value = "月报汇总编辑备注", notes = "传入TrialSummaryMonthlyEditDTO")
|
|
|
- public R<Object> monthlyEdit(@RequestBody TrialSummaryMonthlyEditDTO dto) {
|
|
|
- if (ObjectUtil.isEmpty(dto.getRecordId()) || ObjectUtil.isEmpty(dto.getContractId())) {
|
|
|
- throw new ServiceException("入参异常");
|
|
|
- }
|
|
|
- if (ObjectUtil.isNotEmpty(dto.getRemarks()) && dto.getRemarks().length() > 2000) {
|
|
|
- throw new ServiceException("备注信息最长2000个字符,请重新输入");
|
|
|
- }
|
|
|
- try {
|
|
|
- String sql = "SELECT COUNT(*) FROM m_trial_summary_monthly_remarks WHERE record_id = ? AND contract_id = ?";
|
|
|
- Integer count = jdbcTemplate.queryForObject(sql, Integer.class, dto.getRecordId(), dto.getContractId());
|
|
|
- int rowCount = (count != null) ? count : 0;
|
|
|
- if (rowCount == 0) {
|
|
|
- String insertSql = "INSERT INTO m_trial_summary_monthly_remarks(id, contract_id, record_id, remarks) VALUES (?, ?, ?, ?)";
|
|
|
- Object[] insertParams = {
|
|
|
- SnowFlakeUtil.getId(),
|
|
|
- dto.getContractId(),
|
|
|
- dto.getRecordId(),
|
|
|
- ObjectUtil.isNotEmpty(dto.getRemarks()) ? dto.getRemarks() : null
|
|
|
- };
|
|
|
- jdbcTemplate.update(insertSql, insertParams);
|
|
|
- } else {
|
|
|
- String updateSql = "UPDATE m_trial_summary_monthly_remarks SET remarks = ? WHERE record_id = ? AND contract_id = ?";
|
|
|
- Object[] updateParams = {
|
|
|
- ObjectUtil.isNotEmpty(dto.getRemarks()) ? dto.getRemarks() : null,
|
|
|
- dto.getRecordId(),
|
|
|
- dto.getContractId()
|
|
|
- };
|
|
|
- jdbcTemplate.update(updateSql, updateParams);
|
|
|
- }
|
|
|
-
|
|
|
- return R.data(200, null, "操作成功");
|
|
|
- } catch (Exception e) {
|
|
|
- return R.fail("操作失败" + e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @PostMapping("/monthly/download")
|
|
|
- @ApiOperationSupport(order = 3)
|
|
|
- @ApiOperation(value = "月报汇总下载", notes = "传入TrialSummaryMonthlyPageDTO")
|
|
|
- public void monthlyDownload(@RequestBody TrialSummaryMonthlyPageDTO dto, HttpServletResponse response) {
|
|
|
- List<Map<String, Object>> records = monthlyPage(dto).getData().getRecords();
|
|
|
- try (Workbook workbook = new XSSFWorkbook()) {
|
|
|
- Sheet sheet = workbook.createSheet("Sheet1");
|
|
|
- Row titleRow_1 = sheet.createRow(0);
|
|
|
- sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 1));
|
|
|
- titleRow_1.setHeight((short) 500);
|
|
|
- setCellValueWithNullCheck(titleRow_1.createCell(0), "", "");
|
|
|
-
|
|
|
- CreationHelper helper = workbook.getCreationHelper();
|
|
|
- XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
|
|
|
- ClientAnchor anchor = helper.createClientAnchor();
|
|
|
- anchor.setCol1(0);
|
|
|
- anchor.setCol2(2);
|
|
|
- anchor.setRow1(0);
|
|
|
- anchor.setRow2(2);
|
|
|
- XSSFSimpleShape simpleShape = xssfDrawing.createSimpleShape((XSSFClientAnchor) anchor);
|
|
|
- simpleShape.setShapeType(ShapeTypes.LINE);
|
|
|
- simpleShape.setLineWidth(0.5);
|
|
|
- simpleShape.setLineStyle(0);
|
|
|
- simpleShape.setLineStyleColor(0, 0, 0);
|
|
|
-
|
|
|
- int leftBottomX = anchor.getDx1();
|
|
|
- int leftBottomY = anchor.getDy1();
|
|
|
- int rightTopX = anchor.getDx2();
|
|
|
- int rightTopY = anchor.getDy2();
|
|
|
- XSSFDrawing xssfDrawingText1 = (XSSFDrawing) sheet.createDrawingPatriarch();
|
|
|
- XSSFClientAnchor textBoxAnchor1 = new XSSFClientAnchor(leftBottomX, leftBottomY, leftBottomX, leftBottomY, 1, 0, 2, 1);
|
|
|
- XSSFTextBox textBox1 = xssfDrawingText1.createTextbox(textBoxAnchor1);
|
|
|
- textBox1.setText("试验组数");
|
|
|
- XSSFDrawing xssfDrawingText2 = (XSSFDrawing) sheet.createDrawingPatriarch();
|
|
|
- XSSFClientAnchor textBoxAnchor2 = new XSSFClientAnchor(rightTopX, rightTopY, rightTopX, rightTopY, 0, 1, 1, 2);
|
|
|
- XSSFTextBox textBox2 = xssfDrawingText2.createTextbox(textBoxAnchor2);
|
|
|
- textBox2.setText("试验项目");
|
|
|
-
|
|
|
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 4));
|
|
|
- setCellValueWithNullCheck(titleRow_1.createCell(2), "承包人", "");
|
|
|
- Row titleRow_2 = sheet.createRow(1);
|
|
|
- titleRow_2.setHeight((short) 500);
|
|
|
- setCellValueWithNullCheck(titleRow_2.createCell(2), "合格数", "");
|
|
|
- setCellValueWithNullCheck(titleRow_2.createCell(3), "不合格数", "");
|
|
|
- setCellValueWithNullCheck(titleRow_2.createCell(4), "备注", "");
|
|
|
-
|
|
|
- int rowNum = 2;
|
|
|
- for (Map<String, Object> record : records) {
|
|
|
- Row currentMonthRow = sheet.createRow(rowNum);
|
|
|
- sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + 1, 0, 0));
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(0), "", record.get("trialProjectName"));
|
|
|
-
|
|
|
- Map<String, Object> currentMonth = (Map<String, Object>) record.get("currentMonth");
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(1), "本月", "");
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(2), "", currentMonth.get("qualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(3), "", currentMonth.get("unQualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(4), "", currentMonth.get("remarks"));
|
|
|
-
|
|
|
- Map<String, Object> totalMonth = (Map<String, Object>) record.get("totalMonth");
|
|
|
- Row totalMonthRow = sheet.createRow(++rowNum);
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(1), "累计", "");
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(2), "", totalMonth.get("qualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(3), "", totalMonth.get("unQualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(4), "", totalMonth.get("remarks"));
|
|
|
-
|
|
|
- rowNum++;
|
|
|
- }
|
|
|
-
|
|
|
- sheet.setColumnWidth(0, 5000);
|
|
|
- sheet.setColumnWidth(1, 5000);
|
|
|
- sheet.setColumnWidth(2, 5000);
|
|
|
- sheet.setColumnWidth(3, 5000);
|
|
|
- sheet.setColumnWidth(4, 5000);
|
|
|
-
|
|
|
- try (ServletOutputStream outputStream = response.getOutputStream();
|
|
|
- ByteArrayOutputStream byteArrayOutputStreamResult = new ByteArrayOutputStream()) {
|
|
|
- String fileName = dto.getStartTime() + "~" + dto.getEndTime() + ".xlsx";
|
|
|
- String decodedFileName = URLDecoder.decode(fileName, "UTF-8");
|
|
|
- response.setHeader("Content-Disposition", "attachment; filename=\"" + decodedFileName + "\"");
|
|
|
- response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
- workbook.write(byteArrayOutputStreamResult);
|
|
|
- byte[] excelBytesResult = byteArrayOutputStreamResult.toByteArray();
|
|
|
- outputStream.write(excelBytesResult);
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- workbook.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void setCellValueWithNullCheck(Cell cell, String defaultValue, Object value) {
|
|
|
- if (ObjectUtil.isNotEmpty(value)) {
|
|
|
- cell.setCellValue(String.valueOf(value));
|
|
|
- } else {
|
|
|
- cell.setCellValue(defaultValue);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @PostMapping("/monthly/print")
|
|
|
- @ApiOperationSupport(order = 4)
|
|
|
- @ApiOperation(value = "月报汇总打印", notes = "传入TrialSummaryMonthlyPageDTO")
|
|
|
- public R<Object> monthlyPrint(@RequestBody TrialSummaryMonthlyPageDTO dto) {
|
|
|
- List<Map<String, Object>> records = monthlyPage(dto).getData().getRecords();
|
|
|
- try (Workbook workbook = new XSSFWorkbook()) {
|
|
|
- Sheet sheet = workbook.createSheet("Sheet1");
|
|
|
- Row titleRow_1 = sheet.createRow(0);
|
|
|
- sheet.addMergedRegion(new CellRangeAddress(0, 1, 0, 1));
|
|
|
- titleRow_1.setHeight((short) 500);
|
|
|
- setCellValueWithNullCheck(titleRow_1.createCell(0), "", "");
|
|
|
-
|
|
|
- CreationHelper helper = workbook.getCreationHelper();
|
|
|
- XSSFDrawing xssfDrawing = (XSSFDrawing) sheet.createDrawingPatriarch();
|
|
|
- ClientAnchor anchor = helper.createClientAnchor();
|
|
|
- anchor.setCol1(0);
|
|
|
- anchor.setCol2(2);
|
|
|
- anchor.setRow1(0);
|
|
|
- anchor.setRow2(2);
|
|
|
- XSSFSimpleShape simpleShape = xssfDrawing.createSimpleShape((XSSFClientAnchor) anchor);
|
|
|
- simpleShape.setShapeType(ShapeTypes.LINE);
|
|
|
- simpleShape.setLineWidth(0.5);
|
|
|
- simpleShape.setLineStyle(0);
|
|
|
- simpleShape.setLineStyleColor(0, 0, 0);
|
|
|
-
|
|
|
- int leftBottomX = anchor.getDx1();
|
|
|
- int leftBottomY = anchor.getDy1();
|
|
|
- int rightTopX = anchor.getDx2();
|
|
|
- int rightTopY = anchor.getDy2();
|
|
|
- XSSFDrawing xssfDrawingText1 = (XSSFDrawing) sheet.createDrawingPatriarch();
|
|
|
- XSSFClientAnchor textBoxAnchor1 = new XSSFClientAnchor(leftBottomX, leftBottomY, leftBottomX, leftBottomY, 1, 0, 2, 1);
|
|
|
- XSSFTextBox textBox1 = xssfDrawingText1.createTextbox(textBoxAnchor1);
|
|
|
- textBox1.setText("试验组数");
|
|
|
- XSSFDrawing xssfDrawingText2 = (XSSFDrawing) sheet.createDrawingPatriarch();
|
|
|
- XSSFClientAnchor textBoxAnchor2 = new XSSFClientAnchor(rightTopX, rightTopY, rightTopX, rightTopY, 0, 1, 1, 2);
|
|
|
- XSSFTextBox textBox2 = xssfDrawingText2.createTextbox(textBoxAnchor2);
|
|
|
- textBox2.setText("试验项目");
|
|
|
-
|
|
|
- sheet.addMergedRegion(new CellRangeAddress(0, 0, 2, 4));
|
|
|
- setCellValueWithNullCheck(titleRow_1.createCell(2), "承包人", "");
|
|
|
- Row titleRow_2 = sheet.createRow(1);
|
|
|
- titleRow_2.setHeight((short) 500);
|
|
|
- setCellValueWithNullCheck(titleRow_2.createCell(2), "合格数", "");
|
|
|
- setCellValueWithNullCheck(titleRow_2.createCell(3), "不合格数", "");
|
|
|
- setCellValueWithNullCheck(titleRow_2.createCell(4), "备注", "");
|
|
|
-
|
|
|
- int rowNum = 2;
|
|
|
- for (Map<String, Object> record : records) {
|
|
|
- Row currentMonthRow = sheet.createRow(rowNum);
|
|
|
- sheet.addMergedRegion(new CellRangeAddress(rowNum, rowNum + 1, 0, 0));
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(0), "", record.get("trialProjectName"));
|
|
|
-
|
|
|
- Map<String, Object> currentMonth = (Map<String, Object>) record.get("currentMonth");
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(1), "本月", "");
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(2), "", currentMonth.get("qualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(3), "", currentMonth.get("unQualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(currentMonthRow.createCell(4), "", currentMonth.get("remarks"));
|
|
|
-
|
|
|
- Map<String, Object> totalMonth = (Map<String, Object>) record.get("totalMonth");
|
|
|
- Row totalMonthRow = sheet.createRow(++rowNum);
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(1), "累计", "");
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(2), "", totalMonth.get("qualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(3), "", totalMonth.get("unQualifiedTotal"));
|
|
|
- setCellValueWithNullCheck(totalMonthRow.createCell(4), "", totalMonth.get("remarks"));
|
|
|
-
|
|
|
- rowNum++;
|
|
|
- }
|
|
|
-
|
|
|
- sheet.setColumnWidth(0, 5000);
|
|
|
- sheet.setColumnWidth(1, 5000);
|
|
|
- sheet.setColumnWidth(2, 5000);
|
|
|
- sheet.setColumnWidth(3, 5000);
|
|
|
- sheet.setColumnWidth(4, 5000);
|
|
|
-
|
|
|
- try (ByteArrayOutputStream byteArrayOutputStreamResult = new ByteArrayOutputStream()) {
|
|
|
- workbook.write(byteArrayOutputStreamResult);
|
|
|
- byte[] excelBytesResult = byteArrayOutputStreamResult.toByteArray();
|
|
|
- MultipartFile multipartFile = convertExcelToPdf(excelBytesResult);
|
|
|
- if (multipartFile != null) {
|
|
|
- BladeFile bladeFile = newIOSSClient.uploadFileByInputStream(multipartFile);
|
|
|
- if (bladeFile != null) {
|
|
|
- return R.data(bladeFile.getLink());
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- workbook.close();
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return R.fail("操作失败");
|
|
|
- }
|
|
|
-
|
|
|
- private MultipartFile convertExcelToPdf(byte[] excelBytes) {
|
|
|
- ByteArrayInputStream byteArrayInputStream = null;
|
|
|
- ByteArrayOutputStream outReport = null;
|
|
|
- try {
|
|
|
- byteArrayInputStream = new ByteArrayInputStream(excelBytes);
|
|
|
- Workbook ss = WorkbookFactory.create(byteArrayInputStream);
|
|
|
- Sheet sheet = ss.getSheetAt(0);
|
|
|
- for (int r = 0; r <= sheet.getLastRowNum(); r++) {
|
|
|
- Row row = sheet.getRow(r);
|
|
|
- if (row != null) {
|
|
|
- for (int c = 0; c < row.getLastCellNum(); c++) {
|
|
|
- Cell cell = row.getCell(c);
|
|
|
- if (cell != null) {
|
|
|
- CellStyle cellStyle = ss.createCellStyle();
|
|
|
- cellStyle.cloneStyleFrom(cell.getCellStyle());
|
|
|
- cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
- cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
- cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
- cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
- cell.setCellStyle(cellStyle);
|
|
|
- CellRangeAddress mergedRegion = getMergedRegion(sheet, r, c);
|
|
|
- if (mergedRegion != null) {
|
|
|
- setMergedRegionBorders(sheet, mergedRegion, cellStyle);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- sheet.setPrintGridlines(false);
|
|
|
- sheet.setFitToPage(true);
|
|
|
- }
|
|
|
-
|
|
|
- outReport = new ByteArrayOutputStream();
|
|
|
- ss.write(outReport);
|
|
|
-
|
|
|
- PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
|
|
|
- pdfSaveOptions.setOnePagePerSheet(true);
|
|
|
-
|
|
|
- com.aspose.cells.Workbook wb = new com.aspose.cells.Workbook(new ByteArrayInputStream(outReport.toByteArray()));
|
|
|
- PageSetup pageSetup = wb.getWorksheets().get(0).getPageSetup();
|
|
|
- pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
|
|
|
-
|
|
|
- ByteArrayOutputStream pdfByteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
- wb.save(pdfByteArrayOutputStream, pdfSaveOptions);
|
|
|
-
|
|
|
- ByteArrayResource resource = new ByteArrayResource(pdfByteArrayOutputStream.toByteArray());
|
|
|
-
|
|
|
- return new MockMultipartFile("file", SnowFlakeUtil.getId() + ".pdf", "application/pdf", resource.getInputStream());
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- } finally {
|
|
|
- if (outReport != null) {
|
|
|
- try {
|
|
|
- outReport.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- if (byteArrayInputStream != null) {
|
|
|
- try {
|
|
|
- byteArrayInputStream.close();
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private void setMergedRegionBorders(Sheet sheet, CellRangeAddress region, CellStyle style) {
|
|
|
- for (int row = region.getFirstRow(); row <= region.getLastRow(); row++) {
|
|
|
- Row currentRow = sheet.getRow(row);
|
|
|
- if (currentRow == null) {
|
|
|
- currentRow = sheet.createRow(row);
|
|
|
- }
|
|
|
- for (int col = region.getFirstColumn(); col <= region.getLastColumn(); col++) {
|
|
|
- Cell currentCell = currentRow.getCell(col);
|
|
|
- if (currentCell == null) {
|
|
|
- currentCell = currentRow.createCell(col);
|
|
|
- }
|
|
|
- currentCell.setCellStyle(style);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- private CellRangeAddress getMergedRegion(Sheet sheet, int row, int col) {
|
|
|
- for (int i = 0; i < sheet.getNumMergedRegions(); i++) {
|
|
|
- CellRangeAddress mergedRegion = sheet.getMergedRegion(i);
|
|
|
- if (mergedRegion.isInRange(row, col)) {
|
|
|
- return mergedRegion;
|
|
|
- }
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
}
|