public async Task <ContentResult> GetCarDetails(GetCarInput input)
        {
            try
            {
                ReturnMessage rm   = new ReturnMessage(1, "Success");
                var           cars = await Task.Run(() => _unitOfWork.Cars.GetAsync(filter: w => input.Id != 0 ? (w.Id == input.Id) : true, includeProperties: "CarClassification"));

                var carsToReturn = _mapper.Map <IEnumerable <CarDto> >(cars);
                foreach (var item in carsToReturn)
                {
                    item.PassengerCount = item.CarClassification.PassengerCount;
                    item.CostPerHour    = item.CarClassification.CostPerHour;
                    item.CostPerDay     = item.CarClassification.CostPerDay;
                    item.LateFeePerHour = item.CarClassification.LateFeePerHour;
                }
                return(this.Content(rm.returnMessage(new PagedResultDto <CarDto>
                                                         (carsToReturn.AsQueryable(), input.pagenumber, input.pagesize)),
                                    "application/json"));
            }
            catch (Exception ex)
            {
                return(this.Content(JsonConvert.SerializeObject(new
                {
                    msgCode = -3,
                    msg = ex.Message
                }), "application/json"));
            }
        }
        public async Task <ContentResult> DeleteCar(GetCarInput input)
        {
            ReturnMessage returnmessage = new ReturnMessage(1, "Car Deleted Succesfully");

            try
            {
                var car = await Task.Run(() => _unitOfWork.Cars.GetAsync(filter: w => w.Id == input.Id));

                if (car.Count() == 0)
                {
                    returnmessage.msgCode = -2;
                    returnmessage.msg     = "Car Not Found";
                }
                else
                {
                    _unitOfWork.Cars.Remove(car.First());
                }
                _unitOfWork.Complete();
                _logger.LogInformation("Log:Delete Car for ID: {Id}", input.Id);

                return(this.Content(returnmessage.returnMessage(null),
                                    "application/json"));
            }
            catch (Exception ex)
            {
                returnmessage.msg     = ex.Message.ToString();
                returnmessage.msgCode = -3;
                return(this.Content(returnmessage.returnMessage(null)));
            }
        }
        public async Task <ContentResult> GetCar(GetCarInput input)
        {
            var est = TimeZoneInfo.FindSystemTimeZoneById("AUS Eastern Standard Time");

            input.AvailableDateCheck = TimeZoneInfo.ConvertTime(input.AvailableDateCheck, est);

            try
            {
                ReturnMessage rm   = new ReturnMessage(1, "Success");
                var           cars = await Task.Run(() => _unitOfWork.Cars.GetAsync(filter: w => (input.Id != 0 ? (w.Id == input.Id) : true) &&
                                                                                    (input.RegistrationNumber != null? (w.RegistrationNumber.Contains(input.RegistrationNumber)) : true) &&
                                                                                    (input.Model != null? (w.Model.Contains(input.Model)) : true) &&
                                                                                    (input.Year != 0? (w.Year.ToString().Contains(input.Year.ToString())) : true),

                                                                                    includeProperties: "CarClassification"));

                var carsToReturn = _mapper.Map <IEnumerable <CarDto> >(cars);
                foreach (var item in carsToReturn)
                {
                    item.CarAvailability = GetavailableTime(new GetCarInput()
                    {
                        Id = item.Id, AvailableDateCheck = input.AvailableDateCheck
                    });
                    var location = await Task.Run(() => _unitOfWork.Locations.GetAsync(filter: w => w.CarId == item.Id && w.isAtLocation == true, includeProperties: "Branch"));

                    if (location.Count() != 0)
                    {
                        item.CurrentLocation = location.First().Branch.BranchName;
                    }
                }
                return(this.Content(rm.returnMessage(new PagedResultDto <CarDto>
                                                         (carsToReturn.AsQueryable(), input.pagenumber, input.pagesize)),
                                    "application/json"));
            }
            catch (Exception ex)
            {
                return(this.Content(JsonConvert.SerializeObject(new
                {
                    msgCode = -3,
                    msg = ex.Message
                }), "application/json"));
            }
        }
