optimize covert func of component info

This commit is contained in:
douxu 2025-08-18 17:02:38 +08:00
parent f4ab4e4ea4
commit 3fa0a8c6ca
5 changed files with 106 additions and 75 deletions

View File

@ -9,7 +9,6 @@ import (
"modelRT/diagram" "modelRT/diagram"
"modelRT/logger" "modelRT/logger"
"modelRT/network" "modelRT/network"
"modelRT/orm"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
@ -123,43 +122,22 @@ func CircuitDiagramCreateHandler(c *gin.Context) {
} }
for _, info := range request.ComponentInfos { for _, info := range request.ComponentInfos {
component := &orm.Component{ // TODO 修复赋值问题
GlobalUUID: uuid.FromStringOrNil(info.UUID), component, err := network.ConvertComponentCreateInfosToComponents(info)
// NsPath if err != nil {
// Tag string `gorm:"column:TAG"` logger.Error(c, "convert component params info failed", "component_info", info, "error", err)
Name: info.Name,
// ModelName string `gorm:"column:MODEL_NAME"` resp := network.FailureResponse{
// Description: info.Description, Code: http.StatusBadRequest,
// GridID: info.GridID, Msg: err.Error(),
// ZoneID: info.ZoneID, PayLoad: map[string]interface{}{
// StationID: info.StationID, "uuid": info.UUID,
// Type int `gorm:"column:TYPE"` "component_params": info.Params,
// InService bool `gorm:"column:IN_SERVICE"` },
// State int `gorm:"column:STATE"` }
// Status int `gorm:"column:STATUS"` c.JSON(http.StatusOK, resp)
// Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"` return
// Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
// Context string `gorm:"column:CONTEXT"`
// Op: info.Op,
// Ts: info.Ts,
} }
// paramsJSON, err := simplejson.NewJson([]byte(info.Params))
// if err != nil {
// tx.Rollback()
// logger.Error(c, "unmarshal component params info failed", "component_params", info.Params, "error", err)
// resp := network.FailureResponse{
// Code: http.StatusBadRequest,
// Msg: err.Error(),
// PayLoad: map[string]interface{}{
// "uuid": info.UUID,
// "component_params": info.Params,
// },
// }
// c.JSON(http.StatusOK, resp)
// return
// }
diagram.StoreComponentMap(info.UUID, component) diagram.StoreComponentMap(info.UUID, component)
} }

View File

@ -8,10 +8,8 @@ import (
"modelRT/diagram" "modelRT/diagram"
"modelRT/logger" "modelRT/logger"
"modelRT/network" "modelRT/network"
"modelRT/orm"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gofrs/uuid"
) )
// CircuitDiagramUpdateHandler define circuit diagram update process API // CircuitDiagramUpdateHandler define circuit diagram update process API
@ -123,42 +121,22 @@ func CircuitDiagramUpdateHandler(c *gin.Context) {
} }
for _, info := range request.ComponentInfos { for _, info := range request.ComponentInfos {
component := &orm.Component{ // TODO 修复赋值问题
GlobalUUID: uuid.FromStringOrNil(info.UUID), component, err := network.ConvertComponentUpdateInfosToComponents(info)
// NsPath if err != nil {
// Tag string `gorm:"column:TAG"` logger.Error(c, "convert component params info failed", "component_info", info, "error", err)
Name: info.Name,
// ModelName string `gorm:"column:MODEL_NAME"` resp := network.FailureResponse{
// Description: info.Description, Code: http.StatusBadRequest,
// GridID: info.GridID, Msg: err.Error(),
// ZoneID: info.ZoneID, PayLoad: map[string]interface{}{
// StationID: info.StationID, "uuid": info.UUID,
// Type int `gorm:"column:TYPE"` "component_params": info.Params,
// InService bool `gorm:"column:IN_SERVICE"` },
// State int `gorm:"column:STATE"` }
// Status int `gorm:"column:STATUS"` c.JSON(http.StatusOK, resp)
// Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"` return
// Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"`
// Context string `gorm:"column:CONTEXT"`
Op: info.Op,
// Ts: info.Ts,
} }
// paramsJSON, err := simplejson.NewJson([]byte(info.Params))
// if err != nil {
// logger.Error(c, "unmarshal component info by concurrent map failed", "component_params", info.Params, "error", err)
// resp := network.FailureResponse{
// Code: http.StatusBadRequest,
// Msg: err.Error(),
// PayLoad: map[string]interface{}{
// "uuid": info.UUID,
// "component_params": info.Params,
// },
// }
// c.JSON(http.StatusOK, resp)
// return
// }
diagram.UpdateComponentMap(info.ID, component) diagram.UpdateComponentMap(info.ID, component)
} }

