From 3fa0a8c6ca9df2d57afcc516bff56f7fd56045f5 Mon Sep 17 00:00:00 2001 From: douxu Date: Mon, 18 Aug 2025 17:02:38 +0800 Subject: [PATCH] optimize covert func of component info --- handler/circuit_diagram_create.go | 52 +++++++---------------- handler/circuit_diagram_update.go | 52 +++++++---------------- network/circuit_diagram_create_request.go | 36 +++++++++++++++- network/circuit_diagram_update_request.go | 23 ++++++++++ network/convert.go | 18 ++++++++ 5 files changed, 106 insertions(+), 75 deletions(-) create mode 100644 network/convert.go diff --git a/handler/circuit_diagram_create.go b/handler/circuit_diagram_create.go index 3563ae0..cf61aa6 100644 --- a/handler/circuit_diagram_create.go +++ b/handler/circuit_diagram_create.go @@ -9,7 +9,6 @@ import ( "modelRT/diagram" "modelRT/logger" "modelRT/network" - "modelRT/orm" "github.com/gin-gonic/gin" "github.com/gofrs/uuid" @@ -123,43 +122,22 @@ func CircuitDiagramCreateHandler(c *gin.Context) { } for _, info := range request.ComponentInfos { - component := &orm.Component{ - GlobalUUID: uuid.FromStringOrNil(info.UUID), - // NsPath - // Tag string `gorm:"column:TAG"` - Name: info.Name, - // ModelName string `gorm:"column:MODEL_NAME"` - // Description: info.Description, - // GridID: info.GridID, - // ZoneID: info.ZoneID, - // StationID: info.StationID, - // Type int `gorm:"column:TYPE"` - // InService bool `gorm:"column:IN_SERVICE"` - // State int `gorm:"column:STATE"` - // Status int `gorm:"column:STATUS"` - // Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"` - // Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"` - // Context string `gorm:"column:CONTEXT"` - // Op: info.Op, - // Ts: info.Ts, + // TODO 修复赋值问题 + component, err := network.ConvertComponentCreateInfosToComponents(info) + if err != nil { + logger.Error(c, "convert component params info failed", "component_info", info, "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 } - // 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) } diff --git a/handler/circuit_diagram_update.go b/handler/circuit_diagram_update.go index 7ef69f4..db1900f 100644 --- a/handler/circuit_diagram_update.go +++ b/handler/circuit_diagram_update.go @@ -8,10 +8,8 @@ import ( "modelRT/diagram" "modelRT/logger" "modelRT/network" - "modelRT/orm" "github.com/gin-gonic/gin" - "github.com/gofrs/uuid" ) // CircuitDiagramUpdateHandler define circuit diagram update process API @@ -123,42 +121,22 @@ func CircuitDiagramUpdateHandler(c *gin.Context) { } for _, info := range request.ComponentInfos { - component := &orm.Component{ - GlobalUUID: uuid.FromStringOrNil(info.UUID), - // NsPath - // Tag string `gorm:"column:TAG"` - Name: info.Name, - // ModelName string `gorm:"column:MODEL_NAME"` - // Description: info.Description, - // GridID: info.GridID, - // ZoneID: info.ZoneID, - // StationID: info.StationID, - // Type int `gorm:"column:TYPE"` - // InService bool `gorm:"column:IN_SERVICE"` - // State int `gorm:"column:STATE"` - // Status int `gorm:"column:STATUS"` - // Connection map[string]interface{} `gorm:"column:CONNECTION;type:jsonb;default:'{}'"` - // Label map[string]interface{} `gorm:"column:LABEL;type:jsonb;default:'{}'"` - // Context string `gorm:"column:CONTEXT"` - Op: info.Op, - // Ts: info.Ts, + // TODO 修复赋值问题 + component, err := network.ConvertComponentUpdateInfosToComponents(info) + if err != nil { + logger.Error(c, "convert component params info failed", "component_info", info, "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 } - - // 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) } diff --git a/network/circuit_diagram_create_request.go b/network/circuit_diagram_create_request.go index aa2fbfa..443bc08 100644 --- a/network/circuit_diagram_create_request.go +++ b/network/circuit_diagram_create_request.go @@ -1,7 +1,11 @@ // Package network define struct of network operation package network -import "github.com/gofrs/uuid" +import ( + "modelRT/orm" + + "github.com/gofrs/uuid" +) // TopologicCreateInfo defines circuit diagram topologic create info type TopologicCreateInfo struct { @@ -40,3 +44,33 @@ type CircuitDiagramCreateRequest struct { TopologicLinks []TopologicCreateInfo `json:"topologics"` 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 +} diff --git a/network/circuit_diagram_update_request.go b/network/circuit_diagram_update_request.go index abc1cc6..239796f 100644 --- a/network/circuit_diagram_update_request.go +++ b/network/circuit_diagram_update_request.go @@ -3,9 +3,11 @@ package network import ( "fmt" + "time" "modelRT/common/errcode" "modelRT/constants" + "modelRT/orm" "github.com/gofrs/uuid" ) @@ -138,3 +140,24 @@ func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) { UUIDChangeInfo.Comment = info.Comment 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 +} diff --git a/network/convert.go b/network/convert.go new file mode 100644 index 0000000..3844d87 --- /dev/null +++ b/network/convert.go @@ -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") + } +}