2024-11-29 16:34:03 +08:00
|
|
|
// Package network define struct of network operation
|
|
|
|
|
package network
|
|
|
|
|
|
2024-12-03 16:38:17 +08:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
2025-08-18 17:02:38 +08:00
|
|
|
"time"
|
2024-12-03 16:38:17 +08:00
|
|
|
|
2025-06-13 15:34:49 +08:00
|
|
|
"modelRT/common/errcode"
|
2025-08-05 15:20:07 +08:00
|
|
|
"modelRT/constants"
|
2025-08-18 17:02:38 +08:00
|
|
|
"modelRT/orm"
|
2024-12-03 16:38:17 +08:00
|
|
|
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
|
)
|
|
|
|
|
|
2024-11-29 16:34:03 +08:00
|
|
|
// TopologicChangeInfo defines circuit diagram topologic change info
|
|
|
|
|
type TopologicChangeInfo struct {
|
2024-12-03 16:38:17 +08:00
|
|
|
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"`
|
2024-11-29 16:34:03 +08:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 14:57:23 +08:00
|
|
|
// ComponentUpdateInfo defines circuit diagram component params info
|
|
|
|
|
type ComponentUpdateInfo struct {
|
2025-08-15 16:25:48 +08:00
|
|
|
ID int64 `json:"id"`
|
|
|
|
|
UUID string `json:"uuid"`
|
|
|
|
|
Name string `json:"name"`
|
|
|
|
|
Context string `json:"context"`
|
|
|
|
|
GridID int64 `json:"grid_id"`
|
|
|
|
|
ZoneID int64 `json:"zone_id"`
|
|
|
|
|
StationID int64 `json:"station_id"`
|
|
|
|
|
Params string `json:"params"`
|
|
|
|
|
Op int `json:"op"`
|
|
|
|
|
Tag string `json:"tag"`
|
2024-11-29 16:34:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CircuitDiagramUpdateRequest defines request params of circuit diagram update api
|
|
|
|
|
type CircuitDiagramUpdateRequest struct {
|
|
|
|
|
PageID int64 `json:"page_id"`
|
2024-12-05 16:32:23 +08:00
|
|
|
FreeVertexs []string `json:"free_vertexs"`
|
2024-11-29 16:34:03 +08:00
|
|
|
TopologicLinks []TopologicChangeInfo `json:"topologics"`
|
2024-12-05 14:57:23 +08:00
|
|
|
ComponentInfos []ComponentUpdateInfo `json:"component_infos"`
|
2024-11-29 16:34:03 +08:00
|
|
|
}
|
2024-12-03 16:38:17 +08:00
|
|
|
|
2024-12-05 14:57:23 +08:00
|
|
|
// ParseUUID define parse UUID by change type in topologic change struct
|
2024-12-03 16:38:17 +08:00
|
|
|
func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) {
|
|
|
|
|
var UUIDChangeInfo TopologicUUIDChangeInfos
|
2025-01-10 16:57:29 +08:00
|
|
|
UUIDChangeInfo.ChangeType = info.ChangeType
|
2024-12-04 15:57:11 +08:00
|
|
|
|
2024-12-03 16:38:17 +08:00
|
|
|
switch info.ChangeType {
|
2025-06-13 15:34:49 +08:00
|
|
|
case constants.UUIDFromChangeType:
|
2024-12-03 16:38:17 +08:00
|
|
|
if info.NewUUIDFrom == info.OldUUIDFrom {
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constants.ErrUUIDFromCheckT1)
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
if info.NewUUIDTo != info.OldUUIDTo {
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constants.ErrUUIDToCheckT1)
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2025-06-13 15:34:49 +08:00
|
|
|
case constants.UUIDToChangeType:
|
2024-12-03 16:38:17 +08:00
|
|
|
if info.NewUUIDFrom != info.OldUUIDFrom {
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constants.ErrUUIDFromCheckT2)
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
if info.NewUUIDTo == info.OldUUIDTo {
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constants.ErrUUIDToCheckT2)
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
2025-06-13 15:34:49 +08:00
|
|
|
case constants.UUIDAddChangeType:
|
2024-12-03 16:38:17 +08:00
|
|
|
if info.OldUUIDFrom != "" {
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constants.ErrUUIDFromCheckT3)
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
if info.OldUUIDTo != "" {
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, fmt.Errorf("topologic change data check failed:%w", constants.ErrUUIDToCheckT3)
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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:
|
2025-06-13 15:34:49 +08:00
|
|
|
return UUIDChangeInfo, errcode.ErrUUIDChangeType
|
2024-12-03 16:38:17 +08:00
|
|
|
}
|
|
|
|
|
UUIDChangeInfo.Flag = info.Flag
|
|
|
|
|
UUIDChangeInfo.Comment = info.Comment
|
|
|
|
|
return UUIDChangeInfo, nil
|
|
|
|
|
}
|
2025-08-18 17:02:38 +08:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|