Пример #1
0
        public APIResult <DAL.vwTripsDetails> Share(ShareSeatRequest request)
        {
            var u = APIRequest.User(HttpContext.Current.Request);

            if (request.start_at == null)
            {
                request.start_at = DateTime.Now;
            }

            using (var ctx = new DAL.MainEntities())
            {
                var t = new Controllers.Trips.TripController();

checkCurrent:
                var currTrip = t.Current().data;

                if (currTrip != null)
                {
                    if (currTrip.booked_seats == 0)
                    {
                        var trp = ctx.trip_share_details.Find(currTrip.trip_id);
                        trp.is_active        = false;
                        ctx.Entry(trp).State = System.Data.Entity.EntityState.Modified;
                        ctx.SaveChanges();
                        goto checkCurrent;
                    }
                    else
                    {
                        return(APIResult <DAL.vwTripsDetails> .Error(ResponseCode.UserNotAcceptable, "You have already trip, You cannot share new trip"));
                    }
                }
                var Trip = new DAL.trip_share()
                {
                    created_at    = DateTime.Now,
                    created_by    = u.Entity.id,
                    driver_id     = u.Entity.id,
                    start_at_date = request.start_at,

                    trip_type_id = (int)request.trip_type,
                    vehicle_id   = request.vehicle_id,
                };
                ctx.trip_share.Add(Trip);

                //if (request.shuttle_at != null)
                //    Trip.end_at_date = request.shuttle_at.Value.Date;

                var Leave_Details = new DAL.trip_share_details()
                {
                    from_lat       = request.from.lat,
                    from_lng       = request.from.lng,
                    from_plc       = request.from.country + ", " + request.from.governorate + ", " + request.from.city + ", " + request.from.street,
                    to_lat         = request.to.lat,
                    to_lng         = request.to.lng,
                    to_plc         = request.to.country + ", " + request.to.governorate + ", " + request.to.city + ", " + request.to.street,
                    gender_id      = request.gender_id,
                    is_active      = true,
                    seats          = request.seats,
                    seat_cost      = request.seat_cost,
                    start_at_date  = request.start_at.Value,
                    start_at_time  = request.start_at.Value.TimeOfDay,
                    trip_direction = 1,
                    guid           = Guid.NewGuid()
                };
                Trip.trip_share_details.Add(Leave_Details);
                ctx.SaveChanges();
                BookIfAvailable(Leave_Details, Trip);
                ctx.Entry(Leave_Details).State = System.Data.Entity.EntityState.Modified;

                if (request.trip_type == TripTypes.Round || request.trip_type == TripTypes.Shuttle)
                {
                    var Round_Details = new DAL.trip_share_details()
                    {
                        to_lat         = request.from.lat,
                        to_lng         = request.from.lng,
                        to_plc         = request.from.country + ", " + request.from.governorate + ", " + request.from.city + ", " + request.from.street,
                        from_lat       = request.to.lat,
                        from_lng       = request.to.lng,
                        from_plc       = request.to.country + ", " + request.to.governorate + ", " + request.to.city + ", " + request.to.street,
                        gender_id      = request.gender_id,
                        is_active      = true,
                        seats          = request.seats,
                        seat_cost      = request.seat_cost,
                        start_at_date  = request.round_at == null ? DateTime.Now.Date : request.round_at.Value,
                        start_at_time  = request.round_at == null ? DateTime.Now.TimeOfDay : request.round_at.Value.TimeOfDay,
                        trip_direction = 2,
                        guid           = Guid.NewGuid()
                    };
                    Trip.trip_share_details.Add(Round_Details);
                    ctx.SaveChanges();
                    BookIfAvailable(Round_Details, Trip);
                    ctx.Entry(Round_Details).State = System.Data.Entity.EntityState.Modified;
                }

                if (request.trip_type == TripTypes.Shuttle)
                {
                    var cDate = request.start_at.Value.AddDays(1);
                    var rDate = request.round_at.Value.AddDays(1);
                    while (cDate <= request.shuttle_at.Value)
                    {
                        var Leave = new DAL.trip_share_details()
                        {
                            from_lat       = request.from.lat,
                            from_lng       = request.from.lng,
                            from_plc       = request.from.country + ", " + request.from.governorate + ", " + request.from.city + ", " + request.from.street,
                            to_lat         = request.to.lat,
                            to_lng         = request.to.lng,
                            to_plc         = request.to.country + ", " + request.to.governorate + ", " + request.to.city + ", " + request.to.street,
                            gender_id      = request.gender_id,
                            is_active      = true,
                            seats          = request.seats,
                            seat_cost      = request.seat_cost,
                            start_at_date  = cDate,
                            start_at_time  = request.start_at.Value.TimeOfDay,
                            trip_direction = 1,
                            guid           = Guid.NewGuid()
                        };
                        Trip.trip_share_details.Add(Leave);
                        ctx.SaveChanges();
                        BookIfAvailable(Leave, Trip);
                        ctx.Entry(Leave).State = System.Data.Entity.EntityState.Modified;
                        var Round = new DAL.trip_share_details()
                        {
                            to_lat         = request.from.lat,
                            to_lng         = request.from.lng,
                            to_plc         = request.from.country + ", " + request.from.governorate + ", " + request.from.city + ", " + request.from.street,
                            from_lat       = request.to.lat,
                            from_lng       = request.to.lng,
                            from_plc       = request.to.country + ", " + request.to.governorate + ", " + request.to.city + ", " + request.to.street,
                            gender_id      = request.gender_id,
                            is_active      = true,
                            seats          = request.seats,
                            seat_cost      = request.seat_cost,
                            start_at_date  = rDate,
                            start_at_time  = request.round_at.Value.TimeOfDay,
                            trip_direction = 2,
                            guid           = Guid.NewGuid()
                        };
                        Trip.trip_share_details.Add(Round);
                        ctx.SaveChanges();
                        BookIfAvailable(Round, Trip);
                        ctx.Entry(Round).State = System.Data.Entity.EntityState.Modified;
                        cDate = cDate.AddDays(1);
                        rDate = rDate.AddDays(1);
                    }
                }


                try
                {
                    ctx.SaveChanges();

                    var data = ctx.vwTripsDetails.Where(a => a.trip_share_id == Trip.id).FirstOrDefault();


                    return(APIResult <DAL.vwTripsDetails> .Success(data));
                }
                catch (Exception ex)
                {
                    return(APIResult <DAL.vwTripsDetails> .Error(ResponseCode.BackendDatabase, ex.Message));
                }
            }
        }
