183 lines
5.1 KiB
Go
183 lines
5.1 KiB
Go
// Package handler provides HTTP handlers for various endpoints.
|
|
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"modelRT/database"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
"modelRT/network"
|
|
"modelRT/orm"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gofrs/uuid"
|
|
)
|
|
|
|
// CircuitDiagramCreateHandler define circuit diagram create process API
|
|
func CircuitDiagramCreateHandler(c *gin.Context) {
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
var request network.CircuitDiagramCreateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
logger.Error(c, "unmarshal circuit diagram create info failed", "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
graph, err := diagram.GetGraphMap(request.PageID)
|
|
if err != nil {
|
|
logger.Error(c, "get topologic data from set by pageID failed", "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"page_id": request.PageID,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
var topologicCreateInfos []network.TopologicUUIDCreateInfo
|
|
for _, topologicLink := range request.TopologicLinks {
|
|
var createInfo network.TopologicUUIDCreateInfo
|
|
UUIDFrom, err1 := uuid.FromString(topologicLink.UUIDFrom)
|
|
UUIDTo, err2 := uuid.FromString(topologicLink.UUIDTo)
|
|
if err1 != nil || err2 != nil {
|
|
var err error
|
|
if err1 != nil && err2 == nil {
|
|
err = fmt.Errorf("convert uuid from string failed:%w", err1)
|
|
} else if err1 == nil && err2 != nil {
|
|
err = fmt.Errorf("convert uuid from string failed:%w", err2)
|
|
} else {
|
|
err = fmt.Errorf("convert uuid from string failed:%w:%w", err1, err2)
|
|
}
|
|
|
|
logger.Error(c, "format uuid from string failed", "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"topologic_info": topologicLink,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
createInfo.UUIDFrom = UUIDFrom
|
|
createInfo.UUIDTo = UUIDTo
|
|
topologicCreateInfos = append(topologicCreateInfos, createInfo)
|
|
}
|
|
|
|
// open transaction
|
|
tx := pgClient.Begin()
|
|
|
|
err = database.CreateTopologicIntoDB(c, tx, request.PageID, topologicCreateInfos)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
|
|
logger.Error(c, "create topologic info into DB failed", "topologic_info", topologicCreateInfos, "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"topologic_infos": topologicCreateInfos,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
for _, topologicCreateInfo := range topologicCreateInfos {
|
|
graph.AddEdge(topologicCreateInfo.UUIDFrom, topologicCreateInfo.UUIDTo)
|
|
}
|
|
|
|
for index, info := range request.ComponentInfos {
|
|
componentUUID, err := database.CreateComponentIntoDB(c, tx, info)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
|
|
logger.Error(c, "insert component info into DB failed", "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"component_infos": request.ComponentInfos,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
request.ComponentInfos[index].UUID = componentUUID
|
|
}
|
|
|
|
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,
|
|
}
|
|
// 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)
|
|
}
|
|
|
|
if len(request.FreeVertexs) > 0 {
|
|
for _, freeVertex := range request.FreeVertexs {
|
|
graph.FreeVertexs[freeVertex] = struct{}{}
|
|
}
|
|
}
|
|
|
|
// commit transaction
|
|
tx.Commit()
|
|
resp := network.SuccessResponse{
|
|
Code: http.StatusOK,
|
|
Msg: "success",
|
|
PayLoad: map[string]interface{}{
|
|
"page_id": request.PageID,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|