73 lines
2.1 KiB
Go
73 lines
2.1 KiB
Go
package entity
|
|
|
|
import (
|
|
"time"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type DeviceType string
|
|
type DeviceStatus string
|
|
|
|
const (
|
|
ODP DeviceType = "ODP"
|
|
OTB DeviceType = "OTB"
|
|
|
|
Closure DeviceType = "closure"
|
|
|
|
ActiveDev DeviceStatus = "active"
|
|
InactiveDev DeviceStatus = "inactive"
|
|
MaintenanceDev DeviceStatus = "maintenance"
|
|
)
|
|
|
|
type Device 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"`
|
|
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"` // Store multiple images as JSONB
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
}
|
|
|
|
func (d *Device) 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 != "" && (d.ImageURL == nil || img != *d.ImageURL) { // Avoid duplicates
|
|
allImages = append(allImages, img)
|
|
}
|
|
}
|
|
|
|
return allImages
|
|
}
|
|
|
|
// Set multiple images (first one becomes primary)
|
|
func (d *Device) SetMultipleImages(imageURLs []string) {
|
|
if len(imageURLs) == 0 {
|
|
return
|
|
}
|
|
|
|
// Set primary image
|
|
primaryImage := imageURLs[0]
|
|
d.ImageURL = &primaryImage
|
|
|
|
// Set all images
|
|
d.ImageURLs = StringSlice(imageURLs)
|
|
}
|
|
|
|
func (Device) TableName() string {
|
|
return "devices"
|
|
} |