autobus-api_old/AutobusApi.Domain/Entities/VehicleEnrollment.cs

94 lines
2.9 KiB
C#

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> RouteAddressDetails { get; set; } = null!;
public ICollection<VehicleEnrollmentEmployee> Crew { get; set; } = null!;
public ICollection<Ticket> Tickets { get; set; } = null!;
public ICollection<Review> 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;
}
}