mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
完善抄表记录
This commit is contained in:
17
src/main/java/cn/lihongjie/coal/annotation/HyperTable.java
Normal file
17
src/main/java/cn/lihongjie/coal/annotation/HyperTable.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package cn.lihongjie.coal.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.*;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||
public @interface HyperTable {
|
||||
|
||||
String column() default "'time'";
|
||||
|
||||
String by() default "by_range";
|
||||
|
||||
}
|
||||
@@ -11,6 +11,10 @@ public class CreateMeterDto extends OrgCommonDto {
|
||||
@Comment("类型")
|
||||
private String type;
|
||||
|
||||
|
||||
@Comment("初始值")
|
||||
private Double initValue;
|
||||
|
||||
@Comment("安装位置")
|
||||
private String location;
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ import org.hibernate.annotations.Comment;
|
||||
public class MeterDto extends OrgCommonDto {
|
||||
@Comment("类型")
|
||||
private String type;
|
||||
|
||||
@Comment("初始值")
|
||||
private Double initValue;
|
||||
private String typeName;
|
||||
|
||||
@Comment("安装位置")
|
||||
|
||||
@@ -10,7 +10,8 @@ import org.hibernate.annotations.Comment;
|
||||
public class UpdateMeterDto extends OrgCommonDto {
|
||||
@Comment("类型")
|
||||
private String type;
|
||||
|
||||
@Comment("初始值")
|
||||
private Double initValue;
|
||||
@Comment("安装位置")
|
||||
private String location;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,10 @@ public class MeterEntity extends OrgCommonEntity {
|
||||
@Comment("类型")
|
||||
private String type;
|
||||
|
||||
|
||||
@Comment("初始值")
|
||||
private Double initValue;
|
||||
|
||||
@Formula(
|
||||
"(select i.name\n"
|
||||
+ "from t_dictionary d,\n"
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
package cn.lihongjie.coal.meterLog.entity;
|
||||
|
||||
import cn.lihongjie.coal.annotation.HyperTable;
|
||||
import cn.lihongjie.coal.base.entity.OrgCommonEntity;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Index;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.Table;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.hibernate.annotations.Comment;
|
||||
import org.hibernate.annotations.Formula;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@HyperTable
|
||||
@Table(indexes = {@Index(columnList = "id")})
|
||||
|
||||
public class MeterLogEntity extends OrgCommonEntity {
|
||||
|
||||
@ManyToOne private cn.lihongjie.coal.meter.entity.MeterEntity meter;
|
||||
@@ -21,9 +28,8 @@ public class MeterLogEntity extends OrgCommonEntity {
|
||||
@Comment("抄表值")
|
||||
private java.lang.Double value;
|
||||
|
||||
@Comment("上次抄表值")
|
||||
@Formula("( lag(value, 1, (select tm.init_value from t_meter tm where tm.id = meter_id)) over (partition by meter_id order by time asc ) )")
|
||||
private java.lang.Double previousValue;
|
||||
|
||||
@Comment("使用量")
|
||||
@Formula(" ( value - ( lag(value, 1, (select tm.init_value from t_meter tm where tm.id = meter_id)) over (partition by meter_id order by time asc ) ) )")
|
||||
private java.lang.Double usage;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,18 @@
|
||||
package cn.lihongjie.coal.spring.config;
|
||||
|
||||
import cn.lihongjie.coal.annotation.HyperTable;
|
||||
|
||||
import org.hibernate.boot.Metadata;
|
||||
import org.hibernate.boot.model.relational.SqlStringGenerationContext;
|
||||
import org.hibernate.dialect.PostgreSQLDialect;
|
||||
import org.hibernate.mapping.PersistentClass;
|
||||
import org.hibernate.mapping.Table;
|
||||
import org.hibernate.tool.schema.spi.Exporter;
|
||||
import org.hibernate.type.SqlTypes;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MyPostgreSQLDialect extends PostgreSQLDialect {
|
||||
|
||||
@Override
|
||||
@@ -23,6 +33,67 @@ public class MyPostgreSQLDialect extends PostgreSQLDialect {
|
||||
return " DROP CONSTRAINT IF EXISTS notexist ";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAddPrimaryKeyConstraintString(String constraintName) {
|
||||
return super.getAddPrimaryKeyConstraintString(constraintName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Exporter<Table> getTableExporter() {
|
||||
Exporter<Table> tableExporter = super.getTableExporter();
|
||||
return new Exporter<Table>() {
|
||||
|
||||
@Override
|
||||
public String[] getSqlCreateStrings(
|
||||
Table exportable, Metadata metadata, SqlStringGenerationContext context) {
|
||||
|
||||
PersistentClass persistentClass =
|
||||
metadata.getEntityBindings().stream()
|
||||
.filter(x -> x.getTable() == exportable)
|
||||
.findFirst()
|
||||
.get();
|
||||
|
||||
HyperTable hyperTable =
|
||||
persistentClass.getMappedClass().getAnnotation(HyperTable.class);
|
||||
|
||||
var sqlCreateStrings =
|
||||
Arrays.stream(
|
||||
tableExporter.getSqlCreateStrings(
|
||||
exportable, metadata, context))
|
||||
.toList();
|
||||
if (hyperTable != null) {
|
||||
|
||||
sqlCreateStrings =
|
||||
sqlCreateStrings.stream()
|
||||
.map(x -> x.replace(", primary key (id)", " "))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
String tableName = exportable.getName();
|
||||
|
||||
String partFunction = hyperTable.by();
|
||||
String columns = hyperTable.column();
|
||||
sqlCreateStrings.add("CREATE EXTENSION IF NOT EXISTS timescaledb;");
|
||||
sqlCreateStrings.add(
|
||||
"SELECT create_hypertable('"
|
||||
+ tableName
|
||||
+ "', "
|
||||
+ partFunction
|
||||
+ "("
|
||||
+ columns
|
||||
+ "));");
|
||||
}
|
||||
|
||||
return sqlCreateStrings.toArray(new String[0]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] getSqlDropStrings(
|
||||
Table exportable, Metadata metadata, SqlStringGenerationContext context) {
|
||||
return tableExporter.getSqlDropStrings(exportable, metadata, context);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAddForeignKeyConstraintString(
|
||||
String constraintName, String foreignKeyDefinition) {
|
||||
|
||||
Reference in New Issue
Block a user