package handler import ( "context" "fmt" "net/http" "strconv" "time" "modelRT/config" "modelRT/constant" "modelRT/database" "modelRT/diagram" "modelRT/logger" "modelRT/model" "modelRT/network" "modelRT/orm" realtimedata "modelRT/real-time-data" "github.com/bitly/go-simplejson" "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)) header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()} resp := network.FailureResponse{ FailResponseHeader: header, } 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)) header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: result.Error.Error()} resp := network.FailureResponse{ FailResponseHeader: header, } 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")) header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()} resp := network.FailureResponse{ FailResponseHeader: header, } 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)) header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: result.Error.Error()} resp := network.FailureResponse{ FailResponseHeader: header, } 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)) header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()} resp := network.FailureResponse{ FailResponseHeader: header, } c.JSON(http.StatusOK, resp) return } configsStr, exist := unmarshalMap["anchor_configs_list"] if !exist { logger.Error("can not find anchor config list in model model detail info") header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: "can not find anchor config list in model model detail info"} resp := network.FailureResponse{ FailResponseHeader: header, } c.JSON(http.StatusOK, resp) return } anchorConfigsJSON, err := simplejson.NewJson([]byte(configsStr.(string))) if err != nil { logger.Error("formmat anchor configs json failed", zap.Error(err)) header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: err.Error()} resp := network.FailureResponse{ FailResponseHeader: header, } c.JSON(http.StatusOK, resp) return } var existFlag bool var anchorIndex int paramsList := anchorConfigsJSON.Get("params_list").MustArray() for index := range paramsList { paramAnchorName := anchorConfigsJSON.Get("params_list").GetIndex(index).Get("anchor_name").MustString() if anchorName == paramAnchorName { existFlag = true anchorIndex = index } } if !existFlag { header := network.FailResponseHeader{Status: http.StatusBadRequest, ErrMsg: "can not find new anchor name in model anchor point list"} resp := network.FailureResponse{ FailResponseHeader: header, } c.JSON(http.StatusOK, resp) return } diagram.UpdateAnchorValue(uuid, anchorName) anchorParam := config.AnchorParamConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{ StationID: strconv.FormatInt(componentInfo.StationID, 10), ComponentID: strconv.FormatInt(componentInfo.ID, 10), UUID: uuid, AnchorName: anchorName, CompareValUpperLimit: anchorConfigsJSON.Get("params_list").GetIndex(anchorIndex).Get("upper_limit").MustFloat64(), CompareValLowerLimit: anchorConfigsJSON.Get("params_list").GetIndex(anchorIndex).Get("lower_limit").MustFloat64(), }, } anchorParam.CalculateFunc, anchorParam.CalculateParams = config.SelectAnchorCalculateFuncAndParams(componentInfo.ComponentType, anchorName, unmarshalMap) realtimedata.AnchorParamsChan <- anchorParam resp := network.SuccessResponse{ SuccessResponseHeader: network.SuccessResponseHeader{Status: http.StatusOK}, PayLoad: map[string]interface{}{ "uuid": request.UUID, }, } c.JSON(http.StatusOK, resp) }