View File

@ -1,7 +1,11 @@
// Package network define struct of network operation // Package network define struct of network operation
package network package network
import "github.com/gofrs/uuid" import (
"modelRT/orm"
"github.com/gofrs/uuid"
)
// TopologicCreateInfo defines circuit diagram topologic create info // TopologicCreateInfo defines circuit diagram topologic create info
type TopologicCreateInfo struct { type TopologicCreateInfo struct {
@ -40,3 +44,33 @@ type CircuitDiagramCreateRequest struct {
TopologicLinks []TopologicCreateInfo `json:"topologics"` TopologicLinks []TopologicCreateInfo `json:"topologics"`
ComponentInfos []ComponentCreateInfo `json:"component_infos"` ComponentInfos []ComponentCreateInfo `json:"component_infos"`
} }
// ConvertComponentCreateInfosToComponents define convert component create info to component struct
func ConvertComponentCreateInfosToComponents(info ComponentCreateInfo) (*orm.Component, error) {
uuidVal, err := uuid.FromString(info.UUID)
if err != nil {
return nil, err
}
component := &orm.Component{
GlobalUUID: uuidVal,
// NsPath: info.NsPath,
Tag: info.Tag,
Name: info.Name,
// ModelName: info.ModelName,
// Description: info.Description,
// GridID: info.GridID,
// ZoneID: info.ZoneID,
// StationID: info.StationID,
// Type: info.Type,
// InService: info.InService,
// State: info.State,
// Status: info.Status,
// Connection: info.Connection,
// Label: info.Label,
Context: info.Context,
Op: info.Op,
// Ts: info.Ts,
}
return component, nil
}

View File

@ -3,9 +3,11 @@ package network
import ( import (
"fmt" "fmt"
"time"
"modelRT/common/errcode" "modelRT/common/errcode"
"modelRT/constants" "modelRT/constants"
"modelRT/orm"
"github.com/gofrs/uuid" "github.com/gofrs/uuid"
) )
@ -138,3 +140,24 @@ func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) {
UUIDChangeInfo.Comment = info.Comment UUIDChangeInfo.Comment = info.Comment
return UUIDChangeInfo, nil return UUIDChangeInfo, nil
} }
// ConvertComponentUpdateInfosToComponents define convert component update info to component struct
func ConvertComponentUpdateInfosToComponents(updateInfo ComponentUpdateInfo) (*orm.Component, error) {
uuidVal, err := uuid.FromString(updateInfo.UUID)
if err != nil {
return nil, err
}
component := &orm.Component{
GlobalUUID: uuidVal,
// Name: info.Name,
// Context: info.Context,
// GridID: fmt.Sprintf("%d", info.GridID),
// ZoneID: fmt.Sprintf("%d", info.ZoneID),
// StationID: fmt.Sprintf("%d", info.StationID),
// Op: info.Op,
// Tag: info.Tag,
// 其他字段可根据需要补充
Ts: time.Now(),
}
return component, nil
}

18
network/convert.go Normal file
View File

@ -0,0 +1,18 @@
package network
import (
"fmt"
"modelRT/orm"
)
func ConvertAnyComponentInfosToComponents(anyInfo interface{}) (*orm.Component, error) {
switch info := anyInfo.(type) {
case ComponentCreateInfo:
return ConvertComponentCreateInfosToComponents(info)
case ComponentUpdateInfo:
return ConvertComponentUpdateInfosToComponents(info)
default:
return nil, fmt.Errorf("unsupported type")
}
}