微调
This commit is contained in:
@@ -2,11 +2,14 @@ package com.njcn.influx.core;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.text.StrPool;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.njcn.influx.constant.InfluxDbSqlConstant;
|
||||
import com.njcn.influx.query.InfluxQueryWrapper;
|
||||
import com.njcn.influx.utils.InstantUtil;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.influxdb.InfluxDB;
|
||||
import org.influxdb.annotation.Column;
|
||||
import org.influxdb.annotation.Measurement;
|
||||
import org.influxdb.dto.BatchPoints;
|
||||
import org.influxdb.dto.Point;
|
||||
@@ -18,8 +21,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author hongawen
|
||||
@@ -46,24 +48,64 @@ public class InfluxExecutor {
|
||||
QueryResult queryResult = influxDb.query(new Query(sql, database));
|
||||
results = new ArrayList<>();
|
||||
try {
|
||||
Field[] declaredFields = domainClass.getDeclaredFields();
|
||||
List<Field> fields = new ArrayList<>(Arrays.asList(declaredFields));
|
||||
//映射父级内部私有属性
|
||||
Class superclass = domainClass.getSuperclass();
|
||||
if(ObjectUtil.isNotNull(superclass)){
|
||||
Field[] superDeclaredFields = superclass.getDeclaredFields();
|
||||
List<Field> superFields = new ArrayList<>(Arrays.asList(superDeclaredFields));
|
||||
fields.addAll(superFields);
|
||||
}
|
||||
List<QueryResult.Result> queryResults = queryResult.getResults();
|
||||
if (CollectionUtil.isNotEmpty(queryResults)) {
|
||||
for (QueryResult.Result result : queryResults) {
|
||||
if (CollectionUtil.isNotEmpty(result.getSeries())) {
|
||||
List<QueryResult.Series> seriesList = result.getSeries();
|
||||
Object obj = null;
|
||||
if (CollectionUtil.isNotEmpty(seriesList)) {
|
||||
QueryResult.Series series = seriesList.get(0);
|
||||
//如果查询语句中存在group by,会存在多个series
|
||||
for (QueryResult.Series series : seriesList) {
|
||||
obj = domainClass.newInstance();
|
||||
Map<String, String> tags = series.getTags();
|
||||
if(CollectionUtil.isNotEmpty(tags)){
|
||||
//处理tag分组数据
|
||||
for (String columnName : tags.keySet()) {
|
||||
for (Field declaredField : fields) {
|
||||
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
||||
//获取属性定义的类型
|
||||
declaredField.setAccessible(true);
|
||||
//时间格式
|
||||
if (declaredField.getType() == Instant.class) {
|
||||
declaredField.set(obj, InstantUtil.stringToInstant(tags.get(columnName).toString().replace("+08:00", "Z")));
|
||||
//字符串
|
||||
} else if (declaredField.getType() == String.class) {
|
||||
declaredField.set(obj, tags.get(columnName).toString());
|
||||
//浮点双精度
|
||||
} else if (declaredField.getType() == Double.class) {
|
||||
declaredField.set(obj, Double.parseDouble(tags.get(columnName).toString()));
|
||||
//浮点
|
||||
} else if (declaredField.getType() == Float.class) {
|
||||
declaredField.set(obj, Float.parseFloat(tags.get(columnName).toString()));
|
||||
//整型
|
||||
} else if (declaredField.getType() == Integer.class) {
|
||||
declaredField.set(obj, Integer.parseInt(tags.get(columnName).toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
List<String> columns = series.getColumns();
|
||||
List<List<Object>> values = series.getValues();
|
||||
for (List<Object> columnValue : values) {
|
||||
Object obj = domainClass.newInstance();
|
||||
if(ObjectUtil.isNull(obj)){
|
||||
obj = domainClass.newInstance();
|
||||
}
|
||||
for (int i = 0; i < columnValue.size(); i++) {
|
||||
String columnName = columns.get(i);
|
||||
//属性名有下划线的替换掉
|
||||
columnName = columnName.replaceAll(StrPool.UNDERLINE, "");
|
||||
Field[] declaredFields = domainClass.getDeclaredFields();
|
||||
for (Field declaredField : declaredFields) {
|
||||
if (columnName.equalsIgnoreCase(declaredField.getName())) {
|
||||
for (Field declaredField : fields) {
|
||||
if (columnName.equalsIgnoreCase(InfluxQueryWrapper.getColumnName(declaredField))) {
|
||||
//获取属性定义的类型
|
||||
declaredField.setAccessible(true);
|
||||
//时间格式
|
||||
@@ -91,6 +133,7 @@ public class InfluxExecutor {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("influx反射实例化实体异常:" + e);
|
||||
}
|
||||
@@ -167,4 +210,6 @@ public class InfluxExecutor {
|
||||
}
|
||||
return originalSql.concat(InfluxDbSqlConstant.TZ);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1010,6 +1010,26 @@ public class InfluxQueryWrapper {
|
||||
}
|
||||
|
||||
|
||||
/***
|
||||
* 获取sql拼接的名称,存在注解名就用注解名,否则就用属性名
|
||||
* @author hongawen
|
||||
* @return String
|
||||
*/
|
||||
public static String getColumnName(Field field) {
|
||||
Column column = field.getAnnotation(Column.class);
|
||||
//数据库字段
|
||||
String influxColumn;
|
||||
if (Objects.isNull(column)) {
|
||||
//没有注解,就用属性名
|
||||
influxColumn = field.getName();
|
||||
} else {
|
||||
//获取注解映射的名称
|
||||
influxColumn = column.name();
|
||||
}
|
||||
return influxColumn;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user