2024-12-31 16:18:51 +08:00
|
|
|
// Package handler provides HTTP handlers for various endpoints.
|
2024-12-05 14:57:23 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"modelRT/database"
|
|
|
|
|
"modelRT/diagram"
|
2024-12-25 16:34:57 +08:00
|
|
|
"modelRT/logger"
|
2024-12-05 14:57:23 +08:00
|
|
|
"modelRT/network"
|
|
|
|
|
|
|
|
|
|
"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 {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "unmarshal circuit diagram create info failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graph, err := diagram.GetGraphMap(request.PageID)
|
|
|
|
|
if err != nil {
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "get topologic data from set by pageID failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: map[string]interface{}{
|
2024-12-05 14:57:23 +08:00
|
|
|
"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)
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "format uuid from string failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: map[string]interface{}{
|
2024-12-05 14:57:23 +08:00
|
|
|
"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()
|
|
|
|
|
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "create topologic info into DB failed", "topologic_info", topologicCreateInfos, "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: map[string]interface{}{
|
2024-12-05 14:57:23 +08:00
|
|
|
"topologic_infos": topologicCreateInfos,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, topologicCreateInfo := range topologicCreateInfos {
|
|
|
|
|
graph.AddEdge(topologicCreateInfo.UUIDFrom, topologicCreateInfo.UUIDTo)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 16:25:48 +08:00
|
|
|
for index, info := range request.ComponentInfos {
|
|
|
|
|
componentUUID, err := database.CreateComponentIntoDB(c, tx, info)
|
2024-12-30 16:39:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
tx.Rollback()
|
2024-12-05 14:57:23 +08:00
|
|
|
|
2025-06-06 16:41:52 +08:00
|
|
|
logger.Error(c, "insert component info into DB failed", "error", err)
|
2025-01-23 14:56:01 +08:00
|
|
|
|
2024-12-30 16:39:11 +08:00
|
|
|
resp := network.FailureResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusBadRequest,
|
|
|
|
|
Msg: err.Error(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: map[string]interface{}{
|
2024-12-30 16:39:11 +08:00
|
|
|
"component_infos": request.ComponentInfos,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|
2025-08-15 16:25:48 +08:00
|
|
|
request.ComponentInfos[index].UUID = componentUUID
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-15 16:25:48 +08:00
|
|
|
for _, info := range request.ComponentInfos {
|
2025-08-18 17:02:38 +08:00
|
|
|
// 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(),
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: map[string]interface{}{
|
2025-08-18 17:02:38 +08:00
|
|
|
"uuid": info.UUID,
|
|
|
|
|
"component_params": info.Params,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
2024-12-18 16:25:49 +08:00
|
|
|
}
|
2025-08-15 16:25:48 +08:00
|
|
|
diagram.StoreComponentMap(info.UUID, component)
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|
|
|
|
|
|
2024-12-05 16:32:23 +08:00
|
|
|
if len(request.FreeVertexs) > 0 {
|
|
|
|
|
for _, freeVertex := range request.FreeVertexs {
|
|
|
|
|
graph.FreeVertexs[freeVertex] = struct{}{}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-13 15:54:40 +08:00
|
|
|
// commit transaction
|
2024-12-05 14:57:23 +08:00
|
|
|
tx.Commit()
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.SuccessResponse{
|
2025-01-23 14:56:01 +08:00
|
|
|
Code: http.StatusOK,
|
|
|
|
|
Msg: "success",
|
2025-11-12 17:34:18 +08:00
|
|
|
Payload: map[string]interface{}{
|
2024-12-05 14:57:23 +08:00
|
|
|
"page_id": request.PageID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|