fix: manage quantity && update geoloca
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"gestion/models"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"net/url"
|
||||
@@ -47,35 +48,68 @@ type DeliveryDistance struct {
|
||||
}
|
||||
|
||||
type GeoService struct {
|
||||
redis *redis.Client
|
||||
ctx context.Context
|
||||
httpClient *http.Client
|
||||
redis *redis.Client
|
||||
ctx context.Context
|
||||
httpClient *http.Client
|
||||
correctionService *AddressCorrectionService
|
||||
}
|
||||
|
||||
func NewGeoService(redisClient *redis.Client, ctx context.Context) *GeoService {
|
||||
return &GeoService{
|
||||
gs := &GeoService{
|
||||
redis: redisClient,
|
||||
ctx: ctx,
|
||||
httpClient: &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
},
|
||||
}
|
||||
// Le correctionService est initialisé après, car il a besoin de gs lui-même
|
||||
gs.correctionService = NewAddressCorrectionService(gs)
|
||||
return gs
|
||||
}
|
||||
|
||||
func (gs *GeoService) GeocodeAddress(address string) (*GeoLocation, error) {
|
||||
// 1. Vérifier le cache Redis
|
||||
location, err := gs.getFromCache(address)
|
||||
if err == nil {
|
||||
// 1. Cache Redis (adresse originale)
|
||||
if location, err := gs.getFromCache(address); err == nil {
|
||||
return location, nil
|
||||
}
|
||||
|
||||
location, err = gs.fetchFromNominatim(address)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// 2. Tentative directe via Nominatim
|
||||
if location, err := gs.fetchFromNominatim(address); err == nil {
|
||||
gs.saveToCache(address, location)
|
||||
return location, nil
|
||||
}
|
||||
|
||||
// 3. Sauvegarder en cache
|
||||
// 3. ── NOUVEAU : correction automatique de l'adresse ──────────────────
|
||||
// Déclenché uniquement si le géocodage direct a échoué.
|
||||
log.Printf("🔍 [GEO] Géocodage direct échoué pour '%s', tentative de correction...", address)
|
||||
|
||||
suggestion, err := gs.correctionService.ResolveAddress(address)
|
||||
if err != nil {
|
||||
log.Printf("❌ [GEO] Correction impossible pour '%s': %v", address, err)
|
||||
return nil, fmt.Errorf("adresse introuvable : '%s'", address)
|
||||
}
|
||||
|
||||
if suggestion.CorrectionApplied {
|
||||
log.Printf(
|
||||
"✅ [GEO] Correction appliquée (confiance %.0f%%) : '%s' → '%s'",
|
||||
suggestion.Confidence*100,
|
||||
address,
|
||||
suggestion.CorrectedAddress,
|
||||
)
|
||||
}
|
||||
|
||||
location := &GeoLocation{
|
||||
Latitude: suggestion.Coordinates.Latitude,
|
||||
Longitude: suggestion.Coordinates.Longitude,
|
||||
DisplayName: suggestion.CorrectedAddress,
|
||||
}
|
||||
|
||||
// Mettre en cache avec l'adresse originale pour les prochains appels
|
||||
gs.saveToCache(address, location)
|
||||
// Mettre en cache aussi avec l'adresse corrigée
|
||||
if suggestion.CorrectionApplied {
|
||||
gs.saveToCache(suggestion.CorrectedAddress, location)
|
||||
}
|
||||
|
||||
return location, nil
|
||||
}
|
||||
@@ -491,13 +525,13 @@ func (gs *GeoService) GetAllDeliveryDistances(target Coordinates, availableUsern
|
||||
// ============================================
|
||||
|
||||
// GetDeliveryHeatmap retourne toutes les positions des livreurs
|
||||
func (gs *GeoService) GetDeliveryHeatmap() ([]map[string]interface{}, error) {
|
||||
func (gs *GeoService) GetDeliveryHeatmap() ([]map[string]any, error) {
|
||||
keys, err := gs.redis.Keys(gs.ctx, "delivery:location:*").Result()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var heatmap []map[string]interface{}
|
||||
var heatmap []map[string]any
|
||||
|
||||
for _, key := range keys {
|
||||
data, err := gs.redis.Get(gs.ctx, key).Result()
|
||||
@@ -505,7 +539,7 @@ func (gs *GeoService) GetDeliveryHeatmap() ([]map[string]interface{}, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
var location map[string]interface{}
|
||||
var location map[string]any
|
||||
json.Unmarshal([]byte(data), &location)
|
||||
|
||||
username := key[len("delivery:location:"):]
|
||||
@@ -516,3 +550,7 @@ func (gs *GeoService) GetDeliveryHeatmap() ([]map[string]interface{}, error) {
|
||||
|
||||
return heatmap, nil
|
||||
}
|
||||
|
||||
func (gs *GeoService) CorrectionService() *AddressCorrectionService {
|
||||
return gs.correctionService
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user