public ActionResult PostTicket(int Dates,string Trans,int StartStep,int EndStep)
        {
            TicketOrder MyOrder = new TicketOrder();
            MyOrder.EndStep = EndStep;
            MyOrder.StartStep = StartStep;
            MyOrder.TrainDate = Dates;
            MyOrder.TrainNumber = Trans;
            if (MyOrder.IsValidate())
            {
                TicketDispatch.GetIntence().StartTicketOrder(MyOrder);
                ViewBag.Message = "正在处理您的订单,请耐心等待。";
                ViewBag.VerficationCode = MyOrder.VerificationCode;
            }
            else
                ViewBag.Message = "请购买正确的始发地与目的地车票!";

            //if (mysales.SaleTrainTicket(Trans, StartStep, EndStep, Dates, ref varcharnumber, ref errormessage))
            //{
            //    ViewBag.Message = "购票成功,您的订单号为:" + varcharnumber;
            //}
            //else
            //    ViewBag.Message = errormessage;

            return View();
        }
 public JsonResult JsonPostTicket(int Dates, string Trans, int StartStep, int EndStep)
 {
     TicketOrder MyOrder = new TicketOrder();
     MyOrder.EndStep = EndStep;
     MyOrder.StartStep = StartStep;
     MyOrder.TrainDate = Dates;
     MyOrder.TrainNumber = Trans;
     if (MyOrder.IsValidate())
     {
         TicketDispatch.GetIntence().StartTicketOrder(MyOrder);
         return Json(MyOrder.VerificationCode,JsonRequestBehavior.AllowGet);
     }
     else
         return Json("Error Data:"+StartStep+"-"+EndStep,JsonRequestBehavior.AllowGet);
 }
        public bool SaleTrainTicket(TicketOrder TOrder)
        {
            try
            {
                if (TOrder.StartStep < TOrder.EndStep && TOrder.StartStep > 0)
                {
                    if (TrainDic.ContainsKey(TOrder.TrainDate))
                    {
                        if (TrainDic[TOrder.TrainDate].ContainsKey(TOrder.TrainNumber))
                        {
                            Train MyTrain = TrainDic[TOrder.TrainDate][TOrder.TrainNumber];
                            ///加锁保证每个车次只有一个线程访问
                            lock (MyTrain)
                            {
                                List<Ticket> AlreadySalesTickets = MyTrain.AlreadySalesTickets;

                                ///当前请求购票起始站点之前售出,并且未在当前购票起始站点下车的票数
                                int BeforeStepSalesCount = AlreadySalesTickets.Count(p => (p.StartStep.StepNumber < TOrder.StartStep && p.EndStep.StepNumber > TOrder.EndStep));
                                ///当前请求购票起始站点已经售出的票数
                                int ThisStepSalesCount = AlreadySalesTickets.Count(p => p.StartStep.StepNumber == TOrder.StartStep);
                                ///当前请求购票起始站点之后,请求购票结束站点之前上车的票数
                                int BetweenSalesCount = AlreadySalesTickets.Count(p => p.StartStep.StepNumber > TOrder.StartStep && p.StartStep.StepNumber < TOrder.EndStep);
                                if (MyTrain.SeatCount - BeforeStepSalesCount - ThisStepSalesCount - BetweenSalesCount > 0)
                                {
                                    Ticket mytichet = new Ticket();
                                    mytichet.MyTrain = MyTrain;
                                    mytichet.StartStep = MyTrain.TrainLine.GetStep(TOrder.StartStep);
                                    mytichet.EndStep = MyTrain.TrainLine.GetStep(TOrder.EndStep);
                                    mytichet.VarcharNumber = MyTrain.TrainLine.LineName + MyTrain.TrainDate.ToString() + MyTrain.TrainNumber + "_" + mytichet.StartStep.StepNumber + "-" + mytichet.EndStep.StepNumber + "_" + AlreadySalesTickets.Count.ToString();
                                    TOrder.VarcharNumber = mytichet.VarcharNumber;
                                    TOrder.OrderMessage = "订票成功,您的取票订单号为:"+TOrder.VarcharNumber;
                                    mytichet.VerificationCode = TOrder.VerificationCode;

                                    AlreadySalesTickets.Add(mytichet);
                                    AlreadySaleTicketDic.Add(mytichet.VerificationCode, TOrder);
                                    return true;
                                }
                                TOrder.OrderMessage = "对不起,您请求购买的车票已经售完,请更换车次时间。";

                            }
                        }

                    }
                    if (string.IsNullOrEmpty(TOrder.OrderMessage))
                        TOrder.OrderMessage = "对不起,您请求的车票不在销售期内,请更换车次时间。";
                }
                else
                {
                    TOrder.OrderMessage = "请选择正确的目的站点";
                }
            }
            catch (Exception ex)
            {
                TOrder.OrderMessage = "购票失败,系统错误:" + ex.Message;
            }
            AlreadySaleTicketDic.Add(TOrder.VerificationCode, TOrder);
            return false;
        }
 public void StartTicketOrder(TicketOrder order)
 {
     _ticketOrderProducer.Producer(order);
 }