refactor(optimize storage struct): optimize topologic storage struct

1.optimize uuid start and end node of uuid nil node str
        2.optimize topologic query sql of init topologic in memory
This commit is contained in:
douxu 2025-05-16 14:24:55 +08:00
parent daf30766ba
commit 237c7ecf69
2 changed files with 32 additions and 25 deletions

View File

@ -14,9 +14,9 @@ const (
)
const (
// SpecialUUIDStr 拓扑信息中开始节点与结束节点字符串形式
SpecialUUIDStr = "00000000-0000-0000-0000-000000000000"
// UUIDNilStr 拓扑信息中开始节点与结束节点字符串形式
UUIDNilStr = "00000000-0000-0000-0000-000000000000"
)
// SpecialUUID 拓扑信息中开始节点与结束节点 UUID 格式
var SpecialUUID = uuid.FromStringOrNil(SpecialUUIDStr)
// UUIDNil 拓扑信息中开始节点与结束节点 UUID 格式
var UUIDNil = uuid.FromStringOrNil(UUIDNilStr)

View File

@ -24,16 +24,14 @@ func QueryTopologic(ctx context.Context, tx *gorm.DB, logger *zap.Logger) ([]orm
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(sql.RecursiveSQL, constant.SpecialUUIDStr).Scan(&topologics)
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Raw(sql.RecursiveSQL, constant.UUIDNilStr).Scan(&topologics)
if result.Error != nil {
logger.Error("query circuit diagram topologic info by start node uuid failed", zap.String("start_node_uuid", constant.SpecialUUIDStr), zap.Error(result.Error))
logger.Error("query circuit diagram topologic info by start node uuid failed", zap.String("start_node_uuid", constant.UUIDNilStr), zap.Error(result.Error))
return nil, result.Error
}
return topologics, nil
}
// TODO 电流互感器不单独划分间隔
// TODO 以母线、浇筑母线、变压器为间隔原件
// QueryTopologicFromDB return the result of query topologic info from DB
func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, logger *zap.Logger, componentTypeMap map[uuid.UUID]int) error {
topologicInfos, err := QueryTopologic(ctx, tx, logger)
@ -60,7 +58,7 @@ func QueryTopologicFromDB(ctx context.Context, tx *gorm.DB, logger *zap.Logger,
func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMap map[uuid.UUID]int) error {
var rootVertex *diagram.MultiBranchTreeNode
for _, node := range topologicNodes {
if node.UUIDFrom == constant.SpecialUUID {
if node.UUIDFrom == constant.UUIDNil {
// rootVertex = node.UUIDTo
var componentType int
componentType, ok := componentTypeMap[node.UUIDFrom]
@ -77,7 +75,7 @@ func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMa
}
for _, node := range topologicNodes {
if node.UUIDFrom == constant.SpecialUUID {
if node.UUIDFrom == constant.UUIDNil {
var componentType int
componentType, ok := componentTypeMap[node.UUIDTo]
if !ok {
@ -97,11 +95,18 @@ func InitCircuitDiagramTopologic(topologicNodes []orm.Topologic, componentTypeMa
return nil
}
// TODO 电流互感器不单独划分间隔
// TODO 以母线、浇筑母线、变压器为间隔原件
func IntervalBoundaryDetermine(uuid uuid.UUID) bool {
// TODO 从diagramsOverview中根据 uuid 获取 component 信息
var componentID int64
diagram.GetComponentMap(componentID)
// TODO 判断 component 的类型是否为间隔
// TODO 0x1111A1B2C3D4,高四位表示可以成为间隔的compoent类型的值为FFFF,普通 component 类型的值为 0000。低四位中前二位表示component的一级类型例如母线 PT、母联/母分、进线等,低四位中后二位表示一级类型中包含的具体类型,例如母线 PT中包含的电压互感器、隔离开关、接地开关、避雷器、带电显示器等。
num := uint32(0xA1B2C3D4) // 八位16进制数
high16 := uint16(num >> 16)
fmt.Printf("原始值: 0x%X\n", num) // 输出: 0xA1B2C3D4
fmt.Printf("高十六位: 0x%X\n", high16) // 输出: 0xA1B2
return true
}
@ -111,9 +116,11 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
for _, topo := range topologics {
// skip special uuid
if topo.UUIDFrom != constant.SpecialUUID {
if _, exists := nodeMap[topo.UUIDFrom]; !exists {
componentType, ok := componentTypeMap[topo.UUIDFrom]
var componentType int
if topo.UUIDTo != constant.UUIDNil {
var ok bool
componentType, ok = componentTypeMap[topo.UUIDFrom]
if !ok {
return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDFrom)
}
@ -126,10 +133,14 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
}
if _, exists := nodeMap[topo.UUIDTo]; !exists {
componentType, ok := componentTypeMap[topo.UUIDTo]
var componentType int
if topo.UUIDTo != constant.UUIDNil {
var ok bool
componentType, ok = componentTypeMap[topo.UUIDTo]
if !ok {
return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDTo)
}
}
nodeMap[topo.UUIDTo] = &diagram.MultiBranchTreeNode{
ID: topo.UUIDTo,
@ -140,17 +151,13 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
for _, topo := range topologics {
var parent *diagram.MultiBranchTreeNode
if topo.UUIDFrom == constant.SpecialUUID {
componentType, ok := componentTypeMap[topo.UUIDTo]
if !ok {
return nil, fmt.Errorf("can not get component type by uuid: %s", topo.UUIDTo)
}
if topo.UUIDFrom == constant.UUIDNil {
var componentType int
parent = &diagram.MultiBranchTreeNode{
ID: constant.SpecialUUID,
ID: constant.UUIDNil,
NodeComponentType: componentType,
}
nodeMap[constant.SpecialUUID] = parent
nodeMap[constant.UUIDNil] = parent
} else {
parent = nodeMap[topo.UUIDFrom]
}
@ -161,7 +168,7 @@ func BuildMultiBranchTree(topologics []orm.Topologic, componentTypeMap map[uuid.
}
// return root vertex
root, exists := nodeMap[constant.SpecialUUID]
root, exists := nodeMap[constant.UUIDNil]
if !exists {
return nil, fmt.Errorf("root node not found")
}