2026-01-05 17:20:41 +08:00
// Package database define database operation functions
package database
import (
"context"
2026-01-08 17:34:44 +08:00
"errors"
"fmt"
2026-01-05 17:20:41 +08:00
"time"
"modelRT/logger"
"modelRT/orm"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
// QueryArrtibuteRecordByUUID return the attribute table record info of the component attribute by uuid
func QueryArrtibuteRecordByUUID ( ctx context . Context , tx * gorm . DB , gridID , zoneID , stationID int64 ) ( [ ] orm . Page , error ) {
var pages [ ] orm . Page
// ctx timeout judgment
cancelCtx , cancel := context . WithTimeout ( ctx , 5 * time . Second )
defer cancel ( )
result := tx . Model ( & orm . Page { } ) . WithContext ( cancelCtx ) . Clauses ( clause . Locking { Strength : "UPDATE" } ) . Select ( ` "page".id, "page".Name, "page".status,"page".context ` ) . Joins ( ` inner join "station" on "station".id = "page".station_id ` ) . Joins ( ` inner join "zone" on "zone".id = "station".zone_id ` ) . Joins ( ` inner join "grid" on "grid".id = "zone".grid_id ` ) . Where ( ` "grid".id = ? and "zone".id = ? and "station".id = ? ` , gridID , zoneID , stationID ) . Scan ( & pages )
if result . Error != nil {
logger . Error ( ctx , "query circuit diagram pages by gridID and zoneID and stationID failed" , "grid_id" , gridID , "zone_id" , zoneID , "station_id" , stationID , "error" , result . Error )
return nil , result . Error
}
return pages , nil
}
2026-01-08 17:34:44 +08:00
// GetProjectNameByTagAndGroupName 根据 tag 和 meta_model 获取项目名称
func GetProjectNameByTagAndGroupName ( db * gorm . DB , tag string , groupName string ) ( string , error ) {
var project orm . ProjectManager
// 使用 Select 只提取 name 字段,提高查询效率
// 使用 Where 进行多列条件过滤
err := db . Select ( "name" ) .
Where ( "tag = ? AND meta_model = ?" , tag , groupName ) .
First ( & project ) . Error
if err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
return "" , fmt . Errorf ( "project not found with tag: %s and model: %s" , tag , groupName )
}
return "" , err
}
return project . Name , nil
}
// BatchGetProjectNames define func to batch retrieve name based on multiple tags and metaModel
func BatchGetProjectNames ( db * gorm . DB , identifiers [ ] orm . ProjectIdentifier ) ( map [ orm . ProjectIdentifier ] string , error ) {
if len ( identifiers ) == 0 {
return nil , nil
}
var projects [ ] orm . ProjectManager
queryArgs := make ( [ ] [ ] any , len ( identifiers ) )
for i , id := range identifiers {
2026-01-14 17:32:01 +08:00
queryArgs [ i ] = [ ] any { id . Tag , id . GroupName }
2026-01-08 17:34:44 +08:00
}
2026-01-14 17:32:01 +08:00
err := db . Select ( "tag" , "group_name" , "name" ) .
Where ( "(tag, group_name) IN ?" , queryArgs ) .
2026-01-08 17:34:44 +08:00
Find ( & projects ) . Error
if err != nil {
return nil , err
}
resultMap := make ( map [ orm . ProjectIdentifier ] string )
for _ , p := range projects {
2026-01-14 17:32:01 +08:00
key := orm . ProjectIdentifier { Tag : p . Tag , GroupName : p . GroupName }
2026-01-08 17:34:44 +08:00
resultMap [ key ] = p . Name
}
return resultMap , nil
}