2024-11-22 16:41:04 +08:00
|
|
|
// Package database define database operation functions
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"modelRT/config"
|
|
|
|
|
"modelRT/orm"
|
|
|
|
|
|
2025-01-21 16:35:44 +08:00
|
|
|
"github.com/gofrs/uuid"
|
2024-11-22 16:41:04 +08:00
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
|
"go.uber.org/zap"
|
2025-01-09 15:56:40 +08:00
|
|
|
"gorm.io/gorm"
|
2024-11-22 16:41:04 +08:00
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB
|
2025-04-30 16:44:58 +08:00
|
|
|
func QueryCircuitDiagramComponentFromDB(ctx context.Context, tx *gorm.DB, pool *ants.PoolWithFunc, logger *zap.Logger) (map[uuid.UUID]int, error) {
|
2025-01-21 16:35:44 +08:00
|
|
|
var components []orm.Component
|
2024-11-22 16:41:04 +08:00
|
|
|
// ctx超时判断
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
2025-01-21 16:35:44 +08:00
|
|
|
|
|
|
|
|
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&components)
|
2024-11-22 16:41:04 +08:00
|
|
|
if result.Error != nil {
|
|
|
|
|
logger.Error("query circuit diagram component info failed", zap.Error(result.Error))
|
2025-04-30 16:44:58 +08:00
|
|
|
return nil, result.Error
|
2024-11-22 16:41:04 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-13 16:34:25 +08:00
|
|
|
// TODO 优化componentTypeMap输出
|
2025-04-30 16:44:58 +08:00
|
|
|
componentTypeMap := make(map[uuid.UUID]int, len(components))
|
|
|
|
|
|
2025-01-21 16:35:44 +08:00
|
|
|
for _, component := range components {
|
2024-11-22 16:41:04 +08:00
|
|
|
pool.Invoke(config.ModelParseConfig{
|
|
|
|
|
ComponentInfo: component,
|
|
|
|
|
Context: ctx,
|
|
|
|
|
})
|
2025-04-30 16:44:58 +08:00
|
|
|
|
|
|
|
|
componentTypeMap[component.GlobalUUID] = component.ComponentType
|
2024-11-22 16:41:04 +08:00
|
|
|
}
|
2025-04-30 16:44:58 +08:00
|
|
|
return componentTypeMap, nil
|
2024-11-22 16:41:04 +08:00
|
|
|
}
|
|
|
|
|
|
2025-01-21 16:35:44 +08:00
|
|
|
// QueryComponentByUUID return the result of query circuit diagram component info by uuid from postgresDB
|
|
|
|
|
func QueryComponentByUUID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) {
|
|
|
|
|
var component orm.Component
|
2024-11-22 16:41:04 +08:00
|
|
|
// ctx超时判断
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2025-01-21 16:35:44 +08:00
|
|
|
result := tx.WithContext(cancelCtx).Where("global_uuid = ? ", uuid).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
return orm.Component{}, result.Error
|
2024-11-22 16:41:04 +08:00
|
|
|
}
|
2025-01-21 16:35:44 +08:00
|
|
|
return component, nil
|
2024-11-22 16:41:04 +08:00
|
|
|
}
|
2025-04-30 16:44:58 +08:00
|
|
|
|
|
|
|
|
// QueryComponentByPageID return the result of query circuit diagram component info by page id from postgresDB
|
|
|
|
|
func QueryComponentByPageID(ctx context.Context, tx *gorm.DB, uuid uuid.UUID) (orm.Component, error) {
|
|
|
|
|
var component orm.Component
|
|
|
|
|
// ctx超时判断
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
result := tx.WithContext(cancelCtx).Where("page_id = ? ", uuid).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&component)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
return orm.Component{}, result.Error
|
|
|
|
|
}
|
|
|
|
|
return component, nil
|
|
|
|
|
}
|