Beispiel #4
0
        /// <summary>
        /// 根据查询条件获取车辆管理分页列表
        /// </summary>
        public async Task <PagedResultDto <CarListDto> > GetPagedCarsAsync(GetCarInput input)
        {
            var query = CarRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件

            var carCount = await query.CountAsync();

            var cars = await query
                       .OrderBy(input.Sorting)
                       .PageBy(input)
                       .ToListAsync();

            var carListDtos = cars.MapTo <List <CarListDto> >();

            return(new PagedResultDto <CarListDto>(
                       carCount,
                       carListDtos
                       ));
        }
        public List <CarAvailbilityTimeDto> GetavailableTime(GetCarInput input)
        {
            try
            {
                IEnumerable <int>            notin = new List <int>();
                List <string>                test  = new List <string>();
                List <CarAvailbilityTimeDto> caravailabilitytimelist = new List <CarAvailbilityTimeDto>();
                var        bookings   = _unitOfWork.Bookings.Find(x => x.CarId == input.Id && x.isActive == true && input.AvailableDateCheck.Date >= x.FromDate.Date && input.AvailableDateCheck.Date <= x.ReturnDate.Date);
                List <int> totalhours = new List <int>();
                for (int i = 0; i < 24; i++)
                {
                    totalhours.Add(i);
                }
                IList <int> hoursBetween = new List <int>();
                foreach (var item in bookings)
                {
                    if (input.AvailableDateCheck > item.FromDate)
                    {
                        input.AvailableDateCheck = input.AvailableDateCheck.Date;
                    }
                    if (input.AvailableDateCheck.Date == item.FromDate.Date)
                    {
                        TimeSpan time = item.FromDate.TimeOfDay;
                        input.AvailableDateCheck = input.AvailableDateCheck.Date + time;
                    }

                    if (item.ReturnDate > input.AvailableDateCheck)
                    {
                        TimeSpan ts = item.ReturnDate - input.AvailableDateCheck;
                        hoursBetween = Enumerable.Range(0, ((int)ts.TotalHours) + 1)
                                       .Select(i => input.AvailableDateCheck.AddHours(i).Hour).ToList();
                    }
                    for (int i = 0; i < hoursBetween.Count(); i++)
                    {
                        var date = input.AvailableDateCheck;
                        if (i != 0 && hoursBetween[i] == 0)
                        {
                            break;
                            //date = input.AvailableDateCheck.AddDays(1);
                        }
                        CarAvailbilityTimeDto carAvailbility = new CarAvailbilityTimeDto()
                        {
                            BookingId   = item.Id,
                            CarId       = item.CarId,
                            Date        = date,
                            time        = string.Format("{0}:00", hoursBetween[i].ToString()),
                            IsAvailable = false,
                            Hour        = hoursBetween[i]
                        };
                        caravailabilitytimelist.Add(carAvailbility);
                    }
                    notin = totalhours.Where(p => caravailabilitytimelist.All(p2 => p2.Hour != p));
                    foreach (var item1 in notin)
                    {
                        CarAvailbilityTimeDto carAvailbility = new CarAvailbilityTimeDto()
                        {
                            BookingId   = item.Id,
                            CarId       = item.CarId,
                            Date        = input.AvailableDateCheck,
                            time        = string.Format("{0}:00", item1.ToString()),
                            IsAvailable = true,
                            Hour        = item1
                        };
                        caravailabilitytimelist.Add(carAvailbility);
                    }
                }
                notin = totalhours.Where(p => caravailabilitytimelist.All(p2 => p2.Hour != p));
                foreach (var item1 in notin)
                {
                    CarAvailbilityTimeDto carAvailbility = new CarAvailbilityTimeDto()
                    {
                        BookingId   = 0,
                        CarId       = input.Id,
                        Date        = input.AvailableDateCheck,
                        time        = string.Format("{0}:00", item1.ToString()),
                        IsAvailable = true,
                        Hour        = item1
                    };
                    caravailabilitytimelist.Add(carAvailbility);
                }
                _unitOfWork.Complete();
                return(caravailabilitytimelist.OrderBy(x => x.Hour).ToList());
            }
            catch (Exception ex)
            {
                throw;
            }
        }