using AutobusApi.Domain.Common; namespace AutobusApi.Domain.Entities; public class VehicleEnrollment : EntityBase { public DateTime DepartureDateTimeUtc { get; set; } public int VehicleId { get; set; } public Vehicle Vehicle { get; set; } = null!; public int RouteId { get; set; } public Route Route { get; set; } = null!; public ICollection RouteAddressDetails { get; set; } = null!; public ICollection Crew { get; set; } = null!; public ICollection Tickets { get; set; } = null!; public ICollection Reviews { get; set; } = null!; public DateTime GetDepartureDateTimeUtc(int departureAddressId) { DateTime result = DepartureDateTimeUtc; var routeAddressesDetails = RouteAddressDetails .OrderBy(rad => rad.RouteAddress.Order) .Reverse() .SkipWhile(rad => rad.RouteAddress.AddressId != departureAddressId) .Reverse() .ToArray(); // Ignore stop time on first address routeAddressesDetails.First().CurrentAddressStopTime = TimeSpan.Zero; // Ignore travel time to next address on last address routeAddressesDetails.Last().TimeToNextAddress = TimeSpan.Zero; foreach (var routeAddressDetails in routeAddressesDetails) { result += routeAddressDetails.CurrentAddressStopTime; result += routeAddressDetails.TimeToNextAddress; } return result; } public DateTime GetArrivalDateTimeUtc(int arrivalAddressId) { DateTime result = DepartureDateTimeUtc; var routeAddressesDetails = RouteAddressDetails .OrderBy(rad => rad.RouteAddress.Order) .TakeWhile(rad => rad.RouteAddress.AddressId != arrivalAddressId) .ToArray(); // Ignore stop time on first station routeAddressesDetails.First().CurrentAddressStopTime = TimeSpan.Zero; foreach (var routeAddressDetails in routeAddressesDetails) { result += routeAddressDetails.CurrentAddressStopTime; result += routeAddressDetails.TimeToNextAddress; } return result; } public TimeSpan GetTravelTime(int departureAddressId, int arrivalAddressId) { return GetArrivalDateTimeUtc(arrivalAddressId) - GetDepartureDateTimeUtc(departureAddressId); } public double GetCost(int departureAddressId, int arrivalAddressId) { double result = 0.0; var routeAddressesDetails = RouteAddressDetails .OrderBy(rad => rad.RouteAddress.Order) .SkipWhile(rad => rad.RouteAddress.AddressId != departureAddressId) .TakeWhile(rad => rad.RouteAddress.AddressId != arrivalAddressId) .ToArray(); foreach (var routeAddressDetails in routeAddressesDetails) { result += routeAddressDetails.CostToNextAddress; } return result; } }