carpool-service/servers/ocss-api/search.go

163 lines
6.0 KiB
Go

package ocssapi
import (
"context"
"time"
"git.coopgo.io/coopgo-platform/carpool-service/interoperability/ocss"
"github.com/paulmach/orb"
"github.com/rs/zerolog/log"
)
func (s *OCSSApiService) GetDriverJourneys(ctx context.Context, departureLat float64, departureLng float64, arrivalLat float64, arrivalLng float64, departureDate time.Time, timeDelta *time.Duration, departureRadius *float64, arrivalRadius *float64, count *int64) ([]ocss.DriverJourney, error) {
departure := orb.Point{departureLng, departureLat}
arrival := orb.Point{arrivalLng, arrivalLat}
td := 900 * time.Second
if timeDelta != nil {
td = *timeDelta
}
minDate := departureDate.Add(-td * time.Second)
maxDate := departureDate.Add(td * time.Second)
journeys, err := s.Handler.GetDriverJourneys(departure, arrival, departureRadius, arrivalRadius, minDate, maxDate, count)
if err != nil {
return nil, err
}
results := []ocss.DriverJourney{}
for _, j := range journeys {
properties := j.Route.ExtraMembers["properties"].(map[string]any)
usermap := properties["user"].(map[string]any)
journeyId := j.ID
driverDepartureLat := j.Route.Features[0].Point().Lat()
driverDepartureLng := j.Route.Features[0].Point().Lon()
driverArrivalLat := j.Route.Features[1].Point().Lat()
driverArrivalLng := j.Route.Features[1].Point().Lon()
driverDepartureDate := ocss.JSONTime(j.DepartureDate)
duration := time.Duration(0)
var distance *int64
if len(j.Itinerary.Legs) > 2 {
duration = j.Itinerary.Legs[1].Duration
dist := int64(j.Itinerary.Legs[1].Distance)
distance = &dist
}
results = append(results, ocss.DriverJourney{
DriverTrip: ocss.DriverTrip{
Driver: ocss.User{
ID: usermap["id"].(string),
Operator: usermap["operator"].(string),
Alias: usermap["alias"].(string),
},
Trip: ocss.Trip{
Operator: "ridygo.fr",
PassengerPickupLat: departureLat,
PassengerPickupLng: departureLng,
PassengerDropLat: arrivalLat,
PassengerDropLng: arrivalLng,
DriverDepartureLat: &driverDepartureLat,
DriverDepartureLng: &driverDepartureLng,
DriverArrivalLat: &driverArrivalLat,
DriverArrivalLng: &driverArrivalLng,
Duration: duration,
Distance: distance,
},
},
JourneySchedule: ocss.JourneySchedule{
ID: &journeyId,
PassengerPickupDate: ocss.JSONTime(j.DepartureDate.Add(j.Itinerary.Legs[0].Duration)),
DriverDepartureDate: &driverDepartureDate,
Type: ocss.Planned,
},
// Price: &ocss.Price{
// Amount: ,
// Currency: "EUR",
// },
})
}
return results, nil
}
func (s *OCSSApiService) GetPassengerJourneys(ctx context.Context, departureLat float64, departureLng float64, arrivalLat float64, arrivalLng float64, departureDate time.Time, timeDelta *time.Duration, departureRadius *float64, arrivalRadius *float64, count *int64) ([]ocss.PassengerJourney, error) {
departure := orb.Point{departureLng, departureLat}
arrival := orb.Point{arrivalLng, arrivalLat}
td := 900 * time.Second
if timeDelta != nil {
td = *timeDelta
}
minDate := departureDate.Add(-td * time.Second)
maxDate := departureDate.Add(td * time.Second)
journeys, err := s.Handler.GetPassengerJourneys(departure, arrival, departureRadius, arrivalRadius, minDate, maxDate, count)
if err != nil {
return nil, err
}
results := []ocss.PassengerJourney{}
for _, j := range journeys {
properties := j.Route.ExtraMembers["properties"].(map[string]any)
usermap := properties["user"].(map[string]any)
journeyId := j.ID
passengerDepartureLat := j.Route.Features[0].Point().Lat()
passengerDepartureLng := j.Route.Features[0].Point().Lon()
passengerArrivalLat := j.Route.Features[1].Point().Lat()
passengerArrivalLng := j.Route.Features[1].Point().Lon()
passengerDepartureDate := ocss.JSONTime(j.DepartureDate)
driverDepartureDate := ocss.JSONTime(j.DepartureDate.Add(-j.Itinerary.Legs[0].Duration))
duration := time.Duration(0)
var distance *int64
log.Debug().Any("itinerary", j.Itinerary).Msg("debug itinerary")
if len(j.Itinerary.Legs) > 2 {
duration = j.Itinerary.Legs[1].Duration / time.Second
dist := int64(j.Itinerary.Legs[1].Distance)
distance = &dist
}
results = append(results, ocss.PassengerJourney{
PassengerTrip: ocss.PassengerTrip{
Passenger: ocss.User{
ID: usermap["id"].(string),
Operator: usermap["operator"].(string),
Alias: usermap["alias"].(string),
},
Trip: ocss.Trip{
Operator: "ridygo.fr",
PassengerPickupLat: passengerDepartureLat,
PassengerPickupLng: passengerDepartureLng,
PassengerDropLat: passengerArrivalLat,
PassengerDropLng: passengerArrivalLng,
DriverDepartureLat: &departureLat,
DriverDepartureLng: &departureLng,
DriverArrivalLat: &arrivalLat,
DriverArrivalLng: &arrivalLng,
Duration: duration,
Distance: distance,
},
},
JourneySchedule: ocss.JourneySchedule{
ID: &journeyId,
PassengerPickupDate: passengerDepartureDate,
DriverDepartureDate: &driverDepartureDate,
Type: ocss.Planned,
},
})
}
return results, nil
}
func (s *OCSSApiService) GetDriverRegularTrips(ctx context.Context, departureLat float64, departureLng float64, arrivalLat float64, arrivalLng float64, departureTimeOfDay string, departureWeekDays *[]string, timeDelta *time.Duration, departureRadius *float64, arrivalRadius *float64, minDepartureDate *time.Time, maxDepartureDate *time.Time, count *int64) ([]ocss.DriverTrip, error) {
return []ocss.DriverTrip{}, nil
}
func (s *OCSSApiService) GetPassengerRegularTrips(ctx context.Context, departureLat float64, departureLng float64, arrivalLat float64, arrivalLng float64, departureTimeOfDay string, departureWeekDays *[]string, timeDelta *time.Duration, departureRadius *float64, arrivalRadius *float64, minDepartureDate *time.Time, maxDepartureDate *time.Time, count *int64) ([]ocss.PassengerTrip, error) {
return []ocss.PassengerTrip{}, nil
}