fix:修复产能报表问题

main
HuangHuiKang 3 days ago
parent f6aad2752e
commit c1967dde73

@ -229,6 +229,7 @@ public class DeviceProductionDailyReportServiceImpl implements DeviceProductionD
Workbook workbook = WorkbookFactory.create(inputStream); Workbook workbook = WorkbookFactory.create(inputStream);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
setReportColumnWidths(sheet);
log.info("[production-daily-report] template loaded: path={}, sheetName={}, mergedRegions={}", log.info("[production-daily-report] template loaded: path={}, sheetName={}, mergedRegions={}",
TEMPLATE_PATH, sheet.getSheetName(), sheet.getMergedRegions()); TEMPLATE_PATH, sheet.getSheetName(), sheet.getMergedRegions());
String dateText = "日期:" + reqVO.getReportDate().format(DATE_FORMATTER); String dateText = "日期:" + reqVO.getReportDate().format(DATE_FORMATTER);
@ -252,7 +253,7 @@ public class DeviceProductionDailyReportServiceImpl implements DeviceProductionD
} }
DeviceProductionDailyReportRespVO.DeviceCapacityItem item = items.get(index); DeviceProductionDailyReportRespVO.DeviceCapacityItem item = items.get(index);
setCellValue(row, 0, index + 1); setCellValue(row, 0, index + 1);
setCellValue(row, 1, StrUtil.blankToDefault(item.getDeviceCode(), item.getDeviceName())); setCellValue(row, 1, buildDeviceText(item.getDeviceName(), item.getDeviceCode()));
setCellValue(row, 2, buildCapacityPointText(item)); setCellValue(row, 2, buildCapacityPointText(item));
setCellValue(row, 3, item.getCapacityDiff()); setCellValue(row, 3, item.getCapacityDiff());
setCellValue(row, 4, startTime.toLocalTime() + "-" + endTime.toLocalTime()); setCellValue(row, 4, startTime.toLocalTime() + "-" + endTime.toLocalTime());
@ -270,22 +271,32 @@ public class DeviceProductionDailyReportServiceImpl implements DeviceProductionD
List<DeviceWarinningRecordDO> warningRecords = tDengineService.selectDeviceWarningList(deviceIds, startTime, endTime); List<DeviceWarinningRecordDO> warningRecords = tDengineService.selectDeviceWarningList(deviceIds, startTime, endTime);
Map<Long, List<DeviceWarinningRecordDO>> warningMap = warningRecords.stream() Map<Long, List<DeviceWarinningRecordDO>> warningMap = warningRecords.stream()
.collect(Collectors.groupingBy(DeviceWarinningRecordDO::getDeviceId, LinkedHashMap::new, Collectors.toList())); .collect(Collectors.groupingBy(DeviceWarinningRecordDO::getDeviceId, LinkedHashMap::new, Collectors.toList()));
Map<String, String> alarmLevelLabelMap = buildAlarmLevelLabelMap();
Row templateRow = sheet.getRow(warningRowIndex); Row templateRow = sheet.getRow(warningRowIndex);
for (int index = 0; index < deviceIds.size(); index++) { for (int index = 0; index < deviceIds.size(); index++) {
Row row = sheet.getRow(warningRowIndex + index); Row row = sheet.getRow(warningRowIndex + index);
if (row == null) { if (row == null) {
row = sheet.createRow(warningRowIndex + index); row = sheet.createRow(warningRowIndex + index);
copyRowStyle(templateRow, row, 5); copyRowStyle(templateRow, row, 5);
mergeCellsIfAbsent(sheet, warningRowIndex + index, 2, 4);
} }
mergeCellsIfAbsent(sheet, warningRowIndex + index, 2, 4);
Long deviceId = deviceIds.get(index); Long deviceId = deviceIds.get(index);
setCellValue(row, 0, index + 1); setCellValue(row, 0, index + 1);
setCellValue(row, 1, getDeviceText(deviceId)); setCellValue(row, 1, getDeviceText(deviceId));
String warningDescription = buildWarningDescription(warningMap.getOrDefault(deviceId, Collections.emptyList())); String warningDescription = buildWarningDescription(warningMap.getOrDefault(deviceId, Collections.emptyList()), alarmLevelLabelMap);
setCellValue(row, 2, warningDescription); setCellValue(row, 2, warningDescription);
setWrapText(row, 2); setWrapText(row, 2);
row.setHeightInPoints(Math.max(row.getHeightInPoints(), 18F * Math.max(1, warningDescription.split("\n", -1).length))); row.setHeightInPoints(Math.max(row.getHeightInPoints(), 18F * Math.max(1, warningDescription.split("\n", -1).length)));
} }
removeRowsFromEnd(sheet, warningRowIndex + deviceIds.size());
}
private void setReportColumnWidths(Sheet sheet) {
sheet.setColumnWidth(0, 8 * 256);
sheet.setColumnWidth(1, 32 * 256);
sheet.setColumnWidth(2, 52 * 256);
sheet.setColumnWidth(3, 14 * 256);
sheet.setColumnWidth(4, 22 * 256);
} }
private String buildCapacityPointText(DeviceProductionDailyReportRespVO.DeviceCapacityItem item) { private String buildCapacityPointText(DeviceProductionDailyReportRespVO.DeviceCapacityItem item) {
@ -305,25 +316,35 @@ public class DeviceProductionDailyReportServiceImpl implements DeviceProductionD
if (device == null) { if (device == null) {
return ""; return "";
} }
return StrUtil.blankToDefault(device.getDeviceCode(), device.getDeviceName()); return buildDeviceText(device.getDeviceName(), device.getDeviceCode());
}
private String buildDeviceText(String deviceName, String deviceCode) {
if (StringUtils.isBlank(deviceName)) {
return StrUtil.blankToDefault(deviceCode, "");
}
if (StringUtils.isBlank(deviceCode)) {
return deviceName;
}
return deviceName + "" + deviceCode + "";
} }
private String buildWarningDescription(List<DeviceWarinningRecordDO> warningRecords) { private String buildWarningDescription(List<DeviceWarinningRecordDO> warningRecords, Map<String, String> alarmLevelLabelMap) {
if (CollUtil.isEmpty(warningRecords)) { if (CollUtil.isEmpty(warningRecords)) {
return ""; return "";
} }
return warningRecords.stream() return warningRecords.stream()
.collect(Collectors.groupingBy(this::getWarningPointName, LinkedHashMap::new, Collectors.toList())) .collect(Collectors.groupingBy(this::getWarningPointName, LinkedHashMap::new, Collectors.toList()))
.entrySet().stream() .entrySet().stream()
.map(entry -> entry.getKey() + "" + buildWarningPointSummary(entry.getValue())) .map(entry -> entry.getKey() + "" + buildWarningPointSummary(entry.getValue(), alarmLevelLabelMap))
.collect(Collectors.joining("\n")); .collect(Collectors.joining("\n"));
} }
private String buildWarningPointSummary(List<DeviceWarinningRecordDO> warningRecords) { private String buildWarningPointSummary(List<DeviceWarinningRecordDO> warningRecords, Map<String, String> alarmLevelLabelMap) {
return warningRecords.stream() return warningRecords.stream()
.collect(Collectors.groupingBy(this::getWarningRuleKey, LinkedHashMap::new, Collectors.toList())) .collect(Collectors.groupingBy(this::getWarningRuleKey, LinkedHashMap::new, Collectors.toList()))
.values().stream() .values().stream()
.map(this::buildWarningRuleSummary) .map(records -> buildWarningRuleSummary(records, alarmLevelLabelMap))
.collect(Collectors.joining("")); .collect(Collectors.joining(""));
} }
@ -331,19 +352,46 @@ public class DeviceProductionDailyReportServiceImpl implements DeviceProductionD
return getWarningName(record) + "|" + StrUtil.blankToDefault(record.getAlarmLevel(), ""); return getWarningName(record) + "|" + StrUtil.blankToDefault(record.getAlarmLevel(), "");
} }
private String buildWarningRuleSummary(List<DeviceWarinningRecordDO> warningRecords) { private String buildWarningRuleSummary(List<DeviceWarinningRecordDO> warningRecords, Map<String, String> alarmLevelLabelMap) {
DeviceWarinningRecordDO firstRecord = warningRecords.get(0); DeviceWarinningRecordDO firstRecord = warningRecords.get(0);
String levelText = buildAlarmLevelText(firstRecord.getAlarmLevel()); String levelText = buildAlarmLevelText(firstRecord.getAlarmLevel(), alarmLevelLabelMap);
return getWarningName(firstRecord) + warningRecords.size() + "次" return getWarningName(firstRecord) + warningRecords.size() + "次"
+ buildWarningValueRangeText(warningRecords) + levelText + buildWarningTimeRangeText(warningRecords); + buildWarningValueRangeText(warningRecords) + levelText + buildWarningTimeRangeText(warningRecords);
} }
private String buildAlarmLevelText(String alarmLevel) { private String buildAlarmLevelText(String alarmLevel, Map<String, String> alarmLevelLabelMap) {
if (StringUtils.isBlank(alarmLevel)) { if (StringUtils.isBlank(alarmLevel)) {
return ""; return "";
} }
DictDataRespDTO dictData = dictDataApi.getDictData(ALARM_LEVEL_DICT_TYPE, alarmLevel); String alarmLevelLabel = alarmLevelLabelMap.get(normalizeAlarmLevel(alarmLevel));
return ",等级=" + (dictData != null && StringUtils.isNotBlank(dictData.getLabel()) ? dictData.getLabel() : alarmLevel); return ",等级=" + StrUtil.blankToDefault(alarmLevelLabel, alarmLevel);
}
private Map<String, String> buildAlarmLevelLabelMap() {
List<DictDataRespDTO> dictDataList = dictDataApi.getDictDataList(ALARM_LEVEL_DICT_TYPE);
if (CollUtil.isEmpty(dictDataList)) {
return Collections.emptyMap();
}
Map<String, String> alarmLevelLabelMap = new LinkedHashMap<>();
for (DictDataRespDTO dictData : dictDataList) {
if (dictData == null || StringUtils.isBlank(dictData.getValue())) {
continue;
}
alarmLevelLabelMap.put(normalizeAlarmLevel(dictData.getValue()), dictData.getLabel());
}
return alarmLevelLabelMap;
}
private String normalizeAlarmLevel(String alarmLevel) {
String value = StringUtils.trimToEmpty(alarmLevel);
if (StringUtils.isBlank(value)) {
return "";
}
try {
return BigDecimal.valueOf(Double.parseDouble(value)).stripTrailingZeros().toPlainString();
} catch (NumberFormatException e) {
return value;
}
} }
private String getWarningPointName(DeviceWarinningRecordDO record) { private String getWarningPointName(DeviceWarinningRecordDO record) {
@ -572,6 +620,25 @@ public class DeviceProductionDailyReportServiceImpl implements DeviceProductionD
sheet.addMergedRegion(newRegion); sheet.addMergedRegion(newRegion);
} }
private void removeRowsFromEnd(Sheet sheet, int startRowIndex) {
int lastRowIndex = sheet.getLastRowNum();
if (startRowIndex > lastRowIndex) {
return;
}
for (int index = sheet.getNumMergedRegions() - 1; index >= 0; index--) {
CellRangeAddress mergedRegion = sheet.getMergedRegion(index);
if (mergedRegion.getFirstRow() >= startRowIndex) {
sheet.removeMergedRegion(index);
}
}
for (int rowIndex = lastRowIndex; rowIndex >= startRowIndex; rowIndex--) {
Row row = sheet.getRow(rowIndex);
if (row != null) {
sheet.removeRow(row);
}
}
}
private void setHeaderValue(Sheet sheet, String label, String value, int fallbackRowIndex, int fallbackColumnIndex) { private void setHeaderValue(Sheet sheet, String label, String value, int fallbackRowIndex, int fallbackColumnIndex) {
Cell labelCell = findCellByPrefix(sheet, label); Cell labelCell = findCellByPrefix(sheet, label);
if (labelCell != null) { if (labelCell != null) {

Loading…
Cancel
Save