NAM-APJATEL-BACKEND/utils/helper/nearest_towers.go

114 lines
4.0 KiB
Go

package helper
import (
"fmt"
"log"
"users_management/m/model/dto/res"
"users_management/m/model/entity"
"users_management/m/repository"
"users_management/m/utils/service"
"github.com/google/uuid"
)
func ConvertToNearestTowerResponses(towers []entity.TowerWithDistance, geocoder service.GeocodingService) ([]res.NearestTowerResponse, error) {
var responses []res.NearestTowerResponse
for _, tower := range towers {
// Get address
address := ""
if geocoder != nil {
addr, err := geocoder.GetAddressFromCoordinates(tower.Latitude, tower.Longitude)
if err != nil {
log.Printf("Geocoding error for tower %s: %v", tower.TowerCode, err)
address = fmt.Sprintf("Coordinates: %.6f, %.6f", tower.Latitude, tower.Longitude)
} else {
address = addr
}
}
response := res.NearestTowerResponse{
ID: tower.ID,
TowerCode: tower.TowerCode,
Distance: tower.Distance,
Address: address,
Longitude: tower.Longitude,
Latitude: tower.Latitude,
ImageURL: tower.ImageURL,
ExternalTower: tower.ExternalTower,
DeviceCode: tower.DeviceCode,
Province: tower.Province,
City: tower.City,
District: tower.District,
CreatedAt: tower.CreatedAt,
}
responses = append(responses, response)
}
return responses, nil
}
func ConvertToNearestTowerDetailResponse(tower entity.Tower, distance float64, repo repository.NearestDeviceRepo, geocoder service.GeocodingService) (res.NearestTowerDetailResponse, error) {
// Get address
address := ""
if geocoder != nil {
addr, err := geocoder.GetAddressFromCoordinates(tower.Latitude, tower.Longitude)
if err != nil {
log.Printf("Geocoding error for tower %s: %v", tower.TowerCode, err)
address = fmt.Sprintf("Coordinates: %.6f, %.6f", tower.Latitude, tower.Longitude)
} else {
address = addr
}
}
// Get all image URLs
allImageURLs := tower.GetAllImageURLs()
var deviceInfo *res.DeviceConnectionInfo
if tower.Device.ID != uuid.Nil {
// Get device address
deviceAddress := ""
if geocoder != nil {
addr, err := geocoder.GetAddressFromCoordinates(tower.Device.Latitude, tower.Device.Longitude)
if err != nil {
log.Printf("Geocoding error for device %s: %v", tower.Device.DeviceCode, err)
deviceAddress = fmt.Sprintf("Coordinates: %.6f, %.6f", tower.Device.Latitude, tower.Device.Longitude)
} else {
deviceAddress = addr
}
}
// Calculate distance from tower to device
deviceDistance := calculateDistance(tower.Latitude, tower.Longitude, tower.Device.Latitude, tower.Device.Longitude)
deviceInfo = &res.DeviceConnectionInfo{
ID: tower.Device.ID,
DeviceCode: tower.Device.DeviceCode,
DeviceType: string(tower.Device.DeviceType),
Distance: deviceDistance,
Address: deviceAddress,
Longitude: tower.Device.Longitude,
Latitude: tower.Device.Latitude,
Status: string(tower.Device.Status),
PortAmount: tower.Device.PortAmount,
}
}
response := res.NearestTowerDetailResponse{
ID: tower.ID,
TowerCode: tower.TowerCode,
Distance: distance,
Address: address,
Longitude: tower.Longitude,
Latitude: tower.Latitude,
ImageURL: &tower.ImageURL,
ImageURLs: allImageURLs,
ExternalTower: tower.ExternalTower,
Device: deviceInfo,
CreatedAt: tower.CreatedAt,
UpdatedAt: tower.UpdatedAt,
}
return response, nil
}