提交到处修改
This commit is contained in:
@@ -19,6 +19,8 @@ import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
|
||||
import org.apache.poi.ss.usermodel.*;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@@ -26,6 +28,10 @@ import javax.annotation.Resource;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -65,21 +71,140 @@ public class DataCenterController {
|
||||
|
||||
@ApiOperation("导出设备的历史数据为Excel")
|
||||
@PostMapping("/deviceHistoryExport")
|
||||
public void deviceHistoryExport(@RequestBody DeviceHistoryParam deviceHistoryParam, HttpServletResponse response) {
|
||||
public void deviceHistoryExport(@RequestBody DeviceHistoryParam deviceHistoryParam, HttpServletResponse response) throws Exception {
|
||||
if (StringUtils.isEmpty(deviceHistoryParam.getSerialNumber())) {
|
||||
try {
|
||||
response.setContentType("application/json");
|
||||
response.getWriter().print(JSON.toJSONString(AjaxResult.error("请选择设备")));
|
||||
} catch (IOException e) {
|
||||
log.error("导出失败", e);
|
||||
}
|
||||
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "请选择设备");
|
||||
return;
|
||||
}
|
||||
dataCenterService.deviceHistoryExport(deviceHistoryParam, response);
|
||||
|
||||
List<JSONObject> dataList = dataCenterService.deviceHistory(deviceHistoryParam);
|
||||
|
||||
// 创建工作簿
|
||||
Workbook workbook = new XSSFWorkbook();
|
||||
Sheet sheet = workbook.createSheet("设备历史数据");
|
||||
|
||||
// 创建表头
|
||||
Row headerRow = sheet.createRow(0);
|
||||
headerRow.createCell(0).setCellValue("时间戳");
|
||||
headerRow.createCell(1).setCellValue("指标名");
|
||||
headerRow.createCell(2).setCellValue("值");
|
||||
|
||||
// 填充数据
|
||||
int rowIndex = 1;
|
||||
|
||||
for (JSONObject data : dataList) {
|
||||
if (data == null || data.isEmpty()) continue;
|
||||
|
||||
// 获取时间戳
|
||||
String timestamp = new ArrayList<>(data.keySet()).get(0);
|
||||
|
||||
// 获取指标数据 - 现在处理 ArrayList 类型
|
||||
Object valueObj = data.get(timestamp);
|
||||
|
||||
if (valueObj instanceof java.util.List) {
|
||||
java.util.List<?> valueList = (java.util.List<?>) valueObj;
|
||||
if (!valueList.isEmpty() && valueList.get(0) instanceof JSONObject) {
|
||||
JSONObject valueData = (JSONObject) valueList.get(0);
|
||||
|
||||
// 为每个指标创建一行
|
||||
for (String identifier : valueData.keySet()) {
|
||||
Row row = sheet.createRow(rowIndex++);
|
||||
row.createCell(0).setCellValue(timestamp);
|
||||
row.createCell(1).setCellValue(identifier);
|
||||
Object value = valueData.get(identifier);
|
||||
if (value != null) {
|
||||
row.createCell(2).setCellValue(value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 同时兼容原来的 JSONArray 类型
|
||||
else if (valueObj instanceof com.alibaba.fastjson.JSONArray) {
|
||||
com.alibaba.fastjson.JSONArray valueArray = (com.alibaba.fastjson.JSONArray) valueObj;
|
||||
if (!valueArray.isEmpty() && valueArray.get(0) instanceof JSONObject) {
|
||||
JSONObject valueData = (JSONObject) valueArray.get(0);
|
||||
|
||||
// 为每个指标创建一行
|
||||
for (String identifier : valueData.keySet()) {
|
||||
Row row = sheet.createRow(rowIndex++);
|
||||
row.createCell(0).setCellValue(timestamp);
|
||||
row.createCell(1).setCellValue(identifier);
|
||||
Object value = valueData.get(identifier);
|
||||
if (value != null) {
|
||||
row.createCell(2).setCellValue(value.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 设置列宽
|
||||
sheet.setColumnWidth(0, 25 * 256); // 时间戳列 - 25个字符宽度
|
||||
sheet.setColumnWidth(1, 20 * 256); // 指标名列 - 20个字符宽度
|
||||
sheet.setColumnWidth(2, 20 * 256); // 值列 - 20个字符宽度
|
||||
|
||||
// 或者使用自动调整列宽(根据内容)
|
||||
// sheet.autoSizeColumn(0);
|
||||
// sheet.autoSizeColumn(1);
|
||||
// sheet.autoSizeColumn(2);
|
||||
// 设置响应头
|
||||
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
response.setCharacterEncoding("utf-8");
|
||||
String fileName = URLEncoder.encode("设备历史数据.xlsx", "UTF-8");
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
||||
|
||||
// 写入响应流
|
||||
workbook.write(response.getOutputStream());
|
||||
workbook.close();
|
||||
}
|
||||
|
||||
|
||||
|
||||
// public void deviceHistoryExport(@RequestBody DeviceHistoryParam deviceHistoryParam, HttpServletResponse response) {
|
||||
// if (StringUtils.isEmpty(deviceHistoryParam.getSerialNumber())) {
|
||||
// try {
|
||||
// response.getWriter().write(JSON.toJSONString(AjaxResult.error("请选择设备")));
|
||||
// } catch (IOException e) {
|
||||
// log.error("导出设备历史数据错误", e);
|
||||
// }
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// try {
|
||||
// List<JSONObject> jsonObjectList = dataCenterService.deviceHistory(deviceHistoryParam);
|
||||
// List<DeviceHistoryExcel> exportList = new ArrayList<>();
|
||||
//
|
||||
// // 转换数据格式
|
||||
// for (JSONObject jsonObject : jsonObjectList) {
|
||||
// for (String timestamp : jsonObject.keySet()) {
|
||||
// JSONArray values = jsonObject.getJSONArray(timestamp);
|
||||
// for (int i = 0; i < values.size(); i++) {
|
||||
// JSONObject valueObj = values.getJSONObject(i);
|
||||
// for (String key : valueObj.keySet()) {
|
||||
// exportList.add(new DeviceHistoryExcel(
|
||||
// timestamp,
|
||||
// key,
|
||||
// valueObj.getString(key)
|
||||
// ));
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// // 使用ExcelUtil导出
|
||||
// ExcelUtil<DeviceHistoryExcel> util = new ExcelUtil<>(DeviceHistoryExcel.class);
|
||||
// util.exportExcel(response, exportList, "设备历史数据");
|
||||
// } catch (Exception e) {
|
||||
// log.error("导出设备历史数据错误", e);
|
||||
// try {
|
||||
// response.getWriter().write(JSON.toJSONString(AjaxResult.error("导出Excel失败: " + e.getMessage())));
|
||||
// } catch (IOException ex) {
|
||||
// log.error("响应导出错误信息失败", ex);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
@ApiOperation("查询场景变量历史数据")
|
||||
@PreAuthorize("@ss.hasPermi('dataCenter:history:list')")
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
package com.xinda.data.controller.datacenter;
|
||||
|
||||
import com.alibaba.excel.annotation.ExcelProperty;
|
||||
import com.xinda.common.annotation.Excel;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class DeviceHistoryData {
|
||||
@Excel(name = "时间")
|
||||
private String time;
|
||||
|
||||
@Excel(name = "数据")
|
||||
private String value;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.xinda.data.controller.datacenter;
|
||||
|
||||
|
||||
import com.xinda.common.annotation.Excel;
|
||||
|
||||
// DeviceHistoryExcel.java
|
||||
public class DeviceHistoryExcel {
|
||||
@Excel(name = "时间戳")
|
||||
private String timestamp;
|
||||
|
||||
@Excel(name = "标识符")
|
||||
private String identifier;
|
||||
|
||||
@Excel(name = "值")
|
||||
private String value;
|
||||
|
||||
// 构造函数、getter和setter
|
||||
public DeviceHistoryExcel(String timestamp, String identifier, String value) {
|
||||
this.timestamp = timestamp;
|
||||
this.identifier = identifier;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
// getter和setter方法
|
||||
public String getTimestamp() { return timestamp; }
|
||||
public void setTimestamp(String timestamp) { this.timestamp = timestamp; }
|
||||
public String getIdentifier() { return identifier; }
|
||||
public void setIdentifier(String identifier) { this.identifier = identifier; }
|
||||
public String getValue() { return value; }
|
||||
public void setValue(String value) { this.value = value; }
|
||||
}
|
||||
Reference in New Issue
Block a user