113 lines
3.1 KiB
Go
113 lines
3.1 KiB
Go
// Package handler provides HTTP handlers for various endpoints.
|
|
package handler
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"modelRT/constant"
|
|
"modelRT/database"
|
|
"modelRT/diagram"
|
|
"modelRT/logger"
|
|
"modelRT/model"
|
|
"modelRT/network"
|
|
"modelRT/orm"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// ComponentAnchorReplaceHandler define component anchor point replace process API
|
|
func ComponentAnchorReplaceHandler(c *gin.Context) {
|
|
var uuid, anchorName string
|
|
logger := logger.GetLoggerInstance()
|
|
pgClient := database.GetPostgresDBClient()
|
|
|
|
cancelCtx, cancel := context.WithTimeout(c, 5*time.Second)
|
|
defer cancel()
|
|
|
|
var request network.ComponetAnchorReplaceRequest
|
|
if err := c.ShouldBindJSON(&request); err != nil {
|
|
logger.Error("unmarshal component anchor point replace info failed", zap.Error(err))
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
uuid = request.UUID
|
|
anchorName = request.AnchorName
|
|
|
|
var componentInfo orm.Component
|
|
result := pgClient.WithContext(cancelCtx).Model(&orm.Component{}).Where("global_uuid = ?", uuid).Find(&componentInfo)
|
|
if result.Error != nil {
|
|
logger.Error("query component detail info failed", zap.Error(result.Error))
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: result.Error.Error(),
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
if result.RowsAffected == 0 {
|
|
err := fmt.Errorf("query component detail info by uuid failed:%w", constant.ErrQueryRowZero)
|
|
logger.Error("query component detail info from table is empty", zap.String("table_name", "component"))
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
cancelCtx, cancel = context.WithTimeout(c, 5*time.Second)
|
|
defer cancel()
|
|
unmarshalMap := make(map[string]interface{})
|
|
tableName := model.SelectModelNameByType(componentInfo.ComponentType)
|
|
result = pgClient.WithContext(cancelCtx).Table(tableName).Where("global_uuid = ?", uuid).Find(&unmarshalMap)
|
|
if result.Error != nil {
|
|
logger.Error("query model detail info failed", zap.Error(result.Error))
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: result.Error.Error(),
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
if unmarshalMap == nil {
|
|
err := fmt.Errorf("query model detail info by uuid failed:%w", constant.ErrQueryRowZero)
|
|
logger.Error("query model detail info from table is empty", zap.String("table_name", tableName))
|
|
|
|
resp := network.FailureResponse{
|
|
Code: http.StatusBadRequest,
|
|
Msg: err.Error(),
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
return
|
|
}
|
|
|
|
componentType := unmarshalMap["component_type"].(int)
|
|
if componentType != constant.DemoType {
|
|
logger.Error("can not process real time data of component type not equal DemoType", zap.Int64("component_id", componentInfo.ID))
|
|
}
|
|
diagram.UpdateAnchorValue(componentInfo.ID, anchorName)
|
|
|
|
resp := network.SuccessResponse{
|
|
Code: http.StatusOK,
|
|
Msg: "success",
|
|
PayLoad: map[string]interface{}{
|
|
"uuid": request.UUID,
|
|
},
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|