环境:idea
语言:java
框架:springboot
难点:动态表头
思路:
根据前端给过来的参数,查询指定时间内的历史数据
合成表头
合唱数据段内容
合成报表内容返回
部分代码参考:
用到的对象
@Data
public class Column {
/**
* 英文名
*/
String prop;
/**
* 中文名
*/
String label;
/**
* 排序
*/
int index;
public Column(){}
public Column(int index, PointValue pointValue){
this.setIndex(index);
this.setLabel(pointValue.getPointDisplayName());
this.setProp(pointValue.getPointName());
}
public Column(int index,String label,String prop){
this.setIndex(index);
this.setLabel(label);
this.setProp(prop);
}
}
@Data
public class TableData {
List<Column> columnList;
Page<Object> objectPage;
public TableData(){}
public TableData(List<Column> columnList,Page<Object> objectPage){
this.setColumnList(columnList);
this.setObjectPage(objectPage);
}
}
解析过程中的部分代码
{
//这里的代码只是部分,因为主要讲的是报表,
//历史数据查询,page分页,则是另外一个技术点,这里没有写
list<Column> columnList = this.getColumns(pointValuesSort,size);
List<Object> objectList = this.getBody(pointValuesSort,size);
Page<Object> objectPage = new Page<>();
objectPage.setRecords(objectList);
//todo 更换PAGE
objectPage.setSize(pSize);
objectPage.setCurrent(pCurrent);
//todo 因为带入进去查询的size不正确,所以需要重新计算
objectPage.setTotal(newTotal);
objectPage.setOrders(page.getOrders());
TableData tableData = new TableData(columnList,objectPage);
return tableData;
}
/**
* 获取报表的头
* @param pointValuesSort
* @param size
* @return
*/
private List<Column> getColumns(List<PointValue> pointValuesSort,int size){
List<Column> columnList = new ArrayList<>();
int index = 0;
Column column = new Column(index++,"序号","order");
columnList.add(column);
Column column01 = new Column(index++,"设备时间","originTime");
columnList.add(column01);
Column column02 = new Column(index++,"系统时间","createTime");
columnList.add(column02);
for(int i=0;i<size;i++){
PointValue pointValue = pointValuesSort.get(i);
Column columnData = new Column(i+index,pointValue);
columnList.add(columnData);
}
return columnList;
}
/**
* 获取报表的内容
* @param pointValuesSort 信号list
* @param size 信号列数量
* @return
*/
private List<Object> getBody(List<PointValue> pointValuesSort,int size){
List<Object> objectList = new ArrayList<>();
List<PointValue> pointValueList = null;
int j=1;
for(int i=0;i<pointValuesSort.size();i++){
if (i % size == 0) {
pointValueList = new ArrayList<>();
}
pointValueList.add(pointValuesSort.get(i));
if ((i + 1) % size == 0) {
Map<String,String> oneRow = this.getOneRow(j++,pointValueList);
objectList.add(oneRow);
}
}
return objectList;
}
/**
* 获取一行的数据
* @param order
* @param pointValueList
* @return
*/
private Map<String,String> getOneRow(int order,List<PointValue> pointValueList) {
Map<String, String> oneRow = new HashMap<>();
oneRow.put("order", String.valueOf(order));
oneRow.put("originTime", DateUtil.formatDateTime(pointValueList.get(0).getOriginTime()));
oneRow.put("createTime", DateUtil.formatDateTime(pointValueList.get(0).getCreateTime()));
for (int i = 0; i < pointValueList.size(); i++) {
PointValue pointValue = pointValueList.get(i);
oneRow.put(pointValue.getPointName(), pointValue.getValue());
}
return oneRow;
}