2024-11-22 16:41:04 +08:00
|
|
|
// Package database define database operation functions
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"modelRT/config"
|
|
|
|
|
"modelRT/diagram"
|
|
|
|
|
"modelRT/orm"
|
|
|
|
|
|
|
|
|
|
"github.com/panjf2000/ants/v2"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// QueryCircuitDiagramComponentFromDB return the result of query circuit diagram component info order by page id from postgresDB
|
|
|
|
|
func QueryCircuitDiagramComponentFromDB(ctx context.Context, pool *ants.PoolWithFunc, logger *zap.Logger) error {
|
|
|
|
|
var Components []orm.Component
|
|
|
|
|
// ctx超时判断
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
2025-01-08 16:37:18 +08:00
|
|
|
// TODO 将 for update 操作放到事务中
|
2024-11-22 16:41:04 +08:00
|
|
|
result := _globalPostgresClient.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Find(&Components)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
logger.Error("query circuit diagram component info failed", zap.Error(result.Error))
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, component := range Components {
|
|
|
|
|
pool.Invoke(config.ModelParseConfig{
|
|
|
|
|
ComponentInfo: component,
|
|
|
|
|
Context: ctx,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// QueryElectricalEquipmentUUID return the result of query electrical equipment uuid from postgresDB by circuit diagram id info
|
|
|
|
|
func QueryElectricalEquipmentUUID(ctx context.Context, diagramID int64, logger *zap.Logger) error {
|
|
|
|
|
var uuids []string
|
|
|
|
|
// ctx超时判断
|
|
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
tableName := "circuit_diagram_" + strconv.FormatInt(diagramID, 10)
|
|
|
|
|
result := _globalPostgresClient.Table(tableName).WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Select("uuid").Find(&uuids)
|
|
|
|
|
if result.Error != nil {
|
|
|
|
|
logger.Error("query circuit diagram overview info failed", zap.Error(result.Error))
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, uuid := range uuids {
|
|
|
|
|
diagramParamsMap, err := diagram.GetComponentMap(uuid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.Error("get electrical circuit diagram overview info failed", zap.Error(result.Error))
|
|
|
|
|
return result.Error
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(diagramParamsMap, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|