95 lines
2.8 KiB
Go
95 lines
2.8 KiB
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
)
|
|
|
|
// GetCommandCategories retourne les catégories distinctes des produits d'une commande
|
|
func (d *Database) GetCommandCategories(commandID int) ([]string, error) {
|
|
query := `
|
|
SELECT DISTINCT p.category
|
|
FROM command_items ci
|
|
JOIN products p ON p.id = ci.product_id
|
|
WHERE ci.command_id = $1 AND p.category IS NOT NULL AND p.category != ''`
|
|
|
|
rows, err := d.Query(query, commandID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("erreur lecture catégories commande %d: %w", commandID, err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var categories []string
|
|
for rows.Next() {
|
|
var cat string
|
|
if err := rows.Scan(&cat); err == nil {
|
|
categories = append(categories, cat)
|
|
}
|
|
}
|
|
return categories, rows.Err()
|
|
}
|
|
|
|
// GetEligibleDeliverymenForCommand retourne les usernames des livreurs éligibles pour une commande.
|
|
// En mode "single" → tous les livreurs actifs.
|
|
// En mode "category_based" → le(s) livreur(s) assigné(s) aux catégories de la commande.
|
|
// Si aucune correspondance → fallback sur tous les livreurs actifs.
|
|
func (d *Database) GetEligibleDeliverymenForCommand(commandID int) ([]string, error) {
|
|
settings, err := d.GetSettings()
|
|
if err != nil {
|
|
log.Printf("⚠️ [DELIVERY_MODE] Erreur lecture settings: %v — fallback single", err)
|
|
}
|
|
|
|
allActive := func() ([]string, error) {
|
|
livreurs, err := d.GetAllActiveDeliveryPersons()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
names := make([]string, len(livreurs))
|
|
for i, l := range livreurs {
|
|
names[i] = l.Username
|
|
}
|
|
return names, nil
|
|
}
|
|
|
|
if settings.DeliveryMode.Mode != "category_based" || len(settings.DeliveryMode.CategoryRoutes) == 0 {
|
|
return allActive()
|
|
}
|
|
|
|
// Mode category_based : récupérer les catégories de la commande
|
|
categories, err := d.GetCommandCategories(commandID)
|
|
if err != nil || len(categories) == 0 {
|
|
log.Printf("⚠️ [DELIVERY_MODE] Cmd %d: catégories non trouvées — fallback single", commandID)
|
|
return allActive()
|
|
}
|
|
|
|
// Construire un set des catégories de la commande
|
|
catSet := make(map[string]bool, len(categories))
|
|
for _, c := range categories {
|
|
catSet[c] = true
|
|
}
|
|
|
|
// Trouver les livreurs dont la route intersecte les catégories
|
|
eligible := make(map[string]bool)
|
|
for _, route := range settings.DeliveryMode.CategoryRoutes {
|
|
for _, routeCat := range route.Categories {
|
|
if catSet[routeCat] {
|
|
eligible[route.DeliverymanUsername] = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(eligible) == 0 {
|
|
log.Printf("⚠️ [DELIVERY_MODE] Cmd %d: aucun livreur pour catégories %v — fallback single", commandID, categories)
|
|
return allActive()
|
|
}
|
|
|
|
result := make([]string, 0, len(eligible))
|
|
for u := range eligible {
|
|
result = append(result, u)
|
|
}
|
|
|
|
log.Printf("🎯 [DELIVERY_MODE] Cmd %d: livreurs éligibles %v (catégories: %v)", commandID, result, categories)
|
|
return result, nil
|
|
}
|