From 70bcb00062aed7ab0cc6dd2547ffd62cd3f170b4 Mon Sep 17 00:00:00 2001 From: douxu Date: Thu, 18 Dec 2025 17:50:43 +0800 Subject: [PATCH] add code of component attribute group store --- database/filling_attr_model_info.go | 4 +- database/query_component.go | 23 +++++++- model/attribute_group_model.go | 66 ++++++++++++++++++----- network/circuit_diagram_create_request.go | 5 +- orm/attribute_group.go | 9 ++++ orm/circuit_diagram_component.go | 4 +- 6 files changed, 89 insertions(+), 22 deletions(-) create mode 100644 orm/attribute_group.go diff --git a/database/filling_attr_model_info.go b/database/filling_attr_model_info.go index 6cf2a33..408f7f7 100644 --- a/database/filling_attr_model_info.go +++ b/database/filling_attr_model_info.go @@ -57,7 +57,7 @@ func ParseAttrToken(ctx context.Context, tx *gorm.DB, attrToken, clientToken str // FillingShortAttrModel define filling short attribute model info func FillingShortAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string, attrModel *model.ShortAttrInfo) error { - component, err := QueryComponentByNsPath(ctx, tx, attrItems[0]) + component, err := QueryComponentByNSPath(ctx, tx, attrItems[0]) if err != nil { return err } @@ -82,7 +82,7 @@ func FillingLongAttrModel(ctx context.Context, tx *gorm.DB, attrItems []string, return err } attrModel.StationInfo = &station - component, err := QueryComponentByNsPath(ctx, tx, attrItems[3]) + component, err := QueryComponentByNSPath(ctx, tx, attrItems[3]) if err != nil { return err } diff --git a/database/query_component.go b/database/query_component.go index 7a42de9..3ebc279 100644 --- a/database/query_component.go +++ b/database/query_component.go @@ -69,8 +69,8 @@ func QueryComponentByPageID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (o return component, nil } -// QueryComponentByNsPath return the result of query circuit diagram component info by ns path from postgresDB -func QueryComponentByNsPath(ctx context.Context, tx *gorm.DB, nsPath string) (orm.Component, error) { +// QueryComponentByNSPath return the result of query circuit diagram component info by ns path from postgresDB +func QueryComponentByNSPath(ctx context.Context, tx *gorm.DB, nsPath string) (orm.Component, error) { var component orm.Component // ctx超时判断 cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second) @@ -137,3 +137,22 @@ func QueryShortIdentModelInfoByToken(ctx context.Context, tx *gorm.DB, measTag s } return &resultComp, &meauserment, nil } + +// GetGlobalUUIDTagStringMap define func to query global_uuid、tag、nspath field +func GetGlobalUUIDTagStringMap(db *gorm.DB) (map[string]orm.AttributeSet, error) { + var results []orm.Component + resMap := make(map[string]orm.AttributeSet) + + err := db.Model(&orm.Component{}).Select("global_uuid", "station", "tag", "nspath").Find(&results).Error + if err != nil { + return nil, err + } + + for _, r := range results { + resMap[r.GlobalUUID.String()] = orm.AttributeSet{ + Tag: r.Tag, + NSPath: r.NSPath, + } + } + return resMap, nil +} diff --git a/model/attribute_group_model.go b/model/attribute_group_model.go index 20fae25..b18eb43 100644 --- a/model/attribute_group_model.go +++ b/model/attribute_group_model.go @@ -5,22 +5,27 @@ import ( "context" "errors" "fmt" - "log" "modelRT/logger" "modelRT/orm" + "github.com/gofrs/uuid" "gorm.io/gorm" ) +type columnParam struct { + AttributeType string + AttributeGroup map[string]any +} + // TraverseAttributeGroupTables define func to traverse component attribute group tables -func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB) error { +func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compParamMap map[string]orm.AttributeSet) error { var tableNames []string result := db.Model(&orm.ProjectManager{}).Pluck("name", &tableNames) - fmt.Println(result) if result.Error != nil && result.Error != gorm.ErrRecordNotFound { logger.Error(ctx, "query name column data from postgres table failed", "err", result.Error) + return result.Error } if len(tableNames) == 0 { @@ -54,19 +59,54 @@ func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB) error { return err } - tableSchema := make(map[string]gorm.ColumnType) + for _, record := range records { + attributeGroup := make(map[string]any) + var componentUUIDStr string + var attrSet orm.AttributeSet + var attributeType string + for _, col := range columnTypes { + colName := col.Name() - for _, col := range columnTypes { - name := col.Name() - dataType, _ := col.ColumnType() - log.Printf("column: %s, Type: %s", name, dataType) - if name != "id" { - tableSchema[name] = col + value, exists := record[colName] + if !exists { + err := errors.New("can not find value from record by column name") + logger.Error(ctx, "query value by column name failed", "column_name", colName, "error", err) + return err + } + switch colName { + case "id": + case "global_uuid": + componentUUIDStr = value.(string) + attrSet, exists = compParamMap[componentUUIDStr] + if !exists { + err := errors.New("can not find component tag from record by global uuid") + logger.Error(ctx, "check component info by global uuid failed", "global_uuid", componentUUIDStr, "error", err) + return err + } + case "attribute_group": + attributeType = value.(string) + default: + attributeGroup[colName] = value + } } + + columnParam := columnParam{ + AttributeType: attributeType, + AttributeGroup: attributeGroup, + } + + // TODO 将筛选后的 recordSchema 属性值加入到缓存系统中 + // TODO 增加 station tag的获取 + go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam) } - - // TODO 将筛选后的属性值加入到缓存系统中 } - return nil } + +func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, params columnParam) { + fmt.Println(ctx) + fmt.Println(attributeSet) + uuid, err := uuid.FromString(compUUIDStr) + fmt.Println(uuid, err) + fmt.Println(params) +} diff --git a/network/circuit_diagram_create_request.go b/network/circuit_diagram_create_request.go index 56e8623..761cb6e 100644 --- a/network/circuit_diagram_create_request.go +++ b/network/circuit_diagram_create_request.go @@ -68,9 +68,8 @@ func ConvertComponentCreateInfosToComponents(info ComponentCreateInfo) (*orm.Com component := &orm.Component{ GlobalUUID: uuidVal, - // NsPath: info.NsPath, - Tag: info.Tag, - Name: info.Name, + Tag: info.Tag, + Name: info.Name, // ModelName: info.ModelName, // Description: info.Description, // GridID: info.GridID, diff --git a/orm/attribute_group.go b/orm/attribute_group.go new file mode 100644 index 0000000..2232eb0 --- /dev/null +++ b/orm/attribute_group.go @@ -0,0 +1,9 @@ +// Package orm define database data struct +package orm + +// AttributeSet define struct to return station tag、component Tag、 NSPath field +type AttributeSet struct { + Tag string + NSPath string + StationTag string +} diff --git a/orm/circuit_diagram_component.go b/orm/circuit_diagram_component.go index f137832..968c475 100644 --- a/orm/circuit_diagram_component.go +++ b/orm/circuit_diagram_component.go @@ -10,7 +10,7 @@ import ( // Component structure define abstracted info set of electrical component type Component struct { GlobalUUID uuid.UUID `gorm:"column:global_uuid;primaryKey"` - NsPath string `gorm:"column:nspath"` + NSPath string `gorm:"column:nspath"` Tag string `gorm:"column:tag"` Name string `gorm:"column:name"` ModelName string `gorm:"column:model_name"` @@ -41,5 +41,5 @@ func (c Component) GetTagName() string { // GetNSPath define func to inplement CircuitDiagramNodeInterface interface func (c Component) GetNSPath() string { - return c.NsPath + return c.NSPath }