183 lines
5.0 KiB
Go
183 lines
5.0 KiB
Go
// Package handler provides HTTP handlers for various endpoints.
|
|
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"modelRT/database"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
"modelRT/network"
|
|
"modelRT/orm"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gofrs/uuid"
|
|
)
|
|
|
|
// CircuitDiagramUpdateHandler define circuit diagram update process API
|
|
func CircuitDiagramUpdateHandler(c *gin.Context) {
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
var request network.CircuitDiagramUpdateRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
logger.Error(c, "unmarshal circuit diagram update 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 topologicChangeInfos []network.TopologicUUIDChangeInfos
|
|
for _, topologicLink := range request.TopologicLinks {
|
|
changeInfo, err := network.ParseUUID(topologicLink)
|
|
if err != nil {
|
|
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
|
|
}
|
|
topologicChangeInfos = append(topologicChangeInfos, changeInfo)
|
|
}
|
|
|
|
// open transaction
|
|
tx := pgClient.Begin()
|
|
|
|
for _, topologicChangeInfo := range topologicChangeInfos {
|
|
err = database.UpdateTopologicIntoDB(c, tx, request.PageID, topologicChangeInfo)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
|
|
logger.Error(c, "update topologic info into DB failed", "topologic_info", topologicChangeInfo, "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"topologic_info": topologicChangeInfo,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
err = graph.UpdateEdge(topologicChangeInfo)
|
|
if err != nil {
|
|
tx.Rollback()
|
|
|
|
logger.Error(c, "update topologic info failed", "topologic_info", topologicChangeInfo, "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"topologic_info": topologicChangeInfo,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
}
|
|
|
|
for index, componentInfo := range request.ComponentInfos {
|
|
componentUUID, err := database.UpdateComponentIntoDB(c, tx, componentInfo)
|
|
if err != nil {
|
|
logger.Error(c, "udpate component info into DB failed", "error", err)
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
PayLoad: map[string]interface{}{
|
|
"page_id": request.PageID,
|
|
"component_info": 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 {
|
|
// 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)
|
|
}
|
|
|
|
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)
|
|
}
|