Esempio n. 1
0
        public decimal GetTrainStationWayPrice(TrainStationWay TrainStationWay, SeatTypeConfig SeatType, ContractUserType TicketUserType = ContractUserType.Adult)
        {
            if (TrainStationWay == null)
            {
                throw new ArgumentNullException("TrainStationWay");
            }
            decimal price      = 0;
            decimal decutPrice = 0;


            var list = TrainTicketPrices.Where(o => o.StartTrainStation >= TrainStationWay.StartTrainStation && o.EndTrainStation <= TrainStationWay.EndTrainStation);


            foreach (var item in list)
            {
                var trainSeatPrices = item.TrainSeatPrices.Where(o => o.SeatType == SeatType).FirstOrDefault();
                if (trainSeatPrices == null)
                {
                    throw new Exception($"车次{Code} {item}未配置{SeatType.Name}座位类型");
                }
                price += trainSeatPrices.Price;

                if (TicketUserType == ContractUserType.Children)
                {
                    decutPrice += trainSeatPrices.Price / 2;
                }

                if (TicketUserType == ContractUserType.Student)
                {
                    var defaultTrainSeatPrice = item.TrainSeatPrices.Where(o => o.IsDefault).FirstOrDefault();
                    if (defaultTrainSeatPrice == null)
                    {
                        throw new Exception($"车次{Code} {item}");
                    }
                    decutPrice += defaultTrainSeatPrice.Price;
                }
            }

            price -= decutPrice;

            //待处理学生价格
            return(price);
        }
Esempio n. 2
0
        public (bool, string, TrainOrder) BookTrainTicket(TrainShift TrainShift, TrainStation StartTrainStation, TrainStation EndTrainStation, IList <SeatTypeConfig> SeatTypes, CustomerInfo CustomerInfo, IList <UserContract> UserContracts)
        {
            if (StartTrainStation == null)
            {
                throw new ArgumentNullException("StartTrainStation");
            }
            if (!StartTrainStation.IsSale)
            {
                throw new ArgumentNullException($"{StartTrainStation} is Not Sale");
            }
            if (EndTrainStation == null)
            {
                throw new ArgumentNullException("EndTrainStation");
            }
            if (SeatTypes == null)
            {
                throw new ArgumentNullException("SeatTypes");
            }
            if (UserContracts == null || UserContracts.Count == 0)
            {
                throw new ArgumentNullException("UserContract");
            }
            if (UserContracts.Count(o => o.UserType != ContractUserType.Children) == 0)
            {
                throw new Exception("儿童不能单独买票");
            }
            if (StartTrainStation.Order > EndTrainStation.Order)
            {
                throw new ArgumentNullException("StartTrainStation same EndTrainStation Order");
            }
            if (StartTrainStation.Order > EndTrainStation.Order)
            {
                var temp = StartTrainStation;
                StartTrainStation = EndTrainStation;
                EndTrainStation   = temp;
            }

            var trainStationWay = new TrainStationWay(StartTrainStation, EndTrainStation);

            IList <Seat> seats = isMatchSeats(TrainShift, StartTrainStation, EndTrainStation, SeatTypes);

            //匹配优先在同一车厢
            if (seats == null || seats.Count == 0)
            {
                seats = TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType)).GroupBy(o => o.TrainCarriage).Where(o => o.Count() >= UserContracts.Count).FirstOrDefault().Take(UserContracts.Count).ToList();

                if (seats == null)
                {
                    seats = TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType)).Take(UserContracts.Count).ToList();
                    if (seats == null || seats.Count < UserContracts.Count)
                    {
                        throw new Exception("余票不足,建议单独购买");
                    }
                }
            }
            else if (seats.Count < UserContracts.Count)
            {
                foreach (var item in seats)
                {
                    seats = seats.Union(TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType) && o.TrainCarriage == item.TrainCarriage).Take(UserContracts.Count - seats.Count).ToList()).ToList();
                    if (seats.Count == UserContracts.Count)
                    {
                        break;
                    }
                }

                if (seats.Count < UserContracts.Count)
                {
                    seats = seats.Union(TrainShift.ExtraSeatInfos.Select(o => o.Seat).Where(o => SeatTypes.Contains(o.SeatType)).Take(UserContracts.Count - seats.Count).ToList()).ToList();
                    if (seats.Count < UserContracts.Count)
                    {
                        throw new Exception("余票不足,建议单独购买");
                    }
                }
            }
            else
            {
                seats = seats.GroupBy(o => o.TrainCarriage).Where(o => o.Count() >= UserContracts.Count).FirstOrDefault().Take(UserContracts.Count).ToList();
                if (seats == null || seats.Count == 0)
                {
                    seats = seats.Take(UserContracts.Count).ToList();
                }
            }
            if (seats == null || UserContracts.Count > seats.Count)
            {
                throw new Exception("余票不足");
            }

            IList <TrainOrderItem> trainOrderItems = new List <TrainOrderItem>();

            for (int i = 0; i < seats.Count; i++)
            {
                var seat         = seats[i];
                var userContract = UserContracts[i];
                var b            = TrainShift.ExtraSeatInfos.Remove(new ExtraSeatInfo(TrainShift, seat));


                new FreezeSeatInfo(TrainShift, new DestinationSeat(seat, new List <TrainStationWay>()
                {
                    trainStationWay
                }));
                new FreezeSeatInfo(TrainShift, new DestinationSeat(seat, new List <TrainStationWay>()
                {
                    trainStationWay
                }));

                var            price          = TrainShift.TrainNumber.GetTrainStationWayPrice(trainStationWay, seat.SeatType);
                TrainOrderItem trainOrderItem = new TrainOrderItem(seat, userContract, price);
                trainOrderItems.Add(trainOrderItem);
            }


            TrainOrder trainOrder = new TrainOrder(trainStationWay.StartTrainStation, trainStationWay.EndTrainStation, CustomerInfo, trainOrderItems);

            return(true, "预定成功", trainOrder);
        }