Compare commits
6 Commits
16220a6dd7
...
cd2451c985
| Author | SHA1 | Date |
|---|---|---|
|
|
cd2451c985 | |
|
|
410c08b132 | |
|
|
ebe86539d4 | |
|
|
214b194817 | |
|
|
3899cda8ef | |
|
|
2df821f69a |
|
|
@ -1,4 +1,4 @@
|
|||
// Package config define config struct of wave record project
|
||||
// Package config define config struct of model runtime service
|
||||
package config
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
postgres:
|
||||
host: "localhost"
|
||||
host: "192.168.2.156"
|
||||
port: 5432
|
||||
database: "model_rt"
|
||||
database: "circuit_diagram"
|
||||
user: "postgres"
|
||||
password: "coslight"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
// Package config define config struct of model runtime service
|
||||
package config
|
||||
|
||||
import (
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
package constant
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrUUIDChangeType define error of check uuid from value failed in uuid from change type
|
||||
var ErrUUIDChangeType = errors.New("undefined uuid change type")
|
||||
|
||||
// ErrUpdateRowZero define error of update affected row zero
|
||||
var ErrUpdateRowZero = errors.New("update affected rows is zero")
|
||||
|
||||
// ErrDeleteRowZero define error of delete affected row zero
|
||||
var ErrDeleteRowZero = errors.New("delete affected rows is zero")
|
||||
|
||||
// ErrInsertRowUnexpected define error of insert affected row not reach expected number
|
||||
var ErrInsertRowUnexpected = errors.New("the number of inserted data rows don't reach the expected value")
|
||||
|
||||
var (
|
||||
// ErrUUIDFromCheckT1 define error of check uuid from value failed in uuid from change type
|
||||
ErrUUIDFromCheckT1 = errors.New("in uuid from change type, value of new uuid_from is equal value of old uuid_from")
|
||||
// ErrUUIDToCheckT1 define error of check uuid to value failed in uuid from change type
|
||||
ErrUUIDToCheckT1 = errors.New("in uuid from change type, value of new uuid_to is not equal value of old uuid_to")
|
||||
|
||||
// ErrUUIDFromCheckT2 define error of check uuid from value failed in uuid to change type
|
||||
ErrUUIDFromCheckT2 = errors.New("in uuid to change type, value of new uuid_from is not equal value of old uuid_from")
|
||||
// ErrUUIDToCheckT2 define error of check uuid to value failed in uuid to change type
|
||||
ErrUUIDToCheckT2 = errors.New("in uuid to change type, value of new uuid_to is equal value of old uuid_to")
|
||||
|
||||
// ErrUUIDFromCheckT3 define error of check uuid from value failed in uuid add change type
|
||||
ErrUUIDFromCheckT3 = errors.New("in uuid add change type, value of old uuid_from is not empty")
|
||||
// ErrUUIDToCheckT3 define error of check uuid to value failed in uuid add change type
|
||||
ErrUUIDToCheckT3 = errors.New("in uuid add change type, value of old uuid_to is not empty")
|
||||
)
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package constant
|
||||
|
||||
const (
|
||||
// UUIDErrChangeType 拓扑信息错误改变类型
|
||||
UUIDErrChangeType = iota
|
||||
// UUIDFromChangeType 拓扑信息父节点改变类型
|
||||
UUIDFromChangeType
|
||||
// UUIDToChangeType 拓扑信息子节点改变类型
|
||||
UUIDToChangeType
|
||||
// UUIDAddChangeType 拓扑信息新增类型
|
||||
UUIDAddChangeType
|
||||
)
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateComponentIntoDB define create component info of the circuit diagram into DB
|
||||
func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfos []network.ComponentCreateInfo) error {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var componentSlice []orm.Component
|
||||
for _, info := range componentInfos {
|
||||
globalUUID, err := uuid.FromString(info.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
|
||||
componentInfo := orm.Component{
|
||||
GlobalUUID: globalUUID,
|
||||
GridID: info.GridID,
|
||||
ZoneID: info.ZoneID,
|
||||
StationID: info.StationID,
|
||||
ComponentType: info.ComponentType,
|
||||
State: info.State,
|
||||
ConnectedBus: info.ConnectedBus,
|
||||
Name: info.Name,
|
||||
VisibleID: info.Name,
|
||||
Description: info.Description,
|
||||
Context: info.Context,
|
||||
Comment: info.Comment,
|
||||
InService: info.InService,
|
||||
}
|
||||
componentSlice = append(componentSlice, componentInfo)
|
||||
}
|
||||
|
||||
result := tx.WithContext(cancelCtx).Create(&componentSlice)
|
||||
|
||||
if result.Error != nil || result.RowsAffected != int64(len(componentSlice)) {
|
||||
err := result.Error
|
||||
if result.RowsAffected != int64(len(componentSlice)) {
|
||||
err = fmt.Errorf("%w:please check insert component slice", constant.ErrInsertRowUnexpected)
|
||||
}
|
||||
return fmt.Errorf("insert component info failed:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateModelIntoDB define create component model params of the circuit diagram into DB
|
||||
func CreateModelIntoDB(ctx context.Context, tx *gorm.DB, componentInfos []network.ComponentCreateInfo) error {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
for _, componentInfo := range componentInfos {
|
||||
modelStruct := model.SelectModelByType(componentInfo.ComponentType)
|
||||
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
modelStruct.SetUUID(globalUUID)
|
||||
err = jsoniter.Unmarshal([]byte(componentInfo.Params), modelStruct)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshal component model params failed:%w", err)
|
||||
}
|
||||
|
||||
result := tx.Model(modelStruct).WithContext(cancelCtx).Create(modelStruct)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check insert model params", constant.ErrInsertRowUnexpected)
|
||||
}
|
||||
return fmt.Errorf("insert component model params into table %s failed:%w", modelStruct.ReturnTableName(), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// CreateTopologicIntoDB define create topologic info of the circuit diagram query by pageID and topologic info
|
||||
func CreateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, topologicInfos []network.TopologicUUIDCreateInfo) error {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var topologicSlice []orm.Topologic
|
||||
for _, info := range topologicInfos {
|
||||
topologicInfo := orm.Topologic{
|
||||
PageID: pageID,
|
||||
UUIDFrom: info.UUIDFrom,
|
||||
UUIDTo: info.UUIDTo,
|
||||
Flag: info.Flag,
|
||||
Comment: info.Comment,
|
||||
}
|
||||
topologicSlice = append(topologicSlice, topologicInfo)
|
||||
}
|
||||
|
||||
result := tx.WithContext(cancelCtx).Create(&topologicSlice)
|
||||
|
||||
if result.Error != nil || result.RowsAffected != int64(len(topologicSlice)) {
|
||||
err := result.Error
|
||||
if result.RowsAffected != int64(len(topologicSlice)) {
|
||||
err = fmt.Errorf("%w:please check insert topologic slice", constant.ErrInsertRowUnexpected)
|
||||
}
|
||||
return fmt.Errorf("insert topologic link failed:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// DeleteTopologicIntoDB define delete topologic info of the circuit diagram query by pageID and topologic info
|
||||
func DeleteTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, delInfo network.TopologicUUIDDelInfo) error {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
result := tx.Model(&orm.Topologic{}).WithContext(cancelCtx).Where("page_id = ? and uuid_from = ? and uuid_to = ?", pageID, delInfo.UUIDFrom, delInfo.UUIDTo).Delete(&orm.Topologic{})
|
||||
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check delete topologic where conditions", constant.ErrDeleteRowZero)
|
||||
}
|
||||
return fmt.Errorf("delete topologic link failed:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -17,7 +17,6 @@ func QueryAllPages(ctx context.Context, logger *zap.Logger, gridID, zoneID, stat
|
|||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
// result := _globalPostgresClient.Model(&orm.Page{}).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select("Page.id, Page.Name, Page.status,Page.context").InnerJoins("Station on Station.id = Page.station_id").InnerJoins("Zone on Zone.id = Station.zone_id").InnerJoins("Grid on Grid.id = Zone.grid_id").Scan(&pages)
|
||||
|
||||
result := _globalPostgresClient.Model(&orm.Page{}).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select(`"Page".id, "Page".Name, "Page".status,"Page".context`).Joins(`inner join "Station" on "Station".id = "Page".station_id`).Joins(`inner join "Zone" on "Zone".id = "Station".zone_id`).Joins(`inner join "Grid" on "Grid".id = "Zone".grid_id`).Where(`"Grid".id = ? and "Zone".id = ? and "Station".id = ?`, gridID, zoneID, stationID).Scan(&pages)
|
||||
|
||||
|
|
@ -28,9 +27,3 @@ func QueryAllPages(ctx context.Context, logger *zap.Logger, gridID, zoneID, stat
|
|||
|
||||
return pages, nil
|
||||
}
|
||||
|
||||
// select "Page".id, "Page".station_id,"Station".zone_id,"Zone".grid_id,"Page".Name, "Page".status,"Page".context from "Page" inner join "Station" on "Station".id = "Page".station_id
|
||||
// inner join "Zone" on "Zone".id = "Station".zone_id inner join "Grid" on "Grid".id = "Zone".grid_id
|
||||
// where "Grid".id = 1 and "Zone".id=1 and "Station".id=1
|
||||
|
||||
// _globalPostgresClient.Model(&orm.Page{}).Clauses(clause.Locking{Strength: "UPDATE"}).Select(`"Page".id, "Page".Name, "Page".status,"Page".context`).Joins(`inner join "Station" on "Station".id = "Page".station_id`).Joins(`inner join "Zone" on "Zone".id = "Station".zone_id`).Joins(`inner join "Grid" on "Grid".id = "Zone".grid_id`).Where(`"Grid".id = ? and "Zone".id = ? and "Station".id = ?`, 1, 1, 1).Scan(&pages)
|
||||
|
|
|
|||
|
|
@ -7,30 +7,20 @@ import (
|
|||
|
||||
"modelRT/diagram"
|
||||
"modelRT/orm"
|
||||
"modelRT/sql"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
var recursiveSQL = `WITH RECURSIVE recursive_tree as (
|
||||
SELECT uuid_from,uuid_to,page_id,flag
|
||||
FROM "Topologic"
|
||||
WHERE uuid_from is null and page_id = ?
|
||||
UNION ALL
|
||||
SELECT t.uuid_from,t.uuid_to,t.page_id,t.flag
|
||||
FROM "Topologic" t
|
||||
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
|
||||
)
|
||||
SELECT * FROM recursive_tree;`
|
||||
|
||||
// QueryTopologicByPageID return the topologic info of the circuit diagram query by pageID
|
||||
func QueryTopologicByPageID(ctx context.Context, logger *zap.Logger, pageID int64) ([]orm.Topologic, error) {
|
||||
var topologics []orm.Topologic
|
||||
// ctx超时判断
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
result := _globalPostgresClient.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(recursiveSQL, pageID).Scan(&topologics)
|
||||
result := _globalPostgresClient.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(sql.RecursiveSQL, pageID).Scan(&topologics)
|
||||
if result.Error != nil {
|
||||
logger.Error("query circuit diagram topologic info by pageID failed", zap.Int64("pageID", pageID), zap.Error(result.Error))
|
||||
return nil, result.Error
|
||||
|
|
@ -38,29 +28,6 @@ func QueryTopologicByPageID(ctx context.Context, logger *zap.Logger, pageID int6
|
|||
return topologics, nil
|
||||
}
|
||||
|
||||
// InitCircuitDiagramTopologic return circuit diagram topologic info from postgres
|
||||
func InitCircuitDiagramTopologic(pageID int64, topologicNodes []orm.Topologic) error {
|
||||
var rootVertex uuid.UUID
|
||||
|
||||
for _, node := range topologicNodes {
|
||||
if node.UUIDFrom.IsNil() {
|
||||
rootVertex = node.UUIDTo
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
topologicSet := diagram.NewGraph(rootVertex)
|
||||
|
||||
for _, node := range topologicNodes {
|
||||
if node.UUIDFrom.IsNil() {
|
||||
continue
|
||||
}
|
||||
topologicSet.AddEdge(node.UUIDFrom, node.UUIDTo)
|
||||
}
|
||||
diagram.DiagramsOverview.Store(pageID, topologicSet)
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryTopologicFromDB return the result of query topologic info from postgresDB
|
||||
func QueryTopologicFromDB(ctx context.Context, logger *zap.Logger, gridID, zoneID, stationID int64) error {
|
||||
allPages, err := QueryAllPages(ctx, logger, gridID, zoneID, stationID)
|
||||
|
|
@ -83,3 +50,27 @@ func QueryTopologicFromDB(ctx context.Context, logger *zap.Logger, gridID, zoneI
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// InitCircuitDiagramTopologic return circuit diagram topologic info from postgres
|
||||
func InitCircuitDiagramTopologic(pageID int64, topologicNodes []orm.Topologic) error {
|
||||
var rootVertex uuid.UUID
|
||||
|
||||
for _, node := range topologicNodes {
|
||||
if node.UUIDFrom.IsNil() {
|
||||
rootVertex = node.UUIDTo
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
topologicSet := diagram.NewGraph(rootVertex)
|
||||
|
||||
for _, node := range topologicNodes {
|
||||
if node.UUIDFrom.IsNil() {
|
||||
continue
|
||||
}
|
||||
// TODO 增加对 node.flag值的判断
|
||||
topologicSet.AddEdge(node.UUIDFrom, node.UUIDTo)
|
||||
}
|
||||
diagram.StoreGraphMap(pageID, topologicSet)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,53 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UpdateComponentIntoDB define update component info of the circuit diagram into DB
|
||||
func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfos []network.ComponentUpdateInfo) error {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, info := range componentInfos {
|
||||
globalUUID, err := uuid.FromString(info.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
|
||||
componentInfo := orm.Component{
|
||||
GlobalUUID: globalUUID,
|
||||
GridID: info.GridID,
|
||||
ZoneID: info.ZoneID,
|
||||
StationID: info.StationID,
|
||||
ComponentType: info.ComponentType,
|
||||
State: info.State,
|
||||
ConnectedBus: info.ConnectedBus,
|
||||
Name: info.Name,
|
||||
VisibleID: info.Name,
|
||||
Description: info.Description,
|
||||
Context: info.Context,
|
||||
Comment: info.Comment,
|
||||
InService: info.InService,
|
||||
}
|
||||
result := tx.Model(&orm.Component{}).WithContext(cancelCtx).Updates(&componentInfo)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check update component conditions", constant.ErrUpdateRowZero)
|
||||
}
|
||||
return fmt.Errorf("update component info failed:%w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UpdateModelIntoDB define update component model params of the circuit diagram into DB
|
||||
func UpdateModelIntoDB(ctx context.Context, tx *gorm.DB, componentInfos []network.ComponentUpdateInfo) error {
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, componentInfo := range componentInfos {
|
||||
modelStruct := model.SelectModelByType(componentInfo.ComponentType)
|
||||
if modelStruct == nil {
|
||||
return fmt.Errorf("can not get component model by model type %d", componentInfo.ComponentType)
|
||||
}
|
||||
|
||||
err := jsoniter.Unmarshal([]byte(componentInfo.Params), modelStruct)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unmarshal component info by component struct %s,failed", model.SelectModelNameByType(componentInfo.ComponentType))
|
||||
}
|
||||
|
||||
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("format uuid from string type failed:%w", err)
|
||||
}
|
||||
modelStruct.SetUUID(globalUUID)
|
||||
|
||||
result := tx.Model(modelStruct).WithContext(cancelCtx).Where("uuid = ?", componentInfo.UUID).Updates(modelStruct)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check where conditions", constant.ErrUpdateRowZero)
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
// Package database define database operation functions
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// UpdateTopologicIntoDB define update topologic info of the circuit diagram query by pageID and topologic info
|
||||
func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, changeInfo network.TopologicUUIDChangeInfos) error {
|
||||
var result *gorm.DB
|
||||
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
switch changeInfo.ChangeType {
|
||||
case constant.UUIDFromChangeType:
|
||||
result = tx.WithContext(cancelCtx).Model(&orm.Topologic{}).Where("page_id = ? and uuid_from = ? and uuid_to = ?", pageID, changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo).Updates(orm.Topologic{UUIDFrom: changeInfo.NewUUIDFrom})
|
||||
case constant.UUIDToChangeType:
|
||||
result = tx.WithContext(cancelCtx).Model(&orm.Topologic{}).Where("page_id = ? and uuid_from = ? and uuid_to = ?", pageID, changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo).Updates(&orm.Topologic{UUIDTo: changeInfo.NewUUIDTo})
|
||||
case constant.UUIDAddChangeType:
|
||||
topologic := orm.Topologic{
|
||||
PageID: pageID,
|
||||
Flag: changeInfo.Flag,
|
||||
UUIDFrom: changeInfo.NewUUIDFrom,
|
||||
UUIDTo: changeInfo.OldUUIDFrom,
|
||||
Comment: changeInfo.Comment,
|
||||
}
|
||||
result = tx.WithContext(cancelCtx).Create(&topologic)
|
||||
}
|
||||
// update 检查 result.RowsAffected 结果时,RowsAffected==0
|
||||
// 可能存在 result.Error 为 nil 的情况,谨慎使用 result.Error.Error()
|
||||
// 函数,避免造成空指针问题
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check update topologic where conditions", constant.ErrUpdateRowZero)
|
||||
}
|
||||
return fmt.Errorf("insert or update topologic link failed:%w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -9,11 +9,11 @@ import (
|
|||
)
|
||||
|
||||
// DiagramsOverview define struct of storage all circuit diagram data
|
||||
var DiagramsOverview sync.Map
|
||||
var diagramsOverview sync.Map
|
||||
|
||||
// GetComponentMap define func of get circuit diagram data by global uuid
|
||||
func GetComponentMap(uuid string) (*cmap.ConcurrentMap[string, any], error) {
|
||||
value, ok := DiagramsOverview.Load(uuid)
|
||||
value, ok := diagramsOverview.Load(uuid)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can not find graph by global uuid:%s", uuid)
|
||||
}
|
||||
|
|
@ -23,3 +23,21 @@ func GetComponentMap(uuid string) (*cmap.ConcurrentMap[string, any], error) {
|
|||
}
|
||||
return paramsMap, nil
|
||||
}
|
||||
|
||||
// UpdateComponentMap define func of update circuit diagram data by global uuid and component info
|
||||
func UpdateComponentMap(uuid string, componentInfo *cmap.ConcurrentMap[string, any]) bool {
|
||||
_, result := diagramsOverview.Swap(uuid, componentInfo)
|
||||
return result
|
||||
}
|
||||
|
||||
// StoreComponentMap define func of store circuit diagram data with global uuid and component info
|
||||
func StoreComponentMap(uuid string, componentInfo *cmap.ConcurrentMap[string, any]) {
|
||||
diagramsOverview.Store(uuid, componentInfo)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteComponentMap define func of delete circuit diagram data with global uuid
|
||||
func DeleteComponentMap(uuid string) {
|
||||
diagramsOverview.Delete(uuid)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ import (
|
|||
"fmt"
|
||||
"sync"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
|
|
@ -44,7 +47,8 @@ func (g *Graph) CreateBackLink(vertex string) {
|
|||
}
|
||||
}
|
||||
|
||||
// AddEdge adds an edge between two VerticeLinks
|
||||
// AddEdge adds an edge between two verticeLinks
|
||||
// TODO 在添加拓扑信息时是否考虑过滤重复节点
|
||||
func (g *Graph) AddEdge(from, to uuid.UUID) {
|
||||
g.Lock()
|
||||
defer g.Unlock()
|
||||
|
|
@ -88,7 +92,7 @@ func (g *Graph) DelNode(vertex string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// DelEdge delete an edge between two VerticeLinks
|
||||
// DelEdge delete an edge between two verticeLinks
|
||||
func (g *Graph) DelEdge(from, to uuid.UUID) error {
|
||||
g.Lock()
|
||||
defer g.Unlock()
|
||||
|
|
@ -134,3 +138,14 @@ func (g *Graph) PrintGraph() {
|
|||
fmt.Printf("%s -> %v\n", vertex, edges)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateEdge update edge link info between two verticeLinks
|
||||
func (g *Graph) UpdateEdge(changeInfo network.TopologicUUIDChangeInfos) error {
|
||||
if changeInfo.ChangeType == constant.UUIDFromChangeType || changeInfo.ChangeType == constant.UUIDToChangeType {
|
||||
g.DelEdge(changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo)
|
||||
g.AddEdge(changeInfo.NewUUIDFrom, changeInfo.NewUUIDTo)
|
||||
} else {
|
||||
g.AddEdge(changeInfo.NewUUIDFrom, changeInfo.NewUUIDTo)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,11 +7,11 @@ import (
|
|||
)
|
||||
|
||||
// GraphOverview define struct of storage all circuit diagram topologic data
|
||||
var GraphOverview sync.Map
|
||||
var graphOverview sync.Map
|
||||
|
||||
// GetGraphMap define func of get circuit diagram topologic data by pageID
|
||||
func GetGraphMap(pageID int64) (*Graph, error) {
|
||||
value, ok := GraphOverview.Load(pageID)
|
||||
value, ok := graphOverview.Load(pageID)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("can not find graph by pageID:%d", pageID)
|
||||
}
|
||||
|
|
@ -21,3 +21,21 @@ func GetGraphMap(pageID int64) (*Graph, error) {
|
|||
}
|
||||
return graph, nil
|
||||
}
|
||||
|
||||
// UpdateGrapMap define func of update circuit diagram data by pageID and topologic info
|
||||
func UpdateGrapMap(pageID int64, graphInfo *Graph) bool {
|
||||
_, result := diagramsOverview.Swap(pageID, graphInfo)
|
||||
return result
|
||||
}
|
||||
|
||||
// StoreGraphMap define func of store circuit diagram topologic data with pageID and topologic info
|
||||
func StoreGraphMap(pageID int64, graphInfo *Graph) {
|
||||
diagramsOverview.Store(pageID, graphInfo)
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteGraphMap define func of delete circuit diagram topologic data with pageID
|
||||
func DeleteGraphMap(pageID int64) {
|
||||
diagramsOverview.Delete(pageID)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
{
|
||||
"page_id":1,
|
||||
"topologics":[
|
||||
{
|
||||
"uuid_from":"12311-111",
|
||||
"uuid_to":"12311-114"
|
||||
},
|
||||
{
|
||||
"uuid_from":"12311-115",
|
||||
"uuid_to":"12311-116"
|
||||
}
|
||||
],
|
||||
"free_vertexs":[
|
||||
"12311-111",
|
||||
"12311-112",
|
||||
"12311-115"
|
||||
],
|
||||
"component_infos":[
|
||||
{
|
||||
"grid_id":1,
|
||||
"zone":1,
|
||||
"station_id":1,
|
||||
"uuid":"12311-114",
|
||||
"name":"母线 1",
|
||||
"component_type":1,
|
||||
"in_service":true,
|
||||
"connected_bus":1,
|
||||
"state":1,
|
||||
"visible_id":"",
|
||||
"description":"",
|
||||
"context":"",
|
||||
"comment":"",
|
||||
"params":""
|
||||
},
|
||||
{
|
||||
"grid_id":1,
|
||||
"zone":1,
|
||||
"station_id":1,
|
||||
"uuid":"12311-116",
|
||||
"name":"发电机 1",
|
||||
"component_type":1,
|
||||
"in_service":true,
|
||||
"connected_bus":1,
|
||||
"state":1,
|
||||
"visible_id":"",
|
||||
"description":"",
|
||||
"context":"",
|
||||
"comment":"",
|
||||
"params":""
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"page_id":1,
|
||||
"topologics":[
|
||||
{
|
||||
"uuid_from":"12311-111",
|
||||
"uuid_to":"12311-114"
|
||||
},
|
||||
{
|
||||
"uuid_from":"12311-115",
|
||||
"uuid_to":"12311-116"
|
||||
}
|
||||
],
|
||||
"free_vertexs":[
|
||||
"12311-111",
|
||||
"12311-112",
|
||||
"12311-115"
|
||||
],
|
||||
"component_infos":[
|
||||
{
|
||||
"uuid":"12311-114",
|
||||
"component_type":1
|
||||
},
|
||||
{
|
||||
"uuid":"12311-116",
|
||||
"component_type":1
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
{
|
||||
"page_id":1,
|
||||
"topologics":[
|
||||
{
|
||||
"change_type":1,
|
||||
"old_uuid_from":"12311-111",
|
||||
"old_uuid_to":"12311-113",
|
||||
"new_uuid_from":"12311-111",
|
||||
"new_uuid_to":"12311-114"
|
||||
},
|
||||
{
|
||||
"change_type":2,
|
||||
"old_uuid_from":"12311-111",
|
||||
"old_uuid_to":"12311-113",
|
||||
"new_uuid_from":"12311-112",
|
||||
"new_uuid_to":"12311-113"
|
||||
},
|
||||
{
|
||||
"change_type":3,
|
||||
"old_uuid_from":"",
|
||||
"old_uuid_to":"",
|
||||
"new_uuid_from":"12311-115",
|
||||
"new_uuid_to":"12311-116"
|
||||
}
|
||||
],
|
||||
"free_vertexs":[
|
||||
"12311-111",
|
||||
"12311-112",
|
||||
"12311-115"
|
||||
],
|
||||
"component_infos":[
|
||||
{
|
||||
"grid_id":1,
|
||||
"zone":1,
|
||||
"station_id":1,
|
||||
"uuid":"12311-114",
|
||||
"name":"母线 1",
|
||||
"component_type":1,
|
||||
"in_service":true,
|
||||
"connected_bus":1,
|
||||
"state":1,
|
||||
"visible_id":"",
|
||||
"description":"",
|
||||
"context":"",
|
||||
"comment":"",
|
||||
"params":""
|
||||
},
|
||||
{
|
||||
"grid_id":1,
|
||||
"zone":1,
|
||||
"station_id":1,
|
||||
"uuid":"12311-116",
|
||||
"name":"发电机 1",
|
||||
"component_type":1,
|
||||
"in_service":true,
|
||||
"connected_bus":1,
|
||||
"state":1,
|
||||
"visible_id":"",
|
||||
"description":"",
|
||||
"context":"",
|
||||
"comment":"",
|
||||
"params":""
|
||||
}
|
||||
]
|
||||
}
|
||||
2
go.mod
2
go.mod
|
|
@ -6,6 +6,7 @@ require (
|
|||
github.com/confluentinc/confluent-kafka-go v1.9.2
|
||||
github.com/gin-gonic/gin v1.10.0
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/json-iterator/go v1.1.12
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/orcaman/concurrent-map/v2 v2.0.1
|
||||
github.com/panjf2000/ants/v2 v2.10.0
|
||||
|
|
@ -35,7 +36,6 @@ require (
|
|||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/magiconair/properties v1.8.7 // indirect
|
||||
|
|
|
|||
|
|
@ -0,0 +1,167 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/log"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofrs/uuid"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// CircuitDiagramCreateHandler define circuit diagram create process API
|
||||
func CircuitDiagramCreateHandler(c *gin.Context) {
|
||||
logger := log.GetLoggerInstance()
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
|
||||
var request network.CircuitDiagramCreateRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error("unmarshal circuit diagram create info failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
graph, err := diagram.GetGraphMap(request.PageID)
|
||||
if err != nil {
|
||||
logger.Error("get topologic data from set by pageID failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
var topologicCreateInfos []network.TopologicUUIDCreateInfo
|
||||
for _, topologicLink := range request.TopologicLinks {
|
||||
var createInfo network.TopologicUUIDCreateInfo
|
||||
UUIDFrom, err1 := uuid.FromString(topologicLink.UUIDFrom)
|
||||
UUIDTo, err2 := uuid.FromString(topologicLink.UUIDTo)
|
||||
if err1 != nil || err2 != nil {
|
||||
var err error
|
||||
if err1 != nil && err2 == nil {
|
||||
err = fmt.Errorf("convert uuid from string failed:%w", err1)
|
||||
} else if err1 == nil && err2 != nil {
|
||||
err = fmt.Errorf("convert uuid from string failed:%w", err2)
|
||||
} else {
|
||||
err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2)
|
||||
}
|
||||
|
||||
logger.Error("format uuid from string failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicLink,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
createInfo.UUIDFrom = UUIDFrom
|
||||
createInfo.UUIDTo = UUIDTo
|
||||
topologicCreateInfos = append(topologicCreateInfos, createInfo)
|
||||
}
|
||||
|
||||
// open transaction
|
||||
tx := pgClient.Begin()
|
||||
|
||||
err = database.CreateTopologicIntoDB(c, tx, request.PageID, topologicCreateInfos)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("create topologic info into DB failed", zap.Any("topologic_info", topologicCreateInfos), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_infos": topologicCreateInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
for _, topologicCreateInfo := range topologicCreateInfos {
|
||||
graph.AddEdge(topologicCreateInfo.UUIDFrom, topologicCreateInfo.UUIDTo)
|
||||
}
|
||||
|
||||
err = database.CreateComponentIntoDB(c, tx, request.ComponentInfos)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("insert component info into DB failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"component_infos": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
err = database.CreateModelIntoDB(c, tx, request.ComponentInfos)
|
||||
if err != nil {
|
||||
logger.Error("create component model into DB failed", zap.Any("component_infos", request.ComponentInfos), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": request.PageID,
|
||||
"component_infos": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
for _, componentInfo := range request.ComponentInfos {
|
||||
componentMap := cmap.New[any]()
|
||||
err = componentMap.UnmarshalJSON([]byte(componentInfo.Params))
|
||||
if err != nil {
|
||||
logger.Error("unmarshal component info by concurrent map failed", zap.String("component_params", componentInfo.Params), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
"component_params": componentInfo.Params,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
diagram.StoreComponentMap(componentInfo.UUID, &componentMap)
|
||||
}
|
||||
|
||||
if len(request.FreeVertexs) > 0 {
|
||||
for _, freeVertex := range request.FreeVertexs {
|
||||
graph.FreeVertexs[freeVertex] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// commit transsction
|
||||
tx.Commit()
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: network.ResponseHeader{Status: http.StatusOK},
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
@ -0,0 +1,208 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"modelRT/constant"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/log"
|
||||
"modelRT/model"
|
||||
"modelRT/network"
|
||||
"modelRT/orm"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofrs/uuid"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// CircuitDiagramDeleteHandler define circuit diagram delete process API
|
||||
func CircuitDiagramDeleteHandler(c *gin.Context) {
|
||||
logger := log.GetLoggerInstance()
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
|
||||
var request network.CircuitDiagramDeleteRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error("unmarshal circuit diagram del info failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
graph, err := diagram.GetGraphMap(request.PageID)
|
||||
if err != nil {
|
||||
logger.Error("get topologic data from set by pageID failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
var topologicDelInfos []network.TopologicUUIDDelInfo
|
||||
for _, topologicLink := range request.TopologicLinks {
|
||||
var delInfo network.TopologicUUIDDelInfo
|
||||
UUIDFrom, err1 := uuid.FromString(topologicLink.UUIDFrom)
|
||||
UUIDTo, err2 := uuid.FromString(topologicLink.UUIDTo)
|
||||
if err1 != nil || err2 != nil {
|
||||
var err error
|
||||
if err1 != nil && err2 == nil {
|
||||
err = fmt.Errorf("convert uuid from string failed:%w", err1)
|
||||
} else if err1 == nil && err2 != nil {
|
||||
err = fmt.Errorf("convert uuid from string failed:%w", err2)
|
||||
} else {
|
||||
err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2)
|
||||
}
|
||||
|
||||
logger.Error("format uuid from string failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicLink,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
delInfo.UUIDFrom = UUIDFrom
|
||||
delInfo.UUIDTo = UUIDTo
|
||||
topologicDelInfos = append(topologicDelInfos, delInfo)
|
||||
}
|
||||
|
||||
// open transaction
|
||||
tx := pgClient.Begin()
|
||||
|
||||
for _, topologicDelInfo := range topologicDelInfos {
|
||||
err = database.DeleteTopologicIntoDB(c, tx, request.PageID, topologicDelInfo)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("delete topologic info into DB failed", zap.Any("topologic_info", topologicDelInfo), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicDelInfo,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
err = graph.DelEdge(topologicDelInfo.UUIDFrom, topologicDelInfo.UUIDTo)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("delete topologic info failed", zap.Any("topologic_info", topologicDelInfo), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicDelInfo,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if len(graph.VerticeLinks) == 0 && len(graph.FreeVertexs) == 0 {
|
||||
diagram.DeleteGraphMap(request.PageID)
|
||||
}
|
||||
|
||||
for _, componentInfo := range request.ComponentInfos {
|
||||
cancelCtx, cancel := context.WithTimeout(c, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("format uuid from string failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
component := &orm.Component{GlobalUUID: globalUUID}
|
||||
result := tx.WithContext(cancelCtx).Delete(component)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
tx.Rollback()
|
||||
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
|
||||
}
|
||||
|
||||
logger.Error("delete component info into postgresDB failed", zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
modelStruct := model.SelectModelByType(component.ComponentType)
|
||||
modelStruct.SetUUID(globalUUID)
|
||||
result = tx.WithContext(cancelCtx).Delete(modelStruct)
|
||||
if result.Error != nil || result.RowsAffected == 0 {
|
||||
tx.Rollback()
|
||||
|
||||
err := result.Error
|
||||
if result.RowsAffected == 0 {
|
||||
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
|
||||
}
|
||||
|
||||
msg := fmt.Sprintf("delete component info from table %s failed", modelStruct.ReturnTableName())
|
||||
logger.Error(msg, zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
diagram.DeleteComponentMap(componentInfo.UUID)
|
||||
}
|
||||
|
||||
if len(request.FreeVertexs) > 0 {
|
||||
for _, freeVertex := range request.FreeVertexs {
|
||||
delete(graph.FreeVertexs, freeVertex)
|
||||
}
|
||||
}
|
||||
|
||||
// commit transsction
|
||||
tx.Commit()
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: network.ResponseHeader{Status: http.StatusOK},
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
|
@ -27,6 +26,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
topologicInfo, err := diagram.GetGraphMap(pageID)
|
||||
|
|
@ -40,6 +40,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
payLoad := make(map[string]interface{})
|
||||
payLoad["root_vertex"] = topologicInfo.RootVertex
|
||||
|
|
@ -47,7 +48,6 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
|
||||
componentParamMap := make(map[string][]byte)
|
||||
for _, VerticeLink := range topologicInfo.VerticeLinks {
|
||||
fmt.Println(VerticeLink)
|
||||
for _, componentUUID := range VerticeLink {
|
||||
UUIDStr := componentUUID.String()
|
||||
componentParams, err := diagram.GetComponentMap(UUIDStr)
|
||||
|
|
@ -61,6 +61,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
byteSlice, err := componentParams.MarshalJSON()
|
||||
if err != nil {
|
||||
|
|
@ -70,6 +71,7 @@ func CircuitDiagramLoadHandler(c *gin.Context) {
|
|||
ResponseHeader: header,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
componentParamMap[UUIDStr] = byteSlice
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,166 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/log"
|
||||
"modelRT/network"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// CircuitDiagramUpdateHandler define circuit diagram update process API
|
||||
func CircuitDiagramUpdateHandler(c *gin.Context) {
|
||||
logger := log.GetLoggerInstance()
|
||||
pgClient := database.GetPostgresDBClient()
|
||||
|
||||
var request network.CircuitDiagramUpdateRequest
|
||||
if err := c.ShouldBindJSON(&request); err != nil {
|
||||
logger.Error("unmarshal circuit diagram update info failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
graph, err := diagram.GetGraphMap(request.PageID)
|
||||
if err != nil {
|
||||
logger.Error("get topologic data from set by pageID failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
var topologicChangeInfos []network.TopologicUUIDChangeInfos
|
||||
for _, topologicLink := range request.TopologicLinks {
|
||||
changeInfo, err := network.ParseUUID(topologicLink)
|
||||
if err != nil {
|
||||
logger.Error("format uuid from string failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicLink,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
topologicChangeInfos = append(topologicChangeInfos, changeInfo)
|
||||
}
|
||||
|
||||
// open transaction
|
||||
tx := pgClient.Begin()
|
||||
|
||||
for _, topologicChangeInfo := range topologicChangeInfos {
|
||||
err = database.UpdateTopologicIntoDB(c, tx, request.PageID, topologicChangeInfo)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("update topologic info into DB failed", zap.Any("topologic_info", topologicChangeInfo), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicChangeInfo,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
err = graph.UpdateEdge(topologicChangeInfo)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
|
||||
logger.Error("update topologic info failed", zap.Any("topologic_info", topologicChangeInfo), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"topologic_info": topologicChangeInfo,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = database.UpdateComponentIntoDB(c, tx, request.ComponentInfos)
|
||||
if err != nil {
|
||||
logger.Error("udpate component info into DB failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
"component_info": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
err = database.UpdateModelIntoDB(c, tx, request.ComponentInfos)
|
||||
if err != nil {
|
||||
logger.Error("udpate component model info into DB failed", zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
"component_info": request.ComponentInfos,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
|
||||
for _, componentInfo := range request.ComponentInfos {
|
||||
componentMap := cmap.New[any]()
|
||||
err = componentMap.UnmarshalJSON([]byte(componentInfo.Params))
|
||||
if err != nil {
|
||||
logger.Error("unmarshal component info by concurrent map failed", zap.String("component_params", componentInfo.Params), zap.Error(err))
|
||||
header := network.ResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: header,
|
||||
PayLoad: map[string]interface{}{
|
||||
"uuid": componentInfo.UUID,
|
||||
"component_params": componentInfo.Params,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
return
|
||||
}
|
||||
diagram.UpdateComponentMap(componentInfo.UUID, &componentMap)
|
||||
}
|
||||
|
||||
if len(request.FreeVertexs) > 0 {
|
||||
for _, freeVertex := range request.FreeVertexs {
|
||||
graph.FreeVertexs[freeVertex] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
||||
// commit transsction
|
||||
tx.Commit()
|
||||
|
||||
resp := network.BasicResponse{
|
||||
ResponseHeader: network.ResponseHeader{Status: http.StatusOK},
|
||||
PayLoad: map[string]interface{}{
|
||||
"page_id": request.PageID,
|
||||
},
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
16
main.go
16
main.go
|
|
@ -11,7 +11,8 @@ import (
|
|||
"modelRT/handler"
|
||||
"modelRT/log"
|
||||
"modelRT/middleware"
|
||||
"modelRT/model"
|
||||
"modelRT/pool"
|
||||
subscription "modelRT/real-time-data"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
|
|
@ -37,7 +38,7 @@ var (
|
|||
logger *zap.Logger
|
||||
)
|
||||
|
||||
// TODO 使用 wire 依赖注入
|
||||
// TODO 使用 wire 依赖注入管理 DVIE 面板注册的 panel
|
||||
func main() {
|
||||
flag.Parse()
|
||||
ctx := context.TODO()
|
||||
|
|
@ -59,7 +60,7 @@ func main() {
|
|||
defer logger.Sync()
|
||||
|
||||
// init ants pool
|
||||
pool, err := ants.NewPoolWithFunc(modelRTConfig.ParseConcurrentQuantity, model.ParseFunc)
|
||||
pool, err := ants.NewPoolWithFunc(modelRTConfig.ParseConcurrentQuantity, pool.ParseFunc)
|
||||
if err != nil {
|
||||
logger.Error("init concurrent parse task pool failed", zap.Error(err))
|
||||
panic(err)
|
||||
|
|
@ -79,12 +80,15 @@ func main() {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
// TODO 完成订阅数据分析
|
||||
go subscription.RealTimeDataComputer(ctx, nil, []string{}, "")
|
||||
|
||||
engine := gin.Default()
|
||||
engine.Use(limiter.Middleware)
|
||||
engine.GET("/model/diagram_load", handler.CircuitDiagramLoadHandler)
|
||||
engine.POST("/model/diagram_create", nil)
|
||||
engine.POST("/model/diagram_update", nil)
|
||||
engine.POST("/model/diagram_delete", nil)
|
||||
engine.POST("/model/diagram_create", handler.CircuitDiagramCreateHandler)
|
||||
engine.POST("/model/diagram_update", handler.CircuitDiagramUpdateHandler)
|
||||
engine.POST("/model/diagram_delete", handler.CircuitDiagramDeleteHandler)
|
||||
|
||||
// start route with 8080 port
|
||||
engine.Run(":8080")
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import "github.com/gofrs/uuid"
|
||||
|
||||
// BasicModelInterface define basic model type interface
|
||||
type BasicModelInterface interface {
|
||||
SetUUID(uuid uuid.UUID)
|
||||
ReturnTableName() string
|
||||
}
|
||||
|
|
@ -1,8 +1,21 @@
|
|||
// Package model define model struct of model runtime service
|
||||
package model
|
||||
|
||||
import "modelRT/constant"
|
||||
import (
|
||||
"modelRT/constant"
|
||||
"modelRT/orm"
|
||||
)
|
||||
|
||||
func SelectModelByType(modelType int) string {
|
||||
// SelectModelByType define select the data structure for parsing based on the input model type
|
||||
func SelectModelByType(modelType int) BasicModelInterface {
|
||||
if modelType == constant.BusbarType {
|
||||
return &orm.BusbarSection{}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SelectModelNameByType define select the data structure name for parsing based on the input model type
|
||||
func SelectModelNameByType(modelType int) string {
|
||||
if modelType == constant.BusbarType {
|
||||
return "BusBarSection"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,46 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
import "github.com/gofrs/uuid"
|
||||
|
||||
// TopologicCreateInfo defines circuit diagram topologic create info
|
||||
type TopologicCreateInfo struct {
|
||||
UUIDFrom string `json:"uuid_from"`
|
||||
UUIDTo string `json:"uuid_to"`
|
||||
Flag int `json:"flag"`
|
||||
Comment int `json:"comment"`
|
||||
}
|
||||
|
||||
// TopologicUUIDCreateInfo defines circuit diagram topologic uuid create info
|
||||
type TopologicUUIDCreateInfo struct {
|
||||
UUIDFrom uuid.UUID `json:"uuid_from"`
|
||||
UUIDTo uuid.UUID `json:"uuid_to"`
|
||||
Flag int `json:"flag"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
// ComponentCreateInfo defines circuit diagram component create index info
|
||||
type ComponentCreateInfo struct {
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
VisibleID string `json:"visible_id"`
|
||||
Description string `json:"description"`
|
||||
Context string `json:"context"`
|
||||
Comment string `json:"comment"`
|
||||
Params string `json:"params"`
|
||||
GridID int64 `json:"grid_id"`
|
||||
ZoneID int64 `json:"zone_id"`
|
||||
StationID int64 `json:"station_id"`
|
||||
ComponentType int `json:"component_type"`
|
||||
State int `json:"state"`
|
||||
ConnectedBus int `json:"connected_bus"`
|
||||
InService bool `json:"in_service"`
|
||||
}
|
||||
|
||||
// CircuitDiagramCreateRequest defines request params of circuit diagram create api
|
||||
type CircuitDiagramCreateRequest struct {
|
||||
PageID int64 `json:"page_id"`
|
||||
FreeVertexs []string `json:"free_vertexs"`
|
||||
TopologicLinks []TopologicCreateInfo `json:"topologics"`
|
||||
ComponentInfos []ComponentCreateInfo `json:"component_infos"`
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
import "github.com/gofrs/uuid"
|
||||
|
||||
// TopologicDelInfo defines circuit diagram topologic delete info
|
||||
type TopologicDelInfo struct {
|
||||
UUIDFrom string `json:"uuid_from"`
|
||||
UUIDTo string `json:"uuid_to"`
|
||||
}
|
||||
|
||||
// TopologicUUIDDelInfo defines circuit diagram topologic uuid delete info
|
||||
type TopologicUUIDDelInfo struct {
|
||||
UUIDFrom uuid.UUID `json:"uuid_from"`
|
||||
UUIDTo uuid.UUID `json:"uuid_to"`
|
||||
}
|
||||
|
||||
// ComponentDelInfo defines circuit diagram component delete index info
|
||||
type ComponentDelInfo struct {
|
||||
UUID string `json:"uuid"`
|
||||
ComponentType int `json:"component_type"`
|
||||
}
|
||||
|
||||
// CircuitDiagramDeleteRequest defines request params of circuit diagram delete api
|
||||
type CircuitDiagramDeleteRequest struct {
|
||||
PageID int64 `json:"page_id"`
|
||||
FreeVertexs []string `json:"free_vertexs"`
|
||||
TopologicLinks []TopologicDelInfo `json:"topologics"`
|
||||
ComponentInfos []ComponentDelInfo `json:"component_infos"`
|
||||
}
|
||||
|
|
@ -0,0 +1,142 @@
|
|||
// Package network define struct of network operation
|
||||
package network
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"modelRT/constant"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
)
|
||||
|
||||
// TopologicChangeInfo defines circuit diagram topologic change info
|
||||
type TopologicChangeInfo struct {
|
||||
ChangeType int `json:"change_type"`
|
||||
Flag int `json:"flag"`
|
||||
OldUUIDFrom string `json:"old_uuid_from"`
|
||||
OldUUIDTo string `json:"old_uuid_to"`
|
||||
NewUUIDFrom string `json:"new_uuid_from"`
|
||||
NewUUIDTo string `json:"new_uuid_to"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
// TopologicUUIDChangeInfos defines circuit diagram topologic uuid change info
|
||||
type TopologicUUIDChangeInfos struct {
|
||||
ChangeType int `json:"change_type"`
|
||||
Flag int `json:"flag"`
|
||||
OldUUIDFrom uuid.UUID `json:"old_uuid_from"`
|
||||
OldUUIDTo uuid.UUID `json:"old_uuid_to"`
|
||||
NewUUIDFrom uuid.UUID `json:"new_uuid_from"`
|
||||
NewUUIDTo uuid.UUID `json:"new_uuid_to"`
|
||||
Comment string `json:"comment"`
|
||||
}
|
||||
|
||||
// ComponentUpdateInfo defines circuit diagram component params info
|
||||
type ComponentUpdateInfo struct {
|
||||
UUID string `json:"uuid"`
|
||||
Name string `json:"name"`
|
||||
VisibleID string `json:"visible_id"`
|
||||
Description string `json:"description"`
|
||||
Context string `json:"context"`
|
||||
Comment string `json:"comment"`
|
||||
Params string `json:"params"`
|
||||
GridID int64 `json:"grid_id"`
|
||||
ZoneID int64 `json:"zone_id"`
|
||||
StationID int64 `json:"station_id"`
|
||||
ComponentType int `json:"component_type"`
|
||||
State int `json:"state"`
|
||||
ConnectedBus int `json:"connected_bus"`
|
||||
InService bool `json:"in_service"`
|
||||
}
|
||||
|
||||
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api
|
||||
type CircuitDiagramUpdateRequest struct {
|
||||
PageID int64 `json:"page_id"`
|
||||
FreeVertexs []string `json:"free_vertexs"`
|
||||
TopologicLinks []TopologicChangeInfo `json:"topologics"`
|
||||
ComponentInfos []ComponentUpdateInfo `json:"component_infos"`
|
||||
}
|
||||
|
||||
// ParseUUID define parse UUID by change type in topologic change struct
|
||||
func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) {
|
||||
var UUIDChangeInfo TopologicUUIDChangeInfos
|
||||
|
||||
switch info.ChangeType {
|
||||
case constant.UUIDFromChangeType:
|
||||
if info.NewUUIDFrom == info.OldUUIDFrom {
|
||||
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constant.ErrUUIDFromCheckT1)
|
||||
}
|
||||
if info.NewUUIDTo != info.OldUUIDTo {
|
||||
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constant.ErrUUIDToCheckT1)
|
||||
}
|
||||
|
||||
oldUUIDFrom, err := uuid.FromString(info.OldUUIDFrom)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,old uuid_from value:%s", info.OldUUIDFrom)
|
||||
}
|
||||
UUIDChangeInfo.OldUUIDFrom = oldUUIDFrom
|
||||
|
||||
newUUIDFrom, err := uuid.FromString(info.NewUUIDFrom)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,new uuid_from value:%s", info.NewUUIDFrom)
|
||||
}
|
||||
UUIDChangeInfo.NewUUIDFrom = newUUIDFrom
|
||||
|
||||
OldUUIDTo, err := uuid.FromString(info.OldUUIDTo)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,old uuid_to value:%s", info.OldUUIDTo)
|
||||
}
|
||||
UUIDChangeInfo.OldUUIDTo = OldUUIDTo
|
||||
UUIDChangeInfo.NewUUIDTo = OldUUIDTo
|
||||
case constant.UUIDToChangeType:
|
||||
if info.NewUUIDFrom != info.OldUUIDFrom {
|
||||
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constant.ErrUUIDFromCheckT2)
|
||||
}
|
||||
if info.NewUUIDTo == info.OldUUIDTo {
|
||||
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constant.ErrUUIDToCheckT2)
|
||||
}
|
||||
|
||||
oldUUIDFrom, err := uuid.FromString(info.OldUUIDFrom)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,old uuid_from value:%s", info.OldUUIDFrom)
|
||||
}
|
||||
UUIDChangeInfo.OldUUIDFrom = oldUUIDFrom
|
||||
UUIDChangeInfo.NewUUIDFrom = oldUUIDFrom
|
||||
|
||||
OldUUIDTo, err := uuid.FromString(info.OldUUIDTo)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,old uuid_to value:%s", info.OldUUIDTo)
|
||||
}
|
||||
UUIDChangeInfo.OldUUIDTo = OldUUIDTo
|
||||
|
||||
newUUIDTo, err := uuid.FromString(info.NewUUIDTo)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,new uuid_to value:%s", info.NewUUIDTo)
|
||||
}
|
||||
UUIDChangeInfo.NewUUIDTo = newUUIDTo
|
||||
case constant.UUIDAddChangeType:
|
||||
if info.OldUUIDFrom != "" {
|
||||
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constant.ErrUUIDFromCheckT3)
|
||||
}
|
||||
if info.OldUUIDTo != "" {
|
||||
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constant.ErrUUIDToCheckT3)
|
||||
}
|
||||
|
||||
newUUIDFrom, err := uuid.FromString(info.NewUUIDFrom)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,new uuid_from value:%s", info.NewUUIDFrom)
|
||||
}
|
||||
UUIDChangeInfo.NewUUIDFrom = newUUIDFrom
|
||||
|
||||
newUUIDTo, err := uuid.FromString(info.NewUUIDTo)
|
||||
if err != nil {
|
||||
return UUIDChangeInfo, fmt.Errorf("convert data from string type to uuid type failed,new uuid_to value:%s", info.NewUUIDTo)
|
||||
}
|
||||
UUIDChangeInfo.NewUUIDTo = newUUIDTo
|
||||
default:
|
||||
return UUIDChangeInfo, constant.ErrUUIDChangeType
|
||||
}
|
||||
UUIDChangeInfo.Flag = info.Flag
|
||||
UUIDChangeInfo.Comment = info.Comment
|
||||
return UUIDChangeInfo, nil
|
||||
}
|
||||
|
|
@ -1,19 +1,20 @@
|
|||
package model
|
||||
// Package orm define database data struct
|
||||
package orm
|
||||
|
||||
import "github.com/gofrs/uuid"
|
||||
|
||||
type BusbarSection struct {
|
||||
// 母线基本参数
|
||||
Name string // 母线端名称,默认值BusX
|
||||
BusbarNumber int // 母线编号,默认值1
|
||||
UUID uuid.UUID
|
||||
StandardVoltage float32 // 标准电压,单位kV,范围值在000.01~500.00
|
||||
Desc string // 描述
|
||||
IsService bool // 是否服役,值为运行/退出
|
||||
Status string // 状态,值为现役/新建/计划/检修/库存可用/库存报废
|
||||
PowerGridName string // 当前工程电网的顶层建模时的电网名称
|
||||
RegionName string // 当前工程电网的顶层建模时的区域电网名称
|
||||
FactoryStationName string // 当前工程电网的顶层建模时的厂站名称
|
||||
Name string // 母线端名称,默认值BusX
|
||||
BusbarNumber int // 母线编号,默认值1
|
||||
UUID uuid.UUID `gorm:"column:uuid;primaryKey"`
|
||||
StandardVoltage float32 // 标准电压,单位kV,范围值在000.01~500.00
|
||||
Desc string // 描述
|
||||
IsService bool // 是否服役,值为运行/退出
|
||||
Status string // 状态,值为现役/新建/计划/检修/库存可用/库存报废
|
||||
PowerGridName string // 当前工程电网的顶层建模时的电网名称
|
||||
RegionName string // 当前工程电网的顶层建模时的区域电网名称
|
||||
FactoryStationName string // 当前工程电网的顶层建模时的厂站名称
|
||||
// 母线模型参数
|
||||
VoltagePercentValue float32 // 以母线标称电压为基准的百分数,默认值1.00~200.00
|
||||
VoltageCalculcatedValue float32 // 通过StandardVoltage与VoltagePercentValtage计算得出的电压值,默认值0.01~1000.00
|
||||
|
|
@ -61,6 +62,22 @@ type BusbarSection struct {
|
|||
StatusMeasurementPoint []string // 状态测点测点
|
||||
}
|
||||
|
||||
// TableName func respresent return table name of BusbarSection
|
||||
func (b *BusbarSection) TableName() string {
|
||||
return "BusbarSection"
|
||||
}
|
||||
|
||||
// SetUUID func implement BasicModelInterface interface
|
||||
func (b *BusbarSection) SetUUID(uuid uuid.UUID) {
|
||||
b.UUID = uuid
|
||||
return
|
||||
}
|
||||
|
||||
// ReturnTableName func implement BasicModelInterface interface
|
||||
func (b *BusbarSection) ReturnTableName() string {
|
||||
return "BusbarSection"
|
||||
}
|
||||
|
||||
func NewBusbarSection(name string) (*BusbarSection, error) {
|
||||
uuid, err := uuid.NewV4()
|
||||
if err != nil {
|
||||
|
|
@ -8,7 +8,7 @@ import (
|
|||
// Component structure define abstracted info set of electrical component
|
||||
type Component struct {
|
||||
ID int64 `gorm:"column:id"`
|
||||
GlobalUUID uuid.UUID `gorm:"column:global_uuid"`
|
||||
GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"`
|
||||
GridID int64 `gorm:"column:grid"`
|
||||
ZoneID int64 `gorm:"column:zone"`
|
||||
StationID int64 `gorm:"column:station"`
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
package model
|
||||
// Package pool define concurrency call function in ants
|
||||
package pool
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
@ -8,6 +9,7 @@ import (
|
|||
"modelRT/config"
|
||||
"modelRT/database"
|
||||
"modelRT/diagram"
|
||||
"modelRT/model"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
|
@ -31,7 +33,7 @@ var ParseFunc = func(parseConfig interface{}) {
|
|||
panic(err)
|
||||
}
|
||||
|
||||
tableName := SelectModelByType(modelParseConfig.ComponentInfo.ComponentType)
|
||||
tableName := model.SelectModelNameByType(modelParseConfig.ComponentInfo.ComponentType)
|
||||
result := pgClient.Table(tableName).WithContext(cancelCtx).Find(&unmarshalMap)
|
||||
if result.Error != nil {
|
||||
logger.Error("query component detail info failed", zap.Error(result.Error))
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
// Package readltimedata define real time data operation functions
|
||||
package readltimedata
|
||||
// Package subscription define real time data operation functions
|
||||
package subscription
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
|
|
|||
|
|
@ -0,0 +1,17 @@
|
|||
// Package sql define database sql statement
|
||||
package sql
|
||||
|
||||
// RecursiveSQL define Topologic table recursive query statement
|
||||
var RecursiveSQL = `WITH RECURSIVE recursive_tree as (
|
||||
SELECT uuid_from,uuid_to,page_id,flag
|
||||
FROM "Topologic"
|
||||
WHERE uuid_from is null and page_id = ?
|
||||
UNION ALL
|
||||
SELECT t.uuid_from,t.uuid_to,t.page_id,t.flag
|
||||
FROM "Topologic" t
|
||||
JOIN recursive_tree rt ON t.uuid_from = rt.uuid_to
|
||||
)
|
||||
SELECT * FROM recursive_tree;`
|
||||
|
||||
// TODO 为 Topologic 表增加唯一索引
|
||||
// CREATE UNIQUE INDEX uuid_from_to_page_id_idx ON public."Topologic"(uuid_from,uuid_to,page_id);
|
||||
Loading…
Reference in New Issue