Пример #2
0
        private bool BookIfAvailable(DAL.trip_share_details details, DAL.trip_share trip)
        {
            using (var ctx = new DAL.MainEntities())
            {
                int searchReduis = int.Parse(Settings.Get("search_redius", "500"));

                var current_trips = ctx.trip_request_details.Include("trip_request").AsQueryable();


                //current_trips = current_trips.Where(a => a.from_plc.Contains(details.from_plc));

                //current_trips = current_trips.Where(a => (a.from_plc + ",").Contains(request.from.city + ","));


                //current_trips = current_trips.Where(a => a.from_plc.Contains(request.from.street + ","));

                current_trips = current_trips.Where(a => a.from_lat.Value + searchReduis <details.from_lat && a.from_lat.Value - searchReduis> details.from_lat);

                current_trips = current_trips.Where(a => a.from_lng.Value + searchReduis <details.from_lng && a.from_lng.Value - searchReduis> details.from_lng);



                //current_trips = current_trips.Where(a => a.to_plc.Contains(details.to_plc));



                //current_trips = current_trips.Where(a => a.to_plc.Contains(request.to.street + ","));

                current_trips = current_trips.Where(a => a.to_lat.Value + searchReduis <details.to_lat && a.to_lat.Value - searchReduis> details.to_lat);

                current_trips = current_trips.Where(a => a.to_lng.Value + searchReduis <details.to_lng && a.to_lng.Value - searchReduis> details.to_lng);



                if (details.gender_id > 0)
                {
                    current_trips = current_trips.Where(a => a.gender_id == details.gender_id && a.gender_id == 0);
                }


                current_trips = current_trips.Where(a => a.trip_request.trip_type_id == trip.trip_type_id);

                var sTime = details.start_at_date.Value.AddMinutes(120);
                var rDate = details.start_at_date.Value.Date;
                var rTime = details.start_at_date.Value.AddMinutes(-30);


                current_trips = current_trips.Where(a => a.start_at_date <sTime && a.start_at_date> rTime);

                current_trips = current_trips.Where(a => ((a.seat_cost_from > 0 && a.seat_cost_from <= details.seat_cost) || a.seat_cost_from == 0) && ((a.seat_cost_to > 0 && a.seat_cost_to >= details.seat_cost) || a.seat_cost_to == 0));
                current_trips = current_trips.Where(a => a.seats <= details.seats);
                current_trips = current_trips.Where(a => a.is_active == true);
                current_trips = current_trips.Where(a => a.booked == false || a.booked == null);

                if (details.booked_seats == null)
                {
                    details.booked_seats = 0;
                }
                foreach (var trp in current_trips)
                {
                    if (details.booked_seats >= details.seats)
                    {
                        break;
                    }
                    if (details.booked_seats + trp.seats >= details.seats)
                    {
                        continue;
                    }

                    for (var x = 0; x < trp.seats; x++)
                    {
                        var itm = new DAL.trip_book()
                        {
                            trip_request_details_id = trp.id,
                            booked_at             = DateTime.Now,
                            seats                 = 1,
                            trip_share_details_id = details.id,
                            trip_token            = Guid.NewGuid(),
                        };

                        ctx.trip_book.Add(itm);
                    }
                    details.booked_seats++;
                    trp.booked           = true;
                    ctx.Entry(trp).State = System.Data.Entity.EntityState.Modified;
                    //ctx.Entry(details).State = System.Data.Entity.EntityState.Modified;
                }
                //ctx.Entry(trip)
                ctx.SaveChanges();

                return(true);
            }
        }