public async Task <List <TripOnRoute> > AddExistingTripsOnRouteToRoute(int routeId, List <int> tripIds)
        {
            //Начинаем блядь КВН
            using (var transaction = new TransactionScope(asyncFlowOption: TransactionScopeAsyncFlowOption.Enabled))
            {
                var result = new List <TripOnRoute>();
                foreach (var tripId in tripIds)
                {
                    if (!await CompareDaysFromTurnover(routeId, tripId))
                    {
                        throw new ValidationException("Не совпадают дни графика оборота и рейса");
                    }

                    var sqlRTripOnRoutes = new TripsOnRouteRepository(_logger);
                    if (await sqlRTripOnRoutes.ByRouteIdAndTripId(routeId, tripId) != null)
                    {
                        throw new ValidationException("Данный рейс уже добавлен на этот маршрут");
                    }

                    await CheckTimelineUsing(routeId, tripId);

                    var res = await sqlRTripOnRoutes.Add(new TripOnRoute { RouteId = routeId, TripId = tripId });

                    result.Add(res);
                }
                transaction.Complete();
                return(result);
            }
        }
        public async Task <TripOnRouteWithStationsDto> TripWithStationsByRouteIdAndTripId(int routeId, int tripId)
        {
            var sqlRTripsOnRoute  = new TripsOnRouteRepository(_logger);
            var sqlRTrips         = new TripsRepository(_logger);
            var sqlRStationOnTrip = new StantionOnTripsRepository(_logger);
            var sqlRStation       = new StantionsRepository(_logger);
            var result            = new TripOnRouteWithStationsDto();

            var tripOnRoute = await sqlRTripsOnRoute.ByRouteIdAndTripId(routeId, tripId);

            if (tripOnRoute == null)
            {
                throw new ValidationException("Элемент не найден");
            }
            result.Id      = tripOnRoute.Id;
            result.RouteId = tripOnRoute.Id;
            result.TripId  = tripId;

            //result.TripWithDateTimeStations = new TripWithDateTimeStations();
            result.TripWithDateTimeStations = _mapper.Map <Trip, TripWithDateTimeStations>(await sqlRTrips.ById(tripId));
            var stations = await sqlRStationOnTrip.ByTripId(tripId);

            result.TripWithDateTimeStations.StantionOnTripsWithStringTime = new List <StationOnTripWithStringDateTime>();
            foreach (var stantion in stations)
            {
                var toAdd = _mapper.Map <StantionOnTrip, StationOnTripWithStringDateTime>(stantion);
                toAdd.InTimeString  = stantion.InTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
                toAdd.OutTimeString = stantion.OutTime.ToString("yyyy-MM-ddTHH:mm:ssZ");
                toAdd.Stantion      = await sqlRStation.ById(toAdd.StantionId);

                result.TripWithDateTimeStations.StantionOnTripsWithStringTime.Add(toAdd);
            }

            return(result);
        }
        public async Task <List <TripWithStartEndTimeAndDays> > GetTripsByTurnoverIdAndDays(int turnoverId, int routeId)
        {
            var sqlRTripOnRoute    = new TripsOnRouteRepository(_logger);
            var sqlRRoute          = new RoutesRepository(_logger);
            var sqlRDaysOfRoutes   = new DayOfRoutesRepoisitory(_logger);
            var sqlRTrips          = new TripsRepository(_logger);
            var sqlRStationOnTrips = new StantionOnTripsRepository(_logger);
            var sqlRStation        = new StantionsRepository(_logger);
            var sqlRDayOfTrips     = new DayOfTripsRepoisitory(_logger);
            //Получаем дни циклового графика
            var daysTurnoverObj = await sqlRDaysOfRoutes.DaysByTurnoverId(turnoverId);

            var daysTurnover = daysTurnoverObj.Select(x => x.Day).ToList();

            daysTurnover.Sort();
            //Надо найти трипсы с аналогичными днями.
            var trips = await sqlRTrips.GetAll();

            var result = new List <TripWithStartEndTimeAndDays>();


            var route = await sqlRRoute.ById(routeId);

            foreach (var trip in trips)
            {
                if (await sqlRTripOnRoute.ByRouteIdAndTripId(route.Id, trip.Id) != null)
                {
                    continue;
                }

                var currentTripDaysObj = await sqlRDayOfTrips.DaysByTripId(trip.Id);

                var currentTripDays = currentTripDaysObj.Select(x => x.Day).ToList();
                currentTripDays.Sort();

                var isGoodTrip = true;
                foreach (var d in daysTurnover)
                {
                    if (!currentTripDays.Any(x => x.Equals(d)))
                    {
                        isGoodTrip = false;
                    }
                }
                if (!isGoodTrip)
                {
                    continue;
                }
                {
                    string days = null;
                    foreach (var day in currentTripDays)
                    {
                        if (days == null)
                        {
                            days = GetStringDayOfWeekShort(day);
                        }
                        else
                        {
                            days = days + ", " + GetStringDayOfWeekShort(day);
                        }
                    }
                    var tripStations = await sqlRStationOnTrips.ByTripId(trip.Id);

                    tripStations = tripStations.OrderBy(x => x.OutTime).ToList();
                    var startStation = tripStations.First();
                    var endStation   = tripStations.Last();

                    var tripExtended = _mapper.Map <Trip, TripWithStartEndTimeAndDays>(trip);
                    tripExtended.Days             = currentTripDays;
                    tripExtended.StartTime        = startStation.OutTime.ToStringTimeOnly();
                    tripExtended.EndTime          = endStation.InTime.ToStringTimeOnly();
                    tripExtended.StartStationName = (await sqlRStation.ById(startStation.StantionId)).Name;
                    tripExtended.EndStationName   = (await sqlRStation.ById(endStation.StantionId)).Name;
                    result.Add(tripExtended);
                }
            }

            return(result);
        }