// Package model define model struct of model runtime service package model import ( "context" "errors" "fmt" "modelRT/constants" "modelRT/diagram" "modelRT/logger" "modelRT/orm" "gorm.io/gorm" ) type columnParam struct { AttributeType string AttributeGroup map[string]any } // TraverseAttributeGroupTables define func to traverse component attribute group tables func TraverseAttributeGroupTables(ctx context.Context, db *gorm.DB, compParamMap map[string]orm.AttributeSet) error { var tableNames []string result := db.Model(&orm.ProjectManager{}).Pluck("name", &tableNames) if result.Error != nil && result.Error != gorm.ErrRecordNotFound { logger.Error(ctx, "query name column data from postgres table failed", "err", result.Error) return result.Error } if len(tableNames) == 0 { logger.Info(ctx, "query from postgres successed, but no records found") return nil } for _, tableName := range tableNames { if tableName == "" { continue } var records []map[string]any err := db.Table(tableName).Find(&records).Error if err != nil && err != gorm.ErrRecordNotFound { logger.Error(ctx, fmt.Sprintf("query table '%s' data failed", tableName), "table_name", tableName, "err", err) return err } migrator := db.Migrator() if exists := migrator.HasTable(tableName); !exists { err := errors.New("can not find special table into database") logger.Error(ctx, "table does not exist in the database", "table _name", tableName, "error", err) return err } columnTypes, err := migrator.ColumnTypes(tableName) if err != nil { logger.Error(ctx, "retrieving column structure for table failed", "table _name", tableName, "error", err) return err } for _, record := range records { attributeGroup := make(map[string]any) var componentUUIDStr string var attrSet orm.AttributeSet var attributeType string for _, col := range columnTypes { colName := col.Name() value, exists := record[colName] if !exists { err := errors.New("can not find value from record by column name") logger.Error(ctx, "query value by column name failed", "column_name", colName, "error", err) return err } switch colName { case "id": case "global_uuid": componentUUIDStr = value.(string) attrSet, exists = compParamMap[componentUUIDStr] if !exists { err := errors.New("can not find component tag from record by global uuid") logger.Error(ctx, "check component info by global uuid failed", "global_uuid", componentUUIDStr, "error", err) return err } case "attribute_group": attributeType = value.(string) default: attributeGroup[colName] = value } } columnParam := columnParam{ AttributeType: attributeType, AttributeGroup: attributeGroup, } go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam) } } return nil } func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, colParams columnParam) { rdb := diagram.GetRedisClientInstance() pipe := rdb.Pipeline() // add token6 hierarchy content pipe.SAdd(ctx, constants.RedisAllConfigSetKey, colParams.AttributeType) // add token5-token7 hierarchy collaboration content specCompMeasKey := fmt.Sprintf(constants.RedisSpecCompTagMeasSetKey, attributeSet.CompTag) attrNameMembers := make([]string, 0, len(colParams.AttributeGroup)) for attrName, attrValue := range colParams.AttributeGroup { // TODO 增加元件属性组表的列与对应值的缓存存储 fmt.Println(attrValue) fmt.Println(compUUIDStr) attrNameMembers = append(attrNameMembers, attrName) } pipe.SAdd(ctx, specCompMeasKey, attrNameMembers) _, err := pipe.Exec(ctx) if err != nil { logger.Error(ctx, "init component attribute group recommend content failed", "error", err) } }