49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
|
|
// Package database define database operation functions
|
||
|
|
package database
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"strconv"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"modelRT/orm"
|
||
|
|
|
||
|
|
"go.uber.org/zap"
|
||
|
|
)
|
||
|
|
|
||
|
|
// LoadCircuitDiagramFromPostgresDB return the result of query circuit diagram info from postgresDB
|
||
|
|
func LoadCircuitDiagramFromPostgresDB(ctx context.Context, logger *zap.Logger) ([]orm.CircuitDiagram, error) {
|
||
|
|
var circuitDiagramOverviews []orm.CircuitDiagramOverview
|
||
|
|
// ctx超时判断
|
||
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
result := _globalPostgresClient.WithContext(cancelCtx).Find(&circuitDiagramOverviews)
|
||
|
|
if result.Error != nil {
|
||
|
|
logger.Error("query circuit diagram overview info failed", zap.Error(result.Error))
|
||
|
|
}
|
||
|
|
|
||
|
|
var circuitDiagrams []orm.CircuitDiagram
|
||
|
|
for _, diagram := range circuitDiagramOverviews {
|
||
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
var circuitDiagram orm.CircuitDiagram
|
||
|
|
tableName := "circuit_diagram_" + strconv.FormatInt(diagram.ID, 10)
|
||
|
|
result := _globalPostgresClient.Table(tableName).WithContext(cancelCtx).Find(&circuitDiagram)
|
||
|
|
if result.Error != nil {
|
||
|
|
logger.Error("query circuit diagram info failed", zap.Error(result.Error))
|
||
|
|
return nil, result.Error
|
||
|
|
}
|
||
|
|
circuitDiagrams = append(circuitDiagrams, circuitDiagram)
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, diagram := range circuitDiagrams {
|
||
|
|
// TODO
|
||
|
|
// 根据 diagram 的 type 类型选择不同的承接结构
|
||
|
|
// 换一个解析更快的 json encoder
|
||
|
|
json.Unmarshal([]byte(diagram.OtherParams), nil)
|
||
|
|
// unmarshal diagram
|
||
|
|
}
|
||
|
|
return circuitDiagrams, nil
|
||
|
|
}
|