113 lines
3.2 KiB
Go
113 lines
3.2 KiB
Go
// Package model define model struct of model runtime service
|
|
package model
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"modelRT/logger"
|
|
"modelRT/orm"
|
|
|
|
"github.com/gofrs/uuid"
|
|
"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,
|
|
}
|
|
|
|
// TODO 将筛选后的 recordSchema 属性值加入到缓存系统中
|
|
// TODO 增加 station tag的获取
|
|
go storeAttributeGroup(ctx, attrSet, componentUUIDStr, columnParam)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func storeAttributeGroup(ctx context.Context, attributeSet orm.AttributeSet, compUUIDStr string, params columnParam) {
|
|
fmt.Println(ctx)
|
|
fmt.Println(attributeSet)
|
|
uuid, err := uuid.FromString(compUUIDStr)
|
|
fmt.Println(uuid, err)
|
|
fmt.Println(params)
|
|
}
|