100 lines
2.8 KiB
Go
100 lines
2.8 KiB
Go
// Package database define database operation functions
|
|
package database
|
|
|
|
import (
|
|
"modelRT/orm"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type ZoneWithParent struct {
|
|
orm.Zone
|
|
GridTag string `gorm:"column:grid_tag"`
|
|
}
|
|
|
|
type StationWithParent struct {
|
|
orm.Zone
|
|
ZoneTag string `gorm:"column:zone_tag"`
|
|
}
|
|
|
|
func GetFullMeasurementSet(db *gorm.DB) (*orm.MeasurementSet, error) {
|
|
mSet := &orm.MeasurementSet{
|
|
GridToZoneTags: make(map[string][]string),
|
|
ZoneToStationTags: make(map[string][]string),
|
|
StationToCompNSPaths: make(map[string][]string),
|
|
CompNSPathToCompTags: make(map[string][]string),
|
|
CompTagToMeasTags: make(map[string][]string),
|
|
}
|
|
|
|
var zones []struct {
|
|
orm.Zone
|
|
GridTag string `gorm:"column:grid_tag"`
|
|
}
|
|
if err := db.Table("zone").
|
|
Select("zone.*, grid.tagname as grid_tag").
|
|
Joins("left join grid on zone.grid_id = grid.id").
|
|
Scan(&zones).Error; err == nil {
|
|
for _, z := range zones {
|
|
mSet.AllZoneTags = append(mSet.AllZoneTags, z.TAGNAME)
|
|
if z.GridTag != "" {
|
|
mSet.GridToZoneTags[z.GridTag] = append(mSet.GridToZoneTags[z.GridTag], z.TAGNAME)
|
|
}
|
|
}
|
|
}
|
|
|
|
var stations []struct {
|
|
orm.Station
|
|
ZoneTag string `gorm:"column:zone_tag"`
|
|
}
|
|
if err := db.Table("station").
|
|
Select("station.*, zone.tagname as zone_tag").
|
|
Joins("left join zone on station.zone_id = zone.id").
|
|
Scan(&stations).Error; err == nil {
|
|
for _, s := range stations {
|
|
mSet.AllStationTags = append(mSet.AllStationTags, s.TAGNAME)
|
|
if s.ZoneTag != "" {
|
|
mSet.ZoneToStationTags[s.ZoneTag] = append(mSet.ZoneToStationTags[s.ZoneTag], s.TAGNAME)
|
|
}
|
|
}
|
|
}
|
|
|
|
var comps []struct {
|
|
orm.Component
|
|
StationTag string `gorm:"column:station_tag"`
|
|
}
|
|
if err := db.Table("component").
|
|
Select("component.*, station.tagname as station_tag").
|
|
Joins("left join station on component.station_id = station.id").
|
|
Scan(&comps).Error; err == nil {
|
|
for _, c := range comps {
|
|
mSet.AllCompNSPaths = append(mSet.AllCompNSPaths, c.NSPath)
|
|
mSet.AllCompTags = append(mSet.AllCompTags, c.Tag)
|
|
|
|
if c.StationTag != "" {
|
|
mSet.StationToCompNSPaths[c.StationTag] = append(mSet.StationToCompNSPaths[c.StationTag], c.NSPath)
|
|
}
|
|
|
|
if c.NSPath != "" {
|
|
mSet.CompNSPathToCompTags[c.NSPath] = append(mSet.CompNSPathToCompTags[c.NSPath], c.Tag)
|
|
}
|
|
}
|
|
}
|
|
|
|
var measurements []struct {
|
|
orm.Measurement
|
|
CompTag string `gorm:"column:comp_tag"`
|
|
}
|
|
if err := db.Table("measurement").
|
|
Select("measurement.*, component.tag as comp_tag").
|
|
Joins("left join component on measurement.component_uuid = component.global_uuid").
|
|
Scan(&measurements).Error; err == nil {
|
|
for _, m := range measurements {
|
|
mSet.AllMeasTags = append(mSet.AllMeasTags, m.Tag)
|
|
if m.CompTag != "" {
|
|
mSet.CompTagToMeasTags[m.CompTag] = append(mSet.CompTagToMeasTags[m.CompTag], m.Tag)
|
|
}
|
|
}
|
|
}
|
|
return mSet, nil
|
|
}
|