Beispiel #1
0
        public int CopyOrder(CopyTraOrderModel copyTraOrderModel)
        {
            //原始订单
            TraOrderEntity copyTraOrderEntity = _traOrderDal.Find <TraOrderEntity>(copyTraOrderModel.CopyFromOrderId);

            if (copyTraOrderEntity == null)
            {
                throw new Exception("复制来源订单异常");
            }


            if (copyTraOrderModel.CopyType == "X")
            {
                TraOrderEntity copyTraRefundOrderEntity =
                    _traOrderDal.Query <TraOrderEntity>(
                        n => n.OrderRoot == copyTraOrderEntity.OrderId && n.OrderType == 2 && n.RefundType == 1, true).FirstOrDefault();
                if (copyTraRefundOrderEntity == null)
                {
                    throw new Exception("不是虚退订单,不能虚退复制");
                }
            }

            //原始订单行程信息
            List <TraOrderDetailEntity> copyTraOrderDetailEntities =
                _traOrderDetailDal.Query <TraOrderDetailEntity>(n => n.OrderId == copyTraOrderModel.CopyFromOrderId, true)
                .ToList();
            //原始订单乘车人信息
            List <int> copyPidList = copyTraOrderModel.PassengerList.Select(n => n.Pid).ToList();
            List <TraPassengerEntity> copyTraPassengerEntities =
                _traPassengerDal.Query <TraPassengerEntity>(n => copyPidList.Contains(n.Pid)).ToList();


            //原始订单信息映射复制新实体
            TraOrderEntity traOrderEntity = Mapper.Map <TraOrderEntity, TraOrderEntity>(copyTraOrderEntity);
            List <TraOrderDetailEntity> traOrderDetailEntities =
                Mapper.Map <List <TraOrderDetailEntity>, List <TraOrderDetailEntity> >(copyTraOrderDetailEntities);
            List <TraPassengerEntity> traPassengerEntities =
                Mapper.Map <List <TraPassengerEntity>, List <TraPassengerEntity> >(copyTraPassengerEntities);

            //复制新实体 新增订单
            traOrderEntity.CreateOid            = copyTraOrderModel.CreateOid;
            traOrderEntity.CopyType             = copyTraOrderModel.CopyType;
            traOrderEntity.PayAmount            = copyTraOrderModel.PayAmount;
            traOrderEntity.TotalMoney           = copyTraOrderModel.PayAmount;
            traOrderEntity.PrintProcurementTime = null;
            traOrderEntity.IsNeedPrintTime      = null;
            if (traOrderEntity.CopyType != "X")//不是虚退复制
            {
                traOrderEntity.CreateTime      = DateTime.Now;
                traOrderEntity.CopyFromOrderId = copyTraOrderModel.CopyFromOrderId;
            }
            else
            {
                if (string.IsNullOrEmpty(copyTraOrderEntity.CopyType))
                {
                    traOrderEntity.CopyFromOrderId = copyTraOrderModel.CopyFromOrderId;
                }
                else
                {
                    //如果当前原始订单是虚退复制的,那么它的虚退复制订单的马甲订单号继承原始订单的马甲订单号
                    if (copyTraOrderEntity.CopyType == "X" && copyTraOrderEntity.CopyFromOrderId.HasValue)
                    {
                        traOrderEntity.CopyFromOrderId = copyTraOrderEntity.CopyFromOrderId;
                    }
                    else
                    {
                        traOrderEntity.CopyFromOrderId = copyTraOrderModel.CopyFromOrderId;
                    }
                }
            }

            traOrderEntity = _traOrderDal.Insert(traOrderEntity);

            TraOrderStatusEntity orderStatusEntity = new TraOrderStatusEntity();

            orderStatusEntity.OrderId        = traOrderEntity.OrderId;
            orderStatusEntity.ProccessStatus = 64;//设置处理中
            _traOrderStatusDal.Insert(orderStatusEntity);


            foreach (var detail in traOrderDetailEntities)
            {
                detail.OrderId = traOrderEntity.OrderId;
                List <TraPassengerEntity> thisTravelPassengerList =
                    traPassengerEntities.FindAll(n => n.OdId == detail.OdId);

                detail.ServiceFee = thisTravelPassengerList.Sum(n => (n.ServiceFee ?? 0)) / thisTravelPassengerList.Count;
                detail.FacePrice  = thisTravelPassengerList.Sum(n => (n.FacePrice ?? 0)) / thisTravelPassengerList.Count;

                detail.TotalPrice = (detail.ServiceFee + detail.FacePrice) * (detail.TicketNum ?? 0);
                TraOrderDetailEntity traOrderDetailEntity = _traOrderDetailDal.Insert(detail);

                foreach (var p in traPassengerEntities)
                {
                    p.OdId = traOrderDetailEntity.OdId;
                    _traPassengerDal.Insert(p);
                }
            }

            _traOrderLogDal.Insert <TraOrderLogEntity>(new TraOrderLogEntity()
            {
                OrderId    = traOrderEntity.OrderId,
                CreateOid  = traOrderEntity.CreateOid,
                CreateTime = DateTime.Now,
                LogType    = "OI",
                LogContent = "复制订单,来源订单号:" + copyTraOrderModel.CopyFromOrderId + ",马甲订单号:" + traOrderEntity.CopyFromOrderId
            });

            //将原始订单设置为线上隐藏
            if (copyTraOrderModel.CopyType == "X")
            {
                copyTraOrderEntity.IsOnlineShow = 1;
                _traOrderDal.Update(copyTraOrderEntity, new string[] { "IsOnlineShow" });
            }

            return(traOrderEntity.OrderId);
        }
        /// <summary>
        ///  获取抢票异步成功通知,并做处理
        ///  则自动生成火车订单,通知抢票发起者
        /// </summary>
        public void GetGrabTicketSuccessNotice(GrabTicketSuccessedDataAsyncResponseModel successedData)
        {
            int orderid = Convert.ToInt32(successedData.orderid);

            #region 1.更新抢票信息状态

            TraGrabTicketEntity traGrabTicketEntity =
                _traGrabTicketDal.Query <TraGrabTicketEntity>(n => n.OrderId == orderid).FirstOrDefault();
            if (traGrabTicketEntity == null)
            {
                throw new Exception("未找到对应的系统订单");
            }
            traGrabTicketEntity.GrabStatus = TrainGrabStatusEnum.S.ToString();
            _traGrabTicketDal.Update(traGrabTicketEntity, new string[] { "GrabStatus" });

            #endregion


            #region 2.1 根据订单获取预设订单信息

            TraOrderEntity traOrderEntity = _traOrderDal.Find <TraOrderEntity>(orderid);
            if (traOrderEntity == null)
            {
                throw new Exception("订单信息异常");
            }
            if (traOrderEntity.OrderType == 0)
            {
                throw new Exception("该订单已经回调");
            }

            TraOrderDetailEntity traOrderDetailEntity =
                _traOrderDetailDal.Query <TraOrderDetailEntity>(n => n.OrderId == traOrderEntity.OrderId)
                .FirstOrDefault();
            if (traOrderDetailEntity == null)
            {
                throw new Exception("订单信息异常");
            }

            TraOrderStatusEntity traOrderStatusEntity =
                _traOrderStatusDal.Query <TraOrderStatusEntity>(n => n.OrderId == traOrderEntity.OrderId)
                .FirstOrDefault();
            if (traOrderStatusEntity == null)
            {
                throw new Exception("订单信息异常");
            }

            List <TraPassengerEntity> traPassengerEntities =
                _traPassengerDal.Query <TraPassengerEntity>(n => n.OdId == traOrderDetailEntity.OdId).ToList();
            if (traPassengerEntities.Count == 0)
            {
                throw new Exception("订单信息异常");
            }

            #endregion

            #region 2.2根据返回结果,更新预设订单信息

            traOrderEntity.TransactionId = successedData.transactionid;
            traOrderEntity.OrderType     = 0;
            if (successedData.refund_online == "1")
            {
                traOrderEntity.IsRefundBy12306 = false;
            }
            else
            {
                traOrderEntity.IsRefundBy12306 = true;
            }

            traOrderDetailEntity.OrderId12306 = successedData.ordernumber;
            traOrderDetailEntity.StartName    = successedData.from_station_name;
            traOrderDetailEntity.StartCode    = successedData.from_station_code;
            traOrderDetailEntity.StartTime    = Convert.ToDateTime(successedData.start_time);
            TraAddressEntity startAddressEntity =
                _traAddressDal.Query <TraAddressEntity>(n => n.Addr_Name == traOrderDetailEntity.StartName)
                .FirstOrDefault();
            if (startAddressEntity != null)
            {
                traOrderDetailEntity.StartNameId = startAddressEntity.Aid;
            }

            traOrderDetailEntity.EndName = successedData.to_station_name;
            traOrderDetailEntity.EndCode = successedData.to_station_code;
            traOrderDetailEntity.EndTime = Convert.ToDateTime(successedData.arrive_time);
            TraAddressEntity endAddressEntity =
                _traAddressDal.Query <TraAddressEntity>(n => n.Addr_Name == traOrderDetailEntity.EndName)
                .FirstOrDefault();
            if (endAddressEntity != null)
            {
                traOrderDetailEntity.EndNameId = endAddressEntity.Aid;
            }

            traOrderDetailEntity.TrainNo    = successedData.checi;
            traOrderDetailEntity.FacePrice  = Convert.ToDecimal(successedData.orderamount) / 2;
            traOrderDetailEntity.TotalPrice = traOrderDetailEntity.FacePrice + traOrderDetailEntity.ServiceFee;



            foreach (TraPassengerEntity traPassengerEntity in traPassengerEntities)
            {
                GrabTicketSuccessedPassengerAsyncResponseModel grabTicketPassenger = successedData.passengers.Find(
                    n => n.passengersename == traPassengerEntity.Name && n.passportseno == traPassengerEntity.CardNo);
                traPassengerEntity.FacePrice = Convert.ToDecimal(grabTicketPassenger.price);
                List <string> cxinList = grabTicketPassenger.cxin.Split(',').ToList();
                if (cxinList.Count == 2)
                {
                    traPassengerEntity.PlaceCar    = cxinList[0];
                    traPassengerEntity.PlaceSeatNo = cxinList[1];
                }
                traPassengerEntity.PlaceGrade = grabTicketPassenger.zwname;
                traPassengerEntity.TicketNo   = grabTicketPassenger.ticket_no;

                _traPassengerDal.Update(traPassengerEntity,
                                        new string[] { "FacePrice", "PlaceCar", "PlaceSeatNo", "PlaceGrade", "TicketNo" });
            }

            traOrderEntity.TotalMoney = traPassengerEntities.Sum(n => (n.ServiceFee ?? 0) + (n.FacePrice ?? 0));
            traOrderEntity.PayAmount  = traOrderEntity.TotalMoney;

            traOrderStatusEntity.IsBuy           = 1;
            traOrderStatusEntity.ProccessStatus  = traOrderStatusEntity.ProccessStatus + 1 + 8 + 64; //已付款,已预定
            traOrderStatusEntity.RealPayOid      = "sys";
            traOrderStatusEntity.RealPayDatetime = DateTime.Now;

            //更新订单
            _traOrderDal.Update(traOrderEntity,
                                new string[] { "TransactionId", "IsRefundBy12306", "TotalMoney", "PayAmount", "OrderType" });
            //更新行程
            _traOrderDetailDal.Update(traOrderDetailEntity,
                                      new string[]
            {
                "OrderId12306", "StartName", "StartCode", "StartTime", "EndName", "EndCode", "EndTime",
                "TrainNo",
                "FacePrice", "TotalPrice"
            });
            //更新订单状态
            _traOrderStatusDal.Update(traOrderStatusEntity,
                                      new[] { "IsBuy", "ProccessStatus", "RealPayOid", "RealPayDatetime" });

            #endregion

            #region 2.3 设置付款信息,并记录日志

            PaySupplierEvent?.Invoke(this, new CommonEventArgs <AccountDetailModel>(new AccountDetailModel()
            {
                Aid              = 81,
                Amount           = traPassengerEntities.Sum(n => (n.FacePrice ?? 0)) + 5, //抢票订单+5块
                Detailtype       = "火车票付票款",
                Oid              = "sys",
                CreateTime       = DateTime.Now,
                Source           = "火车票付票款",
                OrderId          = traOrderEntity.OrderId,
                OrderType        = "Tra",
                Provider         = 0,
                CollectionRemark = "",
                EstimateId       = 0
            }));

            _traOrderLogDal.Insert(new TraOrderLogEntity()
            {
                OrderId    = traOrderEntity.OrderId,
                CreateOid  = "sys",
                CreateTime = DateTime.Now,
                LogContent = "抢票出票回调,自动勾已预定、已付票款、已采购",
                LogType    = "修改"
            });

            #endregion

            #region 2.3插入Tra_InterFaceOrder表,以便退改签流程

            TraInterFaceOrderEntity traInterFaceOrderEntity =
                _traInterFaceOrderDal.Insert <TraInterFaceOrderEntity>(new TraInterFaceOrderEntity()
            {
                OrderId       = traOrderEntity.OrderId.ToString(),
                CreateTime    = DateTime.Now,
                Status        = (int)OrderTypeEnum.TicketSuccess,
                Transactionid = traOrderEntity.TransactionId,
                OrderNumber   = traOrderDetailEntity.OrderId12306
            });

            _traOrderOperateDal.Insert(new TraOrderOperateEntity()
            {
                InterfaceId        = traInterFaceOrderEntity.InterfaceId,
                AfterOperateStatus = (int)OrderTypeEnum.TicketSuccess,
                BeforOperateStatus = 0,
                OperateTime        = DateTime.Now
            });

            #endregion
        }