2024-11-22 16:41:04 +08:00
// Package database define database operation functions
package database
import (
"context"
"time"
2025-06-06 16:41:52 +08:00
"modelRT/logger"
2024-11-22 16:41:04 +08:00
"modelRT/orm"
2025-01-09 15:56:40 +08:00
"gorm.io/gorm"
2024-11-22 16:41:04 +08:00
"gorm.io/gorm/clause"
)
// QueryAllPages return the all page info of the circuit diagram query by grid_id and zone_id and station_id
2025-06-06 16:41:52 +08:00
func QueryAllPages ( ctx context . Context , tx * gorm . DB , gridID , zoneID , stationID int64 ) ( [ ] orm . Page , error ) {
2024-11-22 16:41:04 +08:00
var pages [ ] orm . Page
2025-06-06 16:41:52 +08:00
// ctx timeout judgment
2024-11-22 16:41:04 +08:00
cancelCtx , cancel := context . WithTimeout ( ctx , 5 * time . Second )
defer cancel ( )
2025-01-09 15:56:40 +08:00
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 )
2024-11-22 16:41:04 +08:00
if result . Error != nil {
2025-06-06 16:41:52 +08:00
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 )
2024-11-22 16:41:04 +08:00
return nil , result . Error
}
return pages , nil
}