99 lines
3.7 KiB
Go
99 lines
3.7 KiB
Go
package entity
|
|
|
|
import (
|
|
"time"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type DeviceDetails struct {
|
|
ID uuid.UUID `json:"id" gorm:"type:uuid;primaryKey"`
|
|
DeviceCode string `json:"device_code" gorm:"unique"`
|
|
DeviceType DeviceType `json:"device_type"`
|
|
Longitude float64 `json:"longitude"`
|
|
Latitude float64 `json:"latitude"`
|
|
PortAmount int `json:"port_amount"`
|
|
Status DeviceStatus `json:"status"`
|
|
Region *string `json:"region,omitempty" gorm:"type:varchar(255)"`
|
|
Province *string `json:"province,omitempty" gorm:"type:varchar(255)"`
|
|
City *string `json:"city,omitempty" gorm:"type:varchar(255)"`
|
|
District *string `json:"district,omitempty" gorm:"type:varchar(255)"`
|
|
ImageURL *string `json:"image_url,omitempty" gorm:"type:text"`
|
|
ImageURLs StringSlice `json:"image_urls" gorm:"type:jsonb"`
|
|
OLTID *uuid.UUID `json:"olt_id,omitempty" gorm:"type:uuid"`
|
|
TowerID *uuid.UUID `json:"tower_id,omitempty" gorm:"type:uuid"` // Add TowerID field
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
// Relationships
|
|
DevicePort DevicePort `json:"device_port" gorm:"foreignKey:DeviceID;references:ID"`
|
|
BackbonesStart []Backbone `json:"backbones_start" gorm:"foreignKey:DeviceStartID;references:ID"`
|
|
BackbonesEnd []Backbone `json:"backbones_end" gorm:"foreignKey:DeviceEndID;references:ID"`
|
|
FishbonesStart []Fishbone `json:"fishbones_start" gorm:"foreignKey:DeviceStartID;references:ID"`
|
|
FishbonesEnd []Fishbone `json:"fishbones_end" gorm:"foreignKey:DeviceEndID;references:ID"`
|
|
OLT *OLT `json:"olt,omitempty" gorm:"foreignKey:OLTID;references:ID"`
|
|
Tower *Tower `json:"tower,omitempty" gorm:"foreignKey:TowerID;references:ID"` // Add Tower relationship
|
|
Towers []Tower `json:"towers" gorm:"foreignKey:DeviceID;references:ID"`
|
|
}
|
|
|
|
|
|
|
|
func (d *DeviceDetails) GetAllImageURLs() []string {
|
|
var allImages []string
|
|
|
|
// Add main image URL if exists
|
|
if d.ImageURL != nil && *d.ImageURL != "" {
|
|
allImages = append(allImages, *d.ImageURL)
|
|
}
|
|
|
|
// Add additional images
|
|
for _, img := range d.ImageURLs {
|
|
if img != "" {
|
|
// Avoid duplicates
|
|
isDuplicate := false
|
|
for _, existingImg := range allImages {
|
|
if existingImg == img {
|
|
isDuplicate = true
|
|
break
|
|
}
|
|
}
|
|
if !isDuplicate {
|
|
allImages = append(allImages, img)
|
|
}
|
|
}
|
|
}
|
|
|
|
return allImages
|
|
}
|
|
|
|
func (d *DeviceDetails) SetMultipleImages(imageURLs []string) {
|
|
if len(imageURLs) == 0 {
|
|
return
|
|
}
|
|
|
|
// Set primary image
|
|
primaryImage := imageURLs[0]
|
|
d.ImageURL = &primaryImage
|
|
|
|
// Set all images
|
|
d.ImageURLs = StringSlice(imageURLs)
|
|
}
|
|
|
|
// Helper method to get all backbones connected to this device
|
|
func (d *DeviceDetails) GetAllBackbones() []Backbone {
|
|
allBackbones := make([]Backbone, 0)
|
|
allBackbones = append(allBackbones, d.BackbonesStart...)
|
|
allBackbones = append(allBackbones, d.BackbonesEnd...)
|
|
return allBackbones
|
|
}
|
|
|
|
// Helper method to get all fishbones connected to this device
|
|
func (d *DeviceDetails) GetAllFishbones() []Fishbone {
|
|
allFishbones := make([]Fishbone, 0)
|
|
allFishbones = append(allFishbones, d.FishbonesStart...)
|
|
allFishbones = append(allFishbones, d.FishbonesEnd...)
|
|
return allFishbones
|
|
}
|
|
|
|
func (DeviceDetails) TableName() string {
|
|
return "devices" // Use same table as Device entity
|
|
} |