// Package handler provides HTTP handlers for various endpoints. package handler import ( "context" "fmt" "net/http" "time" "modelRT/common/errcode" constants "modelRT/constant" "modelRT/database" "modelRT/diagram" "modelRT/logger" "modelRT/model" "modelRT/network" "modelRT/orm" "github.com/gin-gonic/gin" ) // ComponentAnchorReplaceHandler define component anchor point replace process API func ComponentAnchorReplaceHandler(c *gin.Context) { var uuid, anchorName string 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(c, "unmarshal component anchor point replace info failed", "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(c, "query component detail info failed", "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", errcode.ErrQueryRowZero) logger.Error(c, "query component detail info from table is empty", "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(c, "query model detail info failed", "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", errcode.ErrQueryRowZero) logger.Error(c, "query model detail info from table is empty", "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 != constants.DemoType { logger.Error(c, "can not process real time data of component type not equal DemoType", "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) }