2024-12-31 16:18:51 +08:00
|
|
|
// Package handler provides HTTP handlers for various endpoints.
|
2024-12-04 15:57:11 +08:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"modelRT/constant"
|
|
|
|
|
"modelRT/database"
|
|
|
|
|
"modelRT/diagram"
|
2024-12-25 16:34:57 +08:00
|
|
|
"modelRT/logger"
|
2024-12-04 15:57:11 +08:00
|
|
|
"modelRT/model"
|
|
|
|
|
"modelRT/network"
|
|
|
|
|
"modelRT/orm"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/gofrs/uuid"
|
|
|
|
|
"go.uber.org/zap"
|
2024-12-31 16:18:51 +08:00
|
|
|
"gorm.io/gorm/clause"
|
2024-12-04 15:57:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CircuitDiagramDeleteHandler define circuit diagram delete process API
|
|
|
|
|
func CircuitDiagramDeleteHandler(c *gin.Context) {
|
2024-12-25 16:34:57 +08:00
|
|
|
logger := logger.GetLoggerInstance()
|
2024-12-04 15:57:11 +08:00
|
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
|
|
|
|
|
|
var request network.CircuitDiagramDeleteRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
|
|
|
logger.Error("unmarshal circuit diagram del info failed", zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
graph, err := diagram.GetGraphMap(request.PageID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("get topologic data from set by pageID failed", zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"page_id": request.PageID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 14:57:23 +08:00
|
|
|
var topologicDelInfos []network.TopologicUUIDDelInfo
|
2024-12-04 15:57:11 +08:00
|
|
|
for _, topologicLink := range request.TopologicLinks {
|
2024-12-05 14:57:23 +08:00
|
|
|
var delInfo network.TopologicUUIDDelInfo
|
2024-12-04 15:57:11 +08:00
|
|
|
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("format uuid from string failed", zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"topologic_info": topologicLink,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
delInfo.UUIDFrom = UUIDFrom
|
|
|
|
|
delInfo.UUIDTo = UUIDTo
|
|
|
|
|
topologicDelInfos = append(topologicDelInfos, delInfo)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// open transaction
|
|
|
|
|
tx := pgClient.Begin()
|
|
|
|
|
|
|
|
|
|
for _, topologicDelInfo := range topologicDelInfos {
|
|
|
|
|
err = database.DeleteTopologicIntoDB(c, tx, request.PageID, topologicDelInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
|
|
logger.Error("delete topologic info into DB failed", zap.Any("topologic_info", topologicDelInfo), zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"topologic_info": topologicDelInfo,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = graph.DelEdge(topologicDelInfo.UUIDFrom, topologicDelInfo.UUIDTo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
|
|
logger.Error("delete topologic info failed", zap.Any("topologic_info", topologicDelInfo), zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"topologic_info": topologicDelInfo,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(graph.VerticeLinks) == 0 && len(graph.FreeVertexs) == 0 {
|
|
|
|
|
diagram.DeleteGraphMap(request.PageID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, componentInfo := range request.ComponentInfos {
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(c, 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
|
|
logger.Error("format uuid from string failed", zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"uuid": componentInfo.UUID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-31 16:18:51 +08:00
|
|
|
var component orm.Component
|
2025-01-10 16:57:29 +08:00
|
|
|
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Where("global_uuid = ?", globalUUID).Find(&component)
|
2024-12-31 16:18:51 +08:00
|
|
|
if result.Error != nil || result.RowsAffected == 0 {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
|
|
err := result.Error
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
|
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Error("query component info into postgresDB failed", zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err))
|
|
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
|
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"uuid": componentInfo.UUID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = tx.WithContext(cancelCtx).Delete(component)
|
2024-12-04 15:57:11 +08:00
|
|
|
if result.Error != nil || result.RowsAffected == 0 {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
|
|
err := result.Error
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
|
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.Error("delete component info into postgresDB failed", zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"uuid": componentInfo.UUID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
modelStruct := model.SelectModelByType(component.ComponentType)
|
2024-12-30 16:39:11 +08:00
|
|
|
modelStruct.SetComponentID(component.ID)
|
2025-01-10 16:57:29 +08:00
|
|
|
result = tx.WithContext(cancelCtx).Where("component_id = ?", component.ID).Delete(modelStruct)
|
2024-12-04 15:57:11 +08:00
|
|
|
if result.Error != nil || result.RowsAffected == 0 {
|
|
|
|
|
tx.Rollback()
|
|
|
|
|
|
|
|
|
|
err := result.Error
|
|
|
|
|
if result.RowsAffected == 0 {
|
|
|
|
|
err = fmt.Errorf("%w:please check uuid conditions", constant.ErrDeleteRowZero)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := fmt.Sprintf("delete component info from table %s failed", modelStruct.ReturnTableName())
|
|
|
|
|
logger.Error(msg, zap.String("component_global_uuid", componentInfo.UUID), zap.Error(err))
|
2024-12-06 16:13:11 +08:00
|
|
|
header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()}
|
|
|
|
|
resp := network.FailureResponse{
|
|
|
|
|
FailResponseHeader: header,
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"uuid": componentInfo.UUID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
diagram.DeleteComponentMap(componentInfo.UUID)
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 16:32:23 +08:00
|
|
|
if len(request.FreeVertexs) > 0 {
|
|
|
|
|
for _, freeVertex := range request.FreeVertexs {
|
|
|
|
|
delete(graph.FreeVertexs, freeVertex)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 15:57:11 +08:00
|
|
|
// commit transsction
|
|
|
|
|
tx.Commit()
|
2024-12-06 16:13:11 +08:00
|
|
|
resp := network.SuccessResponse{
|
|
|
|
|
SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK},
|
2024-12-04 15:57:11 +08:00
|
|
|
PayLoad: map[string]interface{}{
|
|
|
|
|
"page_id": request.PageID,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
c.JSON(http.StatusOK, resp)
|
|
|
|
|
